Skip to content

Commit

Permalink
Merge pull request #9 from gy6543721/develop
Browse files Browse the repository at this point in the history
[Release] 0.1.1
  • Loading branch information
gy6543721 authored Jan 29, 2024
2 parents 6fed594 + c265553 commit a52eb3d
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 51 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 = 30
targetSdk = 34
versionCode = 7
versionName = "0.1.0"
versionCode = 8
versionName = "0.1.1"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/Theme.GeminiAndroid"
android:windowSoftInputMode="adjustResize"
tools:targetApi="31">
<activity
android:name=".MainActivity"
Expand Down
31 changes: 7 additions & 24 deletions app/src/main/java/levi/lin/gemini/android/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.google.ai.client.generativeai.GenerativeModel
import kotlinx.coroutines.launch
import levi.lin.gemini.android.ui.view.GeminiScreenContainer
import levi.lin.gemini.android.ui.theme.GeminiAndroidTheme
import levi.lin.gemini.android.utility.GenerativeModelListener
import levi.lin.gemini.android.viewmodel.GeminiViewModel
import java.io.IOException

class MainActivity : ComponentActivity(), GenerativeModelListener {
class MainActivity : ComponentActivity() {
// Gemini AI Model
private lateinit var generativeModel: GenerativeModel
private lateinit var viewModel: GeminiViewModel
Expand All @@ -39,11 +36,10 @@ class MainActivity : ComponentActivity(), GenerativeModelListener {
)
viewModel = GeminiViewModel(generativeModel = generativeModel)

// update Gemini AI Model
lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.modelNameFlow.collect { modelName ->
updateGenerativeModel(modelName)
}
viewModel.generativeModelFlow.collect { targetGenerativeModel ->
generativeModel = targetGenerativeModel
}
}

Expand All @@ -62,18 +58,12 @@ class MainActivity : ComponentActivity(), GenerativeModelListener {
}
}

override fun onModelNameChanged(targetModelName: String) {
generativeModel = GenerativeModel(
modelName = targetModelName,
apiKey = BuildConfig.apiKey
)
viewModel.updateGenerativeModel(generativeModel)
}

private val selectImageResult =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val imageBitmaps = result.data?.let { extractBitmapsFromIntent(it) }.orEmpty()
val imageBitmaps = result.data?.let { intent ->
extractBitmapsFromIntent(intent = intent)
}.orEmpty()
viewModel.clearSelectedImages()
viewModel.setImageCount(imageBitmaps.size)
viewModel.setImageBitmaps(imageBitmaps)
Expand Down Expand Up @@ -106,11 +96,4 @@ class MainActivity : ComponentActivity(), GenerativeModelListener {
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
selectImageResult.launch(intent)
}

private fun updateGenerativeModel(modelName: String) {
generativeModel = GenerativeModel(
modelName = modelName,
apiKey = BuildConfig.apiKey
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.google.ai.client.generativeai.GenerativeModel
import levi.lin.gemini.android.BuildConfig
import levi.lin.gemini.android.ui.state.GeminiUiState
import levi.lin.gemini.android.viewmodel.GeminiViewModel
import levi.lin.gemini.android.R
Expand All @@ -74,7 +72,7 @@ internal fun GeminiScreenContainer(
LaunchedEffect(selectedImageBitmapList) {
val targetModelName =
if (selectedImageBitmapList.isNotEmpty()) "gemini-pro-vision" else "gemini-pro"
geminiViewModel.updateGenerativeModel(GenerativeModel(targetModelName, BuildConfig.apiKey))
geminiViewModel.updateGenerativeModel(targetModelName = targetModelName)
}

GeminiScreen(
Expand Down Expand Up @@ -107,14 +105,20 @@ fun GeminiScreen(
modifier = Modifier
.imePadding()
.pointerInput(Unit) {
detectTapGestures(onTap = { focusManager.clearFocus() })
detectTapGestures(
onTap = {
focusManager.clearFocus()
}
)
},
bottomBar = {
InputBar(
inputText = inputText,
selectedImageBitmapList = selectedImageBitmapList,
selectedImageCount = selectedImageCount,
onTextChange = { inputText = it },
onTextChange = { text ->
inputText = text
},
onButtonClicked = onButtonClicked,
onImageSelected = onImageSelected,
onClearImages = onClearImages
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.ai.client.generativeai.GenerativeModel
import com.google.ai.client.generativeai.type.content
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import levi.lin.gemini.android.BuildConfig
import levi.lin.gemini.android.ui.state.GeminiUiState
import java.util.Locale

Expand All @@ -30,22 +31,19 @@ class GeminiViewModel(
private val _selectedImageCount = MutableStateFlow(0)
val selectedImageCount: StateFlow<Int> = _selectedImageCount.asStateFlow()

val modelNameFlow: Flow<String> = selectedImageBitmaps
.map { images ->
if (images.isNotEmpty()) "gemini-pro-vision" else "gemini-pro"
}
.distinctUntilChanged()
private val _generativeModelFlow = MutableSharedFlow<GenerativeModel>()
val generativeModelFlow: SharedFlow<GenerativeModel> = _generativeModelFlow.asSharedFlow()

fun respond(inputText: String) {
_uiState.value = GeminiUiState.Loading

val deviceLanguage = Locale.getDefault().displayLanguage
val textPrompt =
"According to the provided contents ($inputText), "
"Content:($inputText) \\n You are a lovely assistant. According to the provided content, "
val imagePrompt =
"According to the provided images and contents ($inputText), "
"Content:($inputText) \\n You are a lovely assistant. According to the provided images and content, "
val generalPrompt =
"respond with detail answers or advices in $deviceLanguage as if you are the speaker's special one."
"if the content is a question, answer the question in $deviceLanguage. If the content is a request, respond with detailed information in $deviceLanguage."
val imageList = selectedImageBitmaps.value
val inputContent = content {
if (imageList.isNotEmpty()) {
Expand Down Expand Up @@ -83,7 +81,13 @@ class GeminiViewModel(
_selectedImageCount.value = 0
}

fun updateGenerativeModel(targetModel: GenerativeModel) {
generativeModel = targetModel
suspend fun updateGenerativeModel(targetModelName: String) {
if (generativeModel.modelName != targetModelName) {
generativeModel = GenerativeModel(
modelName = targetModelName,
apiKey = BuildConfig.apiKey
)
_generativeModelFlow.emit(generativeModel)
}
}
}
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-alpha04"
agp = "8.4.0-alpha07"
kotlin = "1.9.0"
coreKtx = "1.12.0"
junit = "4.13.2"
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Dec 31 15:31:52 JST 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-rc-1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

0 comments on commit a52eb3d

Please sign in to comment.