0.6.0
This commit is contained in:
1
compose/persist/.gitignore
vendored
Normal file
1
compose/persist/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/build
|
||||
44
compose/persist/build.gradle.kts
Normal file
44
compose/persist/build.gradle.kts
Normal file
@@ -0,0 +1,44 @@
|
||||
plugins {
|
||||
alias(libs.plugins.android.library)
|
||||
alias(libs.plugins.kotlin.android)
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "it.hamy.compose.persist"
|
||||
compileSdk = 34
|
||||
|
||||
defaultConfig {
|
||||
minSdk = 21
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
isMinifyEnabled = false
|
||||
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"))
|
||||
}
|
||||
}
|
||||
|
||||
sourceSets.all {
|
||||
kotlin.srcDir("src/$name/kotlin")
|
||||
}
|
||||
|
||||
buildFeatures {
|
||||
compose = true
|
||||
}
|
||||
|
||||
composeOptions {
|
||||
kotlinCompilerExtensionVersion = libs.versions.compose.compiler.get()
|
||||
}
|
||||
}
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(libs.versions.jvm.get().toInt())
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(platform(libs.compose.bom))
|
||||
implementation(libs.compose.foundation)
|
||||
|
||||
detektPlugins(libs.detekt.compose)
|
||||
detektPlugins(libs.detekt.formatting)
|
||||
}
|
||||
4
compose/persist/src/main/AndroidManifest.xml
Normal file
4
compose/persist/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest>
|
||||
|
||||
</manifest>
|
||||
@@ -0,0 +1,31 @@
|
||||
package it.hamy.compose.persist
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.MutableState
|
||||
import androidx.compose.runtime.SnapshotMutationPolicy
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.structuralEqualityPolicy
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
@Composable
|
||||
fun <T> persist(
|
||||
tag: String,
|
||||
initialValue: T,
|
||||
policy: SnapshotMutationPolicy<T> = structuralEqualityPolicy()
|
||||
): MutableState<T> {
|
||||
val persistMap = LocalPersistMap.current
|
||||
|
||||
return remember(persistMap) {
|
||||
persistMap?.map?.getOrPut(tag) { mutableStateOf(initialValue, policy) } as? MutableState<T>
|
||||
?: mutableStateOf(initialValue, policy)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun <T> persistList(tag: String): MutableState<List<T>> =
|
||||
persist(tag = tag, initialValue = emptyList())
|
||||
|
||||
@Composable
|
||||
fun <T : Any?> persist(tag: String): MutableState<T?> =
|
||||
persist(tag = tag, initialValue = null)
|
||||
@@ -0,0 +1,16 @@
|
||||
package it.hamy.compose.persist
|
||||
|
||||
import android.util.Log
|
||||
import androidx.compose.runtime.MutableState
|
||||
import androidx.compose.runtime.compositionLocalOf
|
||||
|
||||
@JvmInline
|
||||
value class PersistMap(val map: MutableMap<String, MutableState<*>> = hashMapOf()) {
|
||||
fun clean(prefix: String) = map.keys.removeAll { it.startsWith(prefix) }
|
||||
}
|
||||
|
||||
val LocalPersistMap = compositionLocalOf<PersistMap?> {
|
||||
Log.e("PersistMap", "Tried to reference uninitialized PersistMap, stacktrace:")
|
||||
runCatching { error("Stack:") }.exceptionOrNull()?.printStackTrace()
|
||||
null
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package it.hamy.compose.persist
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.ContextWrapper
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.DisposableEffect
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
|
||||
@Composable
|
||||
fun PersistMapCleanup(prefix: String) {
|
||||
val context = LocalContext.current
|
||||
val persistMap = LocalPersistMap.current
|
||||
|
||||
DisposableEffect(persistMap) {
|
||||
onDispose {
|
||||
if (context.findActivityNullable()?.isChangingConfigurations == false)
|
||||
persistMap?.clean(prefix)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Context.findActivityNullable(): Activity? {
|
||||
var current = this
|
||||
while (current is ContextWrapper) {
|
||||
if (current is Activity) return current
|
||||
current = current.baseContext
|
||||
}
|
||||
return null
|
||||
}
|
||||
Reference in New Issue
Block a user