소스 검색

Added scope delegation (refactoring)

Vadik Sirekanyan 2 년 전
부모
커밋
1b5b2da7a6
2개의 변경된 파일10개의 추가작업 그리고 10개의 파일을 삭제
  1. 6 6
      app/src/main/java/org/sirekanyan/outline/MainState.kt
  2. 4 4
      app/src/main/java/org/sirekanyan/outline/Router.kt

+ 6 - 6
app/src/main/java/org/sirekanyan/outline/MainState.kt

@@ -47,7 +47,7 @@ class MainState(
     val search: SearchState,
     private val router: Router,
     private val prefs: KeyValueDao,
-) {
+) : CoroutineScope by scope {
 
     val drawer = router.drawer
     val drawerDisabled by derivedStateOf { search.isOpened && drawer.isClosed }
@@ -61,7 +61,7 @@ class MainState(
     val sorting = prefs.observe(Sorting.KEY).map(Sorting::getByKey)
 
     fun putSorting(sorting: Sorting) {
-        scope.launch {
+        launch {
             prefs.put(Sorting.KEY, sorting.key)
         }
     }
@@ -102,14 +102,14 @@ class MainState(
     }
 
     fun onRetryButtonClicked() {
-        scope.launch {
+        launch {
             refreshCurrentKeys(showLoading = true)
         }
     }
 
     fun onAddKeyClicked() {
         selectedPage?.let { page ->
-            scope.launch {
+            launch {
                 isFabLoading = true
                 keys.createKey(page.server)
                 refreshCurrentKeys(showLoading = false)
@@ -120,7 +120,7 @@ class MainState(
     }
 
     fun onDeleteKeyConfirmed(key: Key) {
-        scope.launch {
+        launch {
             deletingKey = key
             keys.deleteKey(key)
             refreshCurrentKeys(showLoading = false)
@@ -131,7 +131,7 @@ class MainState(
     }
 
     fun onDeleteServerConfirmed(server: Server) {
-        scope.launch(Dispatchers.IO) {
+        launch {
             servers.deleteServer(server)
         }
         page = HelloPage

+ 4 - 4
app/src/main/java/org/sirekanyan/outline/Router.kt

@@ -22,23 +22,23 @@ fun rememberRouter(): Router {
 }
 
 class Router(
-    private val scope: CoroutineScope,
+    scope: CoroutineScope,
     val pageState: MutableState<Page>,
     val dialogState: MutableState<Dialog?>,
-) {
+) : CoroutineScope by scope {
 
     val drawer = DrawerState(DrawerValue.Closed)
     var page by pageState
     var dialog by dialogState
 
     fun openDrawer() {
-        scope.launch {
+        launch {
             drawer.open()
         }
     }
 
     fun closeDrawer(animated: Boolean = true) {
-        scope.launch {
+        launch {
             if (animated) {
                 drawer.close()
             } else {