-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add some words about Compose #252
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!, thanks @serras , Are there any patterns where Raise
is useful with compose for error / success display? Perhaps we can include something there too.
I've tried to think about an example, but I couldn't find a good one for this case, as opposed to something like a circuit breaker or optics. Do you have any in mind? |
I was thinking about something like: import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import arrow.core.raise.Raise
import arrow.core.raise.either
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
// Error types
sealed class ErrorType
object UserNotFound : ErrorType()
object NetworkError : ErrorType()
// Data class for User Profile
data class UserProfile(val id: String, val name: String)
// Sample function to fetch user profile
suspend fun Raise<ErrorType>.fetchUserProfile(userId: String): UserProfile {
delay(3000)
// Here, you would implement actual logic to fetch the user profile
// For demonstration, let's assume the user is not found
raise(UserNotFound)
}
// Main Composable Function
@Composable
fun UserProfileScreen() {
val coroutineScope = rememberCoroutineScope()
var userProfile by remember { mutableStateOf<UserProfile?>(null) }
var error by remember { mutableStateOf<ErrorType?>(null) }
// Simulate fetch user profile
coroutineScope.launch {
either {
fetchUserProfile("123")
}.fold(
ifLeft = { errorType ->
error = errorType
},
ifRight = { profile ->
userProfile = profile
}
)
}
when {
userProfile != null -> Text("User: ${userProfile!!.name}")
error is UserNotFound -> Text("User not found")
error is NetworkError -> Text("Network error. Please try again.")
else -> Text("Loading...")
}
}
@Composable
@Preview
fun App() {
MaterialTheme {
UserProfileScreen()
}
}
fun main() = application {
Window(onCloseRequest = ::exitApplication) {
App()
}
} But I don't know if this is considered a good pattern in Compose. |
@raulraja I've added a few words about |
I've added some additional documentation in relation to arrow-kt/arrow#3299 |
This section makes it more visible that Arrow is a great companion for Compose, and helps combat the impression that Arrow may be "backend-oriented".