Skip to content

Commit

Permalink
Fixed messages deletion when get error, fixed Crossfade, fixed error …
Browse files Browse the repository at this point in the history
…handling
  • Loading branch information
F0x1d committed Apr 27, 2023
1 parent e169565 commit e2f07dd
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 11 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ android {
applicationId "com.f0x1d.sense"
minSdk 23
targetSdk 33
versionCode 6
versionName "1.0.5"
versionCode 7
versionName "1.0.6"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/com/f0x1d/sense/OpenAIApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ class OpenAIApplication: Application() {
super.onCreate()

applicationScope.launch(Dispatchers.IO) {
database.messagesDao().markAllAsNotGenerating() // maybe some geniuses will remove app from recents
database.messagesDao().apply {
markAllAsNotGenerating()
deleteEmptyMessages()
} // maybe some geniuses will remove app from recents
}
}
}
6 changes: 6 additions & 0 deletions app/src/main/java/com/f0x1d/sense/database/dao/MessagesDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ interface MessagesDao {
@Query("UPDATE ChatMessage SET generating = 0 WHERE generating = 1 AND chat_id = :chatId")
suspend fun markAllAsNotGeneratingInChat(chatId: Long)

@Query("DELETE FROM ChatMessage WHERE content is NULL AND chat_id = :chatId")
suspend fun deleteEmptyMessagesInChat(chatId: Long)

@Query("UPDATE ChatMessage SET generating = 0 WHERE generating = 1")
suspend fun markAllAsNotGenerating()

@Query("DELETE FROM ChatMessage WHERE content is NULL")
suspend fun deleteEmptyMessages()
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.isActive
import retrofit2.HttpException
import javax.inject.Inject
import javax.inject.Singleton

Expand Down Expand Up @@ -45,7 +46,11 @@ class OpenAIRepository @Inject constructor(
)
).execute()

val reader = response.body()?.byteStream()?.bufferedReader() ?: throw Exception()
if (response.errorBody() != null) {
throw HttpException(response)
}

val reader = response.body()?.byteStream()?.bufferedReader() ?: throw Exception("null stream")
try {
val contents = mutableMapOf<Int, String>()
while (currentCoroutineContext().isActive) {
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/f0x1d/sense/ui/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ class MainActivity: ComponentActivity() {
setContent {
OpenAITheme {
Surface(modifier = Modifier.imePadding()) {
val apiKey by viewModel.apiKey.collectAsStateWithLifecycle(initialValue = "")
val openSetup by viewModel.shouldOpenSetup.collectAsStateWithLifecycle(initialValue = false)

Crossfade(targetState = apiKey) {
if (it == null) {
Crossfade(targetState = openSetup) {
if (it) {
SetupScreen()
} else {
val navController = rememberNavController()
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/f0x1d/sense/ui/screen/SettingsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ fun SettingsScreen(navController: NavController) {
) {
Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
SettingsTextField(
value = apiKey ?: "",
value = apiKey,
onValueChange = { viewModel.updateFor(viewModel.apiKey, it) },
labelResource = R.string.api_key
)

SettingsTextField(
value = model ?: "",
value = model,
onValueChange = { viewModel.updateFor(viewModel.model, it) },
labelResource = R.string.model
)
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/com/f0x1d/sense/viewmodel/ChatViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ class ChatViewModel @AssistedInject constructor(
responseMessages.values.map { it.copy(generating = false) }
)
}) {
database.messagesDao().markAllAsNotGeneratingInChat(chatId)
database.messagesDao().apply {
markAllAsNotGeneratingInChat(chatId)
deleteEmptyMessagesInChat(chatId)
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/com/f0x1d/sense/viewmodel/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import android.app.Application
import com.f0x1d.sense.store.datastore.SettingsDataStore
import com.f0x1d.sense.viewmodel.base.BaseViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import javax.inject.Inject

@HiltViewModel
class MainViewModel @Inject constructor(
application: Application,
private val settingsDataStore: SettingsDataStore
): BaseViewModel(application) {
val apiKey = settingsDataStore.apiKey
val shouldOpenSetup = settingsDataStore.apiKey.map {
it == null
}.distinctUntilChanged()
}

0 comments on commit e2f07dd

Please sign in to comment.