소스 검색

Added actual server names

Vadik Sirekanyan 2 년 전
부모
커밋
291176dc62

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

@@ -15,7 +15,7 @@ import org.sirekanyan.outline.ui.KeyContent
 
 @Composable
 fun MainContent(api: OutlineApi, state: MainState, keys: List<AccessKey>) {
-    ModalNavigationDrawer(drawerContent = { DrawerContent(state) }, drawerState = state.drawer) {
+    ModalNavigationDrawer({ DrawerContent(api, state) }, drawerState = state.drawer) {
         LazyColumn(contentPadding = WindowInsets.systemBars.asPaddingValues()) {
             keys.forEach { key ->
                 item {

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

@@ -10,8 +10,9 @@ import io.ktor.serialization.kotlinx.json.json
 import kotlinx.serialization.json.Json
 import org.sirekanyan.outline.api.model.AccessKey
 import org.sirekanyan.outline.api.model.AccessKeysResponse
+import org.sirekanyan.outline.api.model.ServerNameResponse
 
-val API_URLS: List<Pair<String, String>> = listOf(
+val API_URLS: List<String> = listOf(
     // TODO: add api urls
 )
 
@@ -24,13 +25,16 @@ 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 getAccessKeys(index: Int): List<AccessKey> {
-        val (_, apiUrl) = API_URLS.getOrNull(index) ?: return listOf()
+        val apiUrl = API_URLS.getOrNull(index) ?: return listOf()
         return httpClient.get("$apiUrl/access-keys").body<AccessKeysResponse>().accessKeys
     }
 
     suspend fun createAccessKey(index: Int) {
-        val (_, apiUrl) = API_URLS.getOrNull(index) ?: return
+        val apiUrl = API_URLS.getOrNull(index) ?: return
         httpClient.post("$apiUrl/access-keys")
     }
 

+ 6 - 0
app/src/main/java/org/sirekanyan/outline/api/model/ServerNameResponse.kt

@@ -0,0 +1,6 @@
+package org.sirekanyan.outline.api.model
+
+import kotlinx.serialization.Serializable
+
+@Serializable
+class ServerNameResponse(val name: String)

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

@@ -1,19 +1,26 @@
 package org.sirekanyan.outline.ui
 
+import android.net.Uri
 import androidx.compose.material3.ModalDrawerSheet
 import androidx.compose.material3.NavigationDrawerItem
 import androidx.compose.material3.Text
 import androidx.compose.runtime.Composable
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.produceState
 import org.sirekanyan.outline.MainState
 import org.sirekanyan.outline.api.API_URLS
+import org.sirekanyan.outline.api.OutlineApi
 
 @Composable
-fun DrawerContent(state: MainState) {
+fun DrawerContent(api: OutlineApi, state: MainState) {
     ModalDrawerSheet {
-        API_URLS.forEachIndexed { index, (name) ->
+        API_URLS.forEachIndexed { index, apiUrl ->
             val selected = state.selected == index
+            val serverName by produceState(Uri.parse(apiUrl).host.orEmpty()) {
+                value = api.getServerName(apiUrl)
+            }
             NavigationDrawerItem(
-                label = { Text(name) },
+                label = { Text(serverName) },
                 selected = selected,
                 onClick = {
                     state.selected = index
@@ -22,4 +29,4 @@ fun DrawerContent(state: MainState) {
             )
         }
     }
-}
+}