Browse Source

@fun: task

bai 2 years ago
parent
commit
fe08020cba

+ 13 - 0
app/src/main/java/com/ys/bdtp/adm/mvvm/ui/screen/main/Route.kt

@@ -67,3 +67,16 @@ fun NavHostController.projectToLogin() {
 
 }
 
+/**
+ * Task 跳转到 Login 界面
+ */
+fun NavHostController.taskToLogin() {
+    navigate(Route.Login) {
+        launchSingleTop = true
+        popUpTo(Route.Task) {
+            inclusive = true
+        }
+    }
+
+}
+

+ 39 - 9
app/src/main/java/com/ys/bdtp/adm/mvvm/ui/screen/task/Task.kt

@@ -12,22 +12,24 @@ import androidx.compose.foundation.layout.height
 import androidx.compose.foundation.layout.padding
 import androidx.compose.foundation.layout.width
 import androidx.compose.foundation.layout.wrapContentWidth
-import androidx.compose.runtime.Composable
-import androidx.compose.runtime.LaunchedEffect
-import androidx.compose.runtime.mutableStateOf
-import androidx.compose.runtime.remember
-import androidx.compose.runtime.getValue
-import androidx.compose.runtime.setValue
+import androidx.compose.runtime.*
 import androidx.compose.ui.Modifier
 import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.platform.LocalLifecycleOwner
 import androidx.compose.ui.tooling.preview.Preview
 import androidx.compose.ui.unit.dp
+import androidx.lifecycle.Lifecycle
+import androidx.lifecycle.flowWithLifecycle
 import com.ys.bdtp.adm.app.theme.underColor
+import com.ys.bdtp.adm.mvvm.model.Data
+import com.ys.bdtp.adm.mvvm.model.State
 import com.ys.bdtp.adm.mvvm.model.db.entity.task.ProjectEntity
 import com.ys.bdtp.adm.mvvm.ui.compose.Head
 import com.ys.bdtp.adm.mvvm.ui.compose.HeadRight
-import com.ys.bdtp.adm.mvvm.ui.screen.main.LocalNavHostController
+import com.ys.bdtp.adm.mvvm.ui.compose.LogoutPopup
+import com.ys.bdtp.adm.mvvm.ui.screen.main.*
 import com.ys.bdtp.adm.mvvm.vm.task.TaskViewModel
+import kotlinx.coroutines.launch
 import org.kodein.di.compose.rememberViewModel
 
 @Composable
