From 853bbd1c8dd0168be6218bcf234406242d21a33d Mon Sep 17 00:00:00 2001 From: Maxime Biais Date: Wed, 24 Apr 2024 18:17:11 +0200 Subject: [PATCH] SimpleGravatarProfileIntegration + demo usage --- .../gravatar/demoapp/ui/DemoGravatarApp.kt | 1 + .../ui/SimpleGravatarProfileIntegration.kt | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 demo-app/src/main/java/com/gravatar/demoapp/ui/SimpleGravatarProfileIntegration.kt diff --git a/demo-app/src/main/java/com/gravatar/demoapp/ui/DemoGravatarApp.kt b/demo-app/src/main/java/com/gravatar/demoapp/ui/DemoGravatarApp.kt index aa3c137ec..4aff9f828 100644 --- a/demo-app/src/main/java/com/gravatar/demoapp/ui/DemoGravatarApp.kt +++ b/demo-app/src/main/java/com/gravatar/demoapp/ui/DemoGravatarApp.kt @@ -310,6 +310,7 @@ private fun ProfileTab(modifier: Modifier = Modifier, onError: (String?, Throwab } } Spacer(modifier = Modifier.height(16.dp)) + GravatarProfileSummary(email) ProfileComponents(profileState, theme, error) } } diff --git a/demo-app/src/main/java/com/gravatar/demoapp/ui/SimpleGravatarProfileIntegration.kt b/demo-app/src/main/java/com/gravatar/demoapp/ui/SimpleGravatarProfileIntegration.kt new file mode 100644 index 000000000..7ed24ab42 --- /dev/null +++ b/demo-app/src/main/java/com/gravatar/demoapp/ui/SimpleGravatarProfileIntegration.kt @@ -0,0 +1,56 @@ +package com.gravatar.demoapp.ui + +import android.util.Log +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.neverEqualPolicy +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp +import com.gravatar.services.ProfileService +import com.gravatar.services.Result +import com.gravatar.types.Email +import com.gravatar.ui.components.ProfileSummary +import com.gravatar.ui.components.UserProfileState + +@Composable +fun GravatarProfileSummary(emailAddress: String = "gravatar@automattic.com") { + // Create a ProfileService instance + val profileService = ProfileService() + + // Create a mutable state for the user profile state + var profileState: UserProfileState? by remember { mutableStateOf(null, neverEqualPolicy()) } + + // We wrap the fetch call in a LaunchedEffect to fetch the profile when the composable is first launched, but this + // could be triggered by a button click, a text field change, etc. + LaunchedEffect(emailAddress) { + // Set the profile state to loading + profileState = UserProfileState.Loading + // Fetch the user profile + when (val result = profileService.fetch(Email(emailAddress))) { + is Result.Success -> { + // Update the profile state with the loaded profile + result.value.let { + profileState = UserProfileState.Loaded(it) + } + } + is Result.Failure -> { + // An error can occur when a profile doesn't exist, if the phone is in airplane mode, etc. + // Here we log the error, but ideally we should show an error to the user. + Log.e("Gravatar", result.error.name) + // Set the Empty state on error + profileState = UserProfileState.Empty + } + } + } + + // Show the profile as a ProfileCard + profileState?.let { + ProfileSummary(it, modifier = Modifier.fillMaxWidth().padding(16.dp)) + } +}