ソースを参照

Renamed ServerNameRepository to ServerRepository

Vadik Sirekanyan 2 年 前
コミット
111616d0f2

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

@@ -86,10 +86,10 @@ fun MainContent(api: OutlineApi, dao: ApiUrlDao, state: MainState) {
                     state.refreshCurrentKeys(showLoading = true)
                 }
                 val apiUrl = page.apiUrl
-                val serverName by produceState(state.servers.getDefaultName(apiUrl), apiUrl) {
-                    value = state.servers.getName(apiUrl)
+                val server by produceState(state.servers.getCachedServer(apiUrl), apiUrl) {
+                    value = state.servers.getServer(apiUrl)
                 }
-                MainTopAppBar(serverName) {
+                MainTopAppBar(server.name) {
                     state.openDrawer()
                 }
             }

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

@@ -23,7 +23,7 @@ import org.sirekanyan.outline.feature.keys.KeysErrorState
 import org.sirekanyan.outline.feature.keys.KeysLoadingState
 import org.sirekanyan.outline.feature.keys.KeysState
 import org.sirekanyan.outline.feature.keys.KeysSuccessState
-import org.sirekanyan.outline.repository.ServerNameRepository
+import org.sirekanyan.outline.repository.ServerRepository
 import java.net.ConnectException
 import java.net.UnknownHostException
 
@@ -53,7 +53,7 @@ fun rememberMainState(api: OutlineApi): MainState {
 
 class MainState(val scope: CoroutineScope, private val api: OutlineApi) {
 
-    val servers = ServerNameRepository(api)
+    val servers = ServerRepository(api)
     val drawer = DrawerState(DrawerValue.Closed)
     var page by mutableStateOf<Page>(HelloPage)
     var dialog by mutableStateOf<Dialog?>(null)

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

@@ -16,6 +16,7 @@ import kotlinx.serialization.json.Json
 import org.sirekanyan.outline.api.model.AccessKeysResponse
 import org.sirekanyan.outline.api.model.Key
 import org.sirekanyan.outline.api.model.RenameRequest
+import org.sirekanyan.outline.api.model.Server
 import org.sirekanyan.outline.api.model.ServerNameResponse
 import org.sirekanyan.outline.api.model.TransferMetricsResponse
 
@@ -28,8 +29,8 @@ class OutlineApi {
         engine { https.trustManager = InsecureTrustManager } // TODO: remove insecure http
     }
 
-    suspend fun getServerName(apiUrl: String): String =
-        httpClient.get("$apiUrl/server").body<ServerNameResponse>().name
+    suspend fun getServer(apiUrl: String): Server =
+        Server(httpClient.get("$apiUrl/server").body<ServerNameResponse>().name)
 
     suspend fun getKeys(apiUrl: String): List<Key> {
         val accessKeys = getAccessKeys(apiUrl).accessKeys

+ 3 - 0
app/src/main/java/org/sirekanyan/outline/api/model/Server.kt

@@ -0,0 +1,3 @@
+package org.sirekanyan.outline.api.model
+
+class Server(val name: String)

+ 0 - 31
app/src/main/java/org/sirekanyan/outline/repository/ServerNameRepository.kt

@@ -1,31 +0,0 @@
-package org.sirekanyan.outline.repository
-
-import android.net.Uri
-import android.util.Log
-import org.sirekanyan.outline.api.OutlineApi
-import java.util.concurrent.ConcurrentHashMap
-
-class ServerNameRepository(private val api: OutlineApi) {
-
-    private val cache: MutableMap<String, String> = ConcurrentHashMap()
-
-    fun getDefaultName(apiUrl: String): String =
-        cache[apiUrl] ?: Uri.parse(apiUrl).host.orEmpty()
-
-    suspend fun fetchName(apiUrl: String): String =
-        api.getServerName(apiUrl).also { fetched ->
-            cache[apiUrl] = fetched
-        }
-
-    suspend fun getName(apiUrl: String): String {
-        if (!cache.containsKey(apiUrl)) {
-            try {
-                return fetchName(apiUrl)
-            } catch (exception: Exception) {
-                Log.d("OUTLINE", "Cannot fetch server name", exception)
-            }
-        }
-        return getDefaultName(apiUrl)
-    }
-
-}

+ 32 - 0
app/src/main/java/org/sirekanyan/outline/repository/ServerRepository.kt

@@ -0,0 +1,32 @@
+package org.sirekanyan.outline.repository
+
+import android.net.Uri
+import android.util.Log
+import org.sirekanyan.outline.api.OutlineApi
+import org.sirekanyan.outline.api.model.Server
+import java.util.concurrent.ConcurrentHashMap
+
+class ServerRepository(private val api: OutlineApi) {
+
+    private val cache: MutableMap<String, Server> = ConcurrentHashMap()
+
+    fun getCachedServer(apiUrl: String): Server =
+        cache[apiUrl] ?: Server(Uri.parse(apiUrl).host.orEmpty())
+
+    suspend fun fetchServer(apiUrl: String): Server =
+        api.getServer(apiUrl).also { fetched ->
+            cache[apiUrl] = fetched
+        }
+
+    suspend fun getServer(apiUrl: String): Server {
+        if (!cache.containsKey(apiUrl)) {
+            try {
+                return fetchServer(apiUrl)
+            } catch (exception: Exception) {
+                Log.d("OUTLINE", "Cannot fetch server name", exception)
+            }
+        }
+        return getCachedServer(apiUrl)
+    }
+
+}

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

@@ -38,7 +38,7 @@ fun AddServerContent(dao: ApiUrlDao, state: MainState) {
     suspend fun onAddClick() {
         try {
             isLoading = true
-            state.servers.fetchName(draft)
+            state.servers.fetchServer(draft)
             dao.insertUrl(draft)
             state.dialog = null
             state.page = SelectedPage(draft)

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

@@ -34,12 +34,12 @@ fun DrawerContent(dao: ApiUrlDao, state: MainState) {
         val apiUrls by remember { dao.observeUrls() }.collectAsState(listOf())
         apiUrls.forEach { apiUrl ->
             val isSelected = state.selectedPage?.apiUrl == apiUrl
-            val serverName by produceState(state.servers.getDefaultName(apiUrl), state.drawer.isOpen) {
-                value = state.servers.getName(apiUrl)
+            val server by produceState(state.servers.getCachedServer(apiUrl), state.drawer.isOpen) {
+                value = state.servers.getServer(apiUrl)
             }
             NavigationDrawerItem(
                 icon = { Icon(Icons.Default.Done, null) },
-                label = { Text(serverName, style = MaterialTheme.typography.labelLarge) },
+                label = { Text(server.name, style = MaterialTheme.typography.labelLarge) },
                 modifier = Modifier.padding(horizontal = 12.dp),
                 selected = isSelected,
                 onClick = {