Skip to content

Commit

Permalink
[feat]: small improvements and updated dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
F0x1d committed Nov 4, 2023
1 parent db5ad01 commit 8d895c6
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 111 deletions.
5 changes: 2 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ plugins {

id 'com.google.devtools.ksp'
id 'dagger.hilt.android.plugin'
id 'androidx.navigation.safeargs.kotlin'
}

android {
Expand All @@ -17,8 +16,8 @@ android {
applicationId "com.f0x1d.sense"
minSdk 23
targetSdk 34
versionCode 14
versionName "1.1.3"
versionCode 15
versionName "1.1.4"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.widget.Toast
import androidx.annotation.StringRes

fun Context.toast(@StringRes stringRes: Int, length: Int = Toast.LENGTH_SHORT) = Toast.makeText(
this, stringRes, length
).show()

fun Context.openLink(url: String) = startActivity(
Intent(Intent.ACTION_VIEW).setData(Uri.parse(url))
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/f0x1d/sense/model/Screen.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.f0x1d.sense.model

sealed class Screen(val route: String) {
object Chats: Screen("chats")
object Chat: Screen("chat")
object Pictures: Screen("pictures")
object Settings: Screen("settings")
data object Chats: Screen("chats")
data object Chat: Screen("chat")
data object Pictures: Screen("pictures")
data object Settings: Screen("settings")
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,8 @@ class OpenAIRepository @Inject constructor(

lineResponse.choices.firstOrNull()?.also { choice ->
choice.delta?.content?.takeIf { it.isNotEmpty() }?.also { content ->
var currentContent = contents[choice.index]

currentContent = if (currentContent == null)
content
else
currentContent + content
var currentContent = contents[choice.index] ?: ""
currentContent += content

contents[choice.index] = currentContent

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ class MainActivity: ComponentActivity() {
composable(
route = "${Screen.Chat.route}/{id}",
arguments = listOf(
navArgument("id") { type = NavType.LongType }
navArgument("id") {
type = NavType.LongType
}
),
enterTransition = { fadeIn() },
exitTransition = { fadeOut() }
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/f0x1d/sense/ui/screen/ChatScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.f0x1d.sense.ui.screen

import android.content.Context
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.scaleIn
import androidx.compose.animation.scaleOut
import androidx.compose.foundation.layout.Arrangement
Expand Down Expand Up @@ -54,7 +53,7 @@ import com.f0x1d.sense.ui.widget.TypingMessage
import com.f0x1d.sense.viewmodel.ChatViewModel
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterial3Api::class, ExperimentalAnimationApi::class)
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ChatScreen(navController: NavController) {
val viewModel = hiltViewModel<ChatViewModel>()
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/f0x1d/sense/ui/screen/ChatsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ fun ChatsScreen(navController: NavController) {
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun InfoDialog(opened: Boolean, onClose: () -> Unit) {
if (!opened) return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fun PicturesScreen(navController: NavController) {
value = viewModel.query,
onValueChange = { viewModel.query = it },
enabled = !viewModel.loading,
label = { Text(text = stringResource(R.string.picture)) },
label = { Text(text = stringResource(R.string.prompt)) },
shape = RoundedCornerShape(12.dp),
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Go),
keyboardActions = KeyboardActions(onGo = {
Expand Down
71 changes: 53 additions & 18 deletions app/src/main/java/com/f0x1d/sense/ui/screen/SettingsScreen.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
package com.f0x1d.sense.ui.screen

import androidx.annotation.StringRes
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.LargeTopAppBar
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberTopAppBarState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
Expand All @@ -31,15 +36,11 @@ import com.f0x1d.sense.ui.widget.ErrorAlertDialog
import com.f0x1d.sense.ui.widget.NavigationBackIcon
import com.f0x1d.sense.viewmodel.SettingsViewModel

@OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class)
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SettingsScreen(navController: NavController) {
val viewModel = hiltViewModel<SettingsViewModel>()

val endpoint by viewModel.endpoint
val apiKey by viewModel.apiKey
val model by viewModel.model

val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior(rememberTopAppBarState())

Column {
Expand All @@ -62,39 +63,73 @@ fun SettingsScreen(navController: NavController) {
Spacer(modifier = Modifier.size(10.dp))

SettingsTextField(
value = endpoint,
onValueChange = { viewModel.updateFor(viewModel.endpoint, it) },
labelResource = R.string.endpoint
value = viewModel.endpoint,
onValueChange = { viewModel.endpoint = it },
labelResource = R.string.endpoint,
viewModel = viewModel
)

Spacer(modifier = Modifier.size(10.dp))

SettingsTextField(
value = apiKey,
onValueChange = { viewModel.updateFor(viewModel.apiKey, it) },
labelResource = R.string.api_key
value = viewModel.apiKey,
onValueChange = { viewModel.apiKey = it },
labelResource = R.string.api_key,
viewModel = viewModel
)

Spacer(modifier = Modifier.size(10.dp))

SettingsTextField(
value = model,
onValueChange = { viewModel.updateFor(viewModel.model, it) },
labelResource = R.string.model
value = viewModel.model,
onValueChange = { viewModel.model = it },
labelResource = R.string.model,
viewModel = viewModel
)
}

androidx.compose.animation.AnimatedVisibility(
modifier = Modifier
.align(Alignment.BottomEnd)
.padding(16.dp)
.navigationBarsPadding(),
visible = viewModel.changesMade,
enter = fadeIn(),
exit = fadeOut()
) {
FloatingActionButton(
onClick = {
viewModel.save {
navController.popBackStack()
}
}
) {
Icon(
painter = painterResource(id = R.drawable.ic_done),
contentDescription = null
)
}
}
}

ErrorAlertDialog(viewModel = viewModel)
}
}

@Composable
private fun SettingsTextField(value: String, onValueChange: (String) -> Unit, @StringRes labelResource: Int) {
private fun SettingsTextField(
value: String,
onValueChange: (String) -> Unit,
@StringRes labelResource: Int,
viewModel: SettingsViewModel
) {
OutlinedTextField(
modifier = Modifier.fillMaxWidth(),
value = value,
onValueChange = onValueChange,
onValueChange = {
onValueChange(it)
viewModel.changesMade = true
},
label = { Text(text = stringResource(id = labelResource)) },
shape = RoundedCornerShape(12.dp)
)
Expand Down
21 changes: 9 additions & 12 deletions app/src/main/java/com/f0x1d/sense/ui/screen/SetupScreen.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.f0x1d.sense.ui.screen

import android.content.Context
import android.util.TypedValue
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
Expand All @@ -14,7 +13,6 @@ import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.colorResource
Expand Down Expand Up @@ -141,7 +139,7 @@ fun InstructionPart(index: Int, instruction: AnnotatedString) {
val context = LocalContext.current

if (index != 0) {
Divider(color = MaterialTheme.colorScheme.secondaryContainer)
HorizontalDivider(color = MaterialTheme.colorScheme.secondaryContainer)
}

Row(
Expand Down Expand Up @@ -186,13 +184,12 @@ private fun buildFirstInstruction() = buildAnnotatedString {
}

@Composable
fun getColor(color: Int): Color {
val context = LocalContext.current

return colorResource(id = remember(key1 = color) {
context.getColorFromAttrs(color).resourceId
})
}
private fun Context.getColorFromAttrs(attr: Int) = TypedValue().apply {
theme.resolveAttribute(attr, this, true)
fun getColor(color: Int) = LocalContext.current.theme.let { theme ->
colorResource(
remember(key1 = color) {
TypedValue().apply {
theme.resolveAttribute(color, this, true)
}.resourceId
}
)
}
9 changes: 7 additions & 2 deletions app/src/main/java/com/f0x1d/sense/viewmodel/ChatViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import javax.inject.Inject

@HiltViewModel
class ChatViewModel @Inject constructor(
@ChatId private val chatId: Long,
@ChatId val chatId: Long,
private val database: AppDatabase,
private val openAIRepository: OpenAIRepository,
application: Application
Expand Down Expand Up @@ -82,7 +82,12 @@ class ChatViewModel @Inject constructor(
}

database.messagesDao().insert(
responseMessages.values.map { it.copy(generating = false, content = it.content?.trim()) }
responseMessages.values.map {
it.copy(
generating = false,
content = it.content?.trim()
)
}
)
}, errorBlock = {
database.messagesDao().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class PicturesViewModel @Inject constructor(

@OptIn(ExperimentalCoilApi::class)
fun save(url: String?, uri: Uri?) = viewModelScope.onIO {
imageLoader.diskCache?.get(url ?: return@onIO)?.use { snapshot ->
imageLoader.diskCache?.openSnapshot(url ?: return@onIO)?.use { snapshot ->
snapshot.data.toFile().inputStream().use { inputStream ->
ctx.contentResolver.openOutputStream(uri ?: return@onIO)?.use { outputStream ->
inputStream.copyTo(outputStream)
Expand Down
47 changes: 14 additions & 33 deletions app/src/main/java/com/f0x1d/sense/viewmodel/SettingsViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ package com.f0x1d.sense.viewmodel

import android.app.Application
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.viewModelScope
import com.f0x1d.sense.store.datastore.SettingsDataStore
import com.f0x1d.sense.viewmodel.base.BaseViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import javax.inject.Inject

@HiltViewModel
Expand All @@ -22,42 +19,26 @@ class SettingsViewModel @Inject constructor(
application: Application
): BaseViewModel(application) {

val endpoint = mutableStateOf("")
val apiKey = mutableStateOf("")
val model = mutableStateOf("")
var endpoint by mutableStateOf("")
var apiKey by mutableStateOf("")
var model by mutableStateOf("")

private val fillings = mutableMapOf<MutableState<String>, suspend () -> String?>()
private val mutexes = mutableMapOf<MutableState<String>, Mutex>()
private val actions = mutableMapOf<MutableState<String>, suspend (String) -> Unit>()
var changesMade by mutableStateOf(false)

init {
fillings[endpoint] = { settingsDataStore.endpoint.first() }
fillings[apiKey] = { settingsDataStore.apiKey.first() }
fillings[model] = { settingsDataStore.model.first() }

actions[endpoint] = { settingsDataStore.saveEndpoint(it) }
actions[apiKey] = { settingsDataStore.saveApiKey(it) }
actions[model] = { settingsDataStore.saveModel(it) }

viewModelScope.launch {
fillings.entries.map {
async {
StateWithValue(it.key, it.value.invoke())
}
}.awaitAll().forEach {
it.state.value = it.value ?: ""
}
endpoint = settingsDataStore.endpoint.first()
apiKey = settingsDataStore.apiKey.first() ?: ""
model = settingsDataStore.model.first()
}
}

fun updateFor(state: MutableState<String>, value: String) {
state.value = value
fun save(onSaved: () -> Unit) = viewModelScope.launch {
settingsDataStore.saveEndpoint(endpoint)
settingsDataStore.saveApiKey(apiKey)
settingsDataStore.saveModel(model)

viewModelScope.launch(Dispatchers.IO) {
mutexes.getOrPut(state) { Mutex() }.withLock {
actions[state]?.invoke(value)
}
}
onSaved()
}

private data class StateWithValue(
Expand Down
Loading

0 comments on commit 8d895c6

Please sign in to comment.