Skip to content

Commit

Permalink
Merge pull request #6 from gy6543721/develop
Browse files Browse the repository at this point in the history
[Release] 0.0.5
  • Loading branch information
gy6543721 authored Jan 6, 2024
2 parents 1a77595 + 402759c commit fa6ba1d
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 67 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ android {
applicationId = "levi.lin.gemini.android"
minSdk = 29
targetSdk = 34
versionCode = 4
versionName = "0.0.4"
versionCode = 5
versionName = "0.0.5"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
148 changes: 84 additions & 64 deletions app/src/main/java/levi/lin/gemini/android/ui/screen/GeminiScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ package levi.lin.gemini.android.ui.screen
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material3.Button
Expand All @@ -27,8 +30,10 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -57,91 +62,106 @@ fun GeminiScreen(
uiState: GeminiUiState = GeminiUiState.Initial,
onButtonClicked: (String) -> Unit = {}
) {
var inputText by remember { mutableStateOf("") }
var inputText by remember { mutableStateOf(value = "") }

Scaffold(
modifier = Modifier.imePadding(),
bottomBar = {
Column(modifier = Modifier.background(color = LightBlue80)) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(all = 10.dp)
) {
TextField(
value = inputText,
onValueChange = { inputText = it },
modifier = Modifier.weight(1f),
label = { Text(text = stringResource(R.string.gemini_input_label)) },
placeholder = { Text(text = stringResource(R.string.gemini_input_hint)) },
trailingIcon = {
if (inputText.isNotEmpty()) {
IconButton(onClick = { inputText = "" }) {
Icon(
imageVector = Icons.Filled.Clear,
contentDescription = "Clear"
)
}
}
},
)
Spacer(Modifier.weight(0.05f))
Button(
onClick = {
if (inputText.isNotBlank()) {
onButtonClicked(inputText)
}
},
modifier = Modifier.align(Alignment.CenterVertically)
) {
Text(text = stringResource(R.string.action_go))
InputBar(
inputText = inputText,
onTextChange = { inputText = it },
onButtonClicked = onButtonClicked
)
}
) { innerPadding ->
ScreenContent(uiState, innerPadding)
}
}

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun InputBar(inputText: String, onTextChange: (String) -> Unit, onButtonClicked: (String) -> Unit) {
val keyboardController = LocalSoftwareKeyboardController.current
Row(
modifier = Modifier
.background(color = LightBlue80)
.fillMaxWidth()
.padding(all = 10.dp),
verticalAlignment = Alignment.CenterVertically
) {
TextField(
value = inputText,
onValueChange = onTextChange,
modifier = Modifier.weight(1f),
label = { Text(text = stringResource(id = R.string.gemini_input_label)) },
placeholder = { Text(text = stringResource(id = R.string.gemini_input_hint)) },
trailingIcon = {
if (inputText.isNotEmpty()) {
IconButton(onClick = { onTextChange("") }) {
Icon(
imageVector = Icons.Filled.Clear,
contentDescription = "Clear"
)
}
}
}
)
Spacer(modifier = Modifier.weight(0.05f))
Button(
onClick = {
onButtonClicked(inputText)
keyboardController?.hide()
},
) {
Text(text = stringResource(id = R.string.action_go))
}
) { innerPadding ->
Column(modifier = Modifier.padding(innerPadding)) {
when (uiState) {
is GeminiUiState.Success -> {
LazyColumn(
modifier = Modifier
.fillMaxHeight()
.weight(1f)
) {
items(uiState.outputText.lines()) { line ->
}
}

@Composable
fun ScreenContent(uiState: GeminiUiState, innerPadding: PaddingValues) {
Column(modifier = Modifier.padding(innerPadding)) {
when (uiState) {
is GeminiUiState.Success -> {
LazyColumn(modifier = Modifier.fillMaxHeight()) {
items(uiState.outputText.lines()) { line ->
SelectionContainer {
Text(
text = line,
modifier = Modifier.padding(4.dp)
)
}
}
}
}

is GeminiUiState.Loading -> {
Box(modifier = Modifier.fillMaxSize()) {
CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))
}
}

is GeminiUiState.Error -> {
Text(
text = uiState.errorMessage,
color = Color.Red,
modifier = Modifier.align(Alignment.CenterHorizontally)
)
}
is GeminiUiState.Loading -> {
LoadingIndicator()
}

else -> {
Spacer(
modifier = Modifier
.fillMaxHeight()
.weight(1f)
)
}
is GeminiUiState.Error -> {
ErrorMessage(uiState.errorMessage)
}

else -> Spacer(modifier = Modifier.fillMaxHeight())
}
}
}

@Composable
fun LoadingIndicator() {
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
CircularProgressIndicator()
}
}

@Composable
fun ErrorMessage(message: String) {
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
Text(text = message, color = Color.Red)
}
}

@Composable
@Preview(showSystemUi = true)
fun GeminiScreenPreview() {
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
agp = "8.4.0-alpha01"
agp = "8.4.0-alpha03"
kotlin = "1.9.0"
coreKtx = "1.12.0"
junit = "4.13.2"
Expand Down

0 comments on commit fa6ba1d

Please sign in to comment.