package gq.kirmanak.mealient.ui import android.widget.Button import android.widget.ProgressBar import androidx.core.view.isVisible sealed class OperationUiState { val exceptionOrNull: Throwable? get() = (this as? Failure)?.exception val isSuccess: Boolean get() = this is Success val isProgress: Boolean get() = this is Progress val isFailure: Boolean get() = this is Failure fun updateButtonState(button: Button) { button.isEnabled = !isProgress button.isClickable = !isProgress } fun updateProgressState(progressBar: ProgressBar) { progressBar.isVisible = isProgress } class Initial : OperationUiState() { override fun equals(other: Any?): Boolean { if (this === other) return true if (javaClass != other?.javaClass) return false return true } override fun hashCode(): Int { return javaClass.hashCode() } } class Progress : OperationUiState() { override fun equals(other: Any?): Boolean { if (this === other) return true if (javaClass != other?.javaClass) return false return true } override fun hashCode(): Int { return javaClass.hashCode() } } data class Failure(val exception: Throwable) : OperationUiState() data class Success(val value: T) : OperationUiState() companion object { fun fromResult(result: Result) = result.fold({ Success(it) }, { Failure(it) }) } }