Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.redmadrobot.debug.plugin.servers.data.model

data class DebugServer(
val id: Int = 0,
val name: String,
val url: String,
val isDefault: Boolean = false
Expand Down
6 changes: 2 additions & 4 deletions plugins/plugin-servers/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
alias(stack.plugins.ksp)
id("convention.debug.panel.plugin")
alias(stack.plugins.kotlin.serialization)
}

description = "Plugin for switching server hosts"
Expand All @@ -11,9 +11,7 @@ android {

dependencies {
implementation(androidx.core)
implementation(androidx.room)
implementation(androidx.room.runtime)
implementation(androidx.datastore)
implementation(stack.kotlinx.serialization.json)
implementation(stack.okhttp)
ksp(androidx.room.compiler)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ import com.redmadrobot.debug.core.internal.CommonContainer
import com.redmadrobot.debug.core.internal.PluginDependencyContainer
import com.redmadrobot.debug.plugin.servers.data.DebugServerRepository
import com.redmadrobot.debug.plugin.servers.data.model.DebugServer
import com.redmadrobot.debug.plugin.servers.data.storage.ServersPluginDatabase
import com.redmadrobot.debug.plugin.servers.data.storage.ServersDataStore
import com.redmadrobot.debug.plugin.servers.ui.ServersViewModel

internal class ServersPluginContainer(
private val preinstalledServers: List<DebugServer>,
private val container: CommonContainer
) : PluginDependencyContainer {
private val pluginStorage by lazy { ServersPluginDatabase.getInstance(container.context) }
private val serversDataStore by lazy { ServersDataStore(container.context) }

val serversRepository by lazy {
DebugServerRepository(
context = container.context,
debugServersDao = pluginStorage.getDebugServersDao(),
serversDataStore = serversDataStore,
preInstalledServers = preinstalledServers,
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,76 +1,30 @@
package com.redmadrobot.debug.plugin.servers.data

import android.content.Context
import androidx.core.content.edit
import com.redmadrobot.debug.plugin.servers.data.model.DebugServer
import com.redmadrobot.debug.plugin.servers.data.storage.DebugServersDao
import com.redmadrobot.debug.plugin.servers.data.storage.SharedPreferencesProvider
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import com.redmadrobot.debug.plugin.servers.data.storage.ServersDataStore

internal class DebugServerRepository(
private val context: Context,
private val debugServersDao: DebugServersDao,
private val serversDataStore: ServersDataStore,
private val preInstalledServers: List<DebugServer>
) {
private val sharedPreferences by lazy {
SharedPreferencesProvider.get(context)
}
fun getPreInstalledServers(): List<DebugServer> = preInstalledServers

fun getPreInstalledServers(): List<DebugServer> {
return preInstalledServers
}
fun getDefault(): DebugServer = preInstalledServers.first { it.isDefault }

fun saveSelectedServer(selectedServer: DebugServer) {
sharedPreferences.edit {
putString(SELECTED_SERVER_NAME, selectedServer.name)
putString(SELECTED_SERVER_URL, selectedServer.url)
}
suspend fun saveSelectedServer(selectedServer: DebugServer) {
serversDataStore.saveSelected(selectedServer)
}

suspend fun getSelectedServer(): DebugServer {
val serverName = sharedPreferences.getString(SELECTED_SERVER_NAME, null)
val serverUrl = sharedPreferences.getString(SELECTED_SERVER_URL, null)

return if (serverName != null && serverUrl != null) {
preInstalledServers.find { it.name == serverName && it.url == serverUrl }
?: debugServersDao.getServer(serverName, serverUrl)
?: getDefault()
} else {
getDefault()
}
return serversDataStore.getSelected() ?: getDefault()
}

fun getDefault(): DebugServer {
return preInstalledServers.first { it.isDefault }
}
suspend fun addServer(server: DebugServer) = serversDataStore.add(server)

suspend fun addServer(server: DebugServer) {
withContext(Dispatchers.IO) {
debugServersDao.insert(server)
}
}
suspend fun getServers(): List<DebugServer> = serversDataStore.getAll()

suspend fun getServers(): List<DebugServer> {
return withContext(Dispatchers.IO) {
debugServersDao.getAll()
}
}
suspend fun removeServer(server: DebugServer) = serversDataStore.remove(server)

suspend fun removeServer(server: DebugServer) {
withContext(Dispatchers.IO) {
debugServersDao.remove(server)
}
}

suspend fun updateServer(server: DebugServer) {
withContext(Dispatchers.IO) {
debugServersDao.update(server)
}
}

companion object {
private const val SELECTED_SERVER_URL = "SELECTED_SERVER_URL"
private const val SELECTED_SERVER_NAME = "SELECTED_SERVER_NAME"
}
suspend fun updateServer(oldServer: DebugServer, newServer: DebugServer) =
serversDataStore.update(oldServer, newServer)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.redmadrobot.debug.plugin.servers.data.model

import androidx.room.Entity
import androidx.room.PrimaryKey
import kotlinx.serialization.Serializable

@Entity(tableName = DebugServer.TABLE_NAME)
@Serializable
public data class DebugServer(
@PrimaryKey(autoGenerate = true)
val id: Int = 0,
val name: String,
val url: String,
val isDefault: Boolean = false
Expand All @@ -15,8 +12,4 @@ public data class DebugServer(
val otherServer = other as DebugServer
return this.name == otherServer.name && this.url == otherServer.url
}

internal companion object {
const val TABLE_NAME = "debug_server"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.redmadrobot.debug.plugin.servers.data.model

import kotlinx.serialization.Serializable

@Serializable
internal data class DebugServersData(
val servers: List<DebugServer> = emptyList(),
val selectedServer: DebugServer? = null
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.redmadrobot.debug.plugin.servers.data.storage

import androidx.datastore.core.Serializer
import com.redmadrobot.debug.plugin.servers.data.model.DebugServersData
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream
import kotlinx.serialization.json.encodeToStream
import java.io.InputStream
import java.io.OutputStream

@OptIn(ExperimentalSerializationApi::class)
internal object ServersDataSerializer : Serializer<DebugServersData> {
override val defaultValue: DebugServersData = DebugServersData()

override suspend fun readFrom(input: InputStream): DebugServersData {
return Json.decodeFromStream(input)
}

override suspend fun writeTo(t: DebugServersData, output: OutputStream) {
Json.encodeToStream(t, output)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.redmadrobot.debug.plugin.servers.data.storage

import android.content.Context
import com.redmadrobot.debug.plugin.servers.data.model.DebugServer
import kotlinx.coroutines.flow.first

internal class ServersDataStore(private val context: Context) {
private val dataStore by lazy { context.serversStorage }

suspend fun getAll(): List<DebugServer> {
return dataStore.data.first().servers
}

suspend fun add(server: DebugServer) {
dataStore.updateData { data -> data.copy(servers = data.servers + server) }
}

suspend fun remove(server: DebugServer) {
dataStore.updateData { data -> data.copy(servers = data.servers - server) }
}

suspend fun update(oldServer: DebugServer, newServer: DebugServer) {
dataStore.updateData { data ->
val updatedServers = data.servers.map { if (it == oldServer) newServer else it }
data.copy(servers = updatedServers)
}
}

suspend fun saveSelected(server: DebugServer) {
dataStore.updateData { data -> data.copy(selectedServer = server) }
}

suspend fun getSelected(): DebugServer? {
return dataStore.data.first().selectedServer
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.redmadrobot.debug.plugin.servers.data.storage

import android.content.Context
import androidx.datastore.dataStore

internal val Context.serversStorage by dataStore(
fileName = "plugin_servers.json",
serializer = ServersDataSerializer
)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ internal class ServersViewModel(
return
}

if (dialogState.editableServerId == null) {
if (dialogState.editableServer == null) {
addServer(dialogState.serverName, dialogState.serverUrl)
} else {
updateServerData(
dialogState.editableServerId,
dialogState.serverName,
dialogState.serverUrl
oldServer = dialogState.editableServer,
name = dialogState.serverName,
url = dialogState.serverUrl
)
}

Expand Down Expand Up @@ -126,20 +126,15 @@ internal class ServersViewModel(
}
}

private fun updateServerData(id: Int, name: String, url: String) {
val serverForUpdate = _state.value.addedServers
.find { it.server.id == id }
?.server

serverForUpdate?.let {
val updatedServer = serverForUpdate.copy(name = name, url = url)
viewModelScope.safeLaunch {
serversRepository.updateServer(updatedServer)
_state.update { serversState ->
serversState.copy(
addedServers = serversRepository.getServers().mapToServerItems()
)
}
private fun updateServerData(oldServer: DebugServer, name: String, url: String) {
val updatedServer = oldServer.copy(name = name, url = url)

viewModelScope.safeLaunch {
serversRepository.updateServer(oldServer = oldServer, newServer = updatedServer)
_state.update { serversState ->
serversState.copy(
addedServers = serversRepository.getServers().mapToServerItems()
)
}
}
}
Expand All @@ -148,7 +143,7 @@ internal class ServersViewModel(
_state.update { serversState ->
serversState.copy(
serverDialogState = ServerDialogState(
editableServerId = debugServer.id,
editableServer = debugServer,
serverName = debugServer.name,
serverUrl = debugServer.url,
show = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal data class ServerDialogState(
val show: Boolean = false,
val serverName: String = "",
val serverUrl: String = "",
val editableServerId: Int? = null,
val editableServer: DebugServer? = null,
val inputErrors: ServerDialogErrors? = null
)

Expand Down
Loading