Jelajahi Sumber

Added server traffic

Vadik Sirekanyan 2 tahun lalu
induk
melakukan
b69ae974d7

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

@@ -29,8 +29,11 @@ class OutlineApi {
         engine { https.trustManager = InsecureTrustManager } // TODO: remove insecure http
     }
 
-    suspend fun getServer(apiUrl: String): Server =
-        Server(httpClient.get("$apiUrl/server").body<ServerNameResponse>().name)
+    suspend fun getServer(apiUrl: String): Server {
+        val name = httpClient.get("$apiUrl/server").body<ServerNameResponse>().name
+        val transferMetrics = getTransferMetrics(apiUrl).bytesTransferredByUserId
+        return Server(name, transferMetrics.values.sum())
+    }
 
     suspend fun getKeys(apiUrl: String): List<Key> {
         val accessKeys = getAccessKeys(apiUrl).accessKeys

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

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

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

@@ -11,7 +11,7 @@ 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())
+        cache[apiUrl] ?: Server(Uri.parse(apiUrl).host.orEmpty(), traffic = null)
 
     suspend fun fetchServer(apiUrl: String): Server =
         api.getServer(apiUrl).also { fetched ->

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

@@ -22,6 +22,7 @@ import org.sirekanyan.outline.MainState
 import org.sirekanyan.outline.R
 import org.sirekanyan.outline.SelectedPage
 import org.sirekanyan.outline.db.ApiUrlDao
+import org.sirekanyan.outline.text.formatTraffic
 
 @Composable
 fun DrawerContent(dao: ApiUrlDao, state: MainState) {
@@ -40,6 +41,15 @@ fun DrawerContent(dao: ApiUrlDao, state: MainState) {
             NavigationDrawerItem(
                 icon = { Icon(Icons.Default.Done, null) },
                 label = { Text(server.name, style = MaterialTheme.typography.labelLarge) },
+                badge = {
+                    server.traffic?.let { traffic ->
+                        Text(
+                            text = formatTraffic(traffic),
+                            color = MaterialTheme.colorScheme.primary,
+                            style = MaterialTheme.typography.labelLarge,
+                        )
+                    }
+                },
                 modifier = Modifier.padding(horizontal = 12.dp),
                 selected = isSelected,
                 onClick = {