-
Notifications
You must be signed in to change notification settings - Fork 4
Migrate from room to data store #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 0 additions & 1 deletion
1
panel-no-op/src/main/kotlin/com/redmadrobot/debug/noop/plugin/servers/DebugServer.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 12 additions & 58 deletions
70
...ervers/src/main/kotlin/com/redmadrobot/debug/plugin/servers/data/DebugServerRepository.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...rvers/src/main/kotlin/com/redmadrobot/debug/plugin/servers/data/model/DebugServersData.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ) |
22 changes: 0 additions & 22 deletions
22
...vers/src/main/kotlin/com/redmadrobot/debug/plugin/servers/data/storage/DebugServersDao.kt
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
...rc/main/kotlin/com/redmadrobot/debug/plugin/servers/data/storage/ServersDataSerializer.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
...ers/src/main/kotlin/com/redmadrobot/debug/plugin/servers/data/storage/ServersDataStore.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| } |
30 changes: 0 additions & 30 deletions
30
...rc/main/kotlin/com/redmadrobot/debug/plugin/servers/data/storage/ServersPluginDatabase.kt
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
...rvers/src/main/kotlin/com/redmadrobot/debug/plugin/servers/data/storage/ServersStorage.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ) |
13 changes: 0 additions & 13 deletions
13
...ain/kotlin/com/redmadrobot/debug/plugin/servers/data/storage/SharedPreferencesProvider.kt
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.