From d51790811adaf7a754f0ad47750daeccb2a5b061 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Tue, 4 Mar 2025 14:05:45 +0200 Subject: [PATCH] Update error details dialog --- .../core/github/AppUpdateRepository.kt | 3 + .../core/ui/dialog/ErrorDetailsDialog.kt | 60 +++++++++++++------ .../main/res/layout/dialog_error_details.xml | 31 ++++++++-- app/src/main/res/values/strings.xml | 4 ++ 4 files changed, 77 insertions(+), 21 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/github/AppUpdateRepository.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/github/AppUpdateRepository.kt index 5d85d162b..674307bc1 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/github/AppUpdateRepository.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/github/AppUpdateRepository.kt @@ -43,6 +43,9 @@ class AppUpdateRepository @Inject constructor( append("/releases?page=1&per_page=10") } + val isUpdateAvailable: Boolean + get() = availableUpdate.value != null + fun observeAvailableUpdate() = availableUpdate.asStateFlow() suspend fun getAvailableVersions(): List { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/dialog/ErrorDetailsDialog.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/dialog/ErrorDetailsDialog.kt index 86d02b300..1525ac1e6 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/dialog/ErrorDetailsDialog.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/dialog/ErrorDetailsDialog.kt @@ -2,26 +2,33 @@ package org.koitharu.kotatsu.core.ui.dialog import android.os.Bundle import android.view.LayoutInflater +import android.view.View import android.view.ViewGroup -import androidx.core.text.HtmlCompat -import androidx.core.text.htmlEncode -import androidx.core.text.method.LinkMovementMethodCompat -import androidx.core.text.parseAsHtml +import androidx.core.view.isVisible import com.google.android.material.dialog.MaterialAlertDialogBuilder +import dagger.hilt.android.AndroidEntryPoint import org.koitharu.kotatsu.R +import org.koitharu.kotatsu.core.github.AppUpdateRepository import org.koitharu.kotatsu.core.nav.AppRouter +import org.koitharu.kotatsu.core.nav.router import org.koitharu.kotatsu.core.ui.AlertDialogFragment import org.koitharu.kotatsu.core.util.ext.copyToClipboard import org.koitharu.kotatsu.core.util.ext.getCauseUrl import org.koitharu.kotatsu.core.util.ext.isReportable import org.koitharu.kotatsu.core.util.ext.report import org.koitharu.kotatsu.core.util.ext.requireSerializable +import org.koitharu.kotatsu.core.util.ext.setTextAndVisible import org.koitharu.kotatsu.databinding.DialogErrorDetailsBinding +import javax.inject.Inject -class ErrorDetailsDialog : AlertDialogFragment() { +@AndroidEntryPoint +class ErrorDetailsDialog : AlertDialogFragment(), View.OnClickListener { private lateinit var exception: Throwable + @Inject + lateinit var appUpdateRepository: AppUpdateRepository + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val args = requireArguments() @@ -34,31 +41,50 @@ class ErrorDetailsDialog : AlertDialogFragment() { override fun onViewBindingCreated(binding: DialogErrorDetailsBinding, savedInstanceState: Bundle?) { super.onViewBindingCreated(binding, savedInstanceState) - with(binding.textViewMessage) { - movementMethod = LinkMovementMethodCompat.getInstance() - text = context.getString( - R.string.manga_error_description_pattern, - exception.message?.htmlEncode().orEmpty(), - arguments?.getString(AppRouter.KEY_URL) ?: exception.getCauseUrl(), - ).parseAsHtml(HtmlCompat.FROM_HTML_MODE_LEGACY) - } + binding.buttonBrowser.setOnClickListener(this) + binding.textViewSummary.text = exception.message + val isUrlAvailable = !exception.getCauseUrl().isNullOrEmpty() + binding.buttonBrowser.isVisible = isUrlAvailable + binding.textViewBrowser.isVisible = isUrlAvailable + binding.textViewDescription.setTextAndVisible( + if (appUpdateRepository.isUpdateAvailable) { + R.string.error_disclaimer_app_outdated + } else if (exception.isReportable()) { + R.string.error_disclaimer_report + } else { + 0 + }, + ) } @Suppress("NAME_SHADOWING") override fun onBuildDialog(builder: MaterialAlertDialogBuilder): MaterialAlertDialogBuilder { val builder = super.onBuildDialog(builder) .setCancelable(true) - .setNegativeButton(android.R.string.cancel, null) - .setTitle(R.string.error_occurred) + .setNegativeButton(R.string.close, null) + .setTitle(R.string.error_details) .setNeutralButton(androidx.preference.R.string.copy) { _, _ -> context?.copyToClipboard(getString(R.string.error), exception.stackTraceToString()) } - if (exception.isReportable()) { - builder.setPositiveButton(R.string.report) { _, _ -> + if (appUpdateRepository.isUpdateAvailable) { + builder.setPositiveButton(R.string.update) { _, _ -> + router.openAppUpdate() dismiss() + } + } else if (exception.isReportable()) { + builder.setPositiveButton(R.string.report) { _, _ -> exception.report(silent = true) + dismiss() } } return builder } + + override fun onClick(v: View) { + router.openBrowser( + url = exception.getCauseUrl() ?: return, + source = null, + title = null, + ) + } } diff --git a/app/src/main/res/layout/dialog_error_details.xml b/app/src/main/res/layout/dialog_error_details.xml index abf5ae871..0b90ec286 100644 --- a/app/src/main/res/layout/dialog_error_details.xml +++ b/app/src/main/res/layout/dialog_error_details.xml @@ -13,12 +13,35 @@ android:paddingTop="@dimen/margin_normal"> + tools:text="@tools:sample/lorem[14]" /> + + + +