Skip to content
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

Merged
merged 5 commits into from
Feb 28, 2024
Merged

Add some words about Compose #252

merged 5 commits into from
Feb 28, 2024

Conversation

serras
Copy link
Member

@serras serras commented Nov 13, 2023

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".

Copy link
Member

@raulraja raulraja left a 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.

@serras
Copy link
Member Author

serras commented Nov 13, 2023

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?

@raulraja
Copy link
Member

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.

@serras
Copy link
Member Author

serras commented Nov 14, 2023

@raulraja I've added a few words about Either and Ior, which I think it's the part most developer can immediately understand. I think the Raise DSL requires some additional investment, so I've added it at the end of the section. WDYT?

@serras
Copy link
Member Author

serras commented Nov 30, 2023

I've added some additional documentation in relation to arrow-kt/arrow#3299

@serras serras merged commit 9e80f12 into main Feb 28, 2024
1 check passed
@serras serras deleted the serras/compose branch February 28, 2024 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants