ソースを参照

Removed nested access key object (refactoring)

Vadik Sirekanyan 2 年 前
コミット
452a5f407b

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

@@ -46,7 +46,7 @@ class MainActivity : ComponentActivity() {
                                     onConfirm = {
                                         state.scope.launch {
                                             state.deletingKey = key
-                                            state.api.deleteAccessKey(server, key.accessKey.id)
+                                            state.api.deleteAccessKey(server, key.id)
                                             state.refreshCurrentKeys(showLoading = false)
                                             state.refreshHelloPage(key.server)
                                         }.invokeOnCompletion {

+ 1 - 3
app/src/main/java/org/sirekanyan/outline/api/OutlineApi.kt

@@ -17,7 +17,6 @@ import kotlinx.serialization.json.Json
 import okhttp3.OkHttpClient
 import org.sirekanyan.outline.api.model.AccessKeysResponse
 import org.sirekanyan.outline.api.model.Key
-import org.sirekanyan.outline.api.model.Key.AccessKey
 import org.sirekanyan.outline.api.model.RenameRequest
 import org.sirekanyan.outline.api.model.Server
 import org.sirekanyan.outline.api.model.ServerNameResponse
@@ -79,8 +78,7 @@ class OutlineApi {
         val accessKeys = getAccessKeys(server).accessKeys
         val transferMetrics = getTransferMetrics(server)?.bytesTransferredByUserId
         return accessKeys.map {
-            val accessKey = AccessKey(it.id, it.accessUrl, it.name)
-            Key(server, accessKey, transferMetrics?.get(it.id))
+            Key(server, it.id, it.accessUrl, it.name, transferMetrics?.get(it.id))
         }
     }
 

+ 18 - 26
app/src/main/java/org/sirekanyan/outline/api/model/Key.kt

@@ -2,45 +2,37 @@ package org.sirekanyan.outline.api.model
 
 import android.os.Parcelable
 import kotlinx.parcelize.Parcelize
-import org.sirekanyan.outline.api.model.Key.AccessKey
 import org.sirekanyan.outline.db.model.KeyEntity
 import org.sirekanyan.outline.db.model.KeyWithServerEntity
 
 fun List<Key>.toEntities(): List<KeyEntity> =
     map { key ->
-        val accessKey = key.accessKey
-        KeyEntity(key.server.id, accessKey.id, accessKey.accessUrl, accessKey.name, key.traffic)
+        KeyEntity(key.server.id, key.id, key.accessUrl, key.name, key.traffic)
     }
 
 fun List<KeyEntity>.fromEntities(server: Server): List<Key> =
     map { entity ->
-        Key(server, AccessKey(entity.id, entity.url, entity.name), entity.traffic)
+        Key(server, entity.id, entity.url, entity.name, entity.traffic)
     }
 
 fun List<KeyWithServerEntity>.fromEntities(): List<Key> =
     map { entity ->
-        Key(
-            Server(entity.serverId, entity.insecure, entity.serverName, entity.serverTraffic),
-            AccessKey(entity.id, entity.url, entity.name),
-            entity.traffic,
-        )
+        val server = Server(entity.serverId, entity.insecure, entity.serverName, entity.serverTraffic)
+        Key(server, entity.id, entity.url, entity.name, entity.traffic)
     }
 
 @Parcelize
-class Key(val server: Server, val accessKey: AccessKey, val traffic: Long?) : Parcelable {
-
-    @Parcelize
-    class AccessKey(
-        val id: String,
-        val accessUrl: String,
-        val name: String,
-    ) : Parcelable {
-
-        val defaultName: String
-            get() = "Key $id"
-        val nameOrDefault: String
-            get() = name.ifEmpty { defaultName }
-
-    }
-
-}
+class Key(
+    val server: Server,
+    val id: String,
+    val accessUrl: String,
+    val name: String,
+    val traffic: Long?,
+) : Parcelable {
+
+    val defaultName: String
+        get() = "Key $id"
+    val nameOrDefault: String
+        get() = name.ifEmpty { defaultName }
+
+}

+ 1 - 1
app/src/main/java/org/sirekanyan/outline/feature/keys/KeyContent.kt

@@ -30,7 +30,7 @@ fun KeyContent(key: Key, withServer: Boolean, modifier: Modifier, onClick: () ->
     ) {
         Column(Modifier.weight(1f), Arrangement.Center) {
             Text(
-                text = key.accessKey.nameOrDefault,
+                text = key.nameOrDefault,
                 overflow = Ellipsis,
                 maxLines = 1,
             )

+ 1 - 1
app/src/main/java/org/sirekanyan/outline/feature/keys/KeysContent.kt

@@ -33,7 +33,7 @@ fun KeysContent(insets: PaddingValues, state: MainState, keys: List<Key>, sortin
     ) {
         sortedKeys.forEach { key ->
             item {
-                val isDeleting = key.accessKey.accessUrl == state.deletingKey?.accessKey?.accessUrl
+                val isDeleting = key.accessUrl == state.deletingKey?.accessUrl
                 KeyContent(
                     key = key,
                     withServer = state.page is HelloPage,

+ 7 - 7
app/src/main/java/org/sirekanyan/outline/feature/sort/Sorting.kt

@@ -9,28 +9,28 @@ enum class Sorting(val key: String, @StringRes val title: Int, val comparator: C
     ID(
         key = "id",
         title = R.string.outln_sorting_by_id,
-        comparator = compareBy { it.accessKey.id.toLongOrNull() },
+        comparator = compareBy { it.id.toLongOrNull() },
     ),
 
     NAME(
         key = "name",
         title = R.string.outln_sorting_by_name,
-        comparator = compareBy<Key> { it.accessKey.name.isEmpty() }
-            .thenBy { it.accessKey.name.lowercase() }
-            .thenBy { it.accessKey.id.toLongOrNull() },
+        comparator = compareBy<Key> { it.name.isEmpty() }
+            .thenBy { it.name.lowercase() }
+            .thenBy { it.id.toLongOrNull() },
     ),
 
     TRAFFIC(
         key = "traffic",
         title = R.string.outln_sorting_by_traffic,
         comparator = compareByDescending<Key> { it.traffic }
-            .thenBy { it.accessKey.id.toLongOrNull() },
+            .thenBy { it.id.toLongOrNull() },
     );
 
     companion object {
 
         init {
-            check(values().distinctBy(Sorting::key).size == values().size) { "Keys must be unique" }
+            check(entries.distinctBy(Sorting::key).size == entries.size) { "Keys must be unique" }
         }
 
         const val KEY = "Sorting"
@@ -40,7 +40,7 @@ enum class Sorting(val key: String, @StringRes val title: Int, val comparator: C
             key?.let(::findByKey) ?: DEFAULT
 
         private fun findByKey(key: String): Sorting? =
-            values().find { it.key == key }
+            entries.find { it.key == key }
 
     }
 

+ 2 - 3
app/src/main/java/org/sirekanyan/outline/repository/KeyRepository.kt

@@ -7,7 +7,6 @@ import kotlinx.coroutines.flow.map
 import kotlinx.coroutines.withContext
 import org.sirekanyan.outline.api.OutlineApi
 import org.sirekanyan.outline.api.model.Key
-import org.sirekanyan.outline.api.model.Key.AccessKey
 import org.sirekanyan.outline.api.model.Server
 import org.sirekanyan.outline.api.model.fromEntities
 import org.sirekanyan.outline.api.model.toEntities
@@ -29,9 +28,9 @@ class KeyRepository(private val api: OutlineApi, private val keyDao: KeyDao) {
         }
     }
 
-    suspend fun renameKey(server: Server, accessKey: AccessKey, newName: String) {
+    suspend fun renameKey(server: Server, key: Key, newName: String) {
         withContext(IO) {
-            api.renameAccessKey(server, accessKey.id, newName)
+            api.renameAccessKey(server, key.id, newName)
         }
     }
 

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

@@ -13,7 +13,7 @@ import org.sirekanyan.outline.api.model.Key
 @Composable
 fun DeleteKeyContent(key: Key, onDismiss: () -> Unit, onConfirm: () -> Unit) {
     ConfirmationAlertDialog(
-        text = "Are you sure you want to delete the key named \"${key.accessKey.nameOrDefault}\"?",
+        text = "Are you sure you want to delete the key named \"${key.nameOrDefault}\"?",
         onDismiss = onDismiss,
         onConfirm = onConfirm,
     )

+ 3 - 3
app/src/main/java/org/sirekanyan/outline/ui/KeyBottomSheet.kt

@@ -33,14 +33,14 @@ fun KeyBottomSheet(
     val localContext = LocalContext.current
     val coroutineScope = rememberCoroutineScope()
     SimpleBottomSheet(
-        title = key.accessKey.nameOrDefault,
+        title = key.nameOrDefault,
         onDismissRequest = onDismissRequest,
         items = { sheetState ->
             ListItem(
                 headlineContent = { Text("Copy") },
                 leadingContent = { Icon(IconCopy, null) },
                 modifier = Modifier.clickable {
-                    localClipboard.setText(AnnotatedString(key.accessKey.accessUrl))
+                    localClipboard.setText(AnnotatedString(key.accessUrl))
                     localContext.showToast("Copied")
                     coroutineScope.launch {
                         sheetState.hide()
@@ -60,7 +60,7 @@ fun KeyBottomSheet(
                     }
                     localContext.startActivity(
                         Intent(Intent.ACTION_SEND)
-                            .putExtra(Intent.EXTRA_TEXT, key.accessKey.accessUrl)
+                            .putExtra(Intent.EXTRA_TEXT, key.accessUrl)
                             .setType("text/plain")
                     )
                 },

+ 2 - 3
app/src/main/java/org/sirekanyan/outline/ui/RenameKeyContent.kt

@@ -6,9 +6,8 @@ import org.sirekanyan.outline.RenameKeyDialog
 
 @Composable
 fun RenameKeyContent(state: MainState, dialog: RenameKeyDialog) {
-    val accessKey = dialog.key.accessKey
-    RenameContent(state, "Edit key", accessKey.name, accessKey.defaultName) { newName ->
-        state.keys.renameKey(dialog.server, accessKey, newName)
+    RenameContent(state, "Edit key", dialog.key.name, dialog.key.defaultName) { newName ->
+        state.keys.renameKey(dialog.server, dialog.key, newName)
         state.refreshCurrentKeys(showLoading = false)
         state.refreshHelloPage(dialog.server)
     }