From 4ad2f3f608d2f0e00d23c8cce4f24f9c512159dd Mon Sep 17 00:00:00 2001 From: Koitharu Date: Fri, 4 Oct 2024 14:27:01 +0300 Subject: [PATCH] Fixes batch --- .../kotatsu/alternatives/ui/AutoFixService.kt | 9 +++++---- .../kotatsu/core/ErrorReporterReceiver.kt | 9 +++++++-- .../kotatsu/core/ui/ReorderableListAdapter.kt | 6 ++++-- .../ui/worker/DownloadNotificationFactory.kt | 16 +++++++++------- .../koitharu/kotatsu/local/ui/ImportService.kt | 6 ++++-- .../kotatsu/settings/SettingsActivity.kt | 15 +++++++++------ .../sources/manage/SourcesListProducer.kt | 11 +++++++---- .../sources/manage/SourcesManageFragment.kt | 7 +++++-- 8 files changed, 50 insertions(+), 29 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/alternatives/ui/AutoFixService.kt b/app/src/main/kotlin/org/koitharu/kotatsu/alternatives/ui/AutoFixService.kt index 9fa0fdf42..aa6f7d436 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/alternatives/ui/AutoFixService.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/alternatives/ui/AutoFixService.kt @@ -165,13 +165,14 @@ class AutoFixService : CoroutineIntentService() { } else { error.getDisplayMessage(applicationContext.resources) }, - ) - .setSmallIcon(android.R.drawable.stat_notify_error) - .addAction( + ).setSmallIcon(android.R.drawable.stat_notify_error) + ErrorReporterReceiver.getPendingIntent(applicationContext, error)?.let { reportIntent -> + notification.addAction( R.drawable.ic_alert_outline, applicationContext.getString(R.string.report), - ErrorReporterReceiver.getPendingIntent(applicationContext, error), + reportIntent, ) + } } return notification.build() } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/ErrorReporterReceiver.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/ErrorReporterReceiver.kt index 7d190422e..628093428 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/ErrorReporterReceiver.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/ErrorReporterReceiver.kt @@ -5,9 +5,11 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.net.Uri +import android.os.BadParcelableException import androidx.core.app.PendingIntentCompat import org.koitharu.kotatsu.BuildConfig import org.koitharu.kotatsu.core.util.ext.getSerializableExtraCompat +import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug import org.koitharu.kotatsu.core.util.ext.report class ErrorReporterReceiver : BroadcastReceiver() { @@ -22,12 +24,15 @@ class ErrorReporterReceiver : BroadcastReceiver() { private const val EXTRA_ERROR = "err" private const val ACTION_REPORT = "${BuildConfig.APPLICATION_ID}.action.REPORT_ERROR" - fun getPendingIntent(context: Context, e: Throwable): PendingIntent { + fun getPendingIntent(context: Context, e: Throwable): PendingIntent? = try { val intent = Intent(context, ErrorReporterReceiver::class.java) intent.setAction(ACTION_REPORT) intent.setData(Uri.parse("err://${e.hashCode()}")) intent.putExtra(EXTRA_ERROR, e) - return checkNotNull(PendingIntentCompat.getBroadcast(context, 0, intent, 0, false)) + PendingIntentCompat.getBroadcast(context, 0, intent, 0, false) + } catch (e: BadParcelableException) { + e.printStackTraceDebug() + null } } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/ReorderableListAdapter.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/ReorderableListAdapter.kt index 738309af5..97543d692 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/ReorderableListAdapter.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/ReorderableListAdapter.kt @@ -9,7 +9,7 @@ import kotlinx.coroutines.flow.FlowCollector import kotlinx.coroutines.withContext import org.koitharu.kotatsu.list.ui.adapter.ListItemType import org.koitharu.kotatsu.list.ui.model.ListModel -import java.util.Collections +import org.koitharu.kotatsu.parsers.util.move import java.util.LinkedList open class ReorderableListAdapter : ListDelegationAdapter>(), FlowCollector?> { @@ -36,7 +36,9 @@ open class ReorderableListAdapter : ListDelegationAdapter override fun setItems(items: List?) = super.setItems(items) fun reorderItems(oldPos: Int, newPos: Int) { - Collections.swap(items ?: return, oldPos, newPos) + val reordered = items?.toMutableList() ?: return + reordered.move(oldPos, newPos) + super.setItems(reordered) notifyItemMoved(oldPos, newPos) } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadNotificationFactory.kt b/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadNotificationFactory.kt index 03f23e5cf..ece4835a5 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadNotificationFactory.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadNotificationFactory.kt @@ -213,13 +213,15 @@ class DownloadNotificationFactory @AssistedInject constructor( builder.setWhen(System.currentTimeMillis()) builder.setStyle(NotificationCompat.BigTextStyle().bigText(state.errorMessage)) if (state.error.isReportable()) { - builder.addAction( - NotificationCompat.Action( - 0, - context.getString(R.string.report), - ErrorReporterReceiver.getPendingIntent(context, state.error), - ), - ) + ErrorReporterReceiver.getPendingIntent(context, state.error)?.let { reportIntent -> + builder.addAction( + NotificationCompat.Action( + 0, + context.getString(R.string.report), + reportIntent, + ), + ) + } } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/local/ui/ImportService.kt b/app/src/main/kotlin/org/koitharu/kotatsu/local/ui/ImportService.kt index f033847c2..8ff8ca534 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/local/ui/ImportService.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/local/ui/ImportService.kt @@ -139,11 +139,13 @@ class ImportService : CoroutineIntentService() { notification.setContentTitle(applicationContext.getString(R.string.error_occurred)) .setContentText(error.getDisplayMessage(applicationContext.resources)) .setSmallIcon(android.R.drawable.stat_notify_error) - .addAction( + ErrorReporterReceiver.getPendingIntent(applicationContext, error)?.let { reportIntent -> + notification.addAction( R.drawable.ic_alert_outline, applicationContext.getString(R.string.report), - ErrorReporterReceiver.getPendingIntent(applicationContext, error), + reportIntent, ) + } } return notification.build() } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/SettingsActivity.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/SettingsActivity.kt index 2f8d648d1..1112924e2 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/SettingsActivity.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/SettingsActivity.kt @@ -10,6 +10,7 @@ import androidx.core.graphics.Insets import androidx.core.view.updateLayoutParams import androidx.core.view.updatePadding import androidx.fragment.app.Fragment +import androidx.fragment.app.FragmentFactory import androidx.fragment.app.FragmentTransaction import androidx.fragment.app.commit import androidx.preference.Preference @@ -70,10 +71,12 @@ class SettingsActivity : caller: PreferenceFragmentCompat, pref: Preference, ): Boolean { - val fm = supportFragmentManager - val fragment = fm.fragmentFactory.instantiate(classLoader, pref.fragment ?: return false) - fragment.arguments = pref.extras - openFragment(fragment, isFromRoot = caller is RootSettingsFragment) + val fragmentName = pref.fragment ?: return false + openFragment( + fragmentClass = FragmentFactory.loadFragmentClass(classLoader, fragmentName), + args = pref.peekExtras(), + isFromRoot = caller is RootSettingsFragment, + ) return true } @@ -93,11 +96,11 @@ class SettingsActivity : } ?: setTitle(title ?: getString(R.string.settings)) } - fun openFragment(fragment: Fragment, isFromRoot: Boolean) { + fun openFragment(fragmentClass: Class, args: Bundle?, isFromRoot: Boolean) { val hasFragment = supportFragmentManager.findFragmentById(R.id.container) != null supportFragmentManager.commit { setReorderingAllowed(true) - replace(R.id.container, fragment) + replace(R.id.container, fragmentClass, args) setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) if (!isMasterDetails || (hasFragment && !isFromRoot)) { addToBackStack(null) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/manage/SourcesListProducer.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/manage/SourcesListProducer.kt index 85b06587b..7df5129f6 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/manage/SourcesListProducer.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/manage/SourcesListProducer.kt @@ -18,10 +18,13 @@ import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.db.TABLE_SOURCES import org.koitharu.kotatsu.core.model.getTitle import org.koitharu.kotatsu.core.model.isNsfw +import org.koitharu.kotatsu.core.model.unwrap import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.util.ext.lifecycleScope import org.koitharu.kotatsu.explore.data.MangaSourcesRepository import org.koitharu.kotatsu.explore.data.SourcesSortOrder +import org.koitharu.kotatsu.parsers.model.MangaParserSource +import org.koitharu.kotatsu.parsers.util.mapToSet import org.koitharu.kotatsu.settings.sources.model.SourceConfigItem import javax.inject.Inject @@ -63,8 +66,8 @@ class SourcesListProducer @Inject constructor( } private suspend fun buildList(): List { - val enabledSources = repository.getEnabledSources() - val pinned = repository.getPinnedSources() + val enabledSources = repository.getEnabledSources().filter { it.unwrap() is MangaParserSource } + val pinned = repository.getPinnedSources().mapToSet { it.name } val isNsfwDisabled = settings.isNsfwContentDisabled val isReorderAvailable = settings.sourcesSortOrder == SourcesSortOrder.MANUAL val withTip = isReorderAvailable && settings.isTipEnabled(TIP_REORDER) @@ -79,7 +82,7 @@ class SourcesListProducer @Inject constructor( isEnabled = it in enabledSet, isDraggable = false, isAvailable = !isNsfwDisabled || !it.isNsfw(), - isPinned = it in pinned, + isPinned = it.name in pinned, ) }.ifEmpty { listOf(SourceConfigItem.EmptySearchResult) @@ -100,7 +103,7 @@ class SourcesListProducer @Inject constructor( isEnabled = true, isDraggable = isReorderAvailable, isAvailable = false, - isPinned = it in pinned, + isPinned = it.name in pinned, ) } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/manage/SourcesManageFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/manage/SourcesManageFragment.kt index b970465cc..d0b19015b 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/manage/SourcesManageFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/manage/SourcesManageFragment.kt @@ -106,8 +106,11 @@ class SourcesManageFragment : } override fun onItemSettingsClick(item: SourceConfigItem.SourceItem) { - val fragment = SourceSettingsFragment.newInstance(item.source) - (activity as? SettingsActivity)?.openFragment(fragment, false) + (activity as? SettingsActivity)?.openFragment( + fragmentClass = SourceSettingsFragment::class.java, + args = Bundle(1).apply { putString(SourceSettingsFragment.EXTRA_SOURCE, item.source.name) }, + isFromRoot = false, + ) } override fun onItemLiftClick(item: SourceConfigItem.SourceItem) {