Skip to content

Commit

Permalink
AND-81: add compose ktx
Browse files Browse the repository at this point in the history
  • Loading branch information
sonulen committed May 2, 2024
1 parent eb2db95 commit 0a41c84
Show file tree
Hide file tree
Showing 9 changed files with 473 additions and 0 deletions.
10 changes: 10 additions & 0 deletions compose-ktx/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## Unreleased

### Changed

- Create compose-ktx
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
- `SystemBarsStyleEffect` - changes the system UI style on lifecycle event ON_START, at ON_STOP returns the previous style
- `FixedFontScaleContainer` - A container that fixes the font scale, ignoring values, that are set in the phone's system settings
47 changes: 47 additions & 0 deletions compose-ktx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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 -->

Extended set of extensions for compose.

## Installation

Add the dependency:
```groovy
repositories {
mavenCentral()
google()
}
dependencies {
implementation("com.redmadrobot.extensions:compose-ktx:<version>")
}
```

## Usage

| Extension | Description |
|:--------------------|:-----------|
| `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 |
| `SystemBarsStyleEffect` | changes the system UI style on lifecycle event ON_START, at ON_STOP returns the previous style |
| `FixedFontScaleContainer` | A container that fixes the font scale, ignoring values, that are set in the phone's system settings |

## 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
32 changes: 32 additions & 0 deletions compose-ktx/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
plugins {
id("com.redmadrobot.android-library")
id("com.redmadrobot.publish")
convention.library.android
}

version = "1.6.6-0"
description = "Compose extensions"

redmadrobot {
android.minSdk = 21
}

android {
namespace = "com.redmadrobot.compose-ktx"

buildFeatures {
compose = true
}

composeOptions {
kotlinCompilerExtensionVersion = androidx.versions.compose.compiler.get()
}
}

dependencies {
api(androidx.compose.ui)
api(androidx.annotation)
api(androidx.compose.runtime)
api(androidx.core)
api(androidx.lifecycle.common)
}
44 changes: 44 additions & 0 deletions compose-ktx/src/main/kotlin/FixedFontScaleContainer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
@file:Suppress("FunctionNaming")
package com.redmadrobot.extensions.compose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Density

/**
* A container that fixes the font scale, ignoring values,
* that are set in the phone's system settings
*/
@Composable
public fun FixedFontScaleContainer(
content: @Composable () -> Unit,
) {
val fixedFontScaleDensity = Density(LocalDensity.current.density)
CompositionLocalProvider(
LocalDensity provides fixedFontScaleDensity,
content = content,
)
}

/**
* A container that restricts the font scale, ignoring values,
* that are set in the phone's system settings
*
* @param limit - the upper limit of font enlargement
*/
@Composable
public fun LimitedFontScaleContainer(
limit: Float,
content: @Composable () -> Unit,
) {
val fontScale = LocalDensity.current.fontScale.coerceAtMost(limit)
val fixedFontScaleDensity = Density(
density = LocalDensity.current.density,
fontScale = fontScale,
)
CompositionLocalProvider(
LocalDensity provides fixedFontScaleDensity,
content = content,
)
}
32 changes: 32 additions & 0 deletions compose-ktx/src/main/kotlin/modifier/ApplyIf.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.redmadrobot.extensions.compose.modifier

import androidx.compose.ui.Modifier

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

/**
* Chooses between two blocks of modifications based on a condition and returns the resulting Modifier.
*
* @param condition Boolean condition to determine which block to apply.
* @param trueBlock Lambda function to modify the Modifier if the condition is true.
* @param falseBlock Lambda function to modify the Modifier if the condition is false.
* @return Modified Modifier based on the condition.
*/
public inline fun Modifier.applyChoice(
condition: Boolean,
trueBlock: Modifier.() -> Modifier,
falseBlock: Modifier.() -> Modifier,
): Modifier {
val modifier = if (condition) trueBlock() else falseBlock()
return this.then(modifier)
}
50 changes: 50 additions & 0 deletions compose-ktx/src/main/kotlin/systemui/NavigationBarStyleEffect.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
@file:Suppress("FunctionNaming")
package com.redmadrobot.extensions.compose.systemui

import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color

/**
* Changes the color of navigation icons on [Event.ON_START], and reverts to the previous color on [Event.ON_STOP].
* @param darkIcons Whether to use dark or light icons in navigation
* @param withScrim Whether to add a scrim to the navigation for additional contrast of icons
*/
@Composable
public fun NavigationBarStyleEffect(
darkIcons: Boolean,
withScrim: Boolean = false,
) {
NavigationBarStyleEffect(
color = scrimOrTransparent(withScrim, darkIcons),
darkIcons = darkIcons,
)
}

/**
* Changes the color of the navigation on [Event.ON_START], and reverts to the previous color on [Event.ON_STOP].
* The color of the navigation icons will be automatically selected based on the provided color.
* @param color The color to which the navigation will be recolored
*/
@Composable
public fun NavigationBarStyleEffect(color: Color) {
NavigationBarStyleEffect(
color = color,
darkIcons = color.isLight(),
)
}

/**
* Changes the style of navigation on [Event.ON_START], and reverts to the previous style on [Event.ON_STOP].
* @param color The color to which the navigation will be recolored
* @param darkIcons Whether to use dark or light icons in navigation
*/
@Composable
public fun NavigationBarStyleEffect(
color: Color,
darkIcons: Boolean,
) {
SystemBarsStyleEffect(
statusBarStyle = SystemBarStyle.Unspecified,
navigationBarStyle = rememberSystemBarStyle(color, darkIcons),
)
}
38 changes: 38 additions & 0 deletions compose-ktx/src/main/kotlin/systemui/StatusBarStyleEffect.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
@file:Suppress("FunctionNaming")
package com.redmadrobot.extensions.compose.systemui

import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.lifecycle.Lifecycle.Event

/**
* When [Event.ON_START] changes the color of status bar icons, when [Event.ON_STOP] returns the previous color
* @param darkIcons Whether to use dark or light icons in the status bar
* @param withScrim Whether to add darkening/lightening of the status bar for additional contrast of icons
*/
@Composable
public fun StatusBarStyleEffect(
darkIcons: Boolean,
withScrim: Boolean = false,
) {
StatusBarStyleEffect(
color = scrimOrTransparent(withScrim, darkIcons),
darkIcons = darkIcons,
)
}

/**
* When [Event.ON_START] changes the status bar style, when [Event.ON_STOP] returns the previous style
* @param color The color in which the status bar will be repainted
* @param darkIcons Whether to use dark or light icons in the status bar
*/
@Composable
public fun StatusBarStyleEffect(
color: Color,
darkIcons: Boolean,
) {
SystemBarsStyleEffect(
statusBarStyle = rememberSystemBarStyle(color, darkIcons),
navigationBarStyle = SystemBarStyle.Unspecified,
)
}
Loading

0 comments on commit 0a41c84

Please sign in to comment.