From 2de66821008706f7c72be4b3f245c9c4c850fd03 Mon Sep 17 00:00:00 2001 From: ksnd Date: Sun, 30 Apr 2023 19:20:23 +0900 Subject: [PATCH] refactor: remove context in RegisterViewModel --- .../core/mock/FakeColorRepository.kt | 6 +++- .../core/ui/state/RegisterUiState.kt | 1 + .../makecolor/feature/edit/RegisterScreen.kt | 13 ++++++--- .../feature/edit/RegisterViewModel.kt | 28 ++++++++----------- .../feature/edit/RegisterViewModelImplTest.kt | 23 +++++++-------- 5 files changed, 38 insertions(+), 33 deletions(-) diff --git a/core/mock/src/main/java/kosenda/makecolor/core/mock/FakeColorRepository.kt b/core/mock/src/main/java/kosenda/makecolor/core/mock/FakeColorRepository.kt index 55570a4..9bdd9ac 100644 --- a/core/mock/src/main/java/kosenda/makecolor/core/mock/FakeColorRepository.kt +++ b/core/mock/src/main/java/kosenda/makecolor/core/mock/FakeColorRepository.kt @@ -21,7 +21,11 @@ class FakeColorRepository : ColorRepository { } return false } - override suspend fun updateSize(size: Int, name: String) {} + override suspend fun updateSize(size: Int, name: String) { + categories.forEach { + if (it.name == name) it.size = size + } + } override suspend fun updateCategory(oldName: String, newName: String) { categories.forEach { if (it.name == oldName) it.name = newName diff --git a/core/ui/src/main/java/kosenda/makecolor/core/ui/state/RegisterUiState.kt b/core/ui/src/main/java/kosenda/makecolor/core/ui/state/RegisterUiState.kt index 2cb07d4..46a2606 100644 --- a/core/ui/src/main/java/kosenda/makecolor/core/ui/state/RegisterUiState.kt +++ b/core/ui/src/main/java/kosenda/makecolor/core/ui/state/RegisterUiState.kt @@ -11,4 +11,5 @@ data class RegisterUiState( val selectCategory: Category = Category("Category1", 0), val displayCategories: List = emptyList(), val isShowNewCategoryDialog: Boolean = false, + val showToast: Boolean = false, ) diff --git a/feature/edit/src/main/java/kosenda/makecolor/feature/edit/RegisterScreen.kt b/feature/edit/src/main/java/kosenda/makecolor/feature/edit/RegisterScreen.kt index 7752507..bfa4359 100644 --- a/feature/edit/src/main/java/kosenda/makecolor/feature/edit/RegisterScreen.kt +++ b/feature/edit/src/main/java/kosenda/makecolor/feature/edit/RegisterScreen.kt @@ -1,5 +1,6 @@ package kosenda.makecolor.feature.edit +import android.widget.Toast import androidx.compose.foundation.gestures.detectTapGestures import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box @@ -79,6 +80,13 @@ fun RegisterScreenContent( ) } + LaunchedEffect(uiState.showToast) { + if (uiState.showToast) { + Toast.makeText(context, R.string.registrated, Toast.LENGTH_SHORT).show() + viewModel.clearShowToast() + } + } + Scaffold( topBar = { BackTopBar(onClick = onBackScreen, scrollBehavior = scrollBehavior) }, containerColor = Color.Transparent, @@ -166,10 +174,7 @@ fun RegisterScreenContent( containerColor = MaterialTheme.colorScheme.primaryContainer, height = 60.dp, onClick = { - viewModel.registerColor( - hex = uiState.colorData.hex.toString(), - context = context, - ) + viewModel.registerColor(hex = uiState.colorData.hex.toString()) onBackScreen() }, ) diff --git a/feature/edit/src/main/java/kosenda/makecolor/feature/edit/RegisterViewModel.kt b/feature/edit/src/main/java/kosenda/makecolor/feature/edit/RegisterViewModel.kt index 1fb626e..f2fd5a4 100644 --- a/feature/edit/src/main/java/kosenda/makecolor/feature/edit/RegisterViewModel.kt +++ b/feature/edit/src/main/java/kosenda/makecolor/feature/edit/RegisterViewModel.kt @@ -1,18 +1,14 @@ package kosenda.makecolor.feature.edit -import android.content.Context -import android.widget.Toast import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import dagger.hilt.android.lifecycle.HiltViewModel import kosenda.makecolor.core.data.repository.ColorRepository import kosenda.makecolor.core.model.data.Category import kosenda.makecolor.core.model.data.ColorItem +import kosenda.makecolor.core.ui.data.NavKey import kosenda.makecolor.core.ui.state.RegisterUiState import kosenda.makecolor.core.util.IODispatcher -import kosenda.makecolor.core.util.MainDispatcher -import kosenda.makecolor.core.resource.R -import kosenda.makecolor.core.ui.data.NavKey import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.MutableStateFlow @@ -20,7 +16,6 @@ import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json import javax.inject.Inject @@ -33,13 +28,13 @@ abstract class RegisterViewModel : ViewModel() { abstract fun addCategory(newCategory: Category) abstract fun closeAddCategoryDialog() abstract fun openAddCategoryDialog() - abstract fun registerColor(hex: String, context: Context) + abstract fun registerColor(hex: String) + abstract fun clearShowToast() } @HiltViewModel class RegisterViewModelImpl @Inject constructor( @IODispatcher private val ioDispatcher: CoroutineDispatcher, - @MainDispatcher private val mainDispatcher: CoroutineDispatcher, private val colorRepository: ColorRepository, savedStateHandle: SavedStateHandle, ) : RegisterViewModel() { @@ -93,7 +88,7 @@ class RegisterViewModelImpl @Inject constructor( _uiState.update { it.copy(isShowNewCategoryDialog = true) } } - override fun registerColor(hex: String, context: Context) { + override fun registerColor(hex: String) { CoroutineScope(ioDispatcher).launch { val colorItem = ColorItem( hex = hex, @@ -104,15 +99,13 @@ class RegisterViewModelImpl @Inject constructor( uiState.value.selectCategory.name.let { colorRepository.updateSize(colorRepository.getSize(it), it) } - withContext(mainDispatcher) { - Toast.makeText( - context, - R.string.registrated, - Toast.LENGTH_SHORT, - ).show() - } + _uiState.update { it.copy(showToast = true) } } } + + override fun clearShowToast() { + _uiState.update { it.copy(showToast = false) } + } } class PreviewRegisterViewModel : RegisterViewModel() { @@ -123,5 +116,6 @@ class PreviewRegisterViewModel : RegisterViewModel() { override fun addCategory(newCategory: Category) {} override fun closeAddCategoryDialog() {} override fun openAddCategoryDialog() {} - override fun registerColor(hex: String, context: Context) {} + override fun registerColor(hex: String) {} + override fun clearShowToast() {} } \ No newline at end of file diff --git a/feature/edit/src/test/java/kosenda/makecolor/feature/edit/RegisterViewModelImplTest.kt b/feature/edit/src/test/java/kosenda/makecolor/feature/edit/RegisterViewModelImplTest.kt index 792fb1f..80bcb81 100644 --- a/feature/edit/src/test/java/kosenda/makecolor/feature/edit/RegisterViewModelImplTest.kt +++ b/feature/edit/src/test/java/kosenda/makecolor/feature/edit/RegisterViewModelImplTest.kt @@ -1,8 +1,6 @@ package kosenda.makecolor.feature.edit -import android.content.Context import androidx.lifecycle.SavedStateHandle -import androidx.test.core.app.ApplicationProvider import com.google.common.truth.Truth.assertThat import io.mockk.spyk import io.mockk.verify @@ -16,24 +14,18 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runTest import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json -import org.junit.Ignore import org.junit.Rule import org.junit.Test -import org.junit.runner.RunWith -import org.robolectric.RobolectricTestRunner -@OptIn(ExperimentalCoroutinesApi::class) -@RunWith(RobolectricTestRunner::class) +@ExperimentalCoroutinesApi class RegisterViewModelImplTest { @get: Rule val mainDispatcherRule = MainDispatcherRule() - private val context = ApplicationProvider.getApplicationContext() private val registerViewModel = spyk( RegisterViewModelImpl( ioDispatcher = mainDispatcherRule.testDispatcher, - mainDispatcher = mainDispatcherRule.testDispatcher, colorRepository = FakeColorRepository(), savedStateHandle = SavedStateHandle( mapOf(NavKey.COLOR_DATA.key to Json.encodeToString(randomColorData())), @@ -107,17 +99,26 @@ class RegisterViewModelImplTest { assertThat(registerViewModel.uiState.value.isShowNewCategoryDialog).isTrue() } - @Ignore("TODO: Toastを表示するのにContextを使用しているため今後削除する。") @Test fun registerColor_add1_isRegistered() = runTest { // カラーが登録されてサイズが更新されていることを確認 registerViewModel.fetchCategories() assertThat(registerViewModel.uiState.value.selectCategory.size).isEqualTo(0) assertThat(registerViewModel.uiState.value.categories.first().size).isEqualTo(0) - registerViewModel.registerColor(hex = "FFFFFF", context = context) + registerViewModel.registerColor(hex = "FFFFFF") registerViewModel.fetchCategories() assertThat(registerViewModel.uiState.value.selectCategory.size).isEqualTo(1) assertThat(registerViewModel.uiState.value.categories.first().size).isEqualTo(1) + assertThat(registerViewModel.uiState.value.showToast).isTrue() + } + + @Test + fun clearShowToast_once_isFalse() { + // isShowToastがfalseになることを確認 + registerViewModel.registerColor(hex = "FFFFFF") + assertThat(registerViewModel.uiState.value.showToast).isTrue() + registerViewModel.clearShowToast() + assertThat(registerViewModel.uiState.value.showToast).isFalse() } }