Skip to content

Commit

Permalink
Improve the dialog's features related to BackendSyncException
Browse files Browse the repository at this point in the history
Now this dialog has a "Go to setting" button. Also it shows the current time.
  • Loading branch information
peyman79 committed Sep 5, 2024
1 parent eaa0463 commit 750ea0e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
45 changes: 43 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/CoroutineHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.provider.Settings
import android.view.WindowManager
import android.view.WindowManager.BadTokenException
import androidx.annotation.StringRes
Expand Down Expand Up @@ -60,6 +62,9 @@ import net.ankiweb.rsdroid.exceptions.BackendNetworkException
import net.ankiweb.rsdroid.exceptions.BackendSyncException
import org.jetbrains.annotations.VisibleForTesting
import timber.log.Timber
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.Locale
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.resume
Expand Down Expand Up @@ -169,8 +174,12 @@ suspend fun <T> FragmentActivity.runCatching(
Timber.w(exc, errorMessage)
exc.localizedMessage?.let { showSnackbar(it) }
}
is BackendNetworkException, is BackendSyncException -> {
// these exceptions do not generate worthwhile crash reports
is BackendSyncException -> {
// this exceptions do not generate worthwhile crash reports
showInvalidClockError(this, exc.localizedMessage!!, exc, false)
}
is BackendNetworkException -> {
// this exceptions do not generate worthwhile crash reports
showError(this, exc.localizedMessage!!, exc, false)
}
is BackendException -> {
Expand Down Expand Up @@ -278,6 +287,38 @@ fun showError(context: Context, msg: String, exception: Throwable, crashReport:
}
}

fun showInvalidClockError(context: Context, msg: String, exception: Throwable, crashReport: Boolean = true) {
if (throwOnShowError) throw IllegalStateException("throwOnShowError: $msg", exception)
try {
val currentTime = LocalDateTime.now()
val dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss a", Locale.getDefault())
val formattedTime = currentTime.format(dateFormatter)

val fullMessage = "$msg\n\nYour clock is currently set to:\n$formattedTime"
AlertDialog.Builder(context).show {
title(R.string.vague_error)
message(text = fullMessage)
positiveButton(R.string.dialog_go_to_setting) {
// Navigate to Date and Time settings
val intent = Intent(Settings.ACTION_DATE_SETTINGS)
context.startActivity(intent)
}
negativeButton(R.string.dialog_cancel)
if (crashReport) {
setOnDismissListener {
CrashReportService.sendExceptionReport(
exception,
origin = context::class.java.simpleName
)
}
}
}
} catch (ex: BadTokenException) {
// issue 12718: activity provided by `context` was not running
Timber.w(ex, "unable to display error dialog")
}
}

/** In most cases, you'll want [AnkiActivity.withProgress]
* instead. This lower-level routine can be used to integrate your own
* progress UI.
Expand Down
1 change: 1 addition & 0 deletions AnkiDroid/src/main/res/values/03-dialogs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<string name="dialog_positive_overwrite">Overwrite</string>
<string name="dialog_positive_repair">Repair</string>
<string name="dialog_positive_replace">Replace</string>
<string name="dialog_go_to_setting">Go to setting</string>

<string name="dialog_collection_path_not_dir">The path specified wasn’t a valid directory</string>
<string name="dialog_invalid_custom_certificate">The provided text does not resolve to a valid PEM-encoded X509 certificate</string>
Expand Down

0 comments on commit 750ea0e

Please # to comment.