-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from alsterverse/feature/add-ui-state
Add UI state
- Loading branch information
Showing
4 changed files
with
135 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import java.util.Properties | ||
|
||
val githubProperties = Properties().apply { | ||
load(rootProject.file("github.properties").inputStream()) | ||
} | ||
|
||
plugins { | ||
alias(libs.plugins.kotlinMultiplatform) | ||
alias(libs.plugins.androidLibrary) | ||
id("com.vanniktech.maven.publish") | ||
} | ||
|
||
val versionName = githubProperties.getProperty("version") | ||
|
||
kotlin { | ||
androidTarget { | ||
compilations.all { | ||
kotlinOptions { | ||
jvmTarget = "17" | ||
} | ||
} | ||
publishLibraryVariants("release", "debug") | ||
} | ||
|
||
iosX64() | ||
iosArm64() | ||
iosSimulatorArm64() | ||
|
||
jvmToolchain(17) | ||
|
||
sourceSets { | ||
|
||
} | ||
} | ||
|
||
android { | ||
namespace = "se.alster.ui_state" | ||
compileSdk = 34 | ||
defaultConfig { | ||
minSdk = 24 | ||
} | ||
} | ||
|
||
|
||
publishing { | ||
repositories { | ||
maven { | ||
name = "GitHubPackages" | ||
url = uri("https://maven.pkg.github.com/Alsterverse/alster-modules") | ||
credentials { | ||
username = githubProperties.getProperty("gpr.user") | ||
password = githubProperties.getProperty("gpr.token") | ||
} | ||
} | ||
} | ||
} | ||
|
||
mavenPublishing { | ||
coordinates("se.alster", "ui-state", versionName) | ||
} |
63 changes: 63 additions & 0 deletions
63
ui-state/src/commonMain/kotlin/se/alster/ui_state/UiState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package se.alster.ui_state | ||
|
||
/** | ||
* Represents the state of a UI component. It can be in one of the following states: | ||
* - [Idle] - The initial state of the UI component. | ||
* - [Loading] - The UI component is loading data. | ||
* - [Data] - The UI component has received data. | ||
* - [Error] - An error occurred while loading data. | ||
* @param T The type of the data. | ||
* @see Result | ||
* @see toUiState | ||
* @see toUiStateData | ||
* @see mapData | ||
* @see dataOrNull | ||
*/ | ||
sealed interface UiState<out T> { | ||
data object Idle : UiState<Nothing> | ||
data object Loading : UiState<Nothing> | ||
data class Data<out T>(val value: T) : UiState<T> | ||
data class Error(val value: Throwable) : UiState<Nothing> | ||
} | ||
|
||
/** | ||
* Converts a [Result] to a [UiState]. | ||
*/ | ||
fun <T> Result<T>.toUiState(): UiState<T> { | ||
onSuccess { | ||
return UiState.Data(it) | ||
} | ||
onFailure { | ||
return UiState.Error(it) | ||
} | ||
return UiState.Loading | ||
} | ||
|
||
/** | ||
* Converts a value to a [UiState.Data]. | ||
*/ | ||
fun <T : Any> T.toUiStateData(): UiState.Data<T> { | ||
return UiState.Data(this) | ||
} | ||
|
||
/** | ||
* Maps the data of a [UiState] to another type. | ||
*/ | ||
fun <T, R> UiState<T>.mapData(data: (T) -> R): UiState<R> { | ||
return when (this) { | ||
is UiState.Data -> UiState.Data(data(value)) | ||
is UiState.Error -> UiState.Error(value) | ||
is UiState.Loading -> UiState.Loading | ||
is UiState.Idle -> UiState.Idle | ||
} | ||
} | ||
|
||
/** | ||
* Returns the data of a [UiState] or `null` if the state is not [UiState.Data]. | ||
*/ | ||
fun <T> UiState<T>.dataOrNull(): T? { | ||
return when (this) { | ||
is UiState.Data -> value | ||
else -> null | ||
} | ||
} |