Skip to content

Commit

Permalink
feat: 検索結果が見つからなかった時に、スナックバーを表示するよう変更 (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
kokoichi206 committed Dec 7, 2022
1 parent d49b49a commit 560a930
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ fun MainView(
val snackBarHostState = remember { SnackbarHostState() }
var snackBarJob: Job? by remember { mutableStateOf(null) }

val snackBarMessage = stringResource(id = R.string.apiErrorMessage)
LaunchedEffect(uiState.error) {
if (uiState.error.isNotEmpty()) {
snackBarJob?.cancel()
snackBarJob = snackBarCoroutineScope.launch {
snackBarHostState.showSnackbar(snackBarMessage)
// uiState のエラーをそのまま表示する
snackBarHostState.showSnackbar(uiState.error)
}
viewModel.resetError()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
*/
package jp.co.yumemi.android.code_check.presentation.main

import android.app.Application
import android.util.Log
import androidx.compose.ui.text.input.TextFieldValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import jp.co.yumemi.android.code_check.R
import jp.co.yumemi.android.code_check.data.repository.GitHubRepository
import jp.co.yumemi.android.code_check.data.repository.SearchResultRepository
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -23,6 +25,7 @@ import javax.inject.Inject
class MainViewModel @Inject constructor(
private val repository: GitHubRepository,
private val searchRepository: SearchResultRepository,
private val application: Application,
) : ViewModel() {

companion object {
Expand All @@ -43,16 +46,28 @@ class MainViewModel @Inject constructor(

viewModelScope.launch {
try {
_uiState.update { it.copy(isLoading = true) }
_uiState.update { it.copy(isLoading = true, repositories = emptyList()) }

val result = repository.searchRepositories(inputText)

if (result.isEmpty()) {
val noRecordMsg = application.getString(R.string.noRecordFound)
_uiState.update {
it.copy(isLoading = false, error = noRecordMsg)
}
return@launch
}
_uiState.update { it.copy(isLoading = false, repositories = result) }

searchRepository.insertRecord(inputText, true)
} catch (e: Exception) {
e.localizedMessage?.let { msg ->
Log.e(TAG, msg)
_uiState.update { it.copy(isLoading = false, error = msg) }

val errorMsg = application.getString(R.string.apiErrorMessage)
_uiState.update {
it.copy(isLoading = false, error = errorMsg)
}
}
searchRepository.insertRecord(inputText, false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ fun UserViewMain(
val snackBarHostState = remember { SnackbarHostState() }
var snackBarJob: Job? by remember { mutableStateOf(null) }

val snackBarMessage = stringResource(id = R.string.apiErrorMessage)
LaunchedEffect(uiState.error) {
if (uiState.error.isNotEmpty()) {
snackBarJob?.cancel()
snackBarJob = snackBarCoroutineScope.launch {
snackBarHostState.showSnackbar(snackBarMessage)
// uiState のエラーをそのまま表示する
snackBarHostState.showSnackbar(uiState.error)
}
onSnackBarShow()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
*/
package jp.co.yumemi.android.code_check.presentation.user

import android.app.Application
import android.util.Log
import androidx.compose.ui.text.input.TextFieldValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import jp.co.yumemi.android.code_check.R
import jp.co.yumemi.android.code_check.data.repository.GitHubRepository
import jp.co.yumemi.android.code_check.data.repository.SearchResultRepository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
Expand All @@ -22,7 +23,7 @@ import javax.inject.Inject
@HiltViewModel
class UserViewModel @Inject constructor(
private val repository: GitHubRepository,
private val searchRepository: SearchResultRepository,
private val application: Application,
) : ViewModel() {

companion object {
Expand All @@ -43,14 +44,20 @@ class UserViewModel @Inject constructor(

viewModelScope.launch {
try {
_uiState.update { it.copy(isLoading = true) }
_uiState.update { it.copy(isLoading = true, users = emptyList()) }

val result = repository.searchUsers(inputText)
if (result.isEmpty()) {
val noRecordMsg = application.getString(R.string.noRecordFound)
_uiState.update { it.copy(isLoading = false, error = noRecordMsg) }
return@launch
}
_uiState.update { it.copy(isLoading = false, users = result) }
} catch (e: Exception) {
e.localizedMessage?.let { msg ->
Log.e(TAG, msg)
_uiState.update { it.copy(isLoading = false, error = msg) }
val errorMsg = application.getString(R.string.apiErrorMessage)
_uiState.update { it.copy(isLoading = false, error = errorMsg) }
}
}
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
<!--UserView-->
<string name="searchInputText_hint_users">GitHub のユーザーを検索できるよー</string>

<string name="noRecordFound">"結果が1件も見つかりませんでした。\n検索ワードを変えて再度お試しください。"</string>
<string name="apiErrorMessage">"通信エラーが発生しました。\n時間をおいて再度お試しください。"</string>
</resources>

0 comments on commit 560a930

Please sign in to comment.