@@ -35,11 +37,28 @@ fun Task(projectId: String) {
     val nav = LocalNavHostController.current!!
     val vm by rememberViewModel<TaskViewModel>()
     var project by remember { mutableStateOf<ProjectEntity?>(null) }
+    var popupState by remember { mutableStateOf(false) }
+    val scope = rememberCoroutineScope()
+
+    val lifecycleOwner = LocalLifecycleOwner.current
+    val userFlowLifecycleAware = remember(vm.userStateFlow, lifecycleOwner) {
+        vm.userStateFlow.flowWithLifecycle(lifecycleOwner.lifecycle, Lifecycle.State.STARTED)
+    }
+
+    val userState = userFlowLifecycleAware.collectAsState(Data.loading())
+
+    if (userState.value.state == State.Success) {
+        val user = userState.value.data
+        if (user == null) {
+            nav.taskToLogin()
+        }
+    }
 
     LaunchedEffect(Unit) {
         project = vm.getProject(projectId)
     }
 
+
     Column(modifier = Modifier.fillMaxSize().background(Color.White)) {
         Head(
             left = {
@@ -50,12 +69,23 @@ fun Task(projectId: String) {
                     })
             },
             right = {
-                HeadRight(
+                TaskHeadRight(
                     role = { "管家" },
-                    click = { println("heiheihei") }
+                    click = { popupState = !popupState }
                 )
             }
         )
+        if (popupState) {
+            LogoutPopup(
+                click = {
+                    scope.launch {
+                        vm.logout()
+                    }
+                    popupState = false
+                }
+            )
+        }
+
         Spacer(
             modifier = Modifier.fillMaxWidth().height(1.dp).background(Color(0xFFEFF0F1))
         )

+ 86 - 0
app/src/main/java/com/ys/bdtp/adm/mvvm/ui/screen/task/TaskHeadRight.kt

@@ -0,0 +1,86 @@
+package com.ys.bdtp.adm.mvvm.ui.screen.task
+
+import androidx.compose.foundation.Image
+import androidx.compose.foundation.background
+import androidx.compose.foundation.clickable
+import androidx.compose.foundation.layout.*
+import androidx.compose.material.Text
+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.res.painterResource
+import androidx.compose.ui.text.font.FontWeight
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.unit.sp
+import com.ys.bdtp.adm.R
+
+
+@Composable
+fun TaskHeadRight(
+    role: () -> String,
+    click: () -> Unit
+) {
+    Row(
+        modifier = Modifier.height(56.dp),
+        verticalAlignment = Alignment.CenterVertically,
+        horizontalArrangement = Arrangement.End
+    ) {
+        Box(
+            modifier = Modifier.size(56.dp).padding(12.dp)
+        ) {
+            Image(
+                painter = painterResource(R.drawable.bell),
+                contentDescription = "",
+                modifier = Modifier.fillMaxSize()
+            )
+
+            Box(
+                modifier = Modifier.size(16.dp).align(Alignment.TopEnd)
+            ) {
+                Image(
+                    painter = painterResource(R.drawable.red_circle),
+                    contentDescription = "",
+                    modifier = Modifier.align(Alignment.Center)
+                )
+                Text(
+                    text = "2",
+                    fontSize = 10.sp,
+                    color = Color.White,
+                    modifier = Modifier.align(Alignment.Center)
+                )
+            }
+        }
+
+        Spacer(modifier = Modifier.width(4.dp))
+        Spacer(
+            modifier = Modifier.width(1.dp).height(24.dp).background(Color(0xFFD8D8D8))
+        )
+        Spacer(modifier = Modifier.width(16.dp))
+        Text(text = role(), fontSize = 16.sp, fontWeight = FontWeight.Bold, color = Color(0xFF1F2329), maxLines = 1)
+        Spacer(modifier = Modifier.width(4.dp))
+
+        Row(
+            modifier = Modifier
+                .height(56.dp)
+                .clickable {
+                    click()
+                }
+                .padding(12.dp),
+            verticalAlignment = Alignment.CenterVertically
+        ) {
+            Image(
+                painter = painterResource(R.drawable.avatar),
+                contentDescription = "",
+                Modifier.size(32.dp)
+            )
+            Spacer(modifier = Modifier.width(8.dp))
+            Image(
+                painter = painterResource(R.drawable.arrow_down),
+                contentDescription = "",
+                modifier = Modifier.size(16.dp)
+            )
+        }
+        Spacer(modifier = Modifier.width(20.dp))
+    }
+}

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

@@ -23,37 +23,6 @@ import kotlinx.coroutines.withContext
  */
 class ProjectRepository(private val api: Api, private val db: AdmDatabase) {
     /**
-     * 获取账号信息
-     */
-    suspend fun requestAccount(password: String, failure: (Fail) -> Unit) {
-        request(
-            request = { api.getAccount() },
-            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 users = db.userDao().getUsers().onEach { u -> u.state = 0 }.toMutableList()
-                users.add(user)
-                db.userDao().insUsers(users)
-                account.projects.forEach { it.userId = user.id }
-                db.projectDao().insProjects(account.projects)
-                Log.d("requestAccount", "requestAccount success")
-            },
-            failure = { fail ->
-                failure(fail)
-                Log.d("requestAccount", "requestAccount failure : $fail")
-            }
-        )
-    }
-
-    /**
      *  获取用户以及用户项目
      */
     fun getUserWithProjects(): Flow<Data<UserWithProjects>> {

+ 23 - 0
app/src/main/java/com/ys/bdtp/adm/mvvm/vm/task/TaskRepository.kt

@@ -1,9 +1,14 @@
 package com.ys.bdtp.adm.mvvm.vm.task
 
+import com.ys.bdtp.adm.mvvm.model.Data
 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.db.entity.task.ProjectEntity
 import com.ys.bdtp.adm.mvvm.model.nt.Api
+import com.ys.bdtp.adm.mvvm.model.toStateFlow
 import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.collect
 import kotlinx.coroutines.withContext
 
 /**
@@ -21,4 +26,22 @@ class TaskRepository(private val api: Api, private val db: AdmDatabase) {
             db.projectDao().getProjectById(id)
         }
     }
+
+    /**
+     * 获取当前登录用户
+     */
+    fun getUser(): Flow<Data<UserEntity?>> {
+        return db.userDao().getUser().toStateFlow()
+    }
+
+    suspend fun logout() {
+        withContext(Dispatchers.IO) {
+            db.userDao().getUser().collect {
+                it?.let {
+                    it.state = 0
+                    db.userDao().updUser(it)
+                }
+            }
+        }
+    }
 }

+ 19 - 1
app/src/main/java/com/ys/bdtp/adm/mvvm/vm/task/TaskViewModel.kt

@@ -1,7 +1,11 @@
 package com.ys.bdtp.adm.mvvm.vm.task
 
 import androidx.lifecycle.ViewModel
+import androidx.lifecycle.viewModelScope
+import com.ys.bdtp.adm.mvvm.model.Data
 import com.ys.bdtp.adm.mvvm.model.db.entity.task.ProjectEntity
+import kotlinx.coroutines.flow.SharingStarted
+import kotlinx.coroutines.flow.stateIn
 
 /**
  * TaskViewModel
@@ -13,7 +17,21 @@ class TaskViewModel(private val repo: TaskRepository) : ViewModel() {
      * @author bai
      * @param id 项目id
      */
-   suspend fun getProject(id: String): ProjectEntity {
+    suspend fun getProject(id: String): ProjectEntity {
         return repo.getProject(id)
     }
+
+    /**
+     * 带状态 User Flow
+     */
+    val userStateFlow = repo.getUser()
+        .stateIn(
+            scope = viewModelScope,
+            started = SharingStarted.WhileSubscribed(5000),
+            initialValue = Data.loading()
+        )
+
+    suspend fun logout() {
+        repo.logout()
+    }
 }