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 { sealed interface AutocompleteResult {
class AccountResult(val account: TimelineAccount) : 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 { interface AutocompletionProvider {

View File

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

View File

@ -91,15 +91,15 @@ data class LoginData(
val oauthRedirectUrl: Uri val oauthRedirectUrl: Uri
) : Parcelable ) : Parcelable
sealed class LoginResult : Parcelable { sealed interface LoginResult : Parcelable {
@Parcelize @Parcelize
data class Ok(val code: String) : LoginResult() data class Ok(val code: String) : LoginResult
@Parcelize @Parcelize
data class Err(val errorMessage: String) : LoginResult() data class Err(val errorMessage: String) : LoginResult
@Parcelize @Parcelize
data object Cancel : LoginResult() data object Cancel : LoginResult
} }
/** Activity to do Oauth process using WebView. */ /** 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. * Class to represent sum type/tagged union/variant/ADT e.t.c.
* It is either Left or Right. * It is either Left or Right.
*/ */
sealed class Either<out L, out R> { sealed interface Either<out L, out R> {
data class Left<out L, out R>(val value: L) : Either<L, 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>() 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 asLeft(): L = (this as Left<L, R>).value
fun asRight(): R = (this as Right<L, R>).value fun asRight(): R = (this as Right<L, R>).value
inline fun <N> map(crossinline mapper: (R) -> N): Either<L, N> { companion object {
return if (this.isLeft()) { inline fun <L, R, N> Either<L, R>.map(crossinline mapper: (R) -> N): Either<L, N> {
Left(this.asLeft()) return if (this.isLeft()) {
} else { Left(this.asLeft())
Right(mapper(this.asRight())) } else {
Right(mapper(this.asRight()))
}
} }
} }
} }

View File

@ -1,14 +1,16 @@
package com.keylesspalace.tusky.util 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>( class Error<T>(
override val data: T? = null, override val data: T? = null,
val errorMessage: String? = null, val errorMessage: String? = null,
var consumed: Boolean = false, var consumed: Boolean = false,
val cause: Throwable? = null 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.parseAsMastodonHtml
import com.keylesspalace.tusky.util.shouldTrimStatus import com.keylesspalace.tusky.util.shouldTrimStatus
sealed class TranslationViewData { sealed interface TranslationViewData {
abstract val data: Translation? 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? override val data: Translation?
get() = null get() = null
} }

View File

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

View File

@ -23,6 +23,7 @@ import at.connyduck.calladapter.networkresult.fold
import com.keylesspalace.tusky.entity.TimelineAccount import com.keylesspalace.tusky.entity.TimelineAccount
import com.keylesspalace.tusky.network.MastodonApi import com.keylesspalace.tusky.network.MastodonApi
import com.keylesspalace.tusky.util.Either 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.Left
import com.keylesspalace.tusky.util.Either.Right import com.keylesspalace.tusky.util.Either.Right
import com.keylesspalace.tusky.util.withoutFirstWhich import com.keylesspalace.tusky.util.withoutFirstWhich