Bläddra i källkod

Added navigation drawer with list of servers

Vadik Sirekanyan 2 år sedan
förälder
incheckning
3847e1c2b0

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

@@ -19,10 +19,13 @@ class MainActivity : ComponentActivity() {
         super.onCreate(savedInstanceState)
         setContent {
             val api = remember { OutlineApi() }
-            val accessKeys by produceState(listOf<AccessKey>()) { value = api.getAccessKeys() }
+            val state = remember { MainState() }
+            val accessKeys by produceState(listOf<AccessKey>(), state.selected) {
+                value = state.selected?.let { api.getAccessKeys(it) } ?: listOf()
+            }
             OutlineTheme {
                 Surface(Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
-                    MainContent(accessKeys)
+                    MainContent(state, accessKeys)
                 }
             }
         }

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

@@ -1,16 +1,20 @@
 package org.sirekanyan.outline
 
 import androidx.compose.foundation.lazy.LazyColumn
+import androidx.compose.material3.ModalNavigationDrawer
 import androidx.compose.runtime.Composable
 import org.sirekanyan.outline.api.model.AccessKey
+import org.sirekanyan.outline.ui.DrawerContent
 import org.sirekanyan.outline.ui.KeyContent
 
 @Composable
-fun MainContent(keys: List<AccessKey>) {
-    LazyColumn {
-        keys.forEach { key ->
-            item {
-                KeyContent(key)
+fun MainContent(state: MainState, keys: List<AccessKey>) {
+    ModalNavigationDrawer(drawerContent = { DrawerContent(state) }, drawerState = state.drawer) {
+        LazyColumn {
+            keys.forEach { key ->
+                item {
+                    KeyContent(key)
+                }
             }
         }
     }

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

@@ -0,0 +1,12 @@
+package org.sirekanyan.outline
+
+import androidx.compose.material3.DrawerState
+import androidx.compose.material3.DrawerValue
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.setValue
+
+class MainState {
+    val drawer = DrawerState(DrawerValue.Closed)
+    var selected by mutableStateOf<Int?>(null)
+}

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

@@ -10,7 +10,9 @@ import kotlinx.serialization.json.Json
 import org.sirekanyan.outline.api.model.AccessKey
 import org.sirekanyan.outline.api.model.AccessKeysResponse
 
-private const val API_URL = "" // TODO: add api url
+val API_URLS: List<Pair<String, String>> = listOf(
+    // TODO: add api urls
+)
 
 class OutlineApi {
 
@@ -21,7 +23,9 @@ class OutlineApi {
         engine { https.trustManager = InsecureTrustManager } // TODO: remove insecure http
     }
 
-    suspend fun getAccessKeys(): List<AccessKey> =
-        httpClient.get("$API_URL/access-keys").body<AccessKeysResponse>().accessKeys
+    suspend fun getAccessKeys(index: Int): List<AccessKey> {
+        val (_, apiUrl) = API_URLS.getOrNull(index) ?: return listOf()
+        return httpClient.get("$apiUrl/access-keys").body<AccessKeysResponse>().accessKeys
+    }
 
 }

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

@@ -0,0 +1,28 @@
+package org.sirekanyan.outline.ui
+
+import androidx.compose.material3.ModalDrawerSheet
+import androidx.compose.material3.NavigationDrawerItem
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.rememberCoroutineScope
+import kotlinx.coroutines.launch
+import org.sirekanyan.outline.MainState
+import org.sirekanyan.outline.api.API_URLS
+
+@Composable
+fun DrawerContent(state: MainState) {
+    val scope = rememberCoroutineScope()
+    ModalDrawerSheet {
+        API_URLS.forEachIndexed { index, (name) ->
+            val selected = state.selected == index
+            NavigationDrawerItem(
+                label = { Text(name) },
+                selected = selected,
+                onClick = {
+                    state.selected = index
+                    scope.launch { state.drawer.close() }
+                }
+            )
+        }
+    }
+}