Skip to content

Commit de9d0ea

Browse files
committed
Replace validationError with alerts
Remove the inline validationError state and its on-screen text. Introduce showUpdateFailedAlert and an alert UI for update failures, and wire error paths to present this alert (while keeping the existing invalid-URL alert). Update error handling in save/update/clear flows to set the new alert flag instead of storing validationError.
1 parent a9270ae commit de9d0ea

3 files changed

Lines changed: 38 additions & 30 deletions

File tree

android/app/src/main/java/org/bitcoinppl/cove/flows/SettingsFlow/BlockExplorerSettingsScreen.kt

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ import kotlinx.coroutines.Dispatchers
5252
import kotlinx.coroutines.launch
5353
import kotlinx.coroutines.withContext
5454
import org.bitcoinppl.cove.R
55+
import org.bitcoinppl.cove.TaggedItem
5556
import org.bitcoinppl.cove.views.MaterialDivider
5657
import org.bitcoinppl.cove.views.MaterialSection
5758
import org.bitcoinppl.cove.views.SectionHeader
59+
import org.bitcoinppl.cove_core.AppAlertState
5860
import org.bitcoinppl.cove_core.BlockExplorerOption
5961
import org.bitcoinppl.cove_core.Database
6062
import org.bitcoinppl.cove_core.allBlockExplorerOptions
@@ -71,6 +73,10 @@ fun BlockExplorerSettingsScreen(
7173
val snackbarHostState = remember { SnackbarHostState() }
7274
val keyboardController = LocalSoftwareKeyboardController.current
7375
val savedMessage = stringResource(R.string.block_explorer_saved)
76+
val invalidUrlTitle = stringResource(R.string.block_explorer_invalid_url_title)
77+
val invalidUrlMessage = stringResource(R.string.block_explorer_invalid_url_message)
78+
val updateFailedTitle = stringResource(R.string.block_explorer_update_failed_title)
79+
val updateFailedMessage = stringResource(R.string.block_explorer_update_failed_message)
7480

7581
// only Bitcoin explorer overrides are editable; other networks use built-in defaults
7682
val editableNetworks = remember { listOf(Network.BITCOIN) }
@@ -84,24 +90,33 @@ fun BlockExplorerSettingsScreen(
8490
var selectedOption by remember(selectedNetwork) {
8591
mutableStateOf(config.selectedBlockExplorerOption(selectedNetwork))
8692
}
87-
var validationError by remember(selectedNetwork) { mutableStateOf<String?>(null) }
8893
var isSaving by remember(selectedNetwork) { mutableStateOf(false) }
8994
val blockExplorerOptions = remember { allBlockExplorerOptions() }
9095

96+
fun showAlert(
97+
title: String,
98+
message: String,
99+
) {
100+
app.alertState =
101+
TaggedItem(
102+
AppAlertState.General(
103+
title = title,
104+
message = message,
105+
),
106+
)
107+
}
108+
91109
fun reload() {
92110
input = config.customBlockExplorer(selectedNetwork) ?: ""
93111
preview = config.effectiveBlockExplorerPreview(selectedNetwork)
94112
selectedOption = config.selectedBlockExplorerOption(selectedNetwork)
95-
validationError = null
96113
}
97114

98115
fun updatePreview(value: String) {
99116
try {
100117
preview = config.previewCustomBlockExplorer(selectedNetwork, value)
101-
validationError = null
102-
} catch (error: Exception) {
118+
} catch (e: Exception) {
103119
preview = ""
104-
validationError = error.message ?: error.toString()
105120
}
106121
}
107122

@@ -120,15 +135,14 @@ fun BlockExplorerSettingsScreen(
120135
input = normalized ?: ""
121136
preview = config.effectiveBlockExplorerPreview(networkToSave)
122137
selectedOption = config.selectedBlockExplorerOption(networkToSave)
123-
validationError = null
124138
keyboardController?.hide()
125139
isSaving = false
126140

127141
launch {
128142
snackbarHostState.showSnackbar(savedMessage)
129143
}
130-
} catch (error: Exception) {
131-
validationError = error.message ?: error.toString()
144+
} catch (e: Exception) {
145+
showAlert(invalidUrlTitle, invalidUrlMessage)
132146
isSaving = false
133147
}
134148
}
@@ -139,18 +153,17 @@ fun BlockExplorerSettingsScreen(
139153
input = config.setBlockExplorerOption(selectedNetwork, option) ?: ""
140154
preview = config.effectiveBlockExplorerPreview(selectedNetwork)
141155
selectedOption = config.selectedBlockExplorerOption(selectedNetwork)
142-
validationError = null
143-
} catch (error: Exception) {
144-
validationError = error.message ?: error.toString()
156+
} catch (e: Exception) {
157+
showAlert(updateFailedTitle, updateFailedMessage)
145158
}
146159
}
147160

148161
fun reset() {
149162
try {
150163
config.clearCustomBlockExplorer(selectedNetwork)
151164
reload()
152-
} catch (error: Exception) {
153-
validationError = error.message ?: error.toString()
165+
} catch (e: Exception) {
166+
showAlert(updateFailedTitle, updateFailedMessage)
154167
}
155168
}
156169

@@ -286,8 +299,6 @@ fun BlockExplorerSettingsScreen(
286299
),
287300
keyboardActions = KeyboardActions(onDone = { save() }),
288301
singleLine = true,
289-
isError = validationError != null,
290-
supportingText = validationError?.let { error -> { Text(error) } },
291302
modifier = Modifier.fillMaxWidth(),
292303
)
293304

android/app/src/main/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
<string name="block_explorer_save">Save</string>
3535
<string name="block_explorer_saved">Block explorer saved successfully</string>
3636
<string name="block_explorer_reset">Reset to Default</string>
37+
<string name="block_explorer_invalid_url_title">Invalid URL</string>
38+
<string name="block_explorer_invalid_url_message">Enter a valid URL, IP address, or block explorer template.</string>
39+
<string name="block_explorer_update_failed_title">Unable to Update Block Explorer</string>
40+
<string name="block_explorer_update_failed_message">Try again later.</string>
3741
<string name="title_wallet_information">Wallet information</string>
3842
<string name="label_wallet_network">Network</string>
3943
<string name="label_wallet_birthday">Birthday</string>

ios/Cove/Flows/SettingsFlow/BlockExplorerSettingsView.swift

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ struct BlockExplorerSettingsView: View {
88
@State private var input: String
99
@State private var preview: String
1010
@State private var selectedOption: BlockExplorerOption
11-
@State private var validationError: String?
1211
@State private var isSaving = false
1312
@State private var showInvalidUrlAlert = false
13+
@State private var showUpdateFailedAlert = false
1414
@FocusState private var isInputFocused: Bool
1515

1616
init() {
@@ -77,12 +77,6 @@ struct BlockExplorerSettingsView: View {
7777
}
7878
.onSubmit(save)
7979

80-
if let validationError {
81-
Text(validationError)
82-
.font(.caption)
83-
.foregroundStyle(.red)
84-
}
85-
8680
Button("Save", action: save)
8781
.disabled(isSaving || input == (config.customBlockExplorer(network: selectedNetwork) ?? ""))
8882

@@ -100,6 +94,11 @@ struct BlockExplorerSettingsView: View {
10094
} message: {
10195
Text("Enter a valid URL, IP address, or block explorer template.")
10296
}
97+
.alert("Unable to Update Block Explorer", isPresented: $showUpdateFailedAlert) {
98+
Button("OK", role: .cancel) {}
99+
} message: {
100+
Text("Try again later.")
101+
}
103102
}
104103

105104
private func blockExplorerOptionRow(_ option: BlockExplorerOption) -> some View {
@@ -141,17 +140,15 @@ struct BlockExplorerSettingsView: View {
141140
input = normalized ?? ""
142141
preview = config.effectiveBlockExplorerPreview(network: selectedNetwork)
143142
selectedOption = config.selectedBlockExplorerOption(network: selectedNetwork)
144-
validationError = nil
145143
} catch {
146-
validationError = error.localizedDescription
144+
showUpdateFailedAlert = true
147145
}
148146
}
149147

150148
private func reload() {
151149
input = config.customBlockExplorer(network: selectedNetwork) ?? ""
152150
preview = config.effectiveBlockExplorerPreview(network: selectedNetwork)
153151
selectedOption = config.selectedBlockExplorerOption(network: selectedNetwork)
154-
validationError = nil
155152
}
156153

157154
private func updatePreview(for value: String) {
@@ -160,10 +157,8 @@ struct BlockExplorerSettingsView: View {
160157
network: selectedNetwork,
161158
input: value
162159
)
163-
validationError = nil
164160
} catch {
165161
preview = ""
166-
validationError = error.localizedDescription
167162
}
168163
}
169164

@@ -184,7 +179,6 @@ struct BlockExplorerSettingsView: View {
184179
input = normalized ?? ""
185180
preview = config.effectiveBlockExplorerPreview(network: networkToSave)
186181
selectedOption = config.selectedBlockExplorerOption(network: networkToSave)
187-
validationError = nil
188182

189183
Task { @MainActor in
190184
await dismissAllPopups()
@@ -194,7 +188,6 @@ struct BlockExplorerSettingsView: View {
194188
.present()
195189
}
196190
} catch {
197-
validationError = error.localizedDescription
198191
showInvalidUrlAlert = true
199192
}
200193

@@ -206,7 +199,7 @@ struct BlockExplorerSettingsView: View {
206199
try config.clearCustomBlockExplorer(network: selectedNetwork)
207200
reload()
208201
} catch {
209-
validationError = error.localizedDescription
202+
showUpdateFailedAlert = true
210203
}
211204
}
212205
}

0 commit comments

Comments
 (0)