2 Commits 1564b1c9af ... 7b3ebf1ab4

Author SHA1 Message Date
  bai 7b3ebf1ab4 @fun: Flash 3 years ago
  bai 6c61bfa258 @fun: home to main 3 years ago

+ 3 - 4
app/src/main/java/com/ys/bdtp/adm/App.kt

@@ -9,7 +9,8 @@ import androidx.compose.ui.platform.LocalContext
 import com.google.accompanist.insets.ProvideWindowInsets
 import com.google.accompanist.systemuicontroller.rememberSystemUiController
 import com.ys.bdtp.adm.app.theme.AdmTheme
-import com.ys.bdtp.adm.mvvm.ui.screen.home.Home
+import com.ys.bdtp.adm.mvvm.ui.screen.main.Main
+import com.ys.bdtp.adm.mvvm.ui.screen.project.Project
 import org.kodein.di.compose.onDIContext
 
 @Composable
@@ -31,9 +32,7 @@ fun App() = onDIContext(LocalContext.current) {
                         darkIcons = useDarkIcons,
                     )
                 }
-                //LoginScreen()
-                //MainScreen()
-                Home()
+                Main()
             }
         }
     }

+ 6 - 2
app/src/main/java/com/ys/bdtp/adm/app/Constants.kt

@@ -1,7 +1,6 @@
 package com.ys.bdtp.adm.app
 
 
-
 const val DEVICE_ID = "device_id"
 
 const val BDTP_TS = "bdtp_ts"
@@ -13,4 +12,9 @@ const val CLIENT_ID = "client_id"
 /**
  * http 请求头
  */
-const val Token = "token"
+const val Token = "token"
+
+/**
+ * 用户密码
+ */
+const val Password = "password"

+ 30 - 15
app/src/main/java/com/ys/bdtp/adm/mvvm/model/db/dao/ObjectDao.kt

@@ -6,6 +6,7 @@ import com.ys.bdtp.adm.mvvm.model.vo.Object
 /**
  * 表 object Dao
  */
+/*
 @Dao
 interface ObjectDao {
     @Query("select * from object")
@@ -53,45 +54,59 @@ interface ObjectDao {
     @Delete
     suspend fun delObject(obj: Object)
 
-    /**
+    */
+/**
      * 返回楼层类型的 ObjectEntity
-     */
+     *//*
+
     @Query("select * from object where obj_type = 'floor'")
     suspend fun getFloors(): List<Object>
 
-    /**
+    */
+/**
      * 返回楼层类型的 ObjectEntity
-     */
+     *//*
+
     @Query("select * from object where obj_type = 'floor' and building_id =:buildingId")
     suspend fun getFloorsByBuildingId(buildingId: String): List<Object>
 
-    /**
+    */
+/**
      * 返回楼层类型的 ObjectEntity
-     */
+     *//*
+
     @Query("select * from object where obj_type = 'floor' and id =:floorId")
     suspend fun getFloorById(floorId: String): Object
 
-    /**
+    */
+/**
      * 返回所有建筑 ObjectEntity
-     */
+     *//*
+
     @Query("select * from object where obj_type = 'building'")
     suspend fun getBuildings(): List<Object>
 
-    /**
+    */
+/**
      * 返回项目下的建筑
-     * */
+     * *//*
+
     @Query("select * from object where obj_type = 'building' and project_id = :projectId")
     suspend fun getBuildingsByProjectId(projectId: String): List<Object>
 
-    /**
+    */
+/**
      * 返回所有建筑 ObjectEntity
-     */
+     *//*
+
     @Query("select * from object where obj_type = 'space' and class_code ='GeneralZone' and floor_id =:floorId")
     suspend fun getSpaceByFloorId(floorId: String): List<Object>
 
-    /**
+    */
+/**
      * 更新 ObjectEntity
-     */
+     *//*
+
     @Update
     suspend fun updateObject(obj: Object)
 
@@ -101,4 +116,4 @@ interface ObjectDao {
 
     @Query("select count(*) from object where project_id = :projectId and (state = 0 or state = 1)")
     suspend fun countUnDone(projectId: String): Int
-}
+}*/

+ 8 - 0
app/src/main/java/com/ys/bdtp/adm/mvvm/model/db/dao/UserDao.kt

