fix(data): не показывать успех, когда сервер не сохранился
This commit is contained in:
1 parent
9627f2a3c9
commit
66513a243b
4 files changed
+17
-8
No files matched your search
@@ -192,15 +192,17 @@ class AppPreferences(context: Context) {
|
||||
return changed
|
||||
}
|
||||
|
||||
suspend fun addServer(server: Server, activate: Boolean = false): String {
|
||||
suspend fun addServer(server: Server, activate: Boolean = false): String? {
|
||||
var added: String? = null
|
||||
context.dataStore.edit { prefs ->
|
||||
val list = prefs.serversForWrite() ?: return@edit
|
||||
val base = server.name.trim().ifBlank { Server.FALLBACK_NAME }
|
||||
val named = server.copy(name = uniqueServerName(base, list))
|
||||
prefs[SERVERS_JSON] = ServerJson.encodeList(list + named)
|
||||
if (activate || list.isEmpty()) prefs[ACTIVE_SERVER_ID] = named.id
|
||||
added = named.id
|
||||
}
|
||||
return server.id
|
||||
return added
|
||||
}
|
||||
|
||||
suspend fun cloneServer(id: String): String? {
|
||||
|
||||
@@ -61,7 +61,7 @@ class ServerConfigViewModel(
|
||||
fun addManualServer(name: String, onAdded: (String) -> Unit) {
|
||||
viewModelScope.launch {
|
||||
val server = Server(name = name, client = ClientConfig(syncServerSwitches = false))
|
||||
onAdded(prefs.addServer(server))
|
||||
prefs.addServer(server)?.let(onAdded)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -310,10 +310,14 @@ class ServerSetupViewModel(
|
||||
|
||||
// NonCancellable: сервер уже настроен и запущен - уход с экрана не должен
|
||||
// оставить его без записи в приложении.
|
||||
withContext(NonCancellable) {
|
||||
val saved = withContext(NonCancellable) {
|
||||
val server = buildServer(cfg, c, backendPort, wgClientConf)
|
||||
prefs.addServer(server, activate = true)
|
||||
orchestrator.restartProxyIfRunning()
|
||||
prefs.addServer(server, activate = true)?.also { orchestrator.restartProxyIfRunning() }
|
||||
}
|
||||
|
||||
if (saved == null) {
|
||||
fail("Сервер запущен, но профиль не сохранён - список серверов повреждён")
|
||||
return@launch
|
||||
}
|
||||
advance()
|
||||
|
||||
|
||||
@@ -96,11 +96,14 @@ class ImportViewModel(
|
||||
if (!st.canConfirm) return
|
||||
_uiState.update { it.copy(saving = true, saveError = false) }
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val saved = try {
|
||||
prefs.addServer(buildServer(link, st, fallbackName), activate = true)
|
||||
} catch (e: CancellationException) {
|
||||
throw e
|
||||
} catch (e: Exception) {
|
||||
} catch (_: Exception) {
|
||||
null
|
||||
}
|
||||
if (saved == null) {
|
||||
haptics.perform(HapticEvent.ERROR)
|
||||
_uiState.update { it.copy(saving = false, saveError = true) }
|
||||
return@launch
|
||||
|
||||
Reference in new issue
Block a user