-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SimpleGravatarProfileIntegration + demo usage
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 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
56 changes: 56 additions & 0 deletions
56
demo-app/src/main/java/com/gravatar/demoapp/ui/SimpleGravatarProfileIntegration.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,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)) | ||
} | ||
} |