-
Notifications
You must be signed in to change notification settings - Fork 55
[#2215] Add automatic manual server selection #2214
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
Closed
czarcas7ic
wants to merge
8
commits into
zodl-inc:main
from
valargroup:adam/android-auto-manual-server-mode
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
78aec84
Add automatic manual server selection
czarcas7ic 3c560dc
Align server selection UI with Figma
czarcas7ic 367ddef
Reject invalid custom server saves
czarcas7ic b59ed47
Keep automatic server endpoint current
czarcas7ic b34c6f0
Use current SDK rewind API
czarcas7ic 7e82624
Harden automatic server endpoint updates
czarcas7ic 7585d7a
Reconcile manual server selection
czarcas7ic 0272d6d
Harden server selection persistence
czarcas7ic 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
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
71 changes: 71 additions & 0 deletions
71
ui-lib/src/main/java/co/electriccoin/zcash/ui/common/model/ServerSelection.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,71 @@ | ||
| package co.electriccoin.zcash.ui.common.model | ||
|
|
||
| import co.electriccoin.lightwallet.client.model.LightWalletEndpoint | ||
| import org.json.JSONObject | ||
|
|
||
| data class ServerSelection( | ||
| val mode: ConnectionMode, | ||
| val endpoint: LightWalletEndpoint? = null | ||
| ) { | ||
| init { | ||
| require(mode == ConnectionMode.AUTOMATIC || endpoint != null) { | ||
| "Manual server selection requires an endpoint" | ||
| } | ||
| } | ||
|
|
||
| fun toJson() = | ||
| JSONObject().apply { | ||
| put(KEY_MODE, mode.persistedValue) | ||
| endpoint?.let { | ||
| put(KEY_ENDPOINT_HOST, it.host) | ||
| put(KEY_ENDPOINT_PORT, it.port) | ||
| put(KEY_ENDPOINT_IS_SECURE, it.isSecure) | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| private const val KEY_MODE = "mode" | ||
| private const val KEY_ENDPOINT_HOST = "endpoint_host" | ||
| private const val KEY_ENDPOINT_PORT = "endpoint_port" | ||
| private const val KEY_ENDPOINT_IS_SECURE = "endpoint_is_secure" | ||
|
|
||
| fun automatic() = ServerSelection(ConnectionMode.AUTOMATIC) | ||
|
|
||
| fun manual(endpoint: LightWalletEndpoint) = | ||
| ServerSelection( | ||
| mode = ConnectionMode.MANUAL, | ||
| endpoint = endpoint | ||
| ) | ||
|
|
||
| fun from(jsonObject: JSONObject): ServerSelection { | ||
| val mode = | ||
| ConnectionMode.fromPersistedValue( | ||
| jsonObject.getString(KEY_MODE) | ||
| ) | ||
|
|
||
| return when (mode) { | ||
| ConnectionMode.AUTOMATIC -> automatic() | ||
| ConnectionMode.MANUAL -> manual(jsonObject.getEndpoint()) | ||
| } | ||
| } | ||
|
|
||
| private fun JSONObject.getEndpoint() = | ||
| LightWalletEndpoint( | ||
| host = getString(KEY_ENDPOINT_HOST), | ||
| port = getInt(KEY_ENDPOINT_PORT), | ||
| isSecure = getBoolean(KEY_ENDPOINT_IS_SECURE) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| enum class ConnectionMode( | ||
| val persistedValue: String | ||
| ) { | ||
| AUTOMATIC("automatic"), | ||
| MANUAL("manual"); | ||
|
|
||
| companion object { | ||
| fun fromPersistedValue(value: String) = | ||
| entries.firstOrNull { it.persistedValue == value } ?: AUTOMATIC | ||
| } | ||
| } |
52 changes: 52 additions & 0 deletions
52
ui-lib/src/main/java/co/electriccoin/zcash/ui/common/provider/ServerSelectionProvider.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,52 @@ | ||
| package co.electriccoin.zcash.ui.common.provider | ||
|
|
||
| import co.electriccoin.zcash.preference.EncryptedPreferenceProvider | ||
| import co.electriccoin.zcash.preference.api.PreferenceProvider | ||
| import co.electriccoin.zcash.preference.model.entry.PreferenceDefault | ||
| import co.electriccoin.zcash.preference.model.entry.PreferenceKey | ||
| import co.electriccoin.zcash.ui.common.model.ServerSelection | ||
| import kotlinx.coroutines.flow.Flow | ||
| import org.json.JSONObject | ||
|
|
||
| interface ServerSelectionProvider { | ||
| val serverSelection: Flow<ServerSelection?> | ||
|
|
||
| suspend fun store(serverSelection: ServerSelection) | ||
|
|
||
| suspend fun getServerSelection(): ServerSelection? | ||
| } | ||
|
|
||
| class ServerSelectionProviderImpl( | ||
| preferenceHolder: EncryptedPreferenceProvider | ||
| ) : ServerSelectionProvider { | ||
| private val storageProvider = ServerSelectionStorageProviderImpl(preferenceHolder) | ||
|
|
||
| override val serverSelection = storageProvider.observe() | ||
|
|
||
| override suspend fun store(serverSelection: ServerSelection) { | ||
| storageProvider.store(serverSelection) | ||
| } | ||
|
|
||
| override suspend fun getServerSelection() = storageProvider.get() | ||
| } | ||
|
|
||
| private interface ServerSelectionStorageProvider : NullableStorageProvider<ServerSelection> | ||
|
|
||
| private class ServerSelectionStorageProviderImpl( | ||
| override val preferenceHolder: EncryptedPreferenceProvider, | ||
| ) : BaseNullableStorageProvider<ServerSelection>(), | ||
| ServerSelectionStorageProvider { | ||
| override val default = ServerSelectionPreferenceDefault(PreferenceKey("server_selection")) | ||
| } | ||
|
|
||
| private class ServerSelectionPreferenceDefault( | ||
| override val key: PreferenceKey | ||
| ) : PreferenceDefault<ServerSelection?> { | ||
| override suspend fun getValue(preferenceProvider: PreferenceProvider) = | ||
| preferenceProvider.getString(key)?.let { ServerSelection.from(JSONObject(it)) } | ||
|
|
||
| override suspend fun putValue( | ||
| preferenceProvider: PreferenceProvider, | ||
| newValue: ServerSelection? | ||
| ) = preferenceProvider.putString(key, newValue?.toJson()?.toString()) | ||
| } |
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
17 changes: 17 additions & 0 deletions
17
ui-lib/src/main/java/co/electriccoin/zcash/ui/common/usecase/GetServerSelectionUseCase.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,17 @@ | ||
| package co.electriccoin.zcash.ui.common.usecase | ||
|
|
||
| import co.electriccoin.zcash.ui.common.model.ServerSelection | ||
| import co.electriccoin.zcash.ui.common.provider.ServerSelectionProvider | ||
| import kotlinx.coroutines.flow.distinctUntilChanged | ||
| import kotlinx.coroutines.flow.map | ||
|
|
||
| class GetServerSelectionUseCase( | ||
| private val serverSelectionProvider: ServerSelectionProvider | ||
| ) { | ||
| fun observe() = | ||
| serverSelectionProvider.serverSelection | ||
| .map { it ?: ServerSelection.automatic() } | ||
| .distinctUntilChanged() | ||
|
|
||
| suspend operator fun invoke() = serverSelectionProvider.getServerSelection() ?: ServerSelection.automatic() | ||
| } |
5 changes: 5 additions & 0 deletions
5
ui-lib/src/main/java/co/electriccoin/zcash/ui/common/usecase/PersistEndpointException.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,5 @@ | ||
| package co.electriccoin.zcash.ui.common.usecase | ||
|
|
||
| class PersistEndpointException( | ||
| message: String? | ||
| ) : Exception(message) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drive by