Convert some sealed classes to interfaces (#4347)

There is no non-abstract field in them, we can just fall back to
interfaces.
This commit is contained in:
Zongle Wang 2024-03-30 03:11:53 +08:00 committed by GitHub
parent e865ffafde
commit 1acae50845
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 43 additions and 40 deletions

View File

@ -145,12 +145,12 @@ class ComposeAutoCompleteAdapter(
}
}
sealed class AutocompleteResult {
class AccountResult(val account: TimelineAccount) : AutocompleteResult()
sealed interface AutocompleteResult {
class AccountResult(val account: TimelineAccount) : AutocompleteResult
class HashtagResult(val hashtag: String) : AutocompleteResult()
class HashtagResult(val hashtag: String) : AutocompleteResult
class EmojiResult(val emoji: Emoji) : AutocompleteResult()
class EmojiResult(val emoji: Emoji) : AutocompleteResult
}
interface AutocompletionProvider {

View File

@ -64,13 +64,13 @@ import retrofit2.HttpException
sealed interface FinalUploadEvent
sealed class UploadEvent {
data class ProgressEvent(val percentage: Int) : UploadEvent()
sealed interface UploadEvent {
data class ProgressEvent(val percentage: Int) : UploadEvent
data class FinishedEvent(
val mediaId: String,
val processed: Boolean
) : UploadEvent(), FinalUploadEvent
data class ErrorEvent(val error: Throwable) : UploadEvent(), FinalUploadEvent
) : UploadEvent, FinalUploadEvent
data class ErrorEvent(val error: Throwable) : UploadEvent, FinalUploadEvent
}
data class UploadData(

View File

@ -91,15 +91,15 @@ data class LoginData(
val oauthRedirectUrl: Uri
) : Parcelable
sealed class LoginResult : Parcelable {
sealed interface LoginResult : Parcelable {
@Parcelize
data class Ok(val code: String) : LoginResult()
data class Ok(val code: String) : LoginResult
@Parcelize
data class Err(val errorMessage: String) : LoginResult()
data class Err(val errorMessage: String) : LoginResult
@Parcelize
data object Cancel : LoginResult()
data object Cancel : LoginResult
}
/** Activity to do Oauth process using WebView. */

View File

@ -21,27 +21,29 @@ package com.keylesspalace.tusky.util
* Class to represent sum type/tagged union/variant/ADT e.t.c.
* It is either Left or Right.
*/
sealed class Either<out L, out R> {
data class Left<out L, out R>(val value: L) : Either<L, R>()
data class Right<out L, out R>(val value: R) : Either<L, R>()
sealed interface Either<out L, out R> {
data class Left<out L, out R>(val value: L) : Either<L, R>
data class Right<out L, out R>(val value: R) : Either<L, R>
fun isRight() = this is Right
fun isRight(): Boolean = this is Right
fun isLeft() = this is Left
fun isLeft(): Boolean = this is Left
fun asLeftOrNull() = (this as? Left<L, R>)?.value
fun asLeftOrNull(): L? = (this as? Left<L, R>)?.value
fun asRightOrNull() = (this as? Right<L, R>)?.value
fun asRightOrNull(): R? = (this as? Right<L, R>)?.value
fun asLeft(): L = (this as Left<L, R>).value
fun asRight(): R = (this as Right<L, R>).value
inline fun <N> map(crossinline mapper: (R) -> N): Either<L, N> {
return if (this.isLeft()) {
Left(this.asLeft())
} else {
Right(mapper(this.asRight()))
companion object {
inline fun <L, R, N> Either<L, R>.map(crossinline mapper: (R) -> N): Either<L, N> {
return if (this.isLeft()) {
Left(this.asLeft())
} else {
Right(mapper(this.asRight()))
}
}
}
}

View File

@ -1,14 +1,16 @@
package com.keylesspalace.tusky.util
sealed class Resource<T>(open val data: T?)
sealed interface Resource<T> {
val data: T?
}
class Loading<T>(override val data: T? = null) : Resource<T>(data)
class Loading<T>(override val data: T? = null) : Resource<T>
class Success<T>(override val data: T? = null) : Resource<T>(data)
class Success<T>(override val data: T? = null) : Resource<T>
class Error<T>(
override val data: T? = null,
val errorMessage: String? = null,
var consumed: Boolean = false,
val cause: Throwable? = null
) : Resource<T>(data)
) : Resource<T>

View File

@ -22,12 +22,12 @@ import com.keylesspalace.tusky.entity.Translation
import com.keylesspalace.tusky.util.parseAsMastodonHtml
import com.keylesspalace.tusky.util.shouldTrimStatus
sealed class TranslationViewData {
abstract val data: Translation?
sealed interface TranslationViewData {
val data: Translation?
data class Loaded(override val data: Translation) : TranslationViewData()
data class Loaded(override val data: Translation) : TranslationViewData
data object Loading : TranslationViewData() {
data object Loading : TranslationViewData {
override val data: Translation?
get() = null
}

View File

@ -17,15 +17,14 @@ package com.keylesspalace.tusky.viewdata
import java.util.Date
sealed class TrendingViewData {
abstract val id: String
sealed interface TrendingViewData {
val id: String
data class Header(
val start: Date,
val end: Date
) : TrendingViewData() {
override val id: String
get() = start.toString() + end.toString()
) : TrendingViewData {
override val id: String = start.toString() + end.toString()
}
data class Tag(
@ -33,8 +32,7 @@ sealed class TrendingViewData {
val usage: List<Long>,
val accounts: List<Long>,
val maxTrendingValue: Long
) : TrendingViewData() {
override val id: String
get() = name
) : TrendingViewData {
override val id: String = name
}
}

View File

@ -23,6 +23,7 @@ import at.connyduck.calladapter.networkresult.fold
import com.keylesspalace.tusky.entity.TimelineAccount
import com.keylesspalace.tusky.network.MastodonApi
import com.keylesspalace.tusky.util.Either
import com.keylesspalace.tusky.util.Either.Companion.map
import com.keylesspalace.tusky.util.Either.Left
import com.keylesspalace.tusky.util.Either.Right
import com.keylesspalace.tusky.util.withoutFirstWhich