Prechádzať zdrojové kódy

Removed server model (refactoring)

Vadik Sirekanyan 2 rokov pred
rodič
commit
2e32edea13

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

@@ -18,7 +18,6 @@ import okhttp3.OkHttpClient
 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
 import org.sirekanyan.outline.db.model.ServerEntity
@@ -62,10 +61,10 @@ class OutlineApi {
         return client.request(server.id + '/' + path) { method = httpMethod; block() }
     }
 
-    suspend fun getServer(server: ServerEntity): Server {
+    suspend fun getServer(server: ServerEntity): ServerEntity {
         val name = request(HttpMethod.Get, server, "server").body<ServerNameResponse>().name
         val transferMetrics = getTransferMetrics(server)?.bytesTransferredByUserId
-        return Server(name, transferMetrics?.values?.sum())
+        return ServerEntity(server.id, server.insecure, name, transferMetrics?.values?.sum())
     }
 
     suspend fun renameServer(server: ServerEntity, name: String) {

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

@@ -5,5 +5,3 @@ import org.sirekanyan.outline.db.model.ServerEntity
 
 fun ServerEntity.getHost(): String =
     Uri.parse(id).host.orEmpty()
-
-class Server(val name: String, val traffic: Long?)

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

@@ -1,7 +1,6 @@
 package org.sirekanyan.outline.repository
 
 import org.sirekanyan.outline.api.OutlineApi
-import org.sirekanyan.outline.api.model.Server
 import org.sirekanyan.outline.api.model.getHost
 import org.sirekanyan.outline.db.model.ServerEntity
 import org.sirekanyan.outline.ext.logDebug
@@ -9,17 +8,17 @@ import java.util.concurrent.ConcurrentHashMap
 
 class ServerRepository(private val api: OutlineApi) {
 
-    private val cache: MutableMap<String, Server> = ConcurrentHashMap()
+    private val cache: MutableMap<String, ServerEntity> = ConcurrentHashMap()
 
-    fun getCachedServer(server: ServerEntity): Server =
-        cache[server.id] ?: Server(server.getHost(), traffic = null)
+    fun getCachedServer(server: ServerEntity): ServerEntity =
+        cache[server.id] ?: server.copy(name = server.getHost(), traffic = null)
 
-    suspend fun fetchServer(server: ServerEntity): Server =
+    suspend fun fetchServer(server: ServerEntity): ServerEntity =
         api.getServer(server).also { fetched ->
             cache[server.id] = fetched
         }
 
-    suspend fun getServer(server: ServerEntity): Server {
+    suspend fun getServer(server: ServerEntity): ServerEntity {
         if (!cache.containsKey(server.id)) {
             try {
                 return fetchServer(server)