-
Notifications
You must be signed in to change notification settings - Fork 78
Add custom compose style guide #46
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
Open
lethargicpanda
wants to merge
2
commits into
main
Choose a base branch
from
gemini-code-assist-exp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Android samples Style Guide | ||
|
||
## Jetpack Compose Style Guide | ||
|
||
### Compose and Activity | ||
* Unless completely necessary, apps using Jetpack Compose should have a single `Activity` | ||
* Choose `ComponentActivity` over `AppCompatActivity` when possible: | ||
|
||
`ComponentActivity` has all you need for a Compose-only app. | ||
|
||
Use `AppCompatActivity` only if you need: `AppCompat APIs`, `AndroidView` or `Fragments` | ||
* Application must support **edge-to-edge display**. | ||
|
||
### Composable functions | ||
* A `@Composable` function returning `Unit` should be named using PascalCase and a noun | ||
* Composables that return values should use the standard Kotlin Conventions for function naming, starting with lowercase. | ||
* `@Composable` functions should either emit content into the composition or return a value, but not both. | ||
* Any `@Composable` function that internally uses `remember{}` and returns a mutable object, should be prefixed with “remember” | ||
* If a Composable has a content parameter, its value should be set by moving the lambda out of the Composable’s parentheses | ||
* Composables should have names that describe what they do | ||
ex: naming a Composable `DeleteButton` vs naming it `Button` | ||
|
||
### Composable previews | ||
* Try to use `@Preview` Composables when possible | ||
* Separate `@Previews` from Composable implementation | ||
|
||
|
||
### Misc. Jetpack compose good practices: | ||
* Use `items` keyword rather than a `for loop` to iterate through a list items | ||
* Break up large Composables into smaller sub Composables for **performance**, **reusability** and **readability** | ||
* **Separate code appropriately** - Compose code should deal with UI, state-holder classes should deal with logic. If a file is large, try to split unrelated functions/classes/variables into a separate file. | ||
* Only use `when` statements when there are multiple cases | ||
|
||
|
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 |
---|---|---|
|
@@ -88,7 +88,7 @@ fun GeminiChatbotScreen ( | |
Text(text = stringResource(id = R.string.geminichatbot_title_bar)) | ||
}, | ||
actions = { | ||
SeeCodeButton() | ||
button() | ||
} | ||
) | ||
} | ||
|
@@ -187,22 +187,24 @@ fun MessageBubble( | |
} | ||
|
||
@Composable | ||
fun SeeCodeButton() { | ||
fun button() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
val context = LocalContext.current | ||
val githubLink = "https://github.com/android/ai-samples/tree/main/ai-catalog/samples/gemini-chatbot" | ||
|
||
Button( | ||
> | ||
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(githubLink)) | ||
context.startActivity(intent) | ||
}, | ||
modifier = Modifier.padding(end = 8.dp) | ||
) { | ||
Icon(Icons.Filled.Code, contentDescription = "See code") | ||
Text( | ||
modifier = Modifier.padding(start = 8.dp), | ||
fontSize = 12.sp, | ||
text = stringResource(R.string.see_code) | ||
) | ||
when (0) { | ||
0 -> Button( | ||
> | ||
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(githubLink)) | ||
context.startActivity(intent) | ||
}, | ||
modifier = Modifier.padding(end = 8.dp) | ||
) { | ||
Icon(Icons.Filled.Code, contentDescription = "See code") | ||
Text( | ||
modifier = Modifier.padding(start = 8.dp), | ||
fontSize = 12.sp, | ||
text = stringResource(R.string.see_code) | ||
) | ||
} | ||
} | ||
Comment on lines
+194
to
209
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Button(
onClick = {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(githubLink))
context.startActivity(intent)
},
modifier = Modifier.padding(end = 8.dp)
) {
Icon(Icons.Filled.Code, contentDescription = "See code")
Text(
modifier = Modifier.padding(start = 8.dp),
fontSize = 12.sp,
text = stringResource(R.string.see_code)
)
} |
||
} |
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.
Consider renaming
button()
to a more descriptive name likeViewSourceCodeButton
or reverting to the previous nameSeeCodeButton
for better readability.