From da2712db65fdfcaaa446139220b90875fc1f5bd9 Mon Sep 17 00:00:00 2001 From: sonulen Date: Mon, 27 May 2024 20:29:03 +0300 Subject: [PATCH] gears: added package with set of gears for kotlin --- gears/kotlin/CHANGELOG.md | 8 +++++ gears/kotlin/README.md | 45 +++++++++++++++++++++++++ gears/kotlin/build.gradle.kts | 12 +++++++ gears/kotlin/src/main/kotlin/ApplyIf.kt | 27 +++++++++++++++ settings.gradle.kts | 1 + 5 files changed, 93 insertions(+) create mode 100644 gears/kotlin/CHANGELOG.md create mode 100644 gears/kotlin/README.md create mode 100644 gears/kotlin/build.gradle.kts create mode 100644 gears/kotlin/src/main/kotlin/ApplyIf.kt diff --git a/gears/kotlin/CHANGELOG.md b/gears/kotlin/CHANGELOG.md new file mode 100644 index 0000000..e38994d --- /dev/null +++ b/gears/kotlin/CHANGELOG.md @@ -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 diff --git a/gears/kotlin/README.md b/gears/kotlin/README.md new file mode 100644 index 0000000..e82fb36 --- /dev/null +++ b/gears/kotlin/README.md @@ -0,0 +1,45 @@ +# 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] + +--- + + + +- [Installation](#installation) +- [Usage](#usage) +- [Contributing](#contributing) + + + +A set of gears for kotlin. + +## Installation + +Add the dependency: +```groovy +repositories { + mavenCentral() + google() +} + +dependencies { + implementation("com.redmadrobot.gears:kotlin:") +} +``` + +## 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 diff --git a/gears/kotlin/build.gradle.kts b/gears/kotlin/build.gradle.kts new file mode 100644 index 0000000..ea33a63 --- /dev/null +++ b/gears/kotlin/build.gradle.kts @@ -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")) +} diff --git a/gears/kotlin/src/main/kotlin/ApplyIf.kt b/gears/kotlin/src/main/kotlin/ApplyIf.kt new file mode 100644 index 0000000..7878748 --- /dev/null +++ b/gears/kotlin/src/main/kotlin/ApplyIf.kt @@ -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.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.applyIfElse( + condition: Boolean, + trueBlock: T.() -> T, + falseBlock: T.() -> T, +): T = if (condition) trueBlock() else falseBlock() diff --git a/settings.gradle.kts b/settings.gradle.kts index fae2ad2..6a38555 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -35,4 +35,5 @@ include( ":ktx:resources-ktx", ":ktx:viewbinding-ktx", ":gears:compose", + ":gears:kotlin", )