From 308c7eddadf876ce064cf9eda87e621fe917f662 Mon Sep 17 00:00:00 2001 From: takahirom Date: Fri, 20 Sep 2024 22:32:17 +0900 Subject: [PATCH 1/4] Add LICENSE information to LICENSE and README and pom files --- LICENSE | 3 ++- README.md | 20 +++++++++++++++++++ .../main/kotlin/module.publication.gradle.kts | 4 ++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 2792ce0..35fec8e 100644 --- a/LICENSE +++ b/LICENSE @@ -186,7 +186,8 @@ same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright 2024 takahirom and and respective authors and developers. + Copyright 2024 takahirom + Copyright 2022 Slack Technologies, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/README.md b/README.md index f920f3f..d2b9c93 100644 --- a/README.md +++ b/README.md @@ -172,3 +172,23 @@ implementation("io.github.takahirom.rin:rin:[use-latest-release]") * [conference-app-2024](https://github.com/DroidKaigi/conference-app-2024) If you are using Rin, please create a pull request to add your project to this list. Having more users would allow us to dedicate more time to enhancing this library. + + +# LICENSE + +``` +Copyright 2024 takahirom +Copyright 2022 Slack Technologies, LLC + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +``` \ No newline at end of file diff --git a/convention-plugins/src/main/kotlin/module.publication.gradle.kts b/convention-plugins/src/main/kotlin/module.publication.gradle.kts index b80b2b3..ed4ce1a 100644 --- a/convention-plugins/src/main/kotlin/module.publication.gradle.kts +++ b/convention-plugins/src/main/kotlin/module.publication.gradle.kts @@ -33,6 +33,10 @@ publishing { id.set("takahirom") name.set("takahirom") } + developer { + id.set("slack") + name.set("Slack Technologies, LLC") + } } scm { url.set("https://github.com/takahirom/Rin") From 1f9c9ed889f65e21deb3c500b0fb3706034ea00f Mon Sep 17 00:00:00 2001 From: takahirom Date: Fri, 20 Sep 2024 22:39:41 +0900 Subject: [PATCH 2/4] Add license information for codes --- .../io/github/takahirom/rin/NavigationTest.kt | 3 + .../github/takahirom/rin/ReadmeSampleTest.kt | 5 +- .../io/github/takahirom/rin/RetainedTest.kt | 3 + .../github/takahirom/rin/RinBehaviorTest.kt | 3 + .../kotlin/io/github/takahirom/rin/Rin.kt | 69 +++---------------- .../io/github/takahirom/rin/RinExtensions.kt | 69 +++++++++++++++++++ rin/src/iosTest/kotlin/IosNavigationTest.kt | 4 ++ 7 files changed, 95 insertions(+), 61 deletions(-) create mode 100644 rin/src/commonMain/kotlin/io/github/takahirom/rin/RinExtensions.kt diff --git a/rin/src/androidInstrumentedTest/kotlin/io/github/takahirom/rin/NavigationTest.kt b/rin/src/androidInstrumentedTest/kotlin/io/github/takahirom/rin/NavigationTest.kt index ef46aed..1d40e3e 100644 --- a/rin/src/androidInstrumentedTest/kotlin/io/github/takahirom/rin/NavigationTest.kt +++ b/rin/src/androidInstrumentedTest/kotlin/io/github/takahirom/rin/NavigationTest.kt @@ -1,3 +1,6 @@ +// Copyright (C) 2024 takahirom +// Copyright (C) 2022 Slack Technologies, LLC +// SPDX-License-Identifier: Apache-2.0 package io.github.takahirom.rin import androidx.activity.ComponentActivity diff --git a/rin/src/androidInstrumentedTest/kotlin/io/github/takahirom/rin/ReadmeSampleTest.kt b/rin/src/androidInstrumentedTest/kotlin/io/github/takahirom/rin/ReadmeSampleTest.kt index 34ec6b0..fcc75a4 100644 --- a/rin/src/androidInstrumentedTest/kotlin/io/github/takahirom/rin/ReadmeSampleTest.kt +++ b/rin/src/androidInstrumentedTest/kotlin/io/github/takahirom/rin/ReadmeSampleTest.kt @@ -1,5 +1,6 @@ -package io.github.takahirom.rin - +// Copyright (C) 2024 takahirom +// Copyright (C) 2022 Slack Technologies, LLC +// SPDX-License-Identifier: Apache-2.0 import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.Column diff --git a/rin/src/androidInstrumentedTest/kotlin/io/github/takahirom/rin/RetainedTest.kt b/rin/src/androidInstrumentedTest/kotlin/io/github/takahirom/rin/RetainedTest.kt index 428ffd7..6eec5a8 100644 --- a/rin/src/androidInstrumentedTest/kotlin/io/github/takahirom/rin/RetainedTest.kt +++ b/rin/src/androidInstrumentedTest/kotlin/io/github/takahirom/rin/RetainedTest.kt @@ -1,3 +1,6 @@ +// Copyright (C) 2024 takahirom +// Copyright (C) 2022 Slack Technologies, LLC +// SPDX-License-Identifier: Apache-2.0 package io.github.takahirom.rin import androidx.activity.ComponentActivity diff --git a/rin/src/androidInstrumentedTest/kotlin/io/github/takahirom/rin/RinBehaviorTest.kt b/rin/src/androidInstrumentedTest/kotlin/io/github/takahirom/rin/RinBehaviorTest.kt index c45d097..97ed2dc 100644 --- a/rin/src/androidInstrumentedTest/kotlin/io/github/takahirom/rin/RinBehaviorTest.kt +++ b/rin/src/androidInstrumentedTest/kotlin/io/github/takahirom/rin/RinBehaviorTest.kt @@ -1,3 +1,6 @@ +// Copyright (C) 2024 takahirom +// Copyright (C) 2022 Slack Technologies, LLC +// SPDX-License-Identifier: Apache-2.0 package io.github.takahirom.rin import androidx.activity.ComponentActivity diff --git a/rin/src/commonMain/kotlin/io/github/takahirom/rin/Rin.kt b/rin/src/commonMain/kotlin/io/github/takahirom/rin/Rin.kt index 83f3bd7..d7378fd 100644 --- a/rin/src/commonMain/kotlin/io/github/takahirom/rin/Rin.kt +++ b/rin/src/commonMain/kotlin/io/github/takahirom/rin/Rin.kt @@ -1,73 +1,24 @@ +// Copyright (C) 2024 takahirom +// Copyright (C) 2022 Slack Technologies, LLC +// SPDX-License-Identifier: Apache-2.0 package io.github.takahirom.rin -import androidx.compose.runtime.* +import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisallowComposableCalls +import androidx.compose.runtime.RememberObserver +import androidx.compose.runtime.SideEffect +import androidx.compose.runtime.compositionLocalOf +import androidx.compose.runtime.currentCompositeKeyHash +import androidx.compose.runtime.remember import androidx.compose.ui.platform.LocalLifecycleOwner import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.ViewModel import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.viewModelFactory -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.StateFlow -import kotlinx.coroutines.suspendCancellableCoroutine -import kotlinx.coroutines.withContext -import kotlin.coroutines.CoroutineContext -import kotlin.coroutines.EmptyCoroutineContext var RIN_DEBUG = false -private class ProduceRetainedStateScopeImpl( - state: MutableState, - override val coroutineContext: CoroutineContext, -) : ProduceStateScope, MutableState by state { - - override suspend fun awaitDispose(onDispose: () -> Unit): Nothing { - try { - suspendCancellableCoroutine {} - } finally { - onDispose() - } - } -} - -@Composable -public fun produceRetainedState( - initialValue: T, - producer: suspend ProduceStateScope.() -> Unit, -): State { - val result = rememberRetained { mutableStateOf(initialValue) } - LaunchedEffect(Unit) { ProduceRetainedStateScopeImpl(result, coroutineContext).producer() } - return result -} - -@Composable -public fun Flow.collectAsRetainedState( - initial: R, - context: CoroutineContext = EmptyCoroutineContext, -): State = - produceRetainedState(initial, this, context) { - if (context == EmptyCoroutineContext) { - collect { value = it } - } else withContext(context) { collect { value = it } } - } - -@Composable -public fun produceRetainedState( - initialValue: T, - key1: Any?, - key2: Any?, - producer: suspend ProduceStateScope.() -> Unit, -): State { - val result = rememberRetained { mutableStateOf(initialValue) } - LaunchedEffect(key1, key2) { ProduceRetainedStateScopeImpl(result, coroutineContext).producer() } - return result -} - -@Composable -public fun StateFlow.collectAsRetainedState( - context: CoroutineContext = EmptyCoroutineContext -): State = collectAsRetainedState(value, context) - interface RetainedObserver { fun onRemembered() diff --git a/rin/src/commonMain/kotlin/io/github/takahirom/rin/RinExtensions.kt b/rin/src/commonMain/kotlin/io/github/takahirom/rin/RinExtensions.kt new file mode 100644 index 0000000..ee35ec2 --- /dev/null +++ b/rin/src/commonMain/kotlin/io/github/takahirom/rin/RinExtensions.kt @@ -0,0 +1,69 @@ +// Copyright (C) 2024 takahirom +// Copyright (C) 2022 Slack Technologies, LLC +// SPDX-License-Identifier: Apache-2.0 +package io.github.takahirom.rin + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.MutableState +import androidx.compose.runtime.ProduceStateScope +import androidx.compose.runtime.State +import androidx.compose.runtime.mutableStateOf +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.suspendCancellableCoroutine +import kotlinx.coroutines.withContext +import kotlin.coroutines.CoroutineContext +import kotlin.coroutines.EmptyCoroutineContext + +private class ProduceRetainedStateScopeImpl( + state: MutableState, + override val coroutineContext: CoroutineContext, +) : ProduceStateScope, MutableState by state { + + override suspend fun awaitDispose(onDispose: () -> Unit): Nothing { + try { + suspendCancellableCoroutine {} + } finally { + onDispose() + } + } +} + +@Composable +public fun produceRetainedState( + initialValue: T, + producer: suspend ProduceStateScope.() -> Unit, +): State { + val result = rememberRetained { mutableStateOf(initialValue) } + LaunchedEffect(Unit) { ProduceRetainedStateScopeImpl(result, coroutineContext).producer() } + return result +} + +@Composable +public fun Flow.collectAsRetainedState( + initial: R, + context: CoroutineContext = EmptyCoroutineContext, +): State = + produceRetainedState(initial, this, context) { + if (context == EmptyCoroutineContext) { + collect { value = it } + } else withContext(context) { collect { value = it } } + } + +@Composable +public fun produceRetainedState( + initialValue: T, + key1: Any?, + key2: Any?, + producer: suspend ProduceStateScope.() -> Unit, +): State { + val result = rememberRetained { mutableStateOf(initialValue) } + LaunchedEffect(key1, key2) { ProduceRetainedStateScopeImpl(result, coroutineContext).producer() } + return result +} + +@Composable +public fun StateFlow.collectAsRetainedState( + context: CoroutineContext = EmptyCoroutineContext +): State = collectAsRetainedState(value, context) diff --git a/rin/src/iosTest/kotlin/IosNavigationTest.kt b/rin/src/iosTest/kotlin/IosNavigationTest.kt index 58c0d1f..dcaeb34 100644 --- a/rin/src/iosTest/kotlin/IosNavigationTest.kt +++ b/rin/src/iosTest/kotlin/IosNavigationTest.kt @@ -1,3 +1,7 @@ +// Copyright (C) 2024 takahirom +// Copyright (C) 2022 Slack Technologies, LLC +// SPDX-License-Identifier: Apache-2.0 + import androidx.compose.foundation.layout.Column import androidx.compose.material3.* import androidx.compose.runtime.* From e707f18a6df1e752a3ccda4aa1baad59d80eedcd Mon Sep 17 00:00:00 2001 From: takahirom Date: Fri, 20 Sep 2024 23:09:42 +0900 Subject: [PATCH 3/4] Fix package --- .../kotlin/io/github/takahirom/rin/ReadmeSampleTest.kt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/rin/src/androidInstrumentedTest/kotlin/io/github/takahirom/rin/ReadmeSampleTest.kt b/rin/src/androidInstrumentedTest/kotlin/io/github/takahirom/rin/ReadmeSampleTest.kt index fcc75a4..2fd1c9f 100644 --- a/rin/src/androidInstrumentedTest/kotlin/io/github/takahirom/rin/ReadmeSampleTest.kt +++ b/rin/src/androidInstrumentedTest/kotlin/io/github/takahirom/rin/ReadmeSampleTest.kt @@ -1,11 +1,18 @@ // Copyright (C) 2024 takahirom // Copyright (C) 2022 Slack Technologies, LLC // SPDX-License-Identifier: Apache-2.0 +package io.github.takahirom.rin + import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.Column import androidx.compose.material3.Text -import androidx.compose.runtime.* +import androidx.compose.runtime.Composable +import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberUpdatedState import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalLifecycleOwner import androidx.compose.ui.test.assertIsDisplayed From 65c27b2a779f4f7a61d68494e0ea50e97e631a82 Mon Sep 17 00:00:00 2001 From: takahirom Date: Fri, 20 Sep 2024 23:12:11 +0900 Subject: [PATCH 4/4] Fix package name of deprecated LocalLifecycleOwner --- rin/src/commonMain/kotlin/io/github/takahirom/rin/Rin.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rin/src/commonMain/kotlin/io/github/takahirom/rin/Rin.kt b/rin/src/commonMain/kotlin/io/github/takahirom/rin/Rin.kt index d7378fd..a368d8b 100644 --- a/rin/src/commonMain/kotlin/io/github/takahirom/rin/Rin.kt +++ b/rin/src/commonMain/kotlin/io/github/takahirom/rin/Rin.kt @@ -10,10 +10,10 @@ import androidx.compose.runtime.SideEffect import androidx.compose.runtime.compositionLocalOf import androidx.compose.runtime.currentCompositeKeyHash import androidx.compose.runtime.remember -import androidx.compose.ui.platform.LocalLifecycleOwner import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.ViewModel +import androidx.lifecycle.compose.LocalLifecycleOwner import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.viewModelFactory