Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix package name of deprecated LocalLifecycleOwner #11

Merged
merged 5 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add license information for codes
  • Loading branch information
takahirom committed Sep 20, 2024
commit 1f9c9ed889f65e21deb3c500b0fb3706034ea00f
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
69 changes: 10 additions & 59 deletions rin/src/commonMain/kotlin/io/github/takahirom/rin/Rin.kt
Original file line number Diff line number Diff line change
@@ -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<T>(
state: MutableState<T>,
override val coroutineContext: CoroutineContext,
) : ProduceStateScope<T>, MutableState<T> by state {

override suspend fun awaitDispose(onDispose: () -> Unit): Nothing {
try {
suspendCancellableCoroutine<Nothing> {}
} finally {
onDispose()
}
}
}

@Composable
public fun <T> produceRetainedState(
initialValue: T,
producer: suspend ProduceStateScope<T>.() -> Unit,
): State<T> {
val result = rememberRetained { mutableStateOf(initialValue) }
LaunchedEffect(Unit) { ProduceRetainedStateScopeImpl(result, coroutineContext).producer() }
return result
}

@Composable
public fun <T : R, R> Flow<T>.collectAsRetainedState(
initial: R,
context: CoroutineContext = EmptyCoroutineContext,
): State<R> =
produceRetainedState(initial, this, context) {
if (context == EmptyCoroutineContext) {
collect { value = it }
} else withContext(context) { collect { value = it } }
}

@Composable
public fun <T> produceRetainedState(
initialValue: T,
key1: Any?,
key2: Any?,
producer: suspend ProduceStateScope<T>.() -> Unit,
): State<T> {
val result = rememberRetained { mutableStateOf(initialValue) }
LaunchedEffect(key1, key2) { ProduceRetainedStateScopeImpl(result, coroutineContext).producer() }
return result
}

@Composable
public fun <T> StateFlow<T>.collectAsRetainedState(
context: CoroutineContext = EmptyCoroutineContext
): State<T> = collectAsRetainedState(value, context)

interface RetainedObserver {
fun onRemembered()

Expand Down
69 changes: 69 additions & 0 deletions rin/src/commonMain/kotlin/io/github/takahirom/rin/RinExtensions.kt
Original file line number Diff line number Diff line change
@@ -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<T>(
state: MutableState<T>,
override val coroutineContext: CoroutineContext,
) : ProduceStateScope<T>, MutableState<T> by state {

override suspend fun awaitDispose(onDispose: () -> Unit): Nothing {
try {
suspendCancellableCoroutine<Nothing> {}
} finally {
onDispose()
}
}
}

@Composable
public fun <T> produceRetainedState(
initialValue: T,
producer: suspend ProduceStateScope<T>.() -> Unit,
): State<T> {
val result = rememberRetained { mutableStateOf(initialValue) }
LaunchedEffect(Unit) { ProduceRetainedStateScopeImpl(result, coroutineContext).producer() }
return result
}

@Composable
public fun <T : R, R> Flow<T>.collectAsRetainedState(
initial: R,
context: CoroutineContext = EmptyCoroutineContext,
): State<R> =
produceRetainedState(initial, this, context) {
if (context == EmptyCoroutineContext) {
collect { value = it }
} else withContext(context) { collect { value = it } }
}

@Composable
public fun <T> produceRetainedState(
initialValue: T,
key1: Any?,
key2: Any?,
producer: suspend ProduceStateScope<T>.() -> Unit,
): State<T> {
val result = rememberRetained { mutableStateOf(initialValue) }
LaunchedEffect(key1, key2) { ProduceRetainedStateScopeImpl(result, coroutineContext).producer() }
return result
}

@Composable
public fun <T> StateFlow<T>.collectAsRetainedState(
context: CoroutineContext = EmptyCoroutineContext
): State<T> = collectAsRetainedState(value, context)
4 changes: 4 additions & 0 deletions rin/src/iosTest/kotlin/IosNavigationTest.kt
Original file line number Diff line number Diff line change
@@ -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.*
Expand Down
Loading