Browse Source

Removed dispatchers from dao (refactoring)

Vadik Sirekanyan 2 năm trước cách đây
mục cha
commit
d4d2a25d53

+ 2 - 1
app/src/main/java/org/sirekanyan/outline/MainActivity.kt

@@ -9,6 +9,7 @@ import androidx.compose.material3.MaterialTheme
 import androidx.compose.material3.Surface
 import androidx.compose.ui.Modifier
 import androidx.core.view.WindowCompat
+import kotlinx.coroutines.Dispatchers.IO
 import kotlinx.coroutines.launch
 import org.sirekanyan.outline.ui.AddServerContent
 import org.sirekanyan.outline.ui.DeleteKeyContent
@@ -59,7 +60,7 @@ class MainActivity : ComponentActivity() {
                                     serverName = serverName,
                                     onDismiss = { state.dialog = null },
                                     onConfirm = {
-                                        state.scope.launch {
+                                        state.scope.launch(IO) {
                                             state.dao.deleteUrl(server.id)
                                         }
                                         state.page = HelloPage

+ 8 - 12
app/src/main/java/org/sirekanyan/outline/db/DebugDao.kt

@@ -1,7 +1,5 @@
 package org.sirekanyan.outline.db
 
-import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.withContext
 import org.sirekanyan.outline.db.model.ServerEntity
 import org.sirekanyan.outline.isDebugBuild
 
@@ -16,16 +14,14 @@ class DebugDao(private val database: OutlineDatabase) {
         }
     }
 
-    suspend fun reset() {
-        withContext(Dispatchers.IO) {
-            database.transaction {
-                keyQueries.truncate()
-                serverQueries.truncate()
-                listOf<String>(
-                    // add your debug servers here
-                ).forEach { url ->
-                    serverQueries.insertUrl(ServerEntity(url, insecure = true))
-                }
+    fun reset() {
+        database.transaction {
+            keyQueries.truncate()
+            serverQueries.truncate()
+            listOf<String>(
+                // add your debug servers here
+            ).forEach { url ->
+                serverQueries.insertUrl(ServerEntity(url, insecure = true))
             }
         }
     }

+ 7 - 11
app/src/main/java/org/sirekanyan/outline/db/KeyDao.kt

@@ -3,11 +3,9 @@ package org.sirekanyan.outline.db
 import androidx.compose.runtime.Composable
 import androidx.compose.runtime.remember
 import androidx.compose.ui.platform.LocalContext
+import app.cash.sqldelight.Query
 import app.cash.sqldelight.coroutines.asFlow
-import app.cash.sqldelight.coroutines.mapToList
-import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.flow.Flow
-import kotlinx.coroutines.withContext
 import org.sirekanyan.outline.app
 import org.sirekanyan.outline.db.model.KeyEntity
 import org.sirekanyan.outline.db.model.ServerEntity
@@ -22,15 +20,13 @@ class KeyDao(database: OutlineDatabase) {
 
     private val queries = database.keyEntityQueries
 
-    fun observe(server: ServerEntity): Flow<List<KeyEntity>> =
-        queries.selectKeys(server.id).asFlow().mapToList(Dispatchers.IO)
+    fun observe(server: ServerEntity): Flow<Query<KeyEntity>> =
+        queries.selectKeys(server.id).asFlow()
 
-    suspend fun update(server: ServerEntity, keys: List<KeyEntity>) {
-        withContext(Dispatchers.IO) {
-            queries.transaction {
-                queries.deleteKeys(server.id)
-                keys.forEach(queries::insertKey)
-            }
+    fun update(server: ServerEntity, keys: List<KeyEntity>) {
+        queries.transaction {
+            queries.deleteKeys(server.id)
+            keys.forEach(queries::insertKey)
         }
     }
 

+ 2 - 4
app/src/main/java/org/sirekanyan/outline/db/ServerDao.kt

@@ -29,10 +29,8 @@ class ServerDao(database: OutlineDatabase) {
             queries.insertUrl(server)
         }
 
-    suspend fun deleteUrl(id: String) {
-        withContext(Dispatchers.IO) {
-            queries.deleteUrl(id)
-        }
+    fun deleteUrl(id: String) {
+        queries.deleteUrl(id)
     }
 
 }

+ 11 - 4
app/src/main/java/org/sirekanyan/outline/repository/KeyRepository.kt

@@ -1,7 +1,10 @@
 package org.sirekanyan.outline.repository
 
+import app.cash.sqldelight.coroutines.mapToList
+import kotlinx.coroutines.Dispatchers.IO
 import kotlinx.coroutines.flow.Flow
 import kotlinx.coroutines.flow.map
+import kotlinx.coroutines.withContext
 import org.sirekanyan.outline.api.OutlineApi
 import org.sirekanyan.outline.api.model.AccessKey
 import org.sirekanyan.outline.api.model.Key
@@ -14,15 +17,19 @@ import org.sirekanyan.outline.db.model.ServerEntity
 class KeyRepository(private val api: OutlineApi, private val keyDao: KeyDao) {
 
     fun observeKeys(server: ServerEntity): Flow<List<Key>> =
-        keyDao.observe(server).map(List<KeyEntity>::fromEntities)
+        keyDao.observe(server).mapToList(IO).map(List<KeyEntity>::fromEntities)
 
     suspend fun updateKeys(server: ServerEntity) {
-        val keys = api.getKeys(server)
-        keyDao.update(server, keys.toEntities(server))
+        withContext(IO) {
+            val keys = api.getKeys(server)
+            keyDao.update(server, keys.toEntities(server))
+        }
     }
 
     suspend fun renameKey(server: ServerEntity, accessKey: AccessKey, newName: String) {
-        api.renameAccessKey(server, accessKey.id, newName)
+        withContext(IO) {
+            api.renameAccessKey(server, accessKey.id, newName)
+        }
     }
 
 }

+ 2 - 1
app/src/main/java/org/sirekanyan/outline/ui/DrawerContent.kt

@@ -33,6 +33,7 @@ import androidx.compose.ui.graphics.vector.ImageVector
 import androidx.compose.ui.platform.LocalContext
 import androidx.compose.ui.res.stringResource
 import androidx.compose.ui.unit.dp
+import kotlinx.coroutines.Dispatchers.IO
 import kotlinx.coroutines.launch
 import org.sirekanyan.outline.AddServerDialog
 import org.sirekanyan.outline.MainState
@@ -111,7 +112,7 @@ private fun DrawerSheetContent(state: MainState, insets: PaddingValues) {
                     icon = Icons.Default.Warning,
                     label = "Reset database",
                     onClick = {
-                        state.scope.launch {
+                        state.scope.launch(IO) {
                             debugDao.reset()
                         }
                     },