Skip to content

Commit

Permalink
gears: added package with set of gears for kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
sonulen committed May 27, 2024
1 parent e88d32d commit 2d8c98e
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
8 changes: 8 additions & 0 deletions gears/kotlin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Unreleased

### Changed

- Create gears:kotlin
added:
- `Modifier.applyIf` - Applies the given block of modifications to the Modifier if the condition is true
- `Modifier.applyChoice` - Chooses between two blocks of modifications based on a condition and returns the resulting Modifier
45 changes: 45 additions & 0 deletions gears/kotlin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# compose-ktx <GitHub path="RedMadRobot/redmadrobot-android-ktx/tree/main/compose-ktx"/>
[![Version](https://img.shields.io/maven-central/v/com.redmadrobot.extensions/compose-ktx?style=flat-square)][mavenCentral]
[![License](https://img.shields.io/github/license/RedMadRobot/redmadrobot-android-ktx?style=flat-square)][license]

---
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

- [Installation](#installation)
- [Usage](#usage)
- [Contributing](#contributing)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

A set of gears for kotlin.

## Installation

Add the dependency:
```groovy
repositories {
mavenCentral()
google()
}
dependencies {
implementation("com.redmadrobot.gears:kotlin:<version>")
}
```

## Usage

| Gear | Description |
|:-----------------------|:---------------------------------------------------------------------------------------------|
| `Modifier.applyIf` | Applies the given block of modifications to the T if the condition is true |
| `Modifier.applyIfElse` | Chooses between two blocks of modifications based on a condition and returns the resulting T |

## Contributing

Merge requests are welcome.
For major changes, please open an issue first to discuss what you would like to change.


[mavenCentral]: https://search.maven.org/artifact/com.redmadrobot.extensions/compose-ktx
[license]: ../LICENSE
12 changes: 12 additions & 0 deletions gears/kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
id("com.redmadrobot.kotlin-library")
convention.publishing
convention.detekt
}

version = "1.9.23-0"
description = "A set of gears for kotlin"

dependencies {
api(kotlin("stdlib"))
}
27 changes: 27 additions & 0 deletions gears/kotlin/src/main/kotlin/ApplyIf.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.redmadrobot.gears.kotlin

/**
* Applies the given block of modifications to the T if the condition is true.
*
* @param condition condition to determine if the block should be applied.
* @param block Lambda function that modifies the T.
* @return Modified T based on the condition.
*/
public inline fun <T> T.applyIf(
condition: Boolean,
block: T.() -> T,
): T = applyIfElse(condition = condition, trueBlock = block, falseBlock = { this })

/**
* Chooses between two blocks of modifications based on a condition and returns the resulting T.
*
* @param condition Boolean condition to determine which block to apply.
* @param trueBlock Lambda function to modify the T if the condition is true.
* @param falseBlock Lambda function to modify the T if the condition is false.
* @return Modified T based on the condition.
*/
public inline fun <T> T.applyIfElse(
condition: Boolean,
trueBlock: T.() -> T,
falseBlock: T.() -> T,
): T = if (condition) trueBlock() else falseBlock()
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ include(
":ktx:resources-ktx",
":ktx:viewbinding-ktx",
":gears:compose",
":gears:kotlin",
)

0 comments on commit 2d8c98e

Please sign in to comment.