Merge pull request #41 from itsandreramon/useRememberWithLifecycleExt
Add collectAsStateWithLifecycle Flow extension
This commit is contained in:
commit
bb5926fd1a
2 changed files with 24 additions and 8 deletions
|
@ -3,17 +3,14 @@ package com.surrus.peopleinspace.ui
|
|||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.material.*
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.livedata.observeAsState
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalLifecycleOwner
|
||||
import androidx.compose.ui.viewinterop.AndroidView
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.flowWithLifecycle
|
||||
import com.surrus.common.remote.IssPosition
|
||||
import com.surrus.peopleinspace.util.collectAsStateWithLifecycle
|
||||
import org.koin.androidx.compose.getViewModel
|
||||
import org.osmdroid.util.GeoPoint
|
||||
import org.osmdroid.views.CustomZoomButtonsController
|
||||
|
@ -25,11 +22,9 @@ fun ISSPositionScreen() {
|
|||
val peopleInSpaceViewModel = getViewModel<PeopleInSpaceViewModel>()
|
||||
|
||||
val lifecycleOwner = LocalLifecycleOwner.current
|
||||
val locationFlowLifecycleAware = remember(peopleInSpaceViewModel.issPosition, lifecycleOwner) {
|
||||
peopleInSpaceViewModel.issPosition.flowWithLifecycle(lifecycleOwner.lifecycle, Lifecycle.State.STARTED)
|
||||
}
|
||||
|
||||
val issPosition by locationFlowLifecycleAware.collectAsState(IssPosition(0.0, 0.0))
|
||||
val issPosition by peopleInSpaceViewModel.issPosition
|
||||
.collectAsStateWithLifecycle(lifecycleOwner, IssPosition(0.0, 0.0))
|
||||
|
||||
val context = LocalContext.current
|
||||
val map = remember { MapView(context) }
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
package com.surrus.peopleinspace.util
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.State
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.flowWithLifecycle
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Composable
|
||||
fun <T> Flow<T>.collectAsStateWithLifecycle(
|
||||
owner: LifecycleOwner,
|
||||
initial: T,
|
||||
minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
|
||||
): State<T> {
|
||||
return remember(this, owner) {
|
||||
flowWithLifecycle(owner.lifecycle, minActiveState)
|
||||
}.collectAsState(initial)
|
||||
}
|
Loading…
Reference in a new issue