2 Commits d5ff5f461a ... d1abc2ce44

Author SHA1 Message Date
  bai d1abc2ce44 @fun: Offline 2 years ago
  bai 1375328bd1 @fun: TaskHeadLeft Spacer 2 years ago

+ 1 - 1
app/src/main/java/com/ys/bdtp/adm/mvvm/model/db/AdmDatabase.kt

@@ -62,7 +62,7 @@ abstract class AdmDatabase : RoomDatabase() {
     abstract fun infosConfigDao(): InfosConfigDao
     abstract fun pipeConfigDao(): PipeConfigDao
     abstract fun problemInfoConfigDao(): ProblemInfoConfigDao
-    abstract fun problemTypeConfigDao(): ProblemTypeDao
+    abstract fun problemTypeConfigDao(): ProblemTypeConfigDao
     abstract fun relationConfigDao(): RelationConfigDao
 
     // dict

+ 3 - 2
app/src/main/java/com/ys/bdtp/adm/mvvm/model/db/dao/config/ProblemTypeDao.kt

@@ -2,6 +2,7 @@ package com.ys.bdtp.adm.mvvm.model.db.dao.config
 
 import androidx.room.Dao
 import androidx.room.Insert
+import androidx.room.OnConflictStrategy
 import com.ys.bdtp.adm.mvvm.model.db.entity.config.ProblemTypeConfigEntity
 
 /**
@@ -10,8 +11,8 @@ import com.ys.bdtp.adm.mvvm.model.db.entity.config.ProblemTypeConfigEntity
  * @author bai
  */
 @Dao
-interface ProblemTypeDao {
+interface ProblemTypeConfigDao {
 
-    @Insert
+    @Insert(onConflict = OnConflictStrategy.REPLACE)
     fun insProblemTypes(problemTypes: List<ProblemTypeConfigEntity>)
 }

+ 3 - 6
app/src/main/java/com/ys/bdtp/adm/mvvm/model/db/dao/task/BuildingDao.kt

@@ -1,9 +1,6 @@
 package com.ys.bdtp.adm.mvvm.model.db.dao.task
 
-import androidx.room.Dao
-import androidx.room.Insert
-import androidx.room.Query
-import androidx.room.Transaction
+import androidx.room.*
 import com.ys.bdtp.adm.mvvm.model.db.entity.task.BuildingEntity
 import com.ys.bdtp.adm.mvvm.model.db.relation.BuildingWithFloors
 import kotlinx.coroutines.flow.Flow
@@ -19,10 +16,10 @@ interface BuildingDao {
     @Query("select * from building where id = :id")
     suspend fun getBuildingById(id: String): BuildingEntity?
 
-    @Insert
+    @Insert(onConflict = OnConflictStrategy.REPLACE)
     suspend fun insBuilding(vararg floor: BuildingEntity)
 
-    @Insert
+    @Insert(onConflict = OnConflictStrategy.REPLACE)
     suspend fun insBuildings(floors: List<BuildingEntity>)
 
     @Transaction

+ 4 - 0
app/src/main/java/com/ys/bdtp/adm/mvvm/model/db/dao/task/ProjectDao.kt

@@ -49,5 +49,9 @@ interface ProjectDao {
     @Query("select * from project where id = :id")
     fun getProjectWithBuildingAndFloorsById(id: String): Flow<ProjectWithBuildingAndFloors>
 
+    @Transaction
+    @Query("select * from project where id = :id")
+    fun getProjectWithBuildingsById(id: String): Flow<ProjectWithBuildings>
+
 
 }

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

@@ -11,11 +11,16 @@ import androidx.compose.foundation.layout.height
 import androidx.compose.foundation.layout.padding
 import androidx.compose.foundation.layout.width
 import androidx.compose.runtime.Composable
-import androidx.compose.runtime.rememberCoroutineScope
+import androidx.compose.runtime.collectAsState
+import androidx.compose.runtime.remember
 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.mvvm.model.Data
 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
@@ -26,14 +31,24 @@ import org.kodein.di.compose.rememberViewModel
 fun Offline(projectId: String) {
     val nav = LocalNavHostController.current!!
     val vm by rememberViewModel<OfflineViewModel>()
-    val scope = rememberCoroutineScope()
+
+    val lifecycleOwner = LocalLifecycleOwner.current
+    val lifecycleWare = remember(lifecycleOwner) {
+        vm.getProjectWithBuildingsById(projectId).flowWithLifecycle(lifecycleOwner.lifecycle, Lifecycle.State.STARTED)
+    }
+    val projectsState = lifecycleWare.collectAsState(Data.loading())
+
     Column(modifier = Modifier.background(Color.White)) {
         Head(
-            left = { OfflineHeadLeft(nav) },
+            left = {
+                OfflineHeadLeft(back = { nav.navigateUp() })
+            },
             right = {
                 HeadRight(
                     role = { "业务角色" },
-                    click = {}
+                    click = {
+
+                    }
                 )
             }
         )
@@ -46,7 +61,7 @@ fun Offline(projectId: String) {
         )
 
         Row {
-            OfflineSide2(projectId)
+            OfflineSide(projectsState)
             Spacer(
                 modifier = Modifier
                     .width(1.dp)

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

@@ -14,22 +14,23 @@ import androidx.compose.ui.text.font.FontWeight.Companion.Bold
 import androidx.compose.ui.tooling.preview.Preview
 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.main.LocalNavHostController
 
 @Composable
-fun OfflineHeadLeft(nav: NavHostController) {
+fun OfflineHeadLeft(back: () -> Unit) {
     Row(
+        modifier = Modifier.wrapContentWidth(),
         verticalAlignment = Alignment.CenterVertically,
         horizontalArrangement = Arrangement.Start
     ) {
 
         Spacer(modifier = Modifier.width(32.dp))
 
-        IconButton(onClick = {
-            nav.navigateUp()
-        }) {
+        IconButton(
+            modifier = Modifier.size(56.dp),
+            onClick = {
+                back()
+            }) {
             Image(
                 painter = painterResource(R.drawable.arrow_back),
                 contentDescription = "",
@@ -38,10 +39,6 @@ fun OfflineHeadLeft(nav: NavHostController) {
         }
 
         Spacer(
-            modifier = Modifier.width(16.dp)
-        )
-
-        Spacer(
             modifier = Modifier
                 .width(1.dp)
                 .height(16.dp)
@@ -53,6 +50,10 @@ fun OfflineHeadLeft(nav: NavHostController) {
         )
 
         Text(text = "离线数据", fontSize = 16.sp, fontWeight = Bold, color = Color(0xFF1F2329))
+
+        Spacer(
+            modifier = Modifier.width(16.dp)
+        )
     }
 }
 
@@ -60,6 +61,5 @@ fun OfflineHeadLeft(nav: NavHostController) {
 @Preview
 @Composable
 fun PreviewOfflineHeadLeft() {
-    val nav = LocalNavHostController.current!!
-    OfflineHeadLeft(nav)
+    OfflineHeadLeft(back = {})
 }

+ 43 - 30
app/src/main/java/com/ys/bdtp/adm/mvvm/ui/screen/offline/OfflineSide.kt

@@ -8,24 +8,20 @@ import androidx.compose.material.Button
 import androidx.compose.material.ButtonDefaults
 import androidx.compose.material.Text
 import androidx.compose.runtime.Composable
-import androidx.compose.runtime.collectAsState
+import androidx.compose.runtime.State
 import androidx.compose.runtime.remember
 import androidx.compose.runtime.rememberCoroutineScope
 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.lifecycle.Lifecycle
-import androidx.lifecycle.flowWithLifecycle
 import com.ys.bdtp.adm.R
 import com.ys.bdtp.adm.app.theme.bgBlueColor
-import com.ys.bdtp.adm.app.theme.blueColor
 import com.ys.bdtp.adm.app.theme.underColor
 import com.ys.bdtp.adm.app.theme.whiteColor
 import com.ys.bdtp.adm.mvvm.model.Data
+import com.ys.bdtp.adm.mvvm.model.db.relation.ProjectWithBuildings
 import com.ys.bdtp.adm.mvvm.model.vo.request.requestBody
 import com.ys.bdtp.adm.mvvm.ui.compose.Div
 import com.ys.bdtp.adm.mvvm.vm.offline.OfflineViewModel
@@ -37,16 +33,10 @@ import org.kodein.di.compose.rememberViewModel
  * 离线数据左侧UI
  */
 @Composable
-fun OfflineSide() {
+fun OfflineSide(state: State<Data<ProjectWithBuildings>>) {
     val vm by rememberViewModel<OfflineViewModel>()
-
-    val lifecycleOwner = LocalLifecycleOwner.current
-    val projectsAware = remember(vm.projectWithBuildingsFlow, lifecycleOwner) {
-        vm.projectWithBuildingsFlow.flowWithLifecycle(lifecycleOwner.lifecycle, Lifecycle.State.STARTED)
-    }
-
-    val projectsState = projectsAware.collectAsState(Data.loading())
     val scope = rememberCoroutineScope()
+    val projectsState = remember { state }
 
     Column(
         modifier = Modifier
@@ -62,12 +52,13 @@ fun OfflineSide() {
             onClick = {
                 scope.launch {
                     // 下载字典
-                    projectsState.value.data?.forEach {
+                    val data = projectsState.value.data
+                    data?.project?.let {
                         vm.getDict(
                             body = requestBody(
-                                userId = it.project.userId,
-                                groupCode = it.project.groupCode,
-                                projectId = it.project.projectId,
+                                userId = it.userId,
+                                groupCode = it.groupCode,
+                                projectId = it.projectId,
                             ),
                             success = {
                                 println("success: $it")
@@ -80,12 +71,13 @@ fun OfflineSide() {
                 }
                 scope.launch {
                     // 下载配置
-                    projectsState.value.data?.forEach {
+                    val data = projectsState.value.data
+                    data?.project?.let {
                         vm.getConfig(
                             body = requestBody(
-                                userId = it.project.userId,
-                                groupCode = it.project.groupCode,
-                                projectId = it.project.projectId,
+                                userId = it.userId,
+                                groupCode = it.groupCode,
+                                projectId = it.projectId,
                             ),
                             success = {
                                 println("success: $it")
@@ -98,15 +90,37 @@ fun OfflineSide() {
                 }
                 scope.launch {
                     // 下载建筑与楼层数据
-                    projectsState.value.data?.forEach {
-                        vm.getBuildingsAndFloors(
+                    val data = projectsState.value.data
+                    val project = data?.project
+                    vm.getBuildingsAndFloors(
+                        body = requestBody(
+                            userId = project?.userId ?: "",
+                            groupCode = project?.groupCode ?: "",
+                            projectId = project?.projectId ?: "",
+                        ),
+                        success = {
+                            println("success: $it")
+                        },
+                        failure = {
+                            println("failure: $it")
+                        }
+                    )
+                }
+                scope.launch {
+                    // 任务数据
+                    val data = projectsState.value.data
+                    val project = data?.project
+                    val buildings = data?.buildings
+                    buildings?.forEach { building ->
+                        vm.getMission(
                             body = requestBody(
-                                userId = it.project.userId,
-                                groupCode = it.project.groupCode,
-                                projectId = it.project.projectId,
+                                userId = project?.userId ?: "",
+                                groupCode = project?.groupCode ?: "",
+                                projectId = project?.projectId ?: "",
+                                buildingId = building.id
                             ),
                             success = {
-                                println("success: $it")
+                                println("success: ")
                             },
                             failure = {
                                 println("failure: $it")
@@ -114,7 +128,6 @@ fun OfflineSide() {
                         )
                     }
                 }
-
             },
             colors = ButtonDefaults.buttonColors(
                 contentColor = whiteColor,
@@ -258,5 +271,5 @@ fun OfflineSide() {
 @Preview
 @Composable
 fun PreviewOfflineSide() {
-    OfflineSide()
+    // OfflineSide(projectsState)
 }

+ 4 - 25
app/src/main/java/com/ys/bdtp/adm/mvvm/ui/screen/task/TaskHeadLeft.kt

@@ -16,11 +16,10 @@ import androidx.compose.ui.unit.dp
 import androidx.compose.ui.unit.sp
 import com.ys.bdtp.adm.R
 
-
 @Composable
 fun TaskHeadLeft(
     title: String,
-    click: () -> Unit
+    click: () -> Unit,
 ) {
     Row(
         verticalAlignment = Alignment.CenterVertically,
@@ -30,7 +29,7 @@ fun TaskHeadLeft(
         Spacer(modifier = Modifier.width(32.dp))
 
         IconButton(
-            modifier = Modifier.height(56.dp),
+            modifier = Modifier.size(56.dp),
             onClick = {
                 click()
             }
@@ -43,35 +42,15 @@ fun TaskHeadLeft(
         }
 
         Spacer(
-            modifier = Modifier.width(16.dp)
-        )
-
-        Spacer(
             modifier = Modifier
                 .width(1.dp)
                 .height(16.dp)
                 .background(Color(0xFFD8D8D8))
         )
 
-        Row(
-            modifier = Modifier
-                .fillMaxHeight()
-                .clickable {
-                    //click()
-                }
-                .padding(16.dp),
-            verticalAlignment = Alignment.CenterVertically
-        ) {
-            Text(text = title, fontSize = 16.sp, fontWeight = FontWeight.Bold, color = Color(0xFF1F2329))
+        Spacer(modifier = Modifier.width(16.dp))
 
-            Spacer(modifier = Modifier.width(16.dp))
-
-            Image(
-                painter = painterResource(R.drawable.arrow_down),
-                contentDescription = "",
-                modifier = Modifier.size(16.dp)
-            )
-        }
+        Text(text = title, fontSize = 16.sp, fontWeight = FontWeight.Bold, color = Color(0xFF1F2329))
 
     }
 }

+ 4 - 0
app/src/main/java/com/ys/bdtp/adm/mvvm/vm/offline/OfflineRepository.kt

@@ -256,4 +256,8 @@ class OfflineRepository(
         return db.projectDao().getProjectWithBuildings().toStateFlow()
     }
 
+    fun getProjectWithBuildingsById(id: String): Flow<Data<ProjectWithBuildings>> {
+        return db.projectDao().getProjectWithBuildingsById(id).toStateFlow()
+    }
+
 }

+ 7 - 1
app/src/main/java/com/ys/bdtp/adm/mvvm/vm/offline/OfflineViewModel.kt

@@ -56,7 +56,7 @@ class OfflineViewModel(private val repo: OfflineRepository) : ViewModel() {
     fun getBuildingsAndFloors(
         body: Map<String, String>,
         success: (BuildingsAndFloors) -> Unit,
-        failure: (Fail) -> Unit
+        failure: (Fail) -> Unit,
     ) {
         viewModelScope.launch {
             repo.getBuildingsAndFloors(body,
@@ -102,5 +102,11 @@ class OfflineViewModel(private val repo: OfflineRepository) : ViewModel() {
             initialValue = Data.loading()
         )
 
+    fun getProjectWithBuildingsById(id: String) = repo.getProjectWithBuildingsById(id)
+        .stateIn(
+            scope = viewModelScope,
+            started = SharingStarted.WhileSubscribed(5000),
+            initialValue = Data.loading()
+        )
 
 }