@@ -3,6 +3,7 @@ package com.ys.bdtp.adm.mvvm.model.db.dao
 import androidx.room.*
 import com.ys.bdtp.adm.mvvm.model.db.entity.UserEntity
 import com.ys.bdtp.adm.mvvm.model.db.entity.relation.UserWithProjects
+import kotlinx.coroutines.flow.Flow
 
 /**
  * user 表访问接口
@@ -23,12 +24,19 @@ interface UserDao {
     @Query("select * from user")
     suspend fun getUsers(): List<UserEntity>
 
+    @Query("select * from user")
+    fun getUsers1(): Flow<List<UserEntity>>
+
     /**
      * 获取当前登录用户
      */
     @Query("select * from user where state = 1")
     suspend fun currentUser(): UserEntity?
 
+
+    @Query("select * from user where state = 1")
+    fun getUser(): Flow<UserEntity?>
+
     @Query("select * from user where id = :id")
     fun getUserById(id: String): UserEntity
 

+ 13 - 8
app/src/main/java/com/ys/bdtp/adm/mvvm/model/nt/Http.kt

@@ -1,5 +1,8 @@
 package com.ys.bdtp.adm.mvvm.model.nt
 
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.withContext
+
 
 /**
  * 网络请求
@@ -8,15 +11,17 @@ package com.ys.bdtp.adm.mvvm.model.nt
  * @return [Response] 请求结果封装
  */
 suspend fun <T> request(request: suspend () -> Model<T>): Response<T> {
-    return try {
-        val model = request()
-        if (model.result == "success") {
-            Response.create(model.data)
-        } else {
-            Response.create(Fail(model.result, model.message ?: "request ok, but no data"))
+    return withContext(Dispatchers.IO) {
+        try {
+            val model = request()
+            if (model.result == "success") {
+                Response.create(model.data)
+            } else {
+                Response.create(Fail(model.result, model.message ?: "request ok, but no data"))
+            }
+        } catch (t: Throwable) {
+            Response.create(Fail("catch", t.message ?: "request catch and nothing"))
         }
-    } catch (t: Throwable) {
-        Response.create(Fail("catch", t.message ?: "request catch and nothing"))
     }
 }
 

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

@@ -1,7 +1,5 @@
 package com.ys.bdtp.adm.mvvm.ui.screen.flash
 
-import android.content.SharedPreferences
-import android.net.ConnectivityManager
 import androidx.compose.foundation.Image
 import androidx.compose.foundation.background
 import androidx.compose.foundation.layout.*
@@ -16,67 +14,34 @@ import androidx.compose.ui.tooling.preview.Preview
 import androidx.compose.ui.unit.dp
 import androidx.compose.ui.unit.sp
 import com.ys.bdtp.adm.R
-import com.ys.bdtp.adm.app.Token
-import com.ys.bdtp.adm.app.tools.isNet
 import com.ys.bdtp.adm.app.theme.textBlackColor
-import com.ys.bdtp.adm.mvvm.ui.screen.home.LocalNavHostController
-import com.ys.bdtp.adm.mvvm.ui.screen.home.Route
+import com.ys.bdtp.adm.mvvm.ui.screen.main.LocalNavHostController
+import com.ys.bdtp.adm.mvvm.ui.screen.main.Route
 import com.ys.bdtp.adm.mvvm.vm.flash.FlashViewModel
-import org.kodein.di.compose.rememberInstance
 import org.kodein.di.compose.rememberViewModel
 
+/**
+ * 闪屏页面
+ */
 @Composable
 fun Flash() {
-    val vm by rememberViewModel<FlashViewModel>()
-    val cm by rememberInstance<ConnectivityManager>()
-    val sp by rememberInstance<SharedPreferences>()
-
     val nav = LocalNavHostController.current!!
+    val vm by rememberViewModel<FlashViewModel>()
 
     LaunchedEffect(Unit) {
         val user = vm.currentUser()
-        val isNet = isNet(cm)
-
-        when {
-            user == null -> { // 未登录
-                nav.navigate(Route.Login) {
-                    popUpTo(Route.Flash) {
-                        inclusive = true
-                    }
-                }
-            }
-            !isNet -> { // 登录 && 未联网
-                nav.navigate(Route.Project) {
-                    popUpTo(Route.Flash) {
-                        inclusive = true
-                    }
+        if (user != null) {
+            nav.navigate(Route.Project) {
+                launchSingleTop = true
+                popUpTo(Route.Flash) {
+                    inclusive = true
                 }
             }
-            else -> {  // 登录 && 联网
-                val token = sp.getString(Token, "")
-                if (token == "") {
-                    nav.navigate(Route.Login) {
-                        popUpTo(Route.Flash) {
-                            inclusive = true
-                        }
-                    }
-                } else {
-                    vm.getAccount(
-                        success = {
-                            nav.navigate(Route.Project) {
-                                popUpTo(Route.Flash) {
-                                    inclusive = true
-                                }
-                            }
-                        },
-                        failure = {
-                            nav.navigate(Route.Login) {
-                                popUpTo(Route.Flash) {
-                                    inclusive = true
-                                }
-                            }
-                        }
-                    )
+        } else {
+            nav.navigate(Route.Login) {
+                launchSingleTop = true
+                popUpTo(Route.Flash) {
+                    inclusive = true
                 }
             }
         }

+ 4 - 2
app/src/main/java/com/ys/bdtp/adm/mvvm/ui/screen/login/LoginContent.kt

@@ -18,13 +18,14 @@ import androidx.compose.ui.unit.dp
 import androidx.compose.ui.unit.sp
 import androidx.core.content.edit
 import com.ys.bdtp.adm.R
+import com.ys.bdtp.adm.app.Password
 import com.ys.bdtp.adm.app.Token
 import com.ys.bdtp.adm.app.theme.blueColor
 import com.ys.bdtp.adm.app.theme.grayColor
 import com.ys.bdtp.adm.app.theme.textBlackColor
 import com.ys.bdtp.adm.mvvm.model.vo.request.Login
-import com.ys.bdtp.adm.mvvm.ui.screen.home.LocalNavHostController
-import com.ys.bdtp.adm.mvvm.ui.screen.home.Route
+import com.ys.bdtp.adm.mvvm.ui.screen.main.LocalNavHostController
+import com.ys.bdtp.adm.mvvm.ui.screen.main.Route
 import com.ys.bdtp.adm.mvvm.vm.login.LoginViewModel
 import kotlinx.coroutines.launch
 import org.kodein.di.compose.rememberInstance
@@ -200,6 +201,7 @@ fun LoginContent() {
                     ),
                     success = { token ->
                         sp.edit { putString(Token, token) }
+                        sp.edit { putString(Password, password) }
                         nav.navigate(Route.Project) {
                             popUpTo(Route.Login) {
                                 inclusive = true

+ 2 - 4
app/src/main/java/com/ys/bdtp/adm/mvvm/ui/screen/home/Home.kt

@@ -1,4 +1,4 @@
-package com.ys.bdtp.adm.mvvm.ui.screen.home
+package com.ys.bdtp.adm.mvvm.ui.screen.main
 
 import androidx.compose.runtime.Composable
 import androidx.compose.runtime.CompositionLocalProvider
@@ -18,7 +18,7 @@ import com.ys.bdtp.adm.mvvm.ui.screen.task.Tasks
 val LocalNavHostController = compositionLocalOf<NavHostController?> { null }
 
 @Composable
-fun Home() {
+fun Main() {
     val navController = rememberNavController()
     CompositionLocalProvider(LocalNavHostController provides navController) {
         NavHost(navController = navController, startDestination = Route.Flash) {
@@ -43,8 +43,6 @@ fun Home() {
             composable(Route.Cad) {
                 CadScreen()
             }
-
         }
-
     }
 }

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

@@ -1,4 +1,4 @@
-package com.ys.bdtp.adm.mvvm.ui.screen.home
+package com.ys.bdtp.adm.mvvm.ui.screen.main
 
 /**
  * 路由对象

+ 1 - 1
app/src/main/java/com/ys/bdtp/adm/mvvm/ui/screen/offline/Offline.kt

@@ -18,7 +18,7 @@ import androidx.compose.ui.tooling.preview.Preview
 import androidx.compose.ui.unit.dp
 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.home.LocalNavHostController
+import com.ys.bdtp.adm.mvvm.ui.screen.main.LocalNavHostController
 import com.ys.bdtp.adm.mvvm.vm.offline.OfflineViewModel
 import org.kodein.di.compose.rememberViewModel
 

+ 1 - 1
app/src/main/java/com/ys/bdtp/adm/mvvm/ui/screen/offline/OfflineHeadLeft.kt

@@ -16,7 +16,7 @@ import androidx.compose.ui.unit.dp
 import androidx.compose.ui.unit.sp
 import androidx.navigation.NavHostController
 import com.ys.bdtp.adm.R
-import com.ys.bdtp.adm.mvvm.ui.screen.home.LocalNavHostController
+import com.ys.bdtp.adm.mvvm.ui.screen.main.LocalNavHostController
 
 @Composable
 fun OfflineHeadLeft(nav: NavHostController) {

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

@@ -6,8 +6,11 @@ import androidx.compose.foundation.background
 import androidx.compose.foundation.layout.*
 import androidx.compose.runtime.*
 import androidx.compose.ui.Modifier
+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.tools.isNet
 import com.ys.bdtp.adm.app.theme.underColor
 import com.ys.bdtp.adm.mvvm.model.nt.FailureResponse
@@ -15,25 +18,36 @@ import com.ys.bdtp.adm.mvvm.model.nt.SuccessResponse
 import com.ys.bdtp.adm.mvvm.model.db.entity.task.ProjectEntity
 import com.ys.bdtp.adm.mvvm.model.db.entity.UserEntity
 import com.ys.bdtp.adm.mvvm.ui.compose.*
-import com.ys.bdtp.adm.mvvm.ui.screen.home.LocalNavHostController
-import com.ys.bdtp.adm.mvvm.ui.screen.home.Route
+import com.ys.bdtp.adm.mvvm.ui.screen.main.LocalNavHostController
+import com.ys.bdtp.adm.mvvm.ui.screen.main.Route
 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
 
-
+/**
+ * 项目界面
+ */
 @OptIn(ExperimentalFoundationApi::class)
 @Composable
 fun Project() {
+    val nav = LocalNavHostController.current!!
     val vm by rememberViewModel<ProjectViewModel>()
     val cm by rememberInstance<ConnectivityManager>()
-    val nav = LocalNavHostController.current!!
     val scope = rememberCoroutineScope()
-    var user by remember { mutableStateOf<UserEntity?>(null) }
+    // var user by remember { mutableStateOf<UserEntity?>(null) }
     var projects by remember { mutableStateOf<List<ProjectEntity>>(emptyList()) }
-
     var popupState by remember { mutableStateOf(false) }
+
+    val lifecycleOwner = LocalLifecycleOwner.current
+    /*val userFlow = vm.userFlow
+    val userFlowLifecycleAware = remember(vm.userFlow, lifecycleOwner) {
+        userFlow.flowWithLifecycle(lifecycleOwner.lifecycle, Lifecycle.State.STARTED)
+    }
+
+    val user by userFlowLifecycleAware.collectAsState(null, scope.coroutineContext)*/
+    val user by vm.userFlow.collectAsState()
+
     LaunchedEffect(Unit) {
         // isNet 联网,进行网络通信,获取账号信息也判断 token是否能用
         if (isNet(cm)) {
@@ -42,7 +56,7 @@ fun Project() {
                     val account = response.data
                     vm.setCurrentAccount(account)
                     val up = vm.getUserWithProjectsById(account.id)
-                    user = up.user
+
                     projects = up.projects
                 }
 
@@ -66,7 +80,7 @@ fun Project() {
                 }
             } else {
                 val up = vm.getUserWithProjectsById(u.id)
-                user = up.user
+                //user = up.user
                 projects = up.projects
             }
         }
@@ -75,6 +89,8 @@ fun Project() {
     Column(
         modifier = Modifier.fillMaxSize()
     ) {
+        println("user:::::: $user ")
+        println("user:::::: $user ")
         Head(
             left = {
                 ProjectHeadLeft()

+ 2 - 2
app/src/main/java/com/ys/bdtp/adm/mvvm/ui/screen/project/ProjectItem.kt

@@ -17,8 +17,8 @@ import androidx.compose.ui.unit.dp
 import androidx.compose.ui.unit.sp
 import com.ys.bdtp.adm.R
 import com.ys.bdtp.adm.mvvm.model.db.entity.task.ProjectEntity
-import com.ys.bdtp.adm.mvvm.ui.screen.home.LocalNavHostController
-import com.ys.bdtp.adm.mvvm.ui.screen.home.Route
+import com.ys.bdtp.adm.mvvm.ui.screen.main.LocalNavHostController
+import com.ys.bdtp.adm.mvvm.ui.screen.main.Route
 
 @Composable
 fun ProjectItem(project: ProjectEntity) {

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

@@ -26,7 +26,7 @@ import com.ys.bdtp.adm.app.theme.underColor
 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.home.LocalNavHostController
+import com.ys.bdtp.adm.mvvm.ui.screen.main.LocalNavHostController
 import com.ys.bdtp.adm.mvvm.vm.task.TaskViewModel
 import org.kodein.di.compose.rememberViewModel
 

+ 2 - 2
app/src/main/java/com/ys/bdtp/adm/mvvm/ui/screen/task/TaskContent.kt

@@ -14,8 +14,8 @@ import androidx.compose.ui.text.style.TextAlign
 import androidx.compose.ui.tooling.preview.Preview
 import androidx.compose.ui.unit.dp
 import androidx.compose.ui.unit.sp
-import com.ys.bdtp.adm.mvvm.ui.screen.home.LocalNavHostController
-import com.ys.bdtp.adm.mvvm.ui.screen.home.Route
+import com.ys.bdtp.adm.mvvm.ui.screen.main.LocalNavHostController
+import com.ys.bdtp.adm.mvvm.ui.screen.main.Route
 
 /**
  * 近期任务组件

+ 2 - 2
app/src/main/java/com/ys/bdtp/adm/mvvm/ui/screen/task/TaskSide.kt

@@ -12,8 +12,8 @@ import androidx.compose.ui.unit.dp
 import androidx.navigation.NavHostController
 import com.ys.bdtp.adm.R
 import com.ys.bdtp.adm.mvvm.ui.examine.Space3DActivity.Companion.startSpace3DActivity
-import com.ys.bdtp.adm.mvvm.ui.screen.home.LocalNavHostController
-import com.ys.bdtp.adm.mvvm.ui.screen.home.Route
+import com.ys.bdtp.adm.mvvm.ui.screen.main.LocalNavHostController
+import com.ys.bdtp.adm.mvvm.ui.screen.main.Route
 
 @Composable
 fun TaskSide(nav: NavHostController, projectId: String) {

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

@@ -1,5 +1,8 @@
 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
@@ -7,6 +10,7 @@ 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 kotlinx.coroutines.flow.Flow
 import kotlinx.coroutines.withContext
 
 /**
@@ -35,4 +39,40 @@ class FlashRepository(private val api: Api, private val db: AdmDatabase) {
         }
     }
 
+    /*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")
+            }
+        )
+    }
+*/
 }

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

@@ -9,6 +9,7 @@ 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 kotlinx.coroutines.flow.Flow
 import kotlinx.coroutines.withContext
 
 
@@ -78,6 +79,11 @@ class ProjectRepository(private val api: Api, private val db: AdmDatabase) {
         }
     }
 
+    fun getUser(): Flow<UserEntity?> {
+        val flow = db.userDao().getUser()
+        return flow
+    }
+
     suspend fun logout() {
         withContext(Dispatchers.IO) {
             val user = db.userDao().currentUser()

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

@@ -10,6 +10,11 @@ import com.ys.bdtp.adm.mvvm.model.db.entity.task.ProjectEntity
 import com.ys.bdtp.adm.mvvm.model.db.entity.UserEntity
 import com.ys.bdtp.adm.mvvm.model.db.entity.relation.UserWithProjects
 import com.ys.bdtp.adm.mvvm.model.vo.request.Account
+import kotlinx.coroutines.flow.SharingStarted
+import kotlinx.coroutines.flow.SharingStarted.Companion.WhileSubscribed
+import kotlinx.coroutines.flow.StateFlow
+import kotlinx.coroutines.flow.flow
+import kotlinx.coroutines.flow.stateIn
 import kotlinx.coroutines.launch
 
 /**
@@ -69,4 +74,11 @@ class ProjectViewModel(private val repo: ProjectRepository) : ViewModel() {
     suspend fun logout() {
         repo.logout()
     }
+
+    val userFlow: StateFlow<UserEntity?> = repo.getUser().stateIn(
+        scope = viewModelScope,
+        started = WhileSubscribed(5000),
+        initialValue = null
+    )
+
 }