Cleanup code

This commit is contained in:
Koitharu
2020-04-05 20:40:58 +03:00
parent a78774d10e
commit 1d387709f2
11 changed files with 39 additions and 89 deletions

View File

@@ -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)
}

View File

@@ -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
}

View File

@@ -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<File>() {
@@ -81,14 +81,14 @@ class LocalListFragment : MangaListFragment<File>() {
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)

View File

@@ -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
}

View File

@@ -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

View File

@@ -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()
}

View File

@@ -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()
}

View File

@@ -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

View File

@@ -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+"), "_")

View File

@@ -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)
}
fun Context.getThemeColor(@AttrRes resId: Int, @ColorInt default: Int = Color.TRANSPARENT) =
obtainStyledAttributes(intArrayOf(resId)).use {
it.getColor(0, default)
}

View File

@@ -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 <reified T : View> 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