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

[FTA] advance (go back) to (from) address confirmation screen #145

Open
wants to merge 3 commits into
base: feature/free-text-articles-2
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import app.nexd.android.api.model.CreateHelpRequestArticleDto
import app.nexd.android.api.model.HelpRequestCreateDto
import app.nexd.android.api.model.Unit
import app.nexd.android.ui.common.helprequest.HelpRequestCreateArticleBinder.ArticleViewModel
import app.nexd.android.ui.utils.SingleLiveEvent
import app.nexd.android.ui.utils.extensions.currentLanguage
import app.nexd.android.ui.utils.extensions.toLiveData
import io.reactivex.BackpressureStrategy.LATEST
Expand All @@ -25,11 +26,12 @@ class SeekerCreateRequestViewModel(private val context: Context, private val api

sealed class Progress {
object Idle : Progress()
object Loading : Progress()
class Error(@StringRes val message: Int? = null) : Progress()
object Finished : Progress()
}


val navigateToConfirmAddress = SingleLiveEvent<Any>()
val progress: MutableLiveData<Progress> = MutableLiveData(Progress.Idle)

val firstName = MutableLiveData<String?>()
Expand Down Expand Up @@ -213,4 +215,25 @@ class SeekerCreateRequestViewModel(private val context: Context, private val api
true
}
}

fun confirmItems() {
// to also include null check for inputs the negative case
// (no confirmed items, ie = size < 2) is evaluated first
val invalid =
// item name null or empty
inputs.value?.any { it.articleName.value.isNullOrBlank() } ?: true ||
// no item added
inputs.value?.size ?: 1 < 2 ||
// amount is null, empty or less than 1
inputs.value?.any { it.amount.value.isNullOrBlank() || it.amount.value?.toIntOrNull() ?: 0 < 1 } ?: true ||
// no selected Unit
inputs.value?.any { it.selectedUnit.value == null } ?: true

if (invalid) {
progress.value = Progress.Error(R.string.seeker_request_create_no_articles)
} else {
navigateToConfirmAddress.call()
progress.value = Progress.Idle
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ class SeekerCreateRequestConfirmAddressFragment : Fragment() {
is Idle -> {
// nothing to do
}
is Loading -> {
//
}
is Error -> {
it.message?.let { errorMessageId ->
DefaultSnackbar(binding.root, errorMessageId, Snackbar.LENGTH_SHORT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ class SeekerCreateRequestEnterArticlesFragment : Fragment() {

initRecyclerView()
setVmObserver()
setUi()
}

override fun onDestroyView() {
super.onDestroyView()

adapter.removeAllSections()
}

private fun initRecyclerView() {
Expand Down Expand Up @@ -105,9 +112,6 @@ class SeekerCreateRequestEnterArticlesFragment : Fragment() {
is Idle -> {
//nothing to do
}
is Loading -> {
findNavController().navigate(SeekerCreateRequestEnterArticlesFragmentDirections.toSeekerCreateRequestConfirmAddressFragment())
}
is Finished -> {
// state not reachable
}
Expand All @@ -122,5 +126,16 @@ class SeekerCreateRequestEnterArticlesFragment : Fragment() {
)
}
})

vm.navigateToConfirmAddress.observe(viewLifecycleOwner, Observer {
findNavController().navigate(SeekerCreateRequestEnterArticlesFragmentDirections.toSeekerCreateRequestConfirmAddressFragment())
})
}

private fun setUi() {
binding.buttonAccept.setOnClickListener {
vm.confirmItems()
}
}

}