Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✅ test codes of Recipe StepMaking #197

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,12 @@ class StepMakingFragment : Fragment() {
private val binding: FragmentMakingStepBinding
get() = _binding!!
private val viewModel: StepMakingViewModel by viewModels {
/*StepMakingViewModelFactory(
recipeId = 1,
maximumStep = 15,
recipeStepMakingRepository =
DefaultRecipeStepMakingRepository(
recipeStepMakingDataSource =
DefaultRecipeStepMakingDataSource(
stepMakingService = RetrofitClient.service(StepMakingService::class.java),
),
),
)StepMakingViewModelFactory(
recipeId = 1,
maximumStep = 15,
recipeStepMakingRepository =
FakeRecipeStepMakingRepository(
recipeStepMakingDataSource =
FakeRecipeStepMakingDatasource(),
),
)*/

val appModule = (requireContext().applicationContext as DefaultPengcookApplication).appModule
StepMakingViewModelFactory(
recipeId = 1,
maximumStep = 15,
appModule.recipeStepMakingRepository,
)
// HomeViewModelFactory(appModule.feedRepository)
}

override fun onCreateView(
Expand Down Expand Up @@ -76,34 +55,37 @@ class StepMakingFragment : Fragment() {
}

private fun observeViewModel() {
observeEmptyIntroductionState()
observeUploadingImageState()
observeQuitStepMakingState()
observeCompleteStepMakingState()
}

private fun observeEmptyIntroductionState() {
viewModel.emptyIntroductionState.observe(viewLifecycleOwner) { event ->
event.getContentIfNotHandled()?.let {
Toast
.makeText(
requireContext(),
"Introduction Should be filled",
Toast.LENGTH_SHORT,
).show()
showToast("Introduction Should be filled")
}
}
}

private fun observeUploadingImageState() {
viewModel.uploadingImageState.observe(viewLifecycleOwner) { event ->
event.getContentIfNotHandled()?.let {
Toast
.makeText(
requireContext(),
"Cannot move to next step while uploading image",
Toast.LENGTH_SHORT,
).show()
showToast("Cannot move to next step while uploading image")
}
}
}

private fun observeQuitStepMakingState() {
viewModel.quitStepMakingState.observe(viewLifecycleOwner) { event ->
event.getContentIfNotHandled()?.let {
findNavController().popBackStack()
}
}
}

private fun observeCompleteStepMakingState() {
viewModel.completeStepMakingState.observe(viewLifecycleOwner) { event ->
event.getContentIfNotHandled()?.let {
// Seems like legacy code
Expand All @@ -112,4 +94,13 @@ class StepMakingFragment : Fragment() {
}
}
}

private fun showToast(message: String) {
Toast
.makeText(
requireContext(),
message,
Toast.LENGTH_SHORT,
).show()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class StepMakingViewModel(
val imageUrl: LiveData<String>
get() = _imageUrl

private val _fetchedRecipeStep = MutableLiveData<RecipeStep?>()
val fetchedRecipeStep: LiveData<RecipeStep?>
get() = _fetchedRecipeStep

private val isIntroductionContentEmpty = MutableLiveData<Boolean>(true)

private var _emptyIntroductionState = MutableLiveData<Event<Boolean>>()
Expand All @@ -53,6 +57,10 @@ class StepMakingViewModel(
val quitStepMakingState: LiveData<Event<Boolean>>
get() = _quitStepMakingState

private var _uploadErrorState = MutableLiveData<Event<Boolean>>()
val uploadErrorState: LiveData<Event<Boolean>>
get() = _uploadErrorState

init {
initStepData(stepNumber.value!!)
}
Expand All @@ -66,6 +74,8 @@ class StepMakingViewModel(
} else if (isUploadingImage.value == true) {
_uploadingImageState.value = Event(true)
} else {
if (stepNumber.value == maximumStep) return
uploadStepData(stepNumber.value!!)
_stepNumber.value = _stepNumber.value?.plus(1)
initStepData(stepNumber.value!!)
isIntroductionContentEmpty.value = false
Expand All @@ -74,6 +84,7 @@ class StepMakingViewModel(

override fun validatePreviousPageableCondition() {
if (stepNumber.value == 1) return
uploadStepData(stepNumber.value!!)
_stepNumber.value = _stepNumber.value?.minus(1)
initStepData(stepNumber.value!!)
}
Expand Down Expand Up @@ -115,27 +126,28 @@ class StepMakingViewModel(
sequence = stepNumber,
)

_fetchedRecipeStep.value = result.getOrNull()
return result.getOrNull()
}

private suspend fun uploadStepData(stepNumber: Int) {
private fun uploadStepData(stepNumber: Int) {
// Upload step data to repository
recipeStepMakingRepository.uploadRecipeStep(
recipeId = recipeId,
recipeStep =
RecipeStep(
viewModelScope.launch {
recipeStepMakingRepository
.uploadRecipeStep(
recipeId = recipeId,
sequence = stepNumber,
description = introductionContent.value ?: "",
image = imageUrl.value ?: "",
stepId = 1L,
),
)
recipeStep =
RecipeStep(
recipeId = recipeId,
sequence = stepNumber,
description = introductionContent.value ?: "",
image = imageUrl.value ?: "",
stepId = 1L,
),
).onSuccess {
}.onFailure {
_uploadErrorState.value = Event(true)
}
}
}
}

sealed interface StepMakingUiEvent {
data object EmptyIntroductionState : StepMakingUiEvent

data object UploadIngImageState : StepMakingUiEvent
}
Loading
Loading