diff --git a/.idea/misc.xml b/.idea/misc.xml index 3660e44..4c5e777 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + diff --git a/app/src/main/java/com/architectcoders/aacboard/di/ViewModelModule.kt b/app/src/main/java/com/architectcoders/aacboard/di/ViewModelModule.kt index a9138e4..18d26e0 100644 --- a/app/src/main/java/com/architectcoders/aacboard/di/ViewModelModule.kt +++ b/app/src/main/java/com/architectcoders/aacboard/di/ViewModelModule.kt @@ -1,6 +1,7 @@ package com.architectcoders.aacboard.di import androidx.lifecycle.SavedStateHandle +import com.architectcoders.aacboard.ui.fragments.EditBoardCellFragmentArgs import com.architectcoders.aacboard.ui.fragments.viewmodel.EditBoardCellViewModel import com.architectcoders.aacboard.ui.fragments.viewmodel.EditDashBoardViewModel import com.architectcoders.aacboard.ui.fragments.viewmodel.ListDashboardsViewModel @@ -14,9 +15,11 @@ val viewModelModule = module { viewModel { ListDashboardsViewModel(get(), get(), get()) } viewModel { MainDashboardViewModel(get()) } viewModel { SearchPictogramsViewModel(get(), get()) } - viewModel { (handle: SavedStateHandle) -> + viewModel { params-> EditBoardCellViewModel( - handle, + dashBoardId = params.component1(), + row = params.component2(), + column = params.component3(), get(), get(), ) diff --git a/app/src/main/java/com/architectcoders/aacboard/ui/fragments/EditBoardCellFragment.kt b/app/src/main/java/com/architectcoders/aacboard/ui/fragments/EditBoardCellFragment.kt index e71dcfd..0087b3f 100644 --- a/app/src/main/java/com/architectcoders/aacboard/ui/fragments/EditBoardCellFragment.kt +++ b/app/src/main/java/com/architectcoders/aacboard/ui/fragments/EditBoardCellFragment.kt @@ -5,6 +5,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment +import androidx.navigation.fragment.navArgs import com.architectcoders.aacboard.R import com.architectcoders.aacboard.databinding.FragmentEditBoardCellBinding import com.architectcoders.aacboard.ui.fragments.stateholder.EditBoardCellState @@ -14,12 +15,17 @@ import com.architectcoders.aacboard.ui.model.PictogramUI import com.architectcoders.aacboard.ui.utils.diff import com.architectcoders.aacboard.ui.utils.loadUrl import org.koin.androidx.viewmodel.ext.android.viewModel +import org.koin.core.parameter.parametersOf class EditBoardCellFragment : Fragment(R.layout.fragment_edit_board_cell) { private var _binding: FragmentEditBoardCellBinding? = null private val binding get() = _binding!! - private val viewModel: EditBoardCellViewModel by viewModel() + private val args by navArgs() + + private val viewModel: EditBoardCellViewModel by viewModel() { + parametersOf(args.dashboardId, args.row, args.column) + } private lateinit var editBoardCellState: EditBoardCellState @@ -50,29 +56,32 @@ class EditBoardCellFragment : Fragment(R.layout.fragment_edit_board_cell) { editBoardCellState.onCancel() } } + + onColumnChanged() + onRowChanged() } private fun collectState() { viewModel.state.let { uiStateFlow -> - diff(uiStateFlow, { it.column }, ::onColumnChanged) - diff(uiStateFlow, { it.row }, ::onRowChanged) diff(uiStateFlow, { it.pictogram }, ::onPictogramChanged) diff(uiStateFlow, { it.exit }, ::onExitChanged) } } - private fun onColumnChanged(column: Int) { - binding.columnLabel.text = getString(R.string.column_label, column) + private fun onColumnChanged() { + binding.columnLabel.text = getString(R.string.column_label, viewModel.getColumn()) } - private fun onRowChanged(row: Int) { - binding.rowLabel.text = getString(R.string.row_label, row) + private fun onRowChanged() { + binding.rowLabel.text = getString(R.string.row_label, viewModel.getRow()) } private fun onPictogramChanged(pictogram: PictogramUI?) { pictogram?.let { - binding.pictogram.loadUrl(it.url) - binding.keyword.setText(it.keyword) + if(!it.url.isNullOrEmpty()){ + binding.pictogram.loadUrl(it.url) + binding.keyword.setText(it.keyword) + } } } diff --git a/app/src/main/java/com/architectcoders/aacboard/ui/fragments/EditDashboardFragment.kt b/app/src/main/java/com/architectcoders/aacboard/ui/fragments/EditDashboardFragment.kt index 2c09f85..c1ec229 100644 --- a/app/src/main/java/com/architectcoders/aacboard/ui/fragments/EditDashboardFragment.kt +++ b/app/src/main/java/com/architectcoders/aacboard/ui/fragments/EditDashboardFragment.kt @@ -14,6 +14,8 @@ import androidx.recyclerview.widget.GridLayoutManager import com.architectcoders.aacboard.R import com.architectcoders.aacboard.databinding.FragmentEditDashboardBinding import com.architectcoders.aacboard.ui.fragments.adapter.DashboardCellsAdapter +import com.architectcoders.aacboard.ui.fragments.stateholder.EditDashboardState +import com.architectcoders.aacboard.ui.fragments.stateholder.buildEditDashboardState import com.architectcoders.aacboard.ui.fragments.viewmodel.EditDashBoardViewModel import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.launch @@ -27,10 +29,16 @@ class EditDashboardFragment : Fragment() { private val args by navArgs() + private val state: EditDashboardState by lazy { + buildEditDashboardState() + } + private val viewModel: EditDashBoardViewModel by viewModel { parametersOf(args.dashBoardId) } - private val dashboardCellsAdapter = DashboardCellsAdapter { } + private val dashboardCellsAdapter = DashboardCellsAdapter { cell -> + state.onCellClicked(args.dashBoardId, cell.column, cell.row) + } override fun onCreateView( inflater: LayoutInflater, diff --git a/app/src/main/java/com/architectcoders/aacboard/ui/fragments/MainDashboardFragment.kt b/app/src/main/java/com/architectcoders/aacboard/ui/fragments/MainDashboardFragment.kt index 5551cd4..04c7aaf 100644 --- a/app/src/main/java/com/architectcoders/aacboard/ui/fragments/MainDashboardFragment.kt +++ b/app/src/main/java/com/architectcoders/aacboard/ui/fragments/MainDashboardFragment.kt @@ -31,7 +31,7 @@ class MainDashboardFragment : Fragment() { private val viewModel: MainDashboardViewModel by viewModel() - private val dashboardCellsAdapter = DashboardCellsAdapter { viewModel.onPictogramClicked(it) } + private val dashboardCellsAdapter = DashboardCellsAdapter { viewModel.onPictogramClicked(it.cellPictogram) } override fun onCreateView( inflater: LayoutInflater, diff --git a/app/src/main/java/com/architectcoders/aacboard/ui/fragments/adapter/DashboardCellsAdapter.kt b/app/src/main/java/com/architectcoders/aacboard/ui/fragments/adapter/DashboardCellsAdapter.kt index 65f82fa..6840968 100644 --- a/app/src/main/java/com/architectcoders/aacboard/ui/fragments/adapter/DashboardCellsAdapter.kt +++ b/app/src/main/java/com/architectcoders/aacboard/ui/fragments/adapter/DashboardCellsAdapter.kt @@ -12,7 +12,7 @@ import com.architectcoders.aacboard.ui.utils.getScreenSize import com.architectcoders.aacboard.ui.utils.loadUrl class DashboardCellsAdapter( - private val onPictogramClicked: (CellPictogram?) -> Unit, + private val onPictogramClicked: (Cell) -> Unit, ) : RecyclerView.Adapter() { companion object { @@ -65,7 +65,7 @@ class DashboardCellsAdapter( with(binding) { item.cellPictogram?.url?.let { url -> cellPictogram.loadUrl(url) } item.cellPictogram?.keyword?.let { cellPictogramKeyword.text = it } - root.setOnClickListener { onPictogramClicked(item.cellPictogram) } + root.setOnClickListener { onPictogramClicked(item) } } binding.cellPictogram.layoutParams.width = minOf(finalWidth, finalHeight) binding.cellPictogram.layoutParams.height = minOf(finalWidth, finalHeight) diff --git a/app/src/main/java/com/architectcoders/aacboard/ui/fragments/stateholder/EditDashboardState.kt b/app/src/main/java/com/architectcoders/aacboard/ui/fragments/stateholder/EditDashboardState.kt new file mode 100644 index 0000000..e53bd9c --- /dev/null +++ b/app/src/main/java/com/architectcoders/aacboard/ui/fragments/stateholder/EditDashboardState.kt @@ -0,0 +1,26 @@ +package com.architectcoders.aacboard.ui.fragments.stateholder + +import android.content.Context +import androidx.fragment.app.Fragment +import androidx.navigation.NavController +import androidx.navigation.fragment.findNavController +import com.architectcoders.aacboard.R +import com.architectcoders.aacboard.ui.fragments.EditDashboardFragmentDirections + +class EditDashboardState( + private val navController: NavController, +) { + + fun onCellClicked(dashBoardId: Int, row: Int, column: Int) { + val action = EditDashboardFragmentDirections.actionEditDashboardToEditBoardCell( + dashBoardId, + row, + column + ) + navController.navigate(action) + } +} + +fun Fragment.buildEditDashboardState( + navController: NavController = findNavController(), +) = EditDashboardState(navController) \ No newline at end of file diff --git a/app/src/main/java/com/architectcoders/aacboard/ui/fragments/viewmodel/EditBoardCellViewModel.kt b/app/src/main/java/com/architectcoders/aacboard/ui/fragments/viewmodel/EditBoardCellViewModel.kt index 2b495d6..7f376b9 100644 --- a/app/src/main/java/com/architectcoders/aacboard/ui/fragments/viewmodel/EditBoardCellViewModel.kt +++ b/app/src/main/java/com/architectcoders/aacboard/ui/fragments/viewmodel/EditBoardCellViewModel.kt @@ -1,13 +1,11 @@ package com.architectcoders.aacboard.ui.fragments.viewmodel -import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.architectcoders.aacboard.domain.data.cell.Cell import com.architectcoders.aacboard.domain.data.cell.CellPictogram import com.architectcoders.aacboard.domain.use_case.cell.get.GetCellUseCase import com.architectcoders.aacboard.domain.use_case.cell.save.SaveCellUseCase -import com.architectcoders.aacboard.ui.fragments.EditBoardCellFragmentArgs import com.architectcoders.aacboard.ui.model.PictogramUI import com.architectcoders.aacboard.ui.model.toUIPictogram import kotlinx.coroutines.flow.MutableStateFlow @@ -17,7 +15,9 @@ import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch class EditBoardCellViewModel( - savedStateHandle: SavedStateHandle, + private val dashBoardId: Int, + private val row: Int, + private val column: Int, private val saveCellUseCase: SaveCellUseCase, private val getCellUseCase: GetCellUseCase, ) : ViewModel() { @@ -26,30 +26,29 @@ class EditBoardCellViewModel( EditBoardCellUiState(), ) val state: StateFlow get() = _state.asStateFlow() - private val dashboardId: Int init { - EditBoardCellFragmentArgs.fromSavedStateHandle(savedStateHandle).apply { - this@EditBoardCellViewModel.dashboardId = dashboardId - viewModelScope.launch { - val cell: Cell = getCellUseCase(dashboardId, row, column) ?: Cell(row, column, null) - updateUiState( - _state.value.copy( - row = row, - column = column, - pictogram = cell.cellPictogram?.toUIPictogram() - ) + viewModelScope.launch { + val cell: Cell? = getCellUseCase(dashBoardId, row, column) + updateUiState( + _state.value.copy( + row = row, + column = column, + pictogram = cell?.cellPictogram?.toUIPictogram() ) - } + ) } } + fun getColumn() = column + fun getRow() = row + fun onSaveClicked(keyword: String) { _state.value.apply { pictogram?.let { viewModelScope.launch { saveCellUseCase( - dashboardId, + dashBoardId, Cell(row, column, CellPictogram(keyword, it.url)) ) updateUiState(_state.value.copy(exit = true)) diff --git a/app/src/test/java/com/architectcoders/aacboard/ui/fragments/viewmodel/EditBoardCellViewModelTest.kt b/app/src/test/java/com/architectcoders/aacboard/ui/fragments/viewmodel/EditBoardCellViewModelTest.kt index 5476fa7..81c209a 100644 --- a/app/src/test/java/com/architectcoders/aacboard/ui/fragments/viewmodel/EditBoardCellViewModelTest.kt +++ b/app/src/test/java/com/architectcoders/aacboard/ui/fragments/viewmodel/EditBoardCellViewModelTest.kt @@ -28,8 +28,6 @@ class EditBoardCellViewModelTest { @get:Rule val coroutinesTestRule = CoroutinesTestRule() - val stateHandle: SavedStateHandle = - SavedStateHandle(mapOf("dashboardId" to 1, "row" to 2, "column" to 3)) @Mock lateinit var saveCellUseCase: SaveCellUseCase @@ -49,7 +47,7 @@ class EditBoardCellViewModelTest { onBlocking { getCellUseCase(1, 2, 3) }.doReturn(null) } - viewModel = EditBoardCellViewModel(stateHandle, saveCellUseCase, getCellUseCase) + viewModel = EditBoardCellViewModel(1, 2, 3, saveCellUseCase, getCellUseCase) viewModel.state.test { assertEquals(EditBoardCellUiState(), awaitItem()) @@ -68,7 +66,7 @@ class EditBoardCellViewModelTest { onBlocking { getCellUseCase(1, 2, 3) }.doReturn(Cell(2, 3, sampleCellPictogram)) } - viewModel = EditBoardCellViewModel(stateHandle, saveCellUseCase, getCellUseCase) + viewModel = EditBoardCellViewModel(1, 2, 3, saveCellUseCase, getCellUseCase) viewModel.state.test { assertEquals(EditBoardCellUiState(), awaitItem()) @@ -91,7 +89,7 @@ class EditBoardCellViewModelTest { onBlocking { getCellUseCase(1, 2, 3) }.doReturn(Cell(2, 3, sampleCellPictogram)) } - viewModel = EditBoardCellViewModel(stateHandle, saveCellUseCase, getCellUseCase) + viewModel = EditBoardCellViewModel(1, 2, 3, saveCellUseCase, getCellUseCase) viewModel.state.test { assertEquals(EditBoardCellUiState(), awaitItem()) @@ -119,7 +117,7 @@ class EditBoardCellViewModelTest { onBlocking { getCellUseCase(1, 2, 3) }.doReturn(Cell(2, 3, sampleCellPictogram)) } - viewModel = EditBoardCellViewModel(stateHandle, saveCellUseCase, getCellUseCase) + viewModel = EditBoardCellViewModel(1, 2, 3, saveCellUseCase, getCellUseCase) viewModel.state.test { assertEquals(EditBoardCellUiState(), awaitItem())