Skip to content
This repository was archived by the owner on Dec 21, 2024. It is now read-only.

Commit e23092e

Browse files
authored
Merge pull request #150 from tshion/hotfix/#146_be_single_error_dialog
#146 検索画面で、エラーダイアログが多重表示される問題の修正
2 parents f022473 + c6db943 commit e23092e

File tree

3 files changed

+56
-35
lines changed

3 files changed

+56
-35
lines changed

app/src/main/kotlin/jp/co/yumemi/android/code_check/pages/search/SearchFragment.kt

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import jp.co.yumemi.android.code_check.R
1717
import jp.co.yumemi.android.code_check.databinding.PageSearchBinding
1818
import jp.co.yumemi.android.code_check.organisms.repository_list_view.RepositoryListViewAdapter
1919
import jp.co.yumemi.android.code_check.pages.detail.DetailViewData
20+
import kotlinx.coroutines.flow.filterNotNull
2021
import kotlinx.coroutines.launch
2122
import timber.log.Timber
2223
import java.io.IOException
@@ -73,39 +74,44 @@ class SearchFragment : Fragment(R.layout.page_search) {
7374
// ViewModel と表示の結びつけ
7475
viewLifecycleOwner.lifecycleScope.launch {
7576
repeatOnLifecycle(Lifecycle.State.STARTED) {
77+
launch {
78+
viewModel.error.filterNotNull().collect { e ->
79+
when (e) {
80+
is IllegalArgumentException,
81+
is IndexOutOfBoundsException -> NavGraphEntryPointDirections.navShowNotifyDialog(
82+
title = getString(R.string.page_search_error_title_invalid),
83+
message = getString(R.string.page_search_error_message_invalid),
84+
)
85+
86+
is IOException -> NavGraphEntryPointDirections.navShowNotifyDialog(
87+
title = getString(R.string.page_search_error_title_http),
88+
message = getString(R.string.page_search_error_message_offline),
89+
)
90+
91+
else -> {
92+
Timber.e(e)
93+
NavGraphEntryPointDirections.navShowNotifyDialog(
94+
title = getString(R.string.page_search_error_title_http),
95+
message = getString(R.string.page_search_error_message_http),
96+
)
97+
}
98+
}.also {
99+
findNavController().navigate(it)
100+
viewModel.showedError()
101+
}
102+
}
103+
}
104+
76105
launch {
77106
viewModel.isLoading.collect {
78107
binding?.pageSearchLoading?.isVisible = it
79108
}
80109
}
81110

82111
launch {
83-
viewModel.searchResult.collect { result ->
84-
result?.onSuccess {
85-
binding?.pageSearchList?.adapter?.submitList(it)
86-
binding?.pageSearchListEmpty?.isVisible = it.isEmpty()
87-
}?.onFailure { e ->
88-
when (e) {
89-
is IllegalArgumentException,
90-
is IndexOutOfBoundsException -> NavGraphEntryPointDirections.navShowNotifyDialog(
91-
title = getString(R.string.page_search_error_title_invalid),
92-
message = getString(R.string.page_search_error_message_invalid),
93-
)
94-
95-
is IOException -> NavGraphEntryPointDirections.navShowNotifyDialog(
96-
title = getString(R.string.page_search_error_title_http),
97-
message = getString(R.string.page_search_error_message_offline),
98-
)
99-
100-
else -> {
101-
Timber.e(e)
102-
NavGraphEntryPointDirections.navShowNotifyDialog(
103-
title = getString(R.string.page_search_error_title_http),
104-
message = getString(R.string.page_search_error_message_http),
105-
)
106-
}
107-
}.also { findNavController().navigate(it) }
108-
}
112+
viewModel.repositories.filterNotNull().collect {
113+
binding?.pageSearchList?.adapter?.submitList(it)
114+
binding?.pageSearchListEmpty?.isVisible = it.isEmpty()
109115
}
110116
}
111117
}

app/src/main/kotlin/jp/co/yumemi/android/code_check/pages/search/SearchViewModel.kt

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,23 @@ class SearchViewModel(
2323
private val dispatcherDefault: CoroutineDispatcher = Dispatchers.Default,
2424
) : ViewModel() {
2525

26+
/** エラー */
27+
val error: StateFlow<Exception?>
28+
private val _error = MutableStateFlow<Exception?>(null)
29+
2630
/** 読み込み中かどうか */
2731
val isLoading: StateFlow<Boolean>
2832
private val _isLoading = MutableStateFlow(false)
2933

30-
/** 検索結果 */
31-
val searchResult: StateFlow<Result<List<RepositoryListItemViewData>>?>
32-
private val _searchResult = MutableStateFlow<Result<List<RepositoryListItemViewData>>?>(null)
34+
/** リポジトリ情報 */
35+
val repositories: StateFlow<List<RepositoryListItemViewData>?>
36+
private val _repositories = MutableStateFlow<List<RepositoryListItemViewData>?>(null)
3337

3438

3539
init {
40+
error = _error
3641
isLoading = _isLoading
37-
searchResult = _searchResult
42+
repositories = _repositories
3843
}
3944

4045

@@ -45,19 +50,29 @@ class SearchViewModel(
4550
*/
4651
fun search(keyword: String) {
4752
viewModelScope.launch {
53+
_error.value = null
4854
_isLoading.value = true
49-
_searchResult.value = null
50-
51-
_searchResult.value = withContext(dispatcherDefault) {
52-
runCatching {
55+
_repositories.value = null
56+
try {
57+
val mapped = withContext(dispatcherDefault) {
5358
val result = searchUseCase.searchRepositories(keyword, 1)
5459
result.items.map { RepositoryListItemViewData(it) }
5560
}
61+
_repositories.value = mapped
62+
} catch (e: Exception) {
63+
_error.value = e
5664
}
5765
_isLoading.value = false
5866
}
5967
}
6068

69+
/**
70+
* エラー表示の完了
71+
*/
72+
fun showedError() {
73+
_error.value = null
74+
}
75+
6176

6277
companion object {
6378
val Factory = viewModelFactory {

variables.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ext {
88
// アプリバージョン
99
def appVersionMajor = 1
1010
def appVersionMinor = 2
11-
def appVersionPatch = 1
11+
def appVersionPatch = 2
1212
appVersionCode = 10000 * appVersionMajor + 100 * appVersionMinor + appVersionPatch
1313
appVersionName = "${appVersionMajor}.${appVersionMinor}.${appVersionPatch}"
1414

0 commit comments

Comments
 (0)