From 174fa800bee4e682a09c674a60554c663b2871ae Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sat, 17 Aug 2024 09:38:57 +0300 Subject: [PATCH] Refactor and cleanup --- app/build.gradle | 8 +++---- .../kotatsu/core/ErrorReportingAdmin.kt | 24 ------------------- .../kotatsu/core/ui/AlertDialogFragment.kt | 7 ------ .../koitharu/kotatsu/core/ui/BaseFragment.kt | 7 ------ .../kotatsu/core/ui/ReorderableListAdapter.kt | 4 ++-- .../core/ui/sheet/BaseAdaptiveSheet.kt | 4 ---- .../kotatsu/core/ui/util/MenuInvalidator.kt | 4 +--- .../kotatsu/core/ui/util/ReversibleHandle.kt | 7 +----- .../koitharu/kotatsu/core/util/ext/Android.kt | 8 +------ .../kotatsu/core/util/ext/BottomSheet.kt | 23 ------------------ .../koitharu/kotatsu/core/util/ext/Coil.kt | 4 ---- .../kotatsu/core/util/ext/TextView.kt | 1 - .../org/koitharu/kotatsu/core/util/ext/Uri.kt | 6 +++++ .../koitharu/kotatsu/core/util/ext/View.kt | 9 ------- .../koitharu/kotatsu/main/ui/MainActivity.kt | 4 ++-- .../kotatsu/search/ui/SearchActivity.kt | 4 ++-- 16 files changed, 18 insertions(+), 106 deletions(-) delete mode 100644 app/src/main/kotlin/org/koitharu/kotatsu/core/ErrorReportingAdmin.kt delete mode 100644 app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/BottomSheet.kt diff --git a/app/build.gradle b/app/build.gradle index af09422de..034457931 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -16,8 +16,8 @@ android { applicationId 'org.koitharu.kotatsu' minSdk = 21 targetSdk = 35 - versionCode = 659 - versionName = '7.4.2' + versionCode = 660 + versionName = '7.5-a1' generatedDensities = [] testInstrumentationRunner 'org.koitharu.kotatsu.HiltTestRunner' ksp { @@ -142,12 +142,10 @@ dependencies { implementation 'ch.acra:acra-http:5.11.3' implementation 'ch.acra:acra-dialog:5.11.3' - compileOnly 'com.google.auto.service:auto-service-annotations:1.1.1' - ksp 'dev.zacsweers.autoservice:auto-service-ksp:1.1.0' implementation 'org.conscrypt:conscrypt-android:2.5.2' - debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.14' + debugImplementation 'com.squareup.leakcanary:leakcanary-android:3.0-alpha-8' debugImplementation 'com.github.Koitharu:WorkInspector:5778dd1747' testImplementation 'junit:junit:4.13.2' diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/ErrorReportingAdmin.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/ErrorReportingAdmin.kt deleted file mode 100644 index e088254f8..000000000 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/ErrorReportingAdmin.kt +++ /dev/null @@ -1,24 +0,0 @@ -package org.koitharu.kotatsu.core - -import android.content.Context -import com.google.auto.service.AutoService -import org.acra.builder.ReportBuilder -import org.acra.config.CoreConfiguration -import org.acra.config.ReportingAdministrator - -@AutoService(ReportingAdministrator::class) -class ErrorReportingAdmin : ReportingAdministrator { - - override fun shouldStartCollecting( - context: Context, - config: CoreConfiguration, - reportBuilder: ReportBuilder - ): Boolean { - return reportBuilder.exception?.isDeadOs() != true - } - - private fun Throwable.isDeadOs(): Boolean { - val className = javaClass.simpleName - return className == "DeadSystemException" || className == "DeadSystemRuntimeException" || cause?.isDeadOs() == true - } -} diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/AlertDialogFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/AlertDialogFragment.kt index 6809043ef..1ed421479 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/AlertDialogFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/AlertDialogFragment.kt @@ -16,10 +16,6 @@ abstract class AlertDialogFragment : DialogFragment() { var viewBinding: B? = null private set - @Deprecated("", ReplaceWith("requireViewBinding()")) - protected val binding: B - get() = requireViewBinding() - final override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val binding = onCreateViewBinding(layoutInflater, null) viewBinding = binding @@ -51,9 +47,6 @@ abstract class AlertDialogFragment : DialogFragment() { open fun onDialogCreated(dialog: AlertDialog) = Unit - @Deprecated("", ReplaceWith("viewBinding")) - protected fun bindingOrNull() = viewBinding - fun requireViewBinding(): B = checkNotNull(viewBinding) { "Fragment $this did not return a ViewBinding from onCreateView() or this was called before onCreateView()." } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/BaseFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/BaseFragment.kt index 0809799b9..262622209 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/BaseFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/BaseFragment.kt @@ -18,10 +18,6 @@ abstract class BaseFragment : var viewBinding: B? = null private set - @Deprecated("", ReplaceWith("requireViewBinding()")) - protected val binding: B - get() = requireViewBinding() - @JvmField protected val exceptionResolver = ExceptionResolver(this) @@ -59,9 +55,6 @@ abstract class BaseFragment : "Fragment $this did not return a ViewBinding from onCreateView() or this was called before onCreateView()." } - @Deprecated("", ReplaceWith("viewBinding")) - protected fun bindingOrNull() = viewBinding - protected abstract fun onCreateViewBinding(inflater: LayoutInflater, container: ViewGroup?): B protected open fun onViewBindingCreated(binding: B, savedInstanceState: Bundle?) = Unit 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 06a744fd9..ccfc5d0b9 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 @@ -52,8 +52,8 @@ open class ReorderableListAdapter : ListDelegationAdapter } protected class DiffCallback( - val oldList: List, - val newList: List, + private val oldList: List, + private val newList: List, ) : DiffUtil.Callback() { override fun getOldListSize(): Int = oldList.size diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/sheet/BaseAdaptiveSheet.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/sheet/BaseAdaptiveSheet.kt index 0674015e9..b776652e5 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/sheet/BaseAdaptiveSheet.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/sheet/BaseAdaptiveSheet.kt @@ -34,10 +34,6 @@ abstract class BaseAdaptiveSheet : AppCompatDialogFragment() { var viewBinding: B? = null private set - @Deprecated("", ReplaceWith("requireViewBinding()")) - protected val binding: B - get() = requireViewBinding() - protected val behavior: AdaptiveSheetBehavior? get() = AdaptiveSheetBehavior.from(this) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/util/MenuInvalidator.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/util/MenuInvalidator.kt index d2fd6978e..bd40f3c38 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/util/MenuInvalidator.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/util/MenuInvalidator.kt @@ -7,7 +7,5 @@ class MenuInvalidator( private val host: MenuHost, ) : FlowCollector { - override suspend fun emit(value: Any?) { - host.invalidateMenu() - } + override suspend fun emit(value: Any?) = host.invalidateMenu() } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/util/ReversibleHandle.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/util/ReversibleHandle.kt index de38ffff4..f1a8e4146 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/util/ReversibleHandle.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/util/ReversibleHandle.kt @@ -5,9 +5,9 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug import org.koitharu.kotatsu.core.util.ext.processLifecycleScope import org.koitharu.kotatsu.parsers.util.runCatchingCancellable -import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug fun interface ReversibleHandle { @@ -23,8 +23,3 @@ fun ReversibleHandle.reverseAsync() = processLifecycleScope.launch(Dispatchers.D it.printStackTraceDebug() } } - -operator fun ReversibleHandle.plus(other: ReversibleHandle) = ReversibleHandle { - this.reverse() - other.reverse() -} diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Android.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Android.kt index b365a3cd3..d86bcd5c2 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Android.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Android.kt @@ -20,7 +20,6 @@ import android.database.SQLException import android.graphics.Bitmap import android.graphics.Color import android.net.ConnectivityManager -import android.net.Uri import android.os.Build import android.os.Bundle import android.os.PowerManager @@ -31,7 +30,6 @@ import android.view.Window import android.webkit.WebView import androidx.activity.result.ActivityResultLauncher import androidx.annotation.IntegerRes -import androidx.annotation.WorkerThread import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDialog @@ -79,8 +77,6 @@ val Context.powerManager: PowerManager? val Context.connectivityManager: ConnectivityManager get() = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager -fun String.toUriOrNull() = if (isEmpty()) null else Uri.parse(this) - suspend fun CoroutineWorker.trySetForeground(): Boolean = runCatchingCancellable { val info = getForegroundInfo() setForeground(info) @@ -131,8 +127,7 @@ fun SyncResult.onError(error: Throwable) { when (error) { is IOException -> stats.numIoExceptions++ is OperationApplicationException, - is SQLException, - -> databaseError = true + is SQLException -> databaseError = true is JSONException -> stats.numParseExceptions++ else -> if (BuildConfig.DEBUG) throw error @@ -253,7 +248,6 @@ fun Context.checkNotificationPermission(channelId: String?): Boolean { return hasPermission } -@WorkerThread suspend fun Bitmap.compressToPNG(output: File) = runInterruptible(Dispatchers.IO) { output.outputStream().use { os -> if (!compress(Bitmap.CompressFormat.PNG, 100, os)) { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/BottomSheet.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/BottomSheet.kt deleted file mode 100644 index faba08d87..000000000 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/BottomSheet.kt +++ /dev/null @@ -1,23 +0,0 @@ -package org.koitharu.kotatsu.core.util.ext - -import android.view.View -import com.google.android.material.bottomsheet.BottomSheetBehavior -import com.google.android.material.bottomsheet.BottomSheetBehavior.BottomSheetCallback - -fun BottomSheetBehavior<*>.doOnExpansionsChanged(callback: (isExpanded: Boolean) -> Unit) { - var isExpended = state == BottomSheetBehavior.STATE_EXPANDED - callback(isExpended) - addBottomSheetCallback( - object : BottomSheetCallback() { - override fun onStateChanged(bottomSheet: View, newState: Int) { - val expanded = newState == BottomSheetBehavior.STATE_EXPANDED - if (expanded != isExpended) { - isExpended = expanded - callback(expanded) - } - } - - override fun onSlide(bottomSheet: View, slideOffset: Float) = Unit - }, - ) -} diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Coil.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Coil.kt index 1ddba8d32..580b5aeed 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Coil.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Coil.kt @@ -57,10 +57,6 @@ fun ImageResult.toBitmapOrNull() = when (this) { is ErrorResult -> null } -fun ImageRequest.Builder.indicator(indicator: BaseProgressIndicator<*>): ImageRequest.Builder { - return addListener(ImageRequestIndicatorListener(listOf(indicator))) -} - fun ImageRequest.Builder.indicator(indicators: List>): ImageRequest.Builder { return addListener(ImageRequestIndicatorListener(indicators)) } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/TextView.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/TextView.kt index 038619b66..89cbd8faa 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/TextView.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/TextView.kt @@ -11,7 +11,6 @@ import androidx.core.content.res.use import androidx.core.view.isGone import androidx.core.widget.TextViewCompat - var TextView.textAndVisible: CharSequence? get() = text?.takeIf { visibility == View.VISIBLE } set(value) { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Uri.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Uri.kt index f6db7443e..0981bfa79 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Uri.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Uri.kt @@ -5,6 +5,7 @@ import androidx.core.net.toFile import okio.Source import okio.source import okio.use +import org.jetbrains.annotations.Blocking import org.koitharu.kotatsu.local.data.util.withExtraCloseable import java.io.File import java.util.zip.ZipFile @@ -12,6 +13,7 @@ import java.util.zip.ZipFile const val URI_SCHEME_FILE = "file" const val URI_SCHEME_ZIP = "file+zip" +@Blocking fun Uri.exists(): Boolean = when (scheme) { URI_SCHEME_FILE -> toFile().exists() URI_SCHEME_ZIP -> { @@ -22,6 +24,7 @@ fun Uri.exists(): Boolean = when (scheme) { else -> unsupportedUri(this) } +@Blocking fun Uri.isTargetNotEmpty(): Boolean = when (scheme) { URI_SCHEME_FILE -> toFile().isNotEmpty() URI_SCHEME_ZIP -> { @@ -32,6 +35,7 @@ fun Uri.isTargetNotEmpty(): Boolean = when (scheme) { else -> unsupportedUri(this) } +@Blocking fun Uri.source(): Source = when (scheme) { URI_SCHEME_FILE -> toFile().source() URI_SCHEME_ZIP -> { @@ -45,6 +49,8 @@ fun Uri.source(): Source = when (scheme) { fun File.toZipUri(entryName: String): Uri = Uri.parse("$URI_SCHEME_ZIP://$absolutePath#$entryName") +fun String.toUriOrNull() = if (isEmpty()) null else Uri.parse(this) + private fun unsupportedUri(uri: Uri): Nothing { throw IllegalArgumentException("Bad uri $uri: only schemes $URI_SCHEME_FILE and $URI_SCHEME_ZIP are supported") } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/View.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/View.kt index 196269e1a..3a6b9f1ca 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/View.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/View.kt @@ -8,7 +8,6 @@ import android.view.ViewGroup import android.widget.Checkable import androidx.appcompat.widget.ActionMenuView import androidx.appcompat.widget.Toolbar -import androidx.core.view.SoftwareKeyboardControllerCompat import androidx.core.view.children import androidx.core.view.descendants import androidx.core.view.isVisible @@ -23,14 +22,6 @@ import com.google.android.material.slider.Slider import com.google.android.material.tabs.TabLayout import kotlin.math.roundToInt -fun View.hideKeyboard() { - SoftwareKeyboardControllerCompat(this).hide() -} - -fun View.showKeyboard() { - SoftwareKeyboardControllerCompat(this).show() -} - fun View.hasGlobalPoint(x: Int, y: Int): Boolean { if (visibility != View.VISIBLE) { return false diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/main/ui/MainActivity.kt b/app/src/main/kotlin/org/koitharu/kotatsu/main/ui/MainActivity.kt index 471e3f173..1c01fa4c2 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/main/ui/MainActivity.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/main/ui/MainActivity.kt @@ -15,6 +15,7 @@ import androidx.appcompat.view.ActionMode import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import androidx.core.graphics.Insets +import androidx.core.view.SoftwareKeyboardControllerCompat import androidx.core.view.children import androidx.core.view.inputmethod.EditorInfoCompat import androidx.core.view.isInvisible @@ -46,7 +47,6 @@ import org.koitharu.kotatsu.core.ui.util.FadingAppbarMediator import org.koitharu.kotatsu.core.ui.util.MenuInvalidator import org.koitharu.kotatsu.core.ui.util.OptionsMenuBadgeHelper import org.koitharu.kotatsu.core.ui.widgets.SlidingBottomNavigationView -import org.koitharu.kotatsu.core.util.ext.hideKeyboard import org.koitharu.kotatsu.core.util.ext.observe import org.koitharu.kotatsu.core.util.ext.observeEvent import org.koitharu.kotatsu.core.util.ext.scaleUpActivityOptionsOf @@ -332,7 +332,7 @@ class MainActivity : BaseActivity(), AppBarOwner, BottomNav } private fun onSearchClosed() { - viewBinding.searchView.hideKeyboard() + SoftwareKeyboardControllerCompat(viewBinding.searchView).hide() adjustSearchUI(isOpened = false, animate = true) } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/search/ui/SearchActivity.kt b/app/src/main/kotlin/org/koitharu/kotatsu/search/ui/SearchActivity.kt index e88057a82..ec63cf80e 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/search/ui/SearchActivity.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/search/ui/SearchActivity.kt @@ -6,6 +6,7 @@ import android.os.Bundle import androidx.activity.viewModels import androidx.appcompat.widget.SearchView import androidx.core.graphics.Insets +import androidx.core.view.SoftwareKeyboardControllerCompat import androidx.core.view.inputmethod.EditorInfoCompat import androidx.core.view.updatePadding import androidx.fragment.app.commit @@ -15,7 +16,6 @@ import org.koitharu.kotatsu.core.model.MangaSource import org.koitharu.kotatsu.core.model.getTitle import org.koitharu.kotatsu.core.ui.BaseActivity import org.koitharu.kotatsu.core.util.ext.observe -import org.koitharu.kotatsu.core.util.ext.showKeyboard import org.koitharu.kotatsu.databinding.ActivitySearchBinding import org.koitharu.kotatsu.parsers.model.MangaSource import org.koitharu.kotatsu.search.ui.suggestion.SearchSuggestionViewModel @@ -39,7 +39,7 @@ class SearchActivity : BaseActivity(), SearchView.OnQuery if (query.isNullOrBlank()) { requestFocus() - showKeyboard() + SoftwareKeyboardControllerCompat(this).show() } else { setQuery(query, true) }