@ -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 < DialogErrorDetailsBinding > ( ) {
@AndroidEntryPoint
class ErrorDetailsDialog : AlertDialogFragment < DialogErrorDetailsBinding > ( ) , 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<DialogErrorDetailsBinding>() {
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 ,
)
}
}