From 1d387709f27834f84c1ab091a3945c6b55298f01 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sun, 5 Apr 2020 20:40:58 +0300 Subject: [PATCH] Cleanup code --- .../koitharu/kotatsu/domain/local/MangaZip.kt | 4 +-- .../ui/details/MangaDetailsActivity.kt | 13 ++++----- .../ui/main/list/local/LocalListFragment.kt | 14 ++++----- .../kotatsu/ui/reader/ReaderActivity.kt | 29 +++++++++++-------- .../org/koitharu/kotatsu/utils/UiUtils.kt | 12 -------- .../koitharu/kotatsu/utils/ext/ContextExt.kt | 11 ------- .../utils/ext/{JsoupExt.kt => ParseExt.kt} | 5 +--- .../kotatsu/utils/ext/PrimitiveExt.kt | 2 -- .../koitharu/kotatsu/utils/ext/StringExt.kt | 2 +- .../koitharu/kotatsu/utils/ext/ThemeExt.kt | 7 +++-- .../org/koitharu/kotatsu/utils/ext/ViewExt.kt | 29 +------------------ 11 files changed, 39 insertions(+), 89 deletions(-) delete mode 100644 app/src/main/java/org/koitharu/kotatsu/utils/ext/ContextExt.kt rename app/src/main/java/org/koitharu/kotatsu/utils/ext/{JsoupExt.kt => ParseExt.kt} (88%) diff --git a/app/src/main/java/org/koitharu/kotatsu/domain/local/MangaZip.kt b/app/src/main/java/org/koitharu/kotatsu/domain/local/MangaZip.kt index ed6a62e31..8a48d40d9 100644 --- a/app/src/main/java/org/koitharu/kotatsu/domain/local/MangaZip.kt +++ b/app/src/main/java/org/koitharu/kotatsu/domain/local/MangaZip.kt @@ -5,7 +5,7 @@ import org.koitharu.kotatsu.core.model.Manga import org.koitharu.kotatsu.core.model.MangaChapter import org.koitharu.kotatsu.utils.ext.sub import org.koitharu.kotatsu.utils.ext.takeIfReadable -import org.koitharu.kotatsu.utils.ext.toFileName +import org.koitharu.kotatsu.utils.ext.toFileNameSafe import java.io.File import java.util.zip.ZipEntry import java.util.zip.ZipInputStream @@ -91,7 +91,7 @@ class MangaZip(val file: File) { const val INDEX_ENTRY = "index.json" fun findInDir(root: File, manga: Manga): MangaZip { - val name = manga.title.toFileName() + ".cbz" + val name = manga.title.toFileNameSafe() + ".cbz" val file = File(root, name) return MangaZip(file) } diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/details/MangaDetailsActivity.kt b/app/src/main/java/org/koitharu/kotatsu/ui/details/MangaDetailsActivity.kt index a2ba6619a..1bba6e99e 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/details/MangaDetailsActivity.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/details/MangaDetailsActivity.kt @@ -28,7 +28,6 @@ import org.koitharu.kotatsu.ui.download.DownloadService import org.koitharu.kotatsu.utils.ShareHelper import org.koitharu.kotatsu.utils.ShortcutUtils import org.koitharu.kotatsu.utils.ext.getDisplayMessage -import org.koitharu.kotatsu.utils.ext.showDialog class MangaDetailsActivity : BaseActivity(), MangaDetailsView { @@ -119,14 +118,14 @@ class MangaDetailsActivity : BaseActivity(), MangaDetailsView { } R.id.action_delete -> { manga?.let { m -> - showDialog { - setTitle(R.string.delete_manga) - setMessage(getString(R.string.text_delete_local_manga, m.title)) - setPositiveButton(R.string.delete) { _, _ -> + AlertDialog.Builder(this) + .setTitle(R.string.delete_manga) + .setMessage(getString(R.string.text_delete_local_manga, m.title)) + .setPositiveButton(R.string.delete) { _, _ -> presenter.deleteLocal(m) } - setNegativeButton(android.R.string.cancel, null) - } + .setNegativeButton(android.R.string.cancel, null) + .show() } true } diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/main/list/local/LocalListFragment.kt b/app/src/main/java/org/koitharu/kotatsu/ui/main/list/local/LocalListFragment.kt index a2bea3ff5..41900cbea 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/main/list/local/LocalListFragment.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/main/list/local/LocalListFragment.kt @@ -6,6 +6,7 @@ import android.content.Intent import android.view.Menu import android.view.MenuInflater import android.view.MenuItem +import androidx.appcompat.app.AlertDialog import com.google.android.material.snackbar.Snackbar import kotlinx.android.synthetic.main.fragment_list.* import moxy.ktx.moxyPresenter @@ -14,7 +15,6 @@ import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.model.Manga import org.koitharu.kotatsu.ui.main.list.MangaListFragment import org.koitharu.kotatsu.utils.ext.ellipsize -import org.koitharu.kotatsu.utils.ext.showDialog import java.io.File class LocalListFragment : MangaListFragment() { @@ -81,14 +81,14 @@ class LocalListFragment : MangaListFragment() { override fun onPopupMenuItemSelected(item: MenuItem, data: Manga): Boolean { return when (item.itemId) { R.id.action_delete -> { - context?.showDialog { - setTitle(R.string.delete_manga) - setMessage(getString(R.string.text_delete_local_manga, data.title)) - setPositiveButton(R.string.delete) { _, _ -> + AlertDialog.Builder(context ?: return false) + .setTitle(R.string.delete_manga) + .setMessage(getString(R.string.text_delete_local_manga, data.title)) + .setPositiveButton(R.string.delete) { _, _ -> presenter.delete(data) } - setNegativeButton(android.R.string.cancel, null) - } + .setNegativeButton(android.R.string.cancel, null) + .show() true } else -> super.onPopupMenuItemSelected(item, data) diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/reader/ReaderActivity.kt b/app/src/main/java/org/koitharu/kotatsu/ui/reader/ReaderActivity.kt index 8dcfbb64f..6012a77e5 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/reader/ReaderActivity.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/reader/ReaderActivity.kt @@ -9,6 +9,7 @@ import android.os.Build import android.os.Bundle import android.view.* import android.widget.Toast +import androidx.appcompat.app.AlertDialog import androidx.core.view.isVisible import androidx.core.view.postDelayed import androidx.core.view.updatePadding @@ -207,16 +208,16 @@ class ReaderActivity : BaseFullscreenActivity(), ReaderView, ChaptersDialog.OnCh } override fun onError(e: Throwable) { - showDialog { - setTitle(R.string.error_occurred) - setMessage(e.message) - setPositiveButton(R.string.close, null) - if (reader?.hasItems != true) { - setOnDismissListener { - finish() - } + val dialog = AlertDialog.Builder(this) + .setTitle(R.string.error_occurred) + .setMessage(e.message) + .setPositiveButton(R.string.close, null) + if (reader?.hasItems != true) { + dialog.setOnDismissListener { + finish() } } + dialog.show() } override fun onGridTouch(area: Int) { @@ -225,11 +226,13 @@ class ReaderActivity : BaseFullscreenActivity(), ReaderView, ChaptersDialog.OnCh setUiIsVisible(!appbar_top.isVisible) } GridTouchHelper.AREA_TOP, - GridTouchHelper.AREA_LEFT -> if (isTapSwitchEnabled) { + GridTouchHelper.AREA_LEFT, + -> if (isTapSwitchEnabled) { reader?.switchPageBy(-1) } GridTouchHelper.AREA_BOTTOM, - GridTouchHelper.AREA_RIGHT -> if (isTapSwitchEnabled) { + GridTouchHelper.AREA_RIGHT, + -> if (isTapSwitchEnabled) { reader?.switchPageBy(1) } } @@ -267,13 +270,15 @@ class ReaderActivity : BaseFullscreenActivity(), ReaderView, ChaptersDialog.OnCh KeyEvent.KEYCODE_SPACE, KeyEvent.KEYCODE_PAGE_DOWN, KeyEvent.KEYCODE_DPAD_DOWN, - KeyEvent.KEYCODE_DPAD_RIGHT -> { + KeyEvent.KEYCODE_DPAD_RIGHT, + -> { reader?.switchPageBy(1) true } KeyEvent.KEYCODE_PAGE_UP, KeyEvent.KEYCODE_DPAD_UP, - KeyEvent.KEYCODE_DPAD_LEFT -> { + KeyEvent.KEYCODE_DPAD_LEFT, + -> { reader?.switchPageBy(-1) true } diff --git a/app/src/main/java/org/koitharu/kotatsu/utils/UiUtils.kt b/app/src/main/java/org/koitharu/kotatsu/utils/UiUtils.kt index 49ad21f02..1b268a848 100644 --- a/app/src/main/java/org/koitharu/kotatsu/utils/UiUtils.kt +++ b/app/src/main/java/org/koitharu/kotatsu/utils/UiUtils.kt @@ -1,9 +1,7 @@ package org.koitharu.kotatsu.utils import android.content.Context -import android.graphics.Color import android.view.View -import androidx.annotation.ColorInt import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.RecyclerView import org.koitharu.kotatsu.R @@ -14,16 +12,6 @@ import kotlin.math.roundToInt object UiUtils { - @JvmStatic - @ColorInt - fun invertColor(@ColorInt color: Int): Int { - val red = Color.red(color) - val green = Color.green(color) - val blue = Color.blue(color) - val alpha = Color.alpha(color) - return Color.argb(alpha, 255 - red, 255 - green, 255 - blue) - } - @JvmStatic fun resolveGridSpanCount(context: Context, width: Int = 0): Int { val scaleFactor = AppSettings(context).gridSize / 100f diff --git a/app/src/main/java/org/koitharu/kotatsu/utils/ext/ContextExt.kt b/app/src/main/java/org/koitharu/kotatsu/utils/ext/ContextExt.kt deleted file mode 100644 index adad2dfe9..000000000 --- a/app/src/main/java/org/koitharu/kotatsu/utils/ext/ContextExt.kt +++ /dev/null @@ -1,11 +0,0 @@ -package org.koitharu.kotatsu.utils.ext - -import android.content.Context -import androidx.appcompat.app.AlertDialog - -@Deprecated("Useless") -fun Context.showDialog(block: AlertDialog.Builder.() -> Unit): AlertDialog { - return AlertDialog.Builder(this) - .apply(block) - .show() -} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/utils/ext/JsoupExt.kt b/app/src/main/java/org/koitharu/kotatsu/utils/ext/ParseExt.kt similarity index 88% rename from app/src/main/java/org/koitharu/kotatsu/utils/ext/JsoupExt.kt rename to app/src/main/java/org/koitharu/kotatsu/utils/ext/ParseExt.kt index cee7c55b2..ea3cfd91e 100644 --- a/app/src/main/java/org/koitharu/kotatsu/utils/ext/JsoupExt.kt +++ b/app/src/main/java/org/koitharu/kotatsu/utils/ext/ParseExt.kt @@ -5,7 +5,6 @@ import okhttp3.internal.closeQuietly import org.json.JSONObject import org.jsoup.Jsoup import org.jsoup.nodes.Document -import org.jsoup.nodes.Element fun Response.parseHtml(): Document { try { @@ -28,6 +27,4 @@ fun Response.parseJson(): JSONObject { } finally { closeQuietly() } -} - -fun Element.firstChild(): Element? = children().first() \ No newline at end of file +} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/utils/ext/PrimitiveExt.kt b/app/src/main/java/org/koitharu/kotatsu/utils/ext/PrimitiveExt.kt index f5c0eb86b..c2a8c4c16 100644 --- a/app/src/main/java/org/koitharu/kotatsu/utils/ext/PrimitiveExt.kt +++ b/app/src/main/java/org/koitharu/kotatsu/utils/ext/PrimitiveExt.kt @@ -4,8 +4,6 @@ import java.text.DecimalFormat import java.text.NumberFormat import java.util.* -fun Number?.asBoolean() = (this?.toInt() ?: 0) > 0 - fun Number.format(decimals: Int = 0, decPoint: Char = '.', thousandsSep: Char? = ' '): String { val formatter = NumberFormat.getInstance(Locale.US) as DecimalFormat val symbols = formatter.decimalFormatSymbols diff --git a/app/src/main/java/org/koitharu/kotatsu/utils/ext/StringExt.kt b/app/src/main/java/org/koitharu/kotatsu/utils/ext/StringExt.kt index f9cd52848..dde557b31 100644 --- a/app/src/main/java/org/koitharu/kotatsu/utils/ext/StringExt.kt +++ b/app/src/main/java/org/koitharu/kotatsu/utils/ext/StringExt.kt @@ -57,7 +57,7 @@ fun String.transliterate(skipMissing: Boolean): String { } } -fun String.toFileName() = this.transliterate(false) +fun String.toFileNameSafe() = this.transliterate(false) .replace(Regex("[^a-z0-9_\\-]", setOf(RegexOption.IGNORE_CASE)), " ") .replace(Regex("\\s+"), "_") diff --git a/app/src/main/java/org/koitharu/kotatsu/utils/ext/ThemeExt.kt b/app/src/main/java/org/koitharu/kotatsu/utils/ext/ThemeExt.kt index 474a1d4dc..795fd3979 100644 --- a/app/src/main/java/org/koitharu/kotatsu/utils/ext/ThemeExt.kt +++ b/app/src/main/java/org/koitharu/kotatsu/utils/ext/ThemeExt.kt @@ -17,6 +17,7 @@ fun Context.getThemeDrawable(@AttrRes resId: Int) = obtainStyledAttributes(intAr } @ColorInt -fun Context.getThemeColor(@AttrRes resId: Int, @ColorInt default: Int = Color.TRANSPARENT) = obtainStyledAttributes(intArrayOf(resId)).use { - it.getColor(0, default) -} \ No newline at end of file +fun Context.getThemeColor(@AttrRes resId: Int, @ColorInt default: Int = Color.TRANSPARENT) = + obtainStyledAttributes(intArrayOf(resId)).use { + it.getColor(0, default) + } \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/utils/ext/ViewExt.kt b/app/src/main/java/org/koitharu/kotatsu/utils/ext/ViewExt.kt index bba6d1ed0..18ae3ed0f 100644 --- a/app/src/main/java/org/koitharu/kotatsu/utils/ext/ViewExt.kt +++ b/app/src/main/java/org/koitharu/kotatsu/utils/ext/ViewExt.kt @@ -2,11 +2,9 @@ package org.koitharu.kotatsu.utils.ext import android.app.Activity import android.graphics.Rect -import android.graphics.drawable.Drawable import android.util.Log import android.view.* import android.view.inputmethod.InputMethodManager -import android.widget.EditText import android.widget.TextView import androidx.annotation.LayoutRes import androidx.annotation.MenuRes @@ -23,7 +21,6 @@ import androidx.viewpager2.widget.ViewPager2 import com.google.android.material.chip.Chip import com.google.android.material.chip.ChipGroup import org.koitharu.kotatsu.ui.common.ChipsFactory -import kotlin.math.roundToInt fun View.hideKeyboard() { val imm = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager @@ -35,14 +32,9 @@ fun View.showKeyboard() { imm.showSoftInput(this, 0) } -val EditText.plainText - get() = text?.toString().orEmpty() - inline fun ViewGroup.inflate(@LayoutRes resId: Int) = LayoutInflater.from(context).inflate(resId, this, false) as T -val TextView.hasText get() = !text.isNullOrEmpty() - fun RecyclerView.lookupSpanSize(callback: (Int) -> Int) { (layoutManager as? GridLayoutManager)?.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() { @@ -53,20 +45,6 @@ fun RecyclerView.lookupSpanSize(callback: (Int) -> Int) { val RecyclerView.hasItems: Boolean get() = (adapter?.itemCount ?: 0) > 0 -var TextView.drawableStart: Drawable? - get() = compoundDrawablesRelative[0] - set(value) { - val old = compoundDrawablesRelative - setCompoundDrawablesRelativeWithIntrinsicBounds(value, old[1], old[2], old[3]) - } - -var TextView.drawableEnd: Drawable? - get() = compoundDrawablesRelative[2] - set(value) { - val old = compoundDrawablesRelative - setCompoundDrawablesRelativeWithIntrinsicBounds(old[0], old[1], value, old[3]) - } - var TextView.textAndVisible: CharSequence? get() = text?.takeIf { visibility == View.VISIBLE } set(value) { @@ -106,7 +84,7 @@ fun View.disableFor(timeInMillis: Long) { fun View.showPopupMenu( @MenuRes menuRes: Int, onPrepare: ((Menu) -> Unit)? = null, - onItemClick: (MenuItem) -> Boolean + onItemClick: (MenuItem) -> Boolean, ) { val menu = PopupMenu(context, this) menu.inflate(menuRes) @@ -221,11 +199,6 @@ fun ViewPager2.callOnPageChaneListeners() { } } -@Deprecated("") -fun LinearLayoutManager.findMiddleVisibleItemPosition(): Int { - return ((findFirstVisibleItemPosition() + findLastVisibleItemPosition()) / 2.0).roundToInt() -} - fun RecyclerView.findCenterViewPosition(): Int { val centerX = width / 2f val centerY = height / 2f