Browse Source

@fun: lifecycleState

bai 2 years ago
parent
commit
50cae60e8c

+ 29 - 0
app/src/main/java/com/ys/bdtp/adm/app/tools/Lifecycle.kt

@@ -0,0 +1,29 @@
+package com.ys.bdtp.adm.app.tools
+
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.State
+import androidx.compose.runtime.collectAsState
+import androidx.compose.runtime.remember
+import androidx.compose.ui.platform.LocalLifecycleOwner
+import androidx.lifecycle.Lifecycle
+import androidx.lifecycle.flowWithLifecycle
+import com.ys.bdtp.adm.mvvm.model.Data
+import kotlinx.coroutines.flow.Flow
+
+
+/**
+ * 生命周期感知 State
+ *  将 Flow 转换为 State, 并能感知生命周期
+ *
+ * @author bai
+ *
+ * @param flow
+ */
+@Composable
+fun <T> lifecycleState(flow: Flow<Data<T>>): State<Data<T>> {
+    val lifecycleOwner = LocalLifecycleOwner.current
+    val lifecycleFlow = remember(flow, lifecycleOwner) {
+        flow.flowWithLifecycle(lifecycleOwner.lifecycle, Lifecycle.State.STARTED)
+    }
+    return lifecycleFlow.collectAsState(Data.loading())
+}

+ 3 - 11
app/src/main/java/com/ys/bdtp/adm/mvvm/ui/screen/flash/Flash.kt

@@ -4,20 +4,17 @@ import androidx.compose.foundation.Image
 import androidx.compose.foundation.background
 import androidx.compose.foundation.layout.*
 import androidx.compose.material.Text
-import androidx.compose.runtime.*
+import androidx.compose.runtime.Composable
 import androidx.compose.ui.Alignment
 import androidx.compose.ui.Modifier
 import androidx.compose.ui.graphics.Color
-import androidx.compose.ui.platform.LocalLifecycleOwner
 import androidx.compose.ui.res.painterResource
 import androidx.compose.ui.tooling.preview.Preview
 import androidx.compose.ui.unit.dp
 import androidx.compose.ui.unit.sp
-import androidx.lifecycle.Lifecycle
-import androidx.lifecycle.flowWithLifecycle
 import com.ys.bdtp.adm.R
 import com.ys.bdtp.adm.app.theme.textBlackColor
-import com.ys.bdtp.adm.mvvm.model.Data
+import com.ys.bdtp.adm.app.tools.lifecycleState
 import com.ys.bdtp.adm.mvvm.model.State
 import com.ys.bdtp.adm.mvvm.ui.screen.main.LocalNavHostController
 import com.ys.bdtp.adm.mvvm.ui.screen.main.flashToLogin
@@ -33,12 +30,7 @@ fun Flash() {
     val nav = LocalNavHostController.current!!
     val vm by rememberViewModel<FlashViewModel>()
 
-    val lifecycleOwner = LocalLifecycleOwner.current
-    val lifecycleWare = remember(vm.getUser, lifecycleOwner) {
-        vm.getUser.flowWithLifecycle(lifecycleOwner.lifecycle, Lifecycle.State.STARTED)
-    }
-
-    val userState = lifecycleWare.collectAsState(Data.loading())
+    val userState = lifecycleState(vm.userFlow)
 
     if (userState.value.state == State.Success) {
         val user = userState.value.data

+ 1 - 1
app/src/main/java/com/ys/bdtp/adm/mvvm/vm/flash/FlashViewModel.kt

@@ -18,7 +18,7 @@ class FlashViewModel(private val repo: FlashRepository) : ViewModel() {
     /**
      * 获取当前登录用户
      */
-    val getUser = repo.getUser()
+    val userFlow = repo.getUser()
         .stateIn(
             scope = viewModelScope,
             started = SharingStarted.WhileSubscribed(5000),