-
Notifications
You must be signed in to change notification settings - Fork 34
DemoApp - First interation #42
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9848178
First version of demoApp loading the Gravatar
hamorillo 51d5d94
Added DefaultGravatarImage picker in the configuration panel
hamorillo c39cb5d
Extracting settings composable to it's own file
hamorillo eb2ed8f
Added compose tooling dependency to be able to preview composables
hamorillo 52790b9
DefaultAvatarImageDropdown composable extracted to it's own file
hamorillo e8942ee
Using a testing account with an avatar
hamorillo 3dfc24f
Adding a surface as a parent of the composables hierarchy
hamorillo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
68 changes: 68 additions & 0 deletions
68
app/src/main/java/com/gravatar/demoapp/ui/DefaultAvatarImageDropdown.kt
This file contains hidden or 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,68 @@ | ||
package com.gravatar.demoapp.ui | ||
|
||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.material3.Checkbox | ||
import androidx.compose.material3.DropdownMenuItem | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.ExposedDropdownMenuBox | ||
import androidx.compose.material3.ExposedDropdownMenuDefaults | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TextField | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import com.gravatar.DefaultAvatarImage | ||
import com.gravatar.R | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
fun DefaultAvatarImageDropdown( | ||
enabled: Boolean, | ||
onEnabledChanged: (Boolean) -> Unit, | ||
selectedOption: DefaultAvatarImage, | ||
onSelectedOptionChange: (DefaultAvatarImage) -> Unit, | ||
defaultAvatarOptions: List<DefaultAvatarImage>, | ||
modifier: Modifier = Modifier, | ||
) { | ||
var expanded by remember { mutableStateOf(false) } | ||
Row(modifier = modifier) { | ||
Checkbox( | ||
checked = enabled, | ||
onCheckedChange = { | ||
if (enabled) { | ||
expanded = false | ||
} | ||
onEnabledChanged(!enabled) | ||
}, | ||
) | ||
ExposedDropdownMenuBox( | ||
expanded = expanded, | ||
onExpandedChange = { expanded = !expanded }, | ||
modifier = Modifier.fillMaxWidth(), | ||
) { | ||
TextField( | ||
readOnly = true, | ||
value = selectedOption.style, | ||
onValueChange = { }, | ||
label = { Text(stringResource(R.string.default_avatar_image_label)) }, | ||
trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) }, | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.menuAnchor(), | ||
) | ||
ExposedDropdownMenu(expanded = expanded, onDismissRequest = { expanded = false }) { | ||
defaultAvatarOptions.forEach { selectionOption -> | ||
DropdownMenuItem(text = { Text(text = selectionOption.style) }, onClick = { | ||
onSelectedOptionChange.invoke(selectionOption) | ||
expanded = false | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
app/src/main/java/com/gravatar/demoapp/ui/DemoGravatarApp.kt
This file contains hidden or 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,106 @@ | ||
package com.gravatar.demoapp.ui | ||
|
||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.Divider | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Surface | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TextField | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.unit.dp | ||
import coil.compose.AsyncImage | ||
import com.gravatar.DefaultAvatarImage | ||
import com.gravatar.R | ||
import com.gravatar.demoapp.theme.GravatarDemoAppTheme | ||
import com.gravatar.emailAddressToGravatarUrl | ||
|
||
@Composable | ||
fun DemoGravatarApp() { | ||
var email by remember { mutableStateOf("[email protected]") } | ||
var gravatarUrl by remember { mutableStateOf("") } | ||
var avatarSize by remember { mutableStateOf<Int?>(null) } | ||
var defaultAvatarImageEnabled by remember { mutableStateOf(false) } | ||
var selectedDefaultAvatar by remember { mutableStateOf(DefaultAvatarImage.MONSTER) } | ||
val defaultAvatarOptions = DefaultAvatarImage.entries | ||
|
||
GravatarDemoAppTheme { | ||
Surface { | ||
Column( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.background(MaterialTheme.colorScheme.background) | ||
.padding(16.dp), | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
) { | ||
GravatarImageSettings( | ||
email = email, | ||
size = avatarSize, | ||
onEmailChanged = { email = it }, | ||
onSizeChange = { avatarSize = it }, | ||
onLoadGravatarClicked = { | ||
gravatarUrl = emailAddressToGravatarUrl( | ||
email = email, | ||
size = avatarSize, | ||
defaultAvatarImage = if (defaultAvatarImageEnabled) selectedDefaultAvatar else null, | ||
) | ||
}, | ||
onDefaultAvatarImageEnabledChanged = { | ||
defaultAvatarImageEnabled = it | ||
}, | ||
defaultAvatarImageEnabled = defaultAvatarImageEnabled, | ||
selectedDefaultAvatarImage = selectedDefaultAvatar, | ||
onDefaultAvatarImageChanged = { selectedDefaultAvatar = it }, | ||
defaultAvatarOptions = defaultAvatarOptions, | ||
) | ||
|
||
if (gravatarUrl.isNotEmpty()) { | ||
GravatarDivider() | ||
|
||
GravatarGeneratedUrl(gravatarUrl = gravatarUrl) | ||
|
||
GravatarDivider() | ||
|
||
GravatarImage(gravatarUrl = gravatarUrl) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun GravatarDivider() = | ||
Divider(thickness = 1.dp, color = MaterialTheme.colorScheme.primary, modifier = Modifier.padding(vertical = 8.dp)) | ||
|
||
@Composable | ||
fun GravatarGeneratedUrl(gravatarUrl: String) { | ||
Text( | ||
text = stringResource(R.string.gravatar_generated_url_label), | ||
fontWeight = FontWeight.Bold, | ||
) | ||
Text(text = gravatarUrl) | ||
} | ||
|
||
@Composable | ||
fun GravatarImage(gravatarUrl: String) = AsyncImage(model = gravatarUrl, contentDescription = null) | ||
|
||
@Composable | ||
fun GravatarEmailInput(email: String, onValueChange: (String) -> Unit, modifier: Modifier = Modifier) { | ||
TextField( | ||
value = email, | ||
onValueChange = onValueChange, | ||
label = { Text(stringResource(R.string.gravatar_email_input_label)) }, | ||
maxLines = 1, | ||
modifier = modifier, | ||
) | ||
} |
78 changes: 78 additions & 0 deletions
78
app/src/main/java/com/gravatar/demoapp/ui/GravatarImageSettings.kt
This file contains hidden or 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,78 @@ | ||
package com.gravatar.demoapp.ui | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.text.KeyboardOptions | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TextField | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.text.input.KeyboardType | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import com.gravatar.DefaultAvatarImage | ||
import com.gravatar.R | ||
import com.gravatar.demoapp.theme.GravatarDemoAppTheme | ||
|
||
@Composable | ||
fun GravatarImageSettings( | ||
email: String, | ||
size: Int?, | ||
onEmailChanged: (String) -> Unit, | ||
onSizeChange: (Int?) -> Unit, | ||
onLoadGravatarClicked: () -> Unit, | ||
defaultAvatarImageEnabled: Boolean, | ||
onDefaultAvatarImageEnabledChanged: (Boolean) -> Unit, | ||
selectedDefaultAvatarImage: DefaultAvatarImage, | ||
onDefaultAvatarImageChanged: (DefaultAvatarImage) -> Unit, | ||
defaultAvatarOptions: List<DefaultAvatarImage>, | ||
) { | ||
Column( | ||
modifier = Modifier | ||
.fillMaxWidth(), | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
) { | ||
GravatarEmailInput(email = email, onValueChange = onEmailChanged, Modifier.fillMaxWidth()) | ||
DefaultAvatarImageDropdown( | ||
enabled = defaultAvatarImageEnabled, | ||
selectedOption = selectedDefaultAvatarImage, | ||
onEnabledChanged = onDefaultAvatarImageEnabledChanged, | ||
onSelectedOptionChange = onDefaultAvatarImageChanged, | ||
defaultAvatarOptions = defaultAvatarOptions, | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(vertical = 8.dp), | ||
) | ||
TextField( | ||
value = size?.toString() ?: "", | ||
onValueChange = { value -> onSizeChange(value.toIntOrNull()) }, | ||
label = { Text(stringResource(R.string.gravatar_size_input_label)) }, | ||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), | ||
modifier = Modifier.padding(vertical = 8.dp), | ||
) | ||
Button(onClick = onLoadGravatarClicked) { Text(text = "Load Gravatar") } | ||
} | ||
} | ||
|
||
@Preview | ||
@Composable | ||
fun GravatarImageSettingsPreview() { | ||
GravatarDemoAppTheme { | ||
GravatarImageSettings( | ||
email = "[email protected]", | ||
size = null, | ||
onEmailChanged = {}, | ||
onSizeChange = {}, | ||
onLoadGravatarClicked = {}, | ||
defaultAvatarImageEnabled = true, | ||
onDefaultAvatarImageEnabledChanged = {}, | ||
selectedDefaultAvatarImage = DefaultAvatarImage.BLANK, | ||
onDefaultAvatarImageChanged = {}, | ||
defaultAvatarOptions = DefaultAvatarImage.entries, | ||
) | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
<resources> | ||
<string name="app_name">Gravatar Demo App</string> | ||
<string name="gravatar_email_input_label">Gravatar E-Mail</string> | ||
<string name="gravatar_generated_url_label">Gravatar URL</string> | ||
<string name="gravatar_size_input_label">Avatar Size</string> | ||
<string name="default_avatar_image_label">Default Avatar</string> | ||
</resources> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think we should go ahead and log something
onError
here. This might be useful for testing.And I wonder if we should show something specific when we get a 404 (eg. with the "Default avatar: 404" selected)
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.
I agree; I've created a new ticket, #44, to create a PR with the error handling.