Skip to content

Commit

Permalink
SimpleGravatarProfileIntegration + demo usage
Browse files Browse the repository at this point in the history
  • Loading branch information
maxme committed May 17, 2024
1 parent 9bfa4a0 commit 853bbd1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]") {
// 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))
}
}

0 comments on commit 853bbd1

Please sign in to comment.