Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from hoc081098/renovate/deps.compose.androidxc…
…omposecompilerversion Compose to v1.5.6, kotlin to v1.9.21, remove kswift, use SKIE and more...
- Loading branch information
Showing
109 changed files
with
1,424 additions
and
1,242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,26 @@ | ||
root = true | ||
root=true | ||
[*] | ||
indent_size=2 | ||
end_of_line=lf | ||
charset=utf-8 | ||
trim_trailing_whitespace=true | ||
insert_final_newline=true | ||
[*.{kt, kts}] | ||
[*.{kt,kts}] | ||
ij_kotlin_imports_layout=* | ||
disabled_rules=filename | ||
ij_continuation_indent_size=4 | ||
ij_kotlin_allow_trailing_comma_on_call_site=true | ||
ij_kotlin_allow_trailing_comma=true | ||
ktlint_standard_filename=disabled | ||
ktlint_standard_package-name=disabled | ||
ktlint_standard_property-naming=disabled | ||
ktlint_standard_function-naming=disabled | ||
filename=disabled | ||
ktlint_experimental=enabled | ||
ktlint_code_style=android_studio | ||
max_line_length=120 | ||
[*.kts] | ||
max_line_length=300 | ||
ktlint_standard_multiline-expression-wrapping=disabled | ||
ktlint_standard_function-signature=disabled | ||
[*.xml] | ||
indent_size=4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
...hoc081098/github_search_kmm/android/compose_utils/CollectWithLifecycleEffectDispatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package com.hoc081098.github_search_kmm.android.compose_utils | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.Immutable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.NonRestartableComposable | ||
import androidx.compose.runtime.RememberObserver | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.rememberUpdatedState | ||
import androidx.compose.ui.platform.LocalLifecycleOwner | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.repeatOnLifecycle | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.launch | ||
|
||
@Immutable | ||
enum class CollectWithLifecycleEffectDispatcher { | ||
/** | ||
* Use [Dispatchers.Main][kotlinx.coroutines.MainCoroutineDispatcher]. | ||
*/ | ||
Main, | ||
|
||
/** | ||
* Use [Dispatchers.Main.immediate][kotlinx.coroutines.MainCoroutineDispatcher.immediate]. | ||
*/ | ||
ImmediateMain, | ||
|
||
/** | ||
* Use [androidx.compose.runtime.Composer.applyCoroutineContext]. | ||
* Under the hood, it uses Compose [androidx.compose.ui.platform.AndroidUiDispatcher]. | ||
*/ | ||
Composer, | ||
} | ||
|
||
/** | ||
* Collect the given [Flow] in an effect that runs when [LifecycleOwner.lifecycle] is at least at [minActiveState]. | ||
* | ||
* - If [dispatcher] is [CollectWithLifecycleEffectDispatcher.ImmediateMain], the effect will run in | ||
* [Dispatchers.Main.immediate][kotlinx.coroutines.MainCoroutineDispatcher.immediate]. | ||
* - If [dispatcher] is [CollectWithLifecycleEffectDispatcher.Main], the effect will run in | ||
* [Dispatchers.Main][kotlinx.coroutines.MainCoroutineDispatcher]. | ||
* - If [dispatcher] is [CollectWithLifecycleEffectDispatcher.Composer], the effect will run in | ||
* [androidx.compose.runtime.Composer.applyCoroutineContext]. | ||
* | ||
* NOTE: When [dispatcher] or [collector] changes, the effect will **NOT** be restarted. | ||
* The latest [collector] will be used to receive values from the [Flow] ([rememberUpdatedState] is used). | ||
* If you want to restart the effect, you need to change [keys]. | ||
* | ||
* @param keys Keys to be used to [remember] the effect. | ||
* @param lifecycleOwner The [LifecycleOwner] to be used to [repeatOnLifecycle]. | ||
* @param minActiveState The minimum [Lifecycle.State] to be used to [repeatOnLifecycle]. | ||
* @param dispatcher The dispatcher to be used to launch the [Flow]. | ||
* @param collector The collector to be used to collect the [Flow]. | ||
* | ||
* @see [LaunchedEffect] | ||
* @see [CollectWithLifecycleEffectDispatcher] | ||
*/ | ||
@Composable | ||
fun <T> Flow<T>.CollectWithLifecycleEffect( | ||
vararg keys: Any?, | ||
lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current, | ||
minActiveState: Lifecycle.State = Lifecycle.State.STARTED, | ||
dispatcher: CollectWithLifecycleEffectDispatcher = CollectWithLifecycleEffectDispatcher.ImmediateMain, | ||
collector: (T) -> Unit, | ||
) { | ||
val flow = this | ||
val collectorState = rememberUpdatedState(collector) | ||
|
||
val block: suspend CoroutineScope.() -> Unit = { | ||
lifecycleOwner.repeatOnLifecycle(minActiveState) { | ||
// NOTE: we don't use `flow.collect(collectState.value)` because it can use the old value | ||
flow.collect { collectorState.value(it) } | ||
} | ||
} | ||
|
||
when (dispatcher) { | ||
CollectWithLifecycleEffectDispatcher.ImmediateMain -> { | ||
LaunchedEffectInImmediateMain(flow, lifecycleOwner, minActiveState, *keys, block = block) | ||
} | ||
|
||
CollectWithLifecycleEffectDispatcher.Main -> { | ||
LaunchedEffectInMain(flow, lifecycleOwner, minActiveState, *keys, block = block) | ||
} | ||
|
||
CollectWithLifecycleEffectDispatcher.Composer -> { | ||
LaunchedEffect(flow, lifecycleOwner, minActiveState, *keys, block = block) | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
@NonRestartableComposable | ||
@Suppress("ArrayReturn") | ||
private fun LaunchedEffectInImmediateMain(vararg keys: Any?, block: suspend CoroutineScope.() -> Unit) { | ||
remember(*keys) { LaunchedEffectImpl(block, Dispatchers.Main.immediate) } | ||
} | ||
|
||
@Composable | ||
@NonRestartableComposable | ||
@Suppress("ArrayReturn") | ||
private fun LaunchedEffectInMain(vararg keys: Any?, block: suspend CoroutineScope.() -> Unit) { | ||
remember(*keys) { LaunchedEffectImpl(block, Dispatchers.Main) } | ||
} | ||
|
||
private class LaunchedEffectImpl( | ||
private val task: suspend CoroutineScope.() -> Unit, | ||
dispatcher: CoroutineDispatcher, | ||
) : RememberObserver { | ||
private val scope = CoroutineScope(dispatcher) | ||
private var job: Job? = null | ||
|
||
override fun onRemembered() { | ||
job?.cancel("Old job was still running!") | ||
job = scope.launch(block = task) | ||
} | ||
|
||
override fun onForgotten() { | ||
job?.cancel() | ||
job = null | ||
} | ||
|
||
override fun onAbandoned() { | ||
job?.cancel() | ||
job = null | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...dApp/src/main/java/com/hoc081098/github_search_kmm/android/compose_utils/StableWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,18 @@ | ||
package com.hoc081098.github_search_kmm.android.compose_utils | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.Stable | ||
import androidx.compose.runtime.remember | ||
import kotlin.reflect.KProperty | ||
|
||
@Stable | ||
@JvmInline | ||
value class StableWrapper<T>(val value: T) | ||
|
||
@Stable | ||
@Suppress("NOTHING_TO_INLINE") | ||
inline operator fun <T> StableWrapper<T>.getValue(thisRef: Any?, property: KProperty<*>): T = value | ||
|
||
@Suppress("NOTHING_TO_INLINE") | ||
@Composable | ||
inline fun <T> rememberStableWrapperOf(value: T): StableWrapper<T> = remember(value) { StableWrapper(value) } |
84 changes: 0 additions & 84 deletions
84
...n/java/com/hoc081098/github_search_kmm/android/compose_utils/rememberFlowWithLifecycle.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.