Browse Source

@fun: user flow

bai 2 years ago
parent
commit
ee72c63949

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

@@ -4,17 +4,21 @@ import androidx.compose.foundation.Image
 import androidx.compose.foundation.background
 import androidx.compose.foundation.layout.*
 import androidx.compose.material.Text
-import androidx.compose.runtime.Composable
-import androidx.compose.runtime.LaunchedEffect
+import androidx.compose.runtime.*
 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.mvvm.model.State
 import com.ys.bdtp.adm.mvvm.ui.screen.main.LocalNavHostController
 import com.ys.bdtp.adm.mvvm.ui.screen.main.flashToLogin
 import com.ys.bdtp.adm.mvvm.ui.screen.main.flashToProject
@@ -29,8 +33,15 @@ fun Flash() {
     val nav = LocalNavHostController.current!!
     val vm by rememberViewModel<FlashViewModel>()
 
-    LaunchedEffect(Unit) {
-        val user = vm.currentUser()
+    val lifecycleOwner = LocalLifecycleOwner.current
+    val lifecycleWare = remember(vm.getUser, lifecycleOwner) {
+        vm.getUser.flowWithLifecycle(lifecycleOwner.lifecycle, Lifecycle.State.STARTED)
+    }
+
+    val userState = lifecycleWare.collectAsState(Data.loading())
+
+    if (userState.value.state == State.Success) {
+        val user = userState.value.data
         if (user != null) {
             nav.flashToProject()
         } else {

+ 0 - 14
app/src/main/java/com/ys/bdtp/adm/mvvm/ui/screen/project/Project.kt

@@ -24,7 +24,6 @@ import com.ys.bdtp.adm.mvvm.ui.screen.main.LocalNavHostController
 import com.ys.bdtp.adm.mvvm.ui.screen.main.projectToLogin
 import com.ys.bdtp.adm.mvvm.vm.project.ProjectViewModel
 import kotlinx.coroutines.launch
-import org.kodein.di.compose.rememberInstance
 import org.kodein.di.compose.rememberViewModel
 
 /**
@@ -35,8 +34,6 @@ import org.kodein.di.compose.rememberViewModel
 fun Project() {
     val nav = LocalNavHostController.current!!
     val vm by rememberViewModel<ProjectViewModel>()
-    val cm by rememberInstance<ConnectivityManager>()
-    val sp by rememberInstance<SharedPreferences>()
     val scope = rememberCoroutineScope()
     var popupState by remember { mutableStateOf(false) }
 
@@ -47,17 +44,6 @@ fun Project() {
 
     val dataState = userFlowLifecycleAware.collectAsState(Data.loading())
 
-    LaunchedEffect(Unit) {
-        // isNet 联网,token 进行网络通信, 发起请求, 更新数据.
-        if (isNet(cm)) {
-            /*val password = sp.getString(Password, "") ?: ""
-            vm.requestAccount(password) { fail ->
-                // 不同 code 进行不同操作
-                Log.d("Project", "Project requestAccount fail :${fail}")
-            }*/
-        }
-    }
-
     Column(
         modifier = Modifier.fillMaxSize()
     ) {

+ 3 - 56
app/src/main/java/com/ys/bdtp/adm/mvvm/vm/flash/FlashRepository.kt

@@ -1,17 +1,11 @@
 package com.ys.bdtp.adm.mvvm.vm.flash
 
-import android.util.Log
 import com.ys.bdtp.adm.mvvm.model.Data
-import com.ys.bdtp.adm.mvvm.model.data
-import com.ys.bdtp.adm.mvvm.model.nt.Response
 import com.ys.bdtp.adm.mvvm.model.db.AdmDatabase
 import com.ys.bdtp.adm.mvvm.model.db.entity.UserEntity
 import com.ys.bdtp.adm.mvvm.model.nt.Api
-import com.ys.bdtp.adm.mvvm.model.nt.request
-import com.ys.bdtp.adm.mvvm.model.vo.request.Account
-import kotlinx.coroutines.Dispatchers
+import com.ys.bdtp.adm.mvvm.model.toStateFlow
 import kotlinx.coroutines.flow.Flow
-import kotlinx.coroutines.withContext
 
 /**
  * 闪屏页数据仓库
@@ -25,54 +19,7 @@ class FlashRepository(private val api: Api, private val db: AdmDatabase) {
     /**
      * 获取当前登录用户
      */
-    suspend fun currentUser(): UserEntity? {
-        return withContext(Dispatchers.IO) {
-            db.userDao().currentUser()
-        }
+    fun getUser(): Flow<Data<UserEntity?>> {
+        return db.userDao().getUser().toStateFlow()
     }
-
-    suspend fun getAccount(): Response<Account> {
-        return withContext(Dispatchers.IO) {
-            request {
-                api.getAccount()
-            }
-        }
-    }
-
-    /*suspend fun getUser(password: String): Flow<Data<UserEntity?>> {
-        return data(
-            fromDb = {
-                db.userDao().getCurrentUser()
-            },
-            fromNt = {
-                api.getAccount()
-            },
-            needNt = {
-                true
-            },
-            success = { account ->
-                val user = UserEntity(
-                    password = password,
-                    state = 1,
-                    id = account.id,
-                    account = account.account,
-                    deptId = account.deptId,
-                    email = account.email,
-                    phone = account.phone,
-                    name = account.name,
-                )
-                val userDao = db.userDao()
-                val users = userDao.getUsers().onEach { u -> u.state = 0 }
-                userDao.insUsers(users)
-                userDao.insUser(user)
-                account.projects.forEach { it.userId = user.id }
-                db.projectDao().insProjects(account.projects)
-                Log.d("FlashRepository", "FlashRepository getUser success")
-            },
-            failure = { fail ->
-                Log.d("FlashRepository", "FlashRepository getUser failure: $fail")
-            }
-        )
-    }
-*/
 }

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

@@ -2,12 +2,9 @@ package com.ys.bdtp.adm.mvvm.vm.flash
 
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.viewModelScope
-import com.ys.bdtp.adm.mvvm.model.nt.Fail
-import com.ys.bdtp.adm.mvvm.model.nt.FailureResponse
-import com.ys.bdtp.adm.mvvm.model.nt.SuccessResponse
-import com.ys.bdtp.adm.mvvm.model.db.entity.UserEntity
-import com.ys.bdtp.adm.mvvm.model.vo.request.Account
-import kotlinx.coroutines.launch
+import com.ys.bdtp.adm.mvvm.model.Data
+import kotlinx.coroutines.flow.SharingStarted
+import kotlinx.coroutines.flow.stateIn
 
 /**
  * FlashViewModel
@@ -21,29 +18,10 @@ class FlashViewModel(private val repo: FlashRepository) : ViewModel() {
     /**
      * 获取当前登录用户
      */
-    suspend fun currentUser(): UserEntity? {
-        return repo.currentUser()
-    }
-
-    /**
-     * 获取账号信息
-     *  1,用户信息
-     *  2,项目信息
-     *
-     * @param success 获取成功回调
-     * @param failure 获取失败回调
-     */
-    suspend fun getAccount(success: (Account) -> Unit, failure: (Fail) -> Unit) {
-        viewModelScope.launch {
-            when (val response = repo.getAccount()) {
-                is SuccessResponse -> {
-                    success(response.data)
-                }
-
-                is FailureResponse -> {
-                    failure(response.fail)
-                }
-            }
-        }
-    }
+    val getUser = repo.getUser()
+        .stateIn(
+            scope = viewModelScope,
+            started = SharingStarted.WhileSubscribed(5000),
+            initialValue = Data.loading()
+        )
 }

+ 0 - 11
app/src/main/java/com/ys/bdtp/adm/mvvm/vm/project/ProjectViewModel.kt

@@ -3,10 +3,8 @@ package com.ys.bdtp.adm.mvvm.vm.project
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.viewModelScope
 import com.ys.bdtp.adm.mvvm.model.Data
-import com.ys.bdtp.adm.mvvm.model.nt.Fail
 import kotlinx.coroutines.flow.SharingStarted.Companion.WhileSubscribed
 import kotlinx.coroutines.flow.stateIn
-import kotlinx.coroutines.launch
 
 /**
  * ProjectViewModel
@@ -27,15 +25,6 @@ class ProjectViewModel(private val repo: ProjectRepository) : ViewModel() {
     )
 
     /**
-     * 获取账号信息
-     */
-    fun requestAccount(password: String, failure: (Fail) -> Unit) {
-        viewModelScope.launch {
-            repo.requestAccount(password, failure)
-        }
-    }
-
-    /**
      * 退出
      */
     suspend fun logout() {