Code cleanup

This commit is contained in:
Koitharu
2023-07-20 14:09:07 +03:00
parent 82a3b93214
commit 513aa1a285
20 changed files with 109 additions and 140 deletions

View File

@@ -1,6 +1,5 @@
package org.koitharu.kotatsu.core.backup
import org.json.JSONArray
import org.json.JSONObject
import org.koitharu.kotatsu.bookmarks.data.BookmarkEntity
import org.koitharu.kotatsu.core.db.entity.MangaEntity
@@ -92,15 +91,3 @@ class JsonDeserializer(private val json: JSONObject) {
return map
}
}
fun <K, T> JSONArray.mapJSONToSet(block: (K) -> T): Set<T> {
val len = length()
val result = androidx.collection.ArraySet<T>(len)
for (i in 0 until len) {
val jo = get(i) as K
result.add(block(jo))
}
return result
}

View File

@@ -21,9 +21,7 @@ class MangaWithTags(
other as MangaWithTags
if (manga != other.manga) return false
if (tags != other.tags) return false
return true
return tags == other.tags
}
override fun hashCode(): Int {
@@ -31,4 +29,4 @@ class MangaWithTags(
result = 31 * result + tags.hashCode()
return result
}
}
}

View File

@@ -40,9 +40,7 @@ class VersionId(
if (minor != other.minor) return false
if (build != other.build) return false
if (variantType != other.variantType) return false
if (variantNumber != other.variantNumber) return false
return true
return variantNumber == other.variantNumber
}
override fun hashCode(): Int {

View File

@@ -73,9 +73,7 @@ class CookieWrapper(
other as CookieWrapper
if (cookie != other.cookie) return false
return true
return cookie == other.cookie
}
override fun hashCode(): Int {

View File

@@ -3,7 +3,6 @@ package org.koitharu.kotatsu.core.parser
import org.koitharu.kotatsu.parsers.MangaLoaderContext
import org.koitharu.kotatsu.parsers.MangaParser
import org.koitharu.kotatsu.parsers.model.MangaSource
import org.koitharu.kotatsu.parsers.newParser
fun MangaParser(source: MangaSource, loaderContext: MangaLoaderContext): MangaParser {
return if (source == MangaSource.DUMMY) {

View File

@@ -8,6 +8,7 @@ import android.os.Build
import android.provider.Settings
import androidx.annotation.FloatRange
import androidx.appcompat.app.AppCompatDelegate
import androidx.collection.ArraySet
import androidx.collection.arraySetOf
import androidx.core.content.edit
import androidx.core.os.LocaleListCompat
@@ -15,7 +16,6 @@ import androidx.preference.PreferenceManager
import dagger.hilt.android.qualifiers.ApplicationContext
import org.json.JSONArray
import org.koitharu.kotatsu.BuildConfig
import org.koitharu.kotatsu.core.backup.mapJSONToSet
import org.koitharu.kotatsu.core.model.ZoomMode
import org.koitharu.kotatsu.core.network.DoHProvider
import org.koitharu.kotatsu.core.util.ext.connectivityManager
@@ -393,13 +393,13 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) {
fun upsertAll(m: Map<String, *>) {
prefs.edit {
m.forEach { e ->
when (e.value) {
is Boolean -> putBoolean(e.key, e.value as Boolean)
is Int -> putInt(e.key, e.value as Int)
is Long -> putLong(e.key, e.value as Long)
is Float -> putFloat(e.key, e.value as Float)
is String -> putString(e.key, e.value as String)
is JSONArray -> putStringSet(e.key, (e.value as JSONArray).mapJSONToSet<String, String> { it })
when (val v = e.value) {
is Boolean -> putBoolean(e.key, v)
is Int -> putInt(e.key, v)
is Long -> putLong(e.key, v)
is Float -> putFloat(e.key, v)
is String -> putString(e.key, v)
is JSONArray -> putStringSet(e.key, v.toStringSet())
}
}
}
@@ -413,6 +413,15 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) {
}
}
private fun JSONArray.toStringSet(): Set<String> {
val len = length()
val result = ArraySet<String>(len)
for (i in 0 until len) {
result.add(getString(i))
}
return result
}
companion object {
const val PAGE_SWITCH_TAPS = "taps"

View File

@@ -21,10 +21,10 @@ import android.view.View
import androidx.annotation.RequiresApi
import androidx.interpolator.view.animation.FastOutSlowInInterpolator
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.util.ext.dpToPx
import org.koitharu.kotatsu.core.util.ext.draw
import org.koitharu.kotatsu.core.util.ext.getAnimationDuration
import org.koitharu.kotatsu.core.util.ext.spToPx
import org.koitharu.kotatsu.core.util.ext.resolveDp
import org.koitharu.kotatsu.core.util.ext.resolveSp
class PieChart @JvmOverloads constructor(
context: Context,
@@ -33,11 +33,10 @@ class PieChart @JvmOverloads constructor(
) : View(context, attrs, defStyleAttr), PieChartInterface {
companion object {
private const val DEFAULT_MARGIN_TEXT_1 = 2
private const val DEFAULT_MARGIN_TEXT_2 = 10
private const val DEFAULT_MARGIN_TEXT_3 = 2
private const val DEFAULT_MARGIN_SMALL_CIRCLE = 12
private const val ANALYTICAL_PIE_CHART_KEY = "PieChartArrayData"
private const val DEFAULT_MARGIN_TEXT_1 = 2f
private const val DEFAULT_MARGIN_TEXT_2 = 10f
private const val DEFAULT_MARGIN_TEXT_3 = 2f
private const val DEFAULT_MARGIN_SMALL_CIRCLE = 12f
private const val TEXT_WIDTH_PERCENT = 0.40
private const val CIRCLE_WIDTH_PERCENT = 0.50
@@ -46,31 +45,31 @@ class PieChart @JvmOverloads constructor(
const val DEFAULT_VIEW_SIZE_WIDTH = 250
}
private var marginTextFirst: Float = context.dpToPx(DEFAULT_MARGIN_TEXT_1)
private var marginTextSecond: Float = context.dpToPx(DEFAULT_MARGIN_TEXT_2)
private var marginTextThird: Float = context.dpToPx(DEFAULT_MARGIN_TEXT_3)
private var marginSmallCircle: Float = context.dpToPx(DEFAULT_MARGIN_SMALL_CIRCLE)
private var marginTextFirst: Float = context.resources.resolveDp(DEFAULT_MARGIN_TEXT_1)
private var marginTextSecond: Float = context.resources.resolveDp(DEFAULT_MARGIN_TEXT_2)
private var marginTextThird: Float = context.resources.resolveDp(DEFAULT_MARGIN_TEXT_3)
private var marginSmallCircle: Float = context.resources.resolveDp(DEFAULT_MARGIN_SMALL_CIRCLE)
private val marginText: Float = marginTextFirst + marginTextSecond
private val circleRect = RectF()
private var circleStrokeWidth: Float = context.dpToPx(6)
private var circleRadius: Float = 0F
private var circlePadding: Float = context.dpToPx(8)
private var circleStrokeWidth: Float = context.resources.resolveDp(6f)
private var circleRadius: Float = 0f
private var circlePadding: Float = context.resources.resolveDp(8f)
private var circlePaintRoundSize: Boolean = true
private var circleSectionSpace: Float = 3F
private var circleCenterX: Float = 0F
private var circleCenterY: Float = 0F
private var circleSectionSpace: Float = 3f
private var circleCenterX: Float = 0f
private var circleCenterY: Float = 0f
private var numberTextPaint: TextPaint = TextPaint()
private var descriptionTextPain: TextPaint = TextPaint()
private var amountTextPaint: TextPaint = TextPaint()
private var textStartX: Float = 0F
private var textStartY: Float = 0F
private var textStartX: Float = 0f
private var textStartY: Float = 0f
private var textHeight: Int = 0
private var textCircleRadius: Float = context.dpToPx(4)
private var textCircleRadius: Float = context.resources.resolveDp(4f)
private var textAmountStr: String = ""
private var textAmountY: Float = 0F
private var textAmountXNumber: Float = 0F
private var textAmountXDescription: Float = 0F
private var textAmountYDescription: Float = 0F
private var textAmountY: Float = 0f
private var textAmountXNumber: Float = 0f
private var textAmountXDescription: Float = 0f
private var textAmountYDescription: Float = 0f
private var totalAmount: Int = 0
private var pieChartColors: List<String> = listOf()
private var percentageCircleList: List<PieChartModel> = listOf()
@@ -79,9 +78,9 @@ class PieChart @JvmOverloads constructor(
private var animationSweepAngle: Int = 0
init {
var textAmountSize: Float = context.spToPx(22)
var textNumberSize: Float = context.spToPx(20)
var textDescriptionSize: Float = context.spToPx(14)
var textAmountSize: Float = context.resources.resolveSp(22f)
var textNumberSize: Float = context.resources.resolveSp(20f)
var textDescriptionSize: Float = context.resources.resolveSp(14f)
var textAmountColor: Int = Color.WHITE
var textNumberColor: Int = Color.WHITE
var textDescriptionColor: Int = Color.GRAY
@@ -95,20 +94,25 @@ class PieChart @JvmOverloads constructor(
marginTextFirst = typeArray.getDimension(R.styleable.PieChart_pieChartMarginTextFirst, marginTextFirst)
marginTextSecond = typeArray.getDimension(R.styleable.PieChart_pieChartMarginTextSecond, marginTextSecond)
marginTextThird = typeArray.getDimension(R.styleable.PieChart_pieChartMarginTextThird, marginTextThird)
marginSmallCircle = typeArray.getDimension(R.styleable.PieChart_pieChartMarginSmallCircle, marginSmallCircle)
marginSmallCircle =
typeArray.getDimension(R.styleable.PieChart_pieChartMarginSmallCircle, marginSmallCircle)
circleStrokeWidth = typeArray.getDimension(R.styleable.PieChart_pieChartCircleStrokeWidth, circleStrokeWidth)
circleStrokeWidth =
typeArray.getDimension(R.styleable.PieChart_pieChartCircleStrokeWidth, circleStrokeWidth)
circlePadding = typeArray.getDimension(R.styleable.PieChart_pieChartCirclePadding, circlePadding)
circlePaintRoundSize = typeArray.getBoolean(R.styleable.PieChart_pieChartCirclePaintRoundSize, circlePaintRoundSize)
circlePaintRoundSize =
typeArray.getBoolean(R.styleable.PieChart_pieChartCirclePaintRoundSize, circlePaintRoundSize)
circleSectionSpace = typeArray.getFloat(R.styleable.PieChart_pieChartCircleSectionSpace, circleSectionSpace)
textCircleRadius = typeArray.getDimension(R.styleable.PieChart_pieChartTextCircleRadius, textCircleRadius)
textAmountSize = typeArray.getDimension(R.styleable.PieChart_pieChartTextAmountSize, textAmountSize)
textNumberSize = typeArray.getDimension(R.styleable.PieChart_pieChartTextNumberSize, textNumberSize)
textDescriptionSize = typeArray.getDimension(R.styleable.PieChart_pieChartTextDescriptionSize, textDescriptionSize)
textDescriptionSize =
typeArray.getDimension(R.styleable.PieChart_pieChartTextDescriptionSize, textDescriptionSize)
textAmountColor = typeArray.getColor(R.styleable.PieChart_pieChartTextAmountColor, textAmountColor)
textNumberColor = typeArray.getColor(R.styleable.PieChart_pieChartTextNumberColor, textNumberColor)
textDescriptionColor = typeArray.getColor(R.styleable.PieChart_pieChartTextDescriptionColor, textDescriptionColor)
textDescriptionColor =
typeArray.getColor(R.styleable.PieChart_pieChartTextDescriptionColor, textDescriptionColor)
textAmountStr = typeArray.getString(R.styleable.PieChart_pieChartTextAmount) ?: ""
typeArray.recycle()
@@ -176,11 +180,17 @@ class PieChart @JvmOverloads constructor(
}
private fun drawCircle(canvas: Canvas) {
for(percent in percentageCircleList) {
if (animationSweepAngle > percent.percentToStartAt + percent.percentOfCircle){
for (percent in percentageCircleList) {
if (animationSweepAngle > percent.percentToStartAt + percent.percentOfCircle) {
canvas.drawArc(circleRect, percent.percentToStartAt, percent.percentOfCircle, false, percent.paint)
} else if (animationSweepAngle > percent.percentToStartAt) {
canvas.drawArc(circleRect, percent.percentToStartAt, animationSweepAngle - percent.percentToStartAt, false, percent.paint)
canvas.drawArc(
circleRect,
percent.percentToStartAt,
animationSweepAngle - percent.percentToStartAt,
false,
percent.paint,
)
}
}
}
@@ -194,7 +204,7 @@ class PieChart @JvmOverloads constructor(
textStartX + marginSmallCircle / 2,
textBuffY + staticLayout.height / 2 + textCircleRadius / 2,
textCircleRadius,
Paint().apply { color = Color.parseColor(pieChartColors[(index / 2) % pieChartColors.size]) }
Paint().apply { color = Color.parseColor(pieChartColors[(index / 2) % pieChartColors.size]) },
)
textBuffY += staticLayout.height + marginTextFirst
} else {
@@ -216,8 +226,8 @@ class PieChart @JvmOverloads constructor(
}
private fun resolveDefaultSize(spec: Int, defValue: Int): Int {
return when(MeasureSpec.getMode(spec)) {
MeasureSpec.UNSPECIFIED -> context.dpToPx(defValue).toInt()
return when (MeasureSpec.getMode(spec)) {
MeasureSpec.UNSPECIFIED -> resources.resolveDp(defValue)
else -> MeasureSpec.getSize(spec)
}
}
@@ -253,10 +263,10 @@ class PieChart @JvmOverloads constructor(
val sizeTextAmountNumber = getWidthOfAmountText(
totalAmount.toString(),
amountTextPaint
amountTextPaint,
)
textAmountXNumber = circleCenterX - sizeTextAmountNumber.width() / 2
textAmountXNumber = circleCenterX - sizeTextAmountNumber.width() / 2
textAmountXDescription = circleCenterX - getWidthOfAmountText(textAmountStr, descriptionTextPain).width() / 2
textAmountYDescription = circleCenterY + sizeTextAmountNumber.height() + marginTextThird
}
@@ -268,12 +278,12 @@ class PieChart @JvmOverloads constructor(
val textLayoutNumber = getMultilineText(
text = it.first.toString(),
textPaint = numberTextPaint,
width = maxWidth
width = maxWidth,
)
val textLayoutDescription = getMultilineText(
text = it.second,
textPaint = descriptionTextPain,
width = maxWidth
width = maxWidth,
)
textRowList.apply {
add(textLayoutNumber)
@@ -286,21 +296,21 @@ class PieChart @JvmOverloads constructor(
}
private fun calculatePercentageOfData() {
totalAmount = dataList.fold(0) { res, value -> res + value.first}
totalAmount = dataList.fold(0) { res, value -> res + value.first }
var startAt = circleSectionSpace
percentageCircleList = dataList.mapIndexed { index, pair ->
var percent = pair.first * 100 / totalAmount.toFloat() - circleSectionSpace
percent = if (percent < 0F) 0F else percent
percent = if (percent < 0f) 0f else percent
val resultModel = PieChartModel(
percentOfCircle = percent,
percentToStartAt = startAt,
colorOfLine = Color.parseColor(pieChartColors[index % pieChartColors.size]),
stroke = circleStrokeWidth,
paintRound = circlePaintRoundSize
paintRound = circlePaintRoundSize,
)
if (percent != 0F) startAt += percent + circleSectionSpace
if (percent != 0f) startAt += percent + circleSectionSpace
resultModel
}
}
@@ -321,7 +331,8 @@ class PieChart @JvmOverloads constructor(
alignment: Layout.Alignment = Layout.Alignment.ALIGN_NORMAL,
textDir: TextDirectionHeuristic = TextDirectionHeuristics.LTR,
spacingMult: Float = 1f,
spacingAdd: Float = 0f) : StaticLayout {
spacingAdd: Float = 0f
): StaticLayout {
return StaticLayout.Builder
.obtain(text, start, end, textPaint, width)
@@ -340,23 +351,23 @@ interface PieChartInterface {
}
data class PieChartModel(
var percentOfCircle: Float = 0F,
var percentToStartAt: Float = 0F,
var percentOfCircle: Float = 0f,
var percentToStartAt: Float = 0f,
var colorOfLine: Int = 0,
var stroke: Float = 0F,
var stroke: Float = 0f,
var paint: Paint = Paint(),
var paintRound: Boolean = true
) {
init {
if (percentOfCircle < 0 || percentOfCircle > 100) {
percentOfCircle = 100F
percentOfCircle = 100f
}
percentOfCircle = 360 * percentOfCircle / 100
if (percentToStartAt < 0 || percentToStartAt > 100) {
percentToStartAt = 0F
percentToStartAt = 0f
}
percentToStartAt = 360 * percentToStartAt / 100
@@ -370,18 +381,17 @@ data class PieChartModel(
paint.isAntiAlias = true
paint.style = Paint.Style.STROKE
paint.strokeWidth = stroke
paint.isDither = true;
paint.isDither = true
if (paintRound){
paint.strokeJoin = Paint.Join.ROUND;
paint.strokeCap = Paint.Cap.ROUND;
paint.pathEffect = CornerPathEffect(8F);
if (paintRound) {
paint.strokeJoin = Paint.Join.ROUND
paint.strokeCap = Paint.Cap.ROUND
paint.pathEffect = CornerPathEffect(8f)
}
}
}
class PieChartState(
private val superSavedState: Parcelable?,
superSavedState: Parcelable?,
val dataList: List<Pair<Int, String>>
) : View.BaseSavedState(superSavedState), Parcelable {
}
) : View.BaseSavedState(superSavedState), Parcelable

View File

@@ -20,7 +20,6 @@ import android.os.Build
import android.os.Bundle
import android.os.PowerManager
import android.provider.Settings
import android.util.TypedValue
import android.view.View
import android.view.ViewPropertyAnimator
import android.view.Window
@@ -210,11 +209,3 @@ inline fun Activity.catchingWebViewUnavailability(block: () -> Unit): Boolean {
}
}
}
fun Context.dpToPx(dp: Int): Float {
return dp.toFloat() * this.resources.displayMetrics.density
}
fun Context.spToPx(sp: Int): Float {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp.toFloat(), this.resources.displayMetrics);
}

View File

@@ -3,6 +3,7 @@ package org.koitharu.kotatsu.core.util.ext
import android.annotation.SuppressLint
import android.content.Context
import android.content.res.Resources
import android.util.TypedValue
import androidx.annotation.Px
import kotlin.math.roundToInt
@@ -12,6 +13,9 @@ fun Resources.resolveDp(dp: Int) = (dp * displayMetrics.density).roundToInt()
@Px
fun Resources.resolveDp(dp: Float) = dp * displayMetrics.density
@Px
fun Resources.resolveSp(sp: Float) = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, displayMetrics)
@SuppressLint("DiscouragedApi")
fun Context.getSystemBoolean(resName: String, fallback: Boolean): Boolean {
val id = Resources.getSystem().getIdentifier(resName, "bool", "android")

View File

@@ -22,9 +22,7 @@ class HistoryInfo(
if (totalChapters != other.totalChapters) return false
if (currentChapter != other.currentChapter) return false
if (history != other.history) return false
if (isIncognitoMode != other.isIncognitoMode) return false
return true
return isIncognitoMode == other.isIncognitoMode
}
override fun hashCode(): Int {

View File

@@ -5,7 +5,7 @@ import android.view.View
import androidx.recyclerview.widget.RecyclerView
import org.koitharu.kotatsu.R
class ScrobblingItemDecoration() : RecyclerView.ItemDecoration() {
class ScrobblingItemDecoration : RecyclerView.ItemDecoration() {
private var spacing: Int = -1

View File

@@ -30,9 +30,7 @@ data class FavouriteCategoryEntity(
if (title != other.title) return false
if (order != other.order) return false
if (track != other.track) return false
if (isVisibleInLibrary != other.isVisibleInLibrary) return false
return true
return isVisibleInLibrary == other.isVisibleInLibrary
}
override fun hashCode(): Int {

View File

@@ -24,9 +24,7 @@ class ScrobblerManga(
if (name != other.name) return false
if (altName != other.altName) return false
if (cover != other.cover) return false
if (url != other.url) return false
return true
return url == other.url
}
override fun hashCode(): Int {

View File

@@ -16,9 +16,7 @@ class ScrobblerUser(
if (id != other.id) return false
if (nickname != other.nickname) return false
if (avatar != other.avatar) return false
if (service != other.service) return false
return true
return service == other.service
}
override fun hashCode(): Int {

View File

@@ -36,9 +36,7 @@ class ScrobblingInfo(
if (title != other.title) return false
if (coverUrl != other.coverUrl) return false
if (description != other.description) return false
if (externalUrl != other.externalUrl) return false
return true
return externalUrl == other.externalUrl
}
override fun hashCode(): Int {

View File

@@ -84,7 +84,6 @@ class ScrobblingSelectorSheet :
tab.select()
}
}
viewModel.searchQuery.observe(viewLifecycleOwner, ::onSearchQueryChanged)
}
override fun onDestroyView() {
@@ -157,10 +156,6 @@ class ScrobblingSelectorSheet :
menuItem.expandActionView()
}
private fun onSearchQueryChanged(query: String?) {
}
private fun onError(e: Throwable) {
Toast.makeText(requireContext(), e.getDisplayMessage(resources), Toast.LENGTH_LONG).show()
if (viewModel.isEmpty) {

View File

@@ -52,9 +52,7 @@ sealed interface SourceConfigItem : ListModel {
if (localeId != other.localeId) return false
if (title != other.title) return false
if (isExpanded != other.isExpanded) return false
return true
return isExpanded == other.isExpanded
}
override fun hashCode(): Int {
@@ -93,9 +91,7 @@ sealed interface SourceConfigItem : ListModel {
if (source != other.source) return false
if (summary != other.summary) return false
if (isEnabled != other.isEnabled) return false
if (isDraggable != other.isDraggable) return false
return true
return isDraggable == other.isDraggable
}
override fun hashCode(): Int {
@@ -125,9 +121,7 @@ sealed interface SourceConfigItem : ListModel {
if (key != other.key) return false
if (iconResId != other.iconResId) return false
if (textResId != other.textResId) return false
return true
return textResId == other.textResId
}
override fun hashCode(): Int {

View File

@@ -4,7 +4,7 @@ import android.text.TextUtils
import androidx.preference.EditTextPreference
import androidx.preference.Preference
class PasswordSummaryProvider() : Preference.SummaryProvider<EditTextPreference> {
class PasswordSummaryProvider : Preference.SummaryProvider<EditTextPreference> {
private val delegate = EditTextPreference.SimpleSummaryProvider.getInstance()

View File

@@ -21,9 +21,7 @@ class MangaTracking(
if (manga != other.manga) return false
if (lastChapterId != other.lastChapterId) return false
if (lastCheck != other.lastCheck) return false
return true
return lastCheck == other.lastCheck
}
override fun hashCode(): Int {
@@ -32,4 +30,4 @@ class MangaTracking(
result = 31 * result + (lastCheck?.hashCode() ?: 0)
return result
}
}
}

View File

@@ -18,9 +18,7 @@ class TrackingItem(
other as TrackingItem
if (tracking != other.tracking) return false
if (channelId != other.channelId) return false
return true
return channelId == other.channelId
}
override fun hashCode(): Int {
@@ -28,4 +26,4 @@ class TrackingItem(
result = 31 * result + channelId.hashCode()
return result
}
}
}