From 829ea01b18e77aa55ac1a3c20a2d77947d7ee9a1 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Wed, 2 Aug 2023 10:26:18 +0300 Subject: [PATCH] Migrate some classes to data classes --- .../kotatsu/bookmarks/domain/Bookmark.kt | 30 +------- .../kotatsu/list/ui/model/EmptyHint.kt | 22 +----- .../kotatsu/list/ui/model/EmptyState.kt | 22 +----- .../kotatsu/list/ui/model/ErrorFooter.kt | 18 +---- .../kotatsu/list/ui/model/ErrorState.kt | 22 +----- .../kotatsu/list/ui/model/ListHeader.kt | 22 +++--- .../kotatsu/list/ui/model/LoadingFooter.kt | 15 +--- .../kotatsu/list/ui/model/MangaGridModel.kt | 30 +------- .../list/ui/model/MangaListDetailedModel.kt | 33 +-------- .../kotatsu/list/ui/model/MangaListModel.kt | 31 +------- .../kotatsu/list/ui/model/TipModel.kt | 26 +------ .../suggestion/model/SearchSuggestionItem.kt | 70 ++----------------- 12 files changed, 31 insertions(+), 310 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/bookmarks/domain/Bookmark.kt b/app/src/main/kotlin/org/koitharu/kotatsu/bookmarks/domain/Bookmark.kt index ba53800d2..258a53ec8 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/bookmarks/domain/Bookmark.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/bookmarks/domain/Bookmark.kt @@ -6,7 +6,7 @@ import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.MangaPage import java.util.Date -class Bookmark( +data class Bookmark( val manga: Manga, val pageId: Long, val chapterId: Long, @@ -41,32 +41,4 @@ class Bookmark( val extension = imageUrl.substringAfterLast('.') return extension.isNotEmpty() && ImageFileFilter().isExtensionValid(extension) } - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as Bookmark - - if (manga != other.manga) return false - if (pageId != other.pageId) return false - if (chapterId != other.chapterId) return false - if (page != other.page) return false - if (scroll != other.scroll) return false - if (imageUrl != other.imageUrl) return false - if (createdAt != other.createdAt) return false - return percent == other.percent - } - - override fun hashCode(): Int { - var result = manga.hashCode() - result = 31 * result + pageId.hashCode() - result = 31 * result + chapterId.hashCode() - result = 31 * result + page - result = 31 * result + scroll - result = 31 * result + imageUrl.hashCode() - result = 31 * result + createdAt.hashCode() - result = 31 * result + percent.hashCode() - return result - } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/EmptyHint.kt b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/EmptyHint.kt index ad10cba44..ee60ee256 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/EmptyHint.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/EmptyHint.kt @@ -3,7 +3,7 @@ package org.koitharu.kotatsu.list.ui.model import androidx.annotation.DrawableRes import androidx.annotation.StringRes -class EmptyHint( +data class EmptyHint( @DrawableRes val icon: Int, @StringRes val textPrimary: Int, @StringRes val textSecondary: Int, @@ -15,24 +15,4 @@ class EmptyHint( override fun areItemsTheSame(other: ListModel): Boolean { return other is EmptyHint && textPrimary == other.textPrimary } - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as EmptyHint - - if (icon != other.icon) return false - if (textPrimary != other.textPrimary) return false - if (textSecondary != other.textSecondary) return false - return actionStringRes == other.actionStringRes - } - - override fun hashCode(): Int { - var result = icon - result = 31 * result + textPrimary - result = 31 * result + textSecondary - result = 31 * result + actionStringRes - return result - } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/EmptyState.kt b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/EmptyState.kt index 609a6ea46..e586eff44 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/EmptyState.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/EmptyState.kt @@ -3,33 +3,13 @@ package org.koitharu.kotatsu.list.ui.model import androidx.annotation.DrawableRes import androidx.annotation.StringRes -class EmptyState( +data class EmptyState( @DrawableRes val icon: Int, @StringRes val textPrimary: Int, @StringRes val textSecondary: Int, @StringRes val actionStringRes: Int, ) : ListModel { - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as EmptyState - - if (icon != other.icon) return false - if (textPrimary != other.textPrimary) return false - if (textSecondary != other.textSecondary) return false - return actionStringRes == other.actionStringRes - } - - override fun hashCode(): Int { - var result = icon - result = 31 * result + textPrimary - result = 31 * result + textSecondary - result = 31 * result + actionStringRes - return result - } - override fun areItemsTheSame(other: ListModel): Boolean { return other is EmptyState } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/ErrorFooter.kt b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/ErrorFooter.kt index e29ac986f..4cdbb9c1b 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/ErrorFooter.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/ErrorFooter.kt @@ -2,27 +2,11 @@ package org.koitharu.kotatsu.list.ui.model import androidx.annotation.DrawableRes -class ErrorFooter( +data class ErrorFooter( val exception: Throwable, @DrawableRes val icon: Int ) : ListModel { - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as ErrorFooter - - if (exception != other.exception) return false - return icon == other.icon - } - - override fun hashCode(): Int { - var result = exception.hashCode() - result = 31 * result + icon - return result - } - override fun areItemsTheSame(other: ListModel): Boolean { return other is ErrorFooter && exception == other.exception } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/ErrorState.kt b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/ErrorState.kt index a53c8bdc2..9f1bf50a5 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/ErrorState.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/ErrorState.kt @@ -3,32 +3,12 @@ package org.koitharu.kotatsu.list.ui.model import androidx.annotation.DrawableRes import androidx.annotation.StringRes -class ErrorState( +data class ErrorState( val exception: Throwable, @DrawableRes val icon: Int, val canRetry: Boolean, @StringRes val buttonText: Int ) : ListModel { - override fun areItemsTheSame(other: ListModel) = other is ErrorState - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as ErrorState - - if (exception != other.exception) return false - if (icon != other.icon) return false - if (canRetry != other.canRetry) return false - return buttonText == other.buttonText - } - - override fun hashCode(): Int { - var result = exception.hashCode() - result = 31 * result + icon - result = 31 * result + canRetry.hashCode() - result = 31 * result + buttonText - return result - } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/ListHeader.kt b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/ListHeader.kt index e4182c5cf..e225c1f28 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/ListHeader.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/ListHeader.kt @@ -4,12 +4,13 @@ import android.content.Context import androidx.annotation.StringRes import org.koitharu.kotatsu.core.ui.model.DateTimeAgo +@Suppress("DataClassPrivateConstructor") data class ListHeader private constructor( - private val text: CharSequence? = null, - @StringRes private val textRes: Int = 0, - private val dateTimeAgo: DateTimeAgo? = null, - @StringRes val buttonTextRes: Int = 0, - val payload: Any? = null, + private val text: CharSequence?, + @StringRes private val textRes: Int, + private val dateTimeAgo: DateTimeAgo?, + @StringRes val buttonTextRes: Int, + val payload: Any?, ) : ListModel { constructor( @@ -20,10 +21,15 @@ data class ListHeader private constructor( constructor( @StringRes textRes: Int, - @StringRes buttonTextRes: Int = 0 - ) : this(null, textRes, null, buttonTextRes) + @StringRes buttonTextRes: Int = 0, + payload: Any? = null, + ) : this(null, textRes, null, buttonTextRes, payload) - constructor(dateTimeAgo: DateTimeAgo) : this(null, dateTimeAgo = dateTimeAgo) + constructor( + dateTimeAgo: DateTimeAgo, + @StringRes buttonTextRes: Int = 0, + payload: Any? = null, + ) : this(null, 0, dateTimeAgo, buttonTextRes, payload) fun getText(context: Context): CharSequence? = when { text != null -> text diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/LoadingFooter.kt b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/LoadingFooter.kt index 1a2f8382e..7030d059d 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/LoadingFooter.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/LoadingFooter.kt @@ -1,22 +1,9 @@ package org.koitharu.kotatsu.list.ui.model -class LoadingFooter @JvmOverloads constructor( +data class LoadingFooter @JvmOverloads constructor( val key: Int = 0, ) : ListModel { - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as LoadingFooter - - return key == other.key - } - - override fun hashCode(): Int { - return key - } - override fun areItemsTheSame(other: ListModel): Boolean { return other is LoadingFooter && key == other.key } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/MangaGridModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/MangaGridModel.kt index 42f09464b..2b94795c6 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/MangaGridModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/MangaGridModel.kt @@ -1,38 +1,12 @@ package org.koitharu.kotatsu.list.ui.model -import org.koitharu.kotatsu.list.ui.adapter.MangaListAdapter import org.koitharu.kotatsu.parsers.model.Manga -class MangaGridModel( +data class MangaGridModel( override val id: Long, override val title: String, override val coverUrl: String, override val manga: Manga, override val counter: Int, override val progress: Float, -) : MangaItemModel() { - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as MangaGridModel - - if (id != other.id) return false - if (title != other.title) return false - if (coverUrl != other.coverUrl) return false - if (manga != other.manga) return false - if (counter != other.counter) return false - return progress == other.progress - } - - override fun hashCode(): Int { - var result = id.hashCode() - result = 31 * result + title.hashCode() - result = 31 * result + coverUrl.hashCode() - result = 31 * result + manga.hashCode() - result = 31 * result + counter - result = 31 * result + progress.hashCode() - return result - } -} +) : MangaItemModel() diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/MangaListDetailedModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/MangaListDetailedModel.kt index a8840c16c..e64c47ada 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/MangaListDetailedModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/MangaListDetailedModel.kt @@ -3,7 +3,7 @@ package org.koitharu.kotatsu.list.ui.model import org.koitharu.kotatsu.core.ui.widgets.ChipsView import org.koitharu.kotatsu.parsers.model.Manga -class MangaListDetailedModel( +data class MangaListDetailedModel( override val id: Long, override val title: String, val subtitle: String?, @@ -12,33 +12,4 @@ class MangaListDetailedModel( override val counter: Int, override val progress: Float, val tags: List, -) : MangaItemModel() { - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as MangaListDetailedModel - - if (id != other.id) return false - if (title != other.title) return false - if (subtitle != other.subtitle) return false - if (coverUrl != other.coverUrl) return false - if (manga != other.manga) return false - if (counter != other.counter) return false - if (progress != other.progress) return false - return tags == other.tags - } - - override fun hashCode(): Int { - var result = id.hashCode() - result = 31 * result + title.hashCode() - result = 31 * result + (subtitle?.hashCode() ?: 0) - result = 31 * result + coverUrl.hashCode() - result = 31 * result + manga.hashCode() - result = 31 * result + counter - result = 31 * result + progress.hashCode() - result = 31 * result + tags.hashCode() - return result - } -} +) : MangaItemModel() diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/MangaListModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/MangaListModel.kt index 7566505b0..54b0a800a 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/MangaListModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/MangaListModel.kt @@ -2,7 +2,7 @@ package org.koitharu.kotatsu.list.ui.model import org.koitharu.kotatsu.parsers.model.Manga -class MangaListModel( +data class MangaListModel( override val id: Long, override val title: String, val subtitle: String, @@ -10,31 +10,4 @@ class MangaListModel( override val manga: Manga, override val counter: Int, override val progress: Float, -) : MangaItemModel() { - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as MangaListModel - - if (id != other.id) return false - if (title != other.title) return false - if (subtitle != other.subtitle) return false - if (coverUrl != other.coverUrl) return false - if (manga != other.manga) return false - if (counter != other.counter) return false - return progress == other.progress - } - - override fun hashCode(): Int { - var result = id.hashCode() - result = 31 * result + title.hashCode() - result = 31 * result + subtitle.hashCode() - result = 31 * result + coverUrl.hashCode() - result = 31 * result + manga.hashCode() - result = 31 * result + counter - result = 31 * result + progress.hashCode() - return result - } -} +) : MangaItemModel() diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/TipModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/TipModel.kt index 9ff72b6cc..a3eb66045 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/TipModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/list/ui/model/TipModel.kt @@ -3,7 +3,7 @@ package org.koitharu.kotatsu.list.ui.model import androidx.annotation.DrawableRes import androidx.annotation.StringRes -class TipModel( +data class TipModel( val key: String, @StringRes val title: Int, @StringRes val text: Int, @@ -15,28 +15,4 @@ class TipModel( override fun areItemsTheSame(other: ListModel): Boolean { return other is TipModel && other.key == key } - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as TipModel - - if (key != other.key) return false - if (title != other.title) return false - if (text != other.text) return false - if (icon != other.icon) return false - if (primaryButtonText != other.primaryButtonText) return false - return secondaryButtonText == other.secondaryButtonText - } - - override fun hashCode(): Int { - var result = key.hashCode() - result = 31 * result + title - result = 31 * result + text - result = 31 * result + icon - result = 31 * result + primaryButtonText - result = 31 * result + secondaryButtonText - return result - } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/search/ui/suggestion/model/SearchSuggestionItem.kt b/app/src/main/kotlin/org/koitharu/kotatsu/search/ui/suggestion/model/SearchSuggestionItem.kt index e2445dbe2..9bf285e5d 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/search/ui/suggestion/model/SearchSuggestionItem.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/search/ui/suggestion/model/SearchSuggestionItem.kt @@ -6,61 +6,28 @@ import org.koitharu.kotatsu.list.ui.model.ListModel import org.koitharu.kotatsu.parsers.model.ContentType import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.MangaSource -import org.koitharu.kotatsu.parsers.util.areItemsEquals sealed interface SearchSuggestionItem : ListModel { - class MangaList( + data class MangaList( val items: List, ) : SearchSuggestionItem { override fun areItemsTheSame(other: ListModel): Boolean { return other is MangaList } - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as MangaList - - return items.areItemsEquals(other.items) { a, b -> - a.title == b.title && a.coverUrl == b.coverUrl - } - } - - override fun hashCode(): Int { - return items.fold(0) { acc, t -> - var r = 31 * acc + t.title.hashCode() - r = 31 * r + t.coverUrl.hashCode() - r - } - } } - class RecentQuery( + data class RecentQuery( val query: String, ) : SearchSuggestionItem { override fun areItemsTheSame(other: ListModel): Boolean { return other is RecentQuery && query == other.query } - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as RecentQuery - - return query == other.query - } - - override fun hashCode(): Int { - return query.hashCode() - } } - class Source( + data class Source( val source: MangaSource, val isEnabled: Boolean, ) : SearchSuggestionItem { @@ -82,25 +49,9 @@ sealed interface SearchSuggestionItem : ListModel { null } } - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as Source - - if (source != other.source) return false - return isEnabled == other.isEnabled - } - - override fun hashCode(): Int { - var result = source.hashCode() - result = 31 * result + isEnabled.hashCode() - return result - } } - class Tags( + data class Tags( val tags: List, ) : SearchSuggestionItem { @@ -111,18 +62,5 @@ sealed interface SearchSuggestionItem : ListModel { override fun getChangePayload(previousState: ListModel): Any { return ListModelDiffCallback.PAYLOAD_NESTED_LIST_CHANGED } - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as Tags - - return tags == other.tags - } - - override fun hashCode(): Int { - return tags.hashCode() - } } }