@@ -246,6 +246,32 @@ class TextViewerInstance(
246246 }
247247 }
248248
249+ fun showUnsavedChangesWarningDialog (
250+ onSaved : () -> Unit ,
251+ onFailed : (error: Exception ) -> Unit ,
252+ onIgnore : () -> Unit
253+ ) {
254+ warningDialogProperties.apply {
255+ title = globalClass.getString(R .string.warning)
256+ message = globalClass.getString(R .string.unsaved_changes)
257+ confirmText = globalClass.getString(R .string.save)
258+ dismissText = globalClass.getString(R .string.ignore_changes)
259+ onDismiss = {
260+ onIgnore()
261+ showWarningDialog = false
262+ }
263+ warningDialogProperties.onConfirm = {
264+ save(
265+ onSaved = onSaved,
266+ onFailed = onFailed
267+ )
268+ requireSave = false
269+ showWarningDialog = false
270+ }
271+ showWarningDialog = true
272+ }
273+ }
274+
249275 fun updateSymbols (): List <SymbolHolder > {
250276 if (! customSymbolsFile.exists()) {
251277 customSymbolsFile.writeText(
@@ -266,15 +292,20 @@ class TextViewerInstance(
266292 return customSymbolHolders.ifEmpty { defaultSymbolHolders }
267293 }
268294
269- fun save (onSaved : () -> Unit , onFailed : () -> Unit ) {
295+ fun save (onSaved : () -> Unit , onFailed : (error: Exception ) -> Unit ) {
270296 isSaving = true
271297 scope.launch {
272- globalClass.contentResolver.openOutputStream(uri, " wt" )?.use { outputStream ->
273- BufferedWriter (OutputStreamWriter (outputStream)).use { writer ->
274- writer.write(content)
298+ try {
299+ globalClass.contentResolver.openOutputStream(uri, " wt" ).use { outputStream ->
300+ BufferedWriter (OutputStreamWriter (outputStream)).use { writer ->
301+ writer.write(content)
302+ }
303+ withContext(Dispatchers .Main ) { onSaved().also { isSaving = false } }
275304 }
276- withContext(Dispatchers .Main ) { onSaved().also { isSaving = false } }
277- } ? : withContext(Dispatchers .Main ) { onFailed().also { isSaving = false } }
305+ } catch (e: Exception ) {
306+ logger.logError(e)
307+ withContext(Dispatchers .Main ) { onFailed(e).also { isSaving = false } }
308+ }
278309 }
279310 }
280311
0 commit comments