8000 Add custom compose style guide by lethargicpanda · Pull Request #46 · android/ai-samples · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .gemini/styleguide.md
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


Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fun GeminiChatbotScreen (
Text(text = stringResource(id = R.string.geminichatbot_title_bar))
},
actions = {
SeeCodeButton()
button()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider renaming button() to a more descriptive name like ViewSourceCodeButton or reverting to the previous name SeeCodeButton for better readability.

Suggested change
button()
SeeCodeButton()

}
)
}
Expand Down Expand Up @@ -187,22 +187,24 @@ fun MessageBubble(
}

@Composable
fun SeeCodeButton() {
fun button() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The function name button() is too generic. A more descriptive name, such as ViewSourceCodeButton or the original SeeCodeButton, would improve code clarity.

Suggested change
fun button() {
fun SeeCodeButton() {

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The when (0) { 0 -> ... } structure is unnecessary. Remove it to simplify the logic.

    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)
        )
    }

}
0