Skip to content

Commit

Permalink
- fix issue with country selector; (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
MykhailoNester authored Sep 20, 2021
1 parent b6d9454 commit 9513880
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 53 deletions.
75 changes: 35 additions & 40 deletions app/src/main/java/dgca/verifier/app/android/CodeReaderFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import android.widget.AdapterView.OnItemSelectedListener
import androidx.activity.addCallback
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.fragment.app.setFragmentResultListener
import androidx.fragment.app.viewModels
import androidx.navigation.NavController
Expand Down Expand Up @@ -62,10 +63,12 @@ class CodeReaderFragment : BindingFragment<FragmentCodeReaderBinding>(),

private val viewModel by viewModels<CodeReaderViewModel>()

private lateinit var beepManager: BeepManager
private var lastText: String? = null
private var refinedCountries: List<String> = emptyList()

private lateinit var beepManager: BeepManager
private lateinit var adapter: CountriesAdapter

private val callback: BarcodeCallback = object : BarcodeCallback {
override fun barcodeResult(result: BarcodeResult) {
if (result.text == null || result.text == lastText) {
Expand All @@ -87,6 +90,7 @@ class CodeReaderFragment : BindingFragment<FragmentCodeReaderBinding>(),
super.onCreate(savedInstanceState)
requireActivity().onBackPressedDispatcher.addCallback { requireActivity().finish() }
(activity as MainActivity).clearBackground()
adapter = CountriesAdapter(layoutInflater)
}

override fun onCreateBinding(
Expand All @@ -109,8 +113,6 @@ class CodeReaderFragment : BindingFragment<FragmentCodeReaderBinding>(),
findNavController().navigate(action)
}

setUpCountriesProcessing()

setFragmentResultListener(VERIFY_REQUEST_KEY) { _, bundle ->
val standardizedVerificationResult: StandardizedVerificationResult? =
bundle.getSerializable(
Expand All @@ -135,6 +137,36 @@ class CodeReaderFragment : BindingFragment<FragmentCodeReaderBinding>(),
)
}
}

binding.countrySelector.adapter = adapter
binding.countrySelector.onItemSelectedListener = object : OnItemSelectedListener {
override fun onItemSelected(
parentView: AdapterView<*>?,
selectedItemView: View?,
position: Int,
id: Long
) {
viewModel.selectCountry(adapter.getItem(position))
}

override fun onNothingSelected(parentView: AdapterView<*>?) {
}
}

viewModel.countries.observe(viewLifecycleOwner, { list ->
val hasCountries = list.isNotEmpty()
binding.validateWith.isVisible = hasCountries
binding.countrySelector.isVisible = hasCountries

refinedCountries = list.sortedBy { Locale("", COUNTRIES_MAP[it] ?: it).displayCountry }
adapter.update(refinedCountries)
})
viewModel.selectedCountry.observe(viewLifecycleOwner) {
val position = refinedCountries.indexOf(it)
if (position > 0) {
binding.countrySelector.setSelection(position)
}
}
}

private fun showVerificationResult(
Expand Down Expand Up @@ -177,43 +209,6 @@ class CodeReaderFragment : BindingFragment<FragmentCodeReaderBinding>(),
binding.barcodeScanner.pause()
}

private fun setUpCountriesProcessing() {
viewModel.countries.observe(viewLifecycleOwner, { pair ->
if (pair.first.isEmpty() || pair.second == null) {
View.GONE
} else {
val countries = pair.first
refinedCountries =
countries.sortedBy { Locale("", COUNTRIES_MAP[it] ?: it).displayCountry }
binding.countrySelector.adapter = CountriesAdapter(refinedCountries, layoutInflater)
if (pair.second!!.isNotBlank()) {
val selectedCountryIndex =
refinedCountries.indexOf(pair.second!!)
if (selectedCountryIndex >= 0) {
binding.countrySelector.setSelection(selectedCountryIndex)
}
}
binding.countrySelector.onItemSelectedListener = object : OnItemSelectedListener {
override fun onItemSelected(
parentView: AdapterView<*>?,
selectedItemView: View?,
position: Int,
id: Long
) {
viewModel.selectCountry(refinedCountries[position].toLowerCase(Locale.ROOT))
}

override fun onNothingSelected(parentView: AdapterView<*>?) {
}
}
View.VISIBLE
}.apply {
binding.validateWith.visibility = this
binding.countrySelector.visibility = this
}
})
}

private fun navigateToVerificationPage(text: String) {
val action =
CodeReaderFragmentDirections.actionCodeReaderFragmentToVerificationDialogFragment(
Expand Down
25 changes: 13 additions & 12 deletions app/src/main/java/dgca/verifier/app/android/CodeReaderViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,36 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import dgca.verifier.app.android.data.local.Preferences
import dgca.verifier.app.android.settings.debug.mode.DebugModeState
import dgca.verifier.app.engine.data.source.countries.CountriesRepository
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class CodeReaderViewModel @Inject constructor(
countriesRepository: CountriesRepository,
private val preferences: Preferences
) : ViewModel() {
private val _countries: MediatorLiveData<Pair<List<String>, String?>> = MediatorLiveData()
val countries: LiveData<Pair<List<String>, String?>> = _countries
private val _selectedCountry: LiveData<String?> = liveData {
emit(preferences.selectedCountryIsoCode)
}

private val _countries = MediatorLiveData<List<String>>()
val countries: LiveData<List<String>> = _countries

private val _selectedCountry = MutableLiveData<String>()
val selectedCountry: LiveData<String> = _selectedCountry

val debugModeState: LiveData<DebugModeState> = liveData {
emit(preferences.debugModeState?.let { DebugModeState.valueOf(it) } ?: DebugModeState.OFF)
}

fun selectCountry(countryIsoCode: String) {
preferences.selectedCountryIsoCode = countryIsoCode
_selectedCountry.value = countryIsoCode
}

init {
_countries.addSource(countriesRepository.getCountries().asLiveData()) {
_countries.value = Pair(it, _countries.value?.second)
}

_countries.addSource(_selectedCountry) {
if (_countries.value?.second == null || _countries.value?.second != it) {
_countries.value = Pair(_countries.value?.first ?: emptyList(), it ?: "")
viewModelScope.launch {
countriesRepository.getCountries().collectLatest {
_countries.value = it
_selectedCountry.value = preferences.selectedCountryIsoCode
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ import java.util.*
* Created by osarapulov on 08.07.21 0:01
*/
class CountriesAdapter(
private val refinedCountries: List<String>,
private val layoutInflater: LayoutInflater
) : BaseAdapter() {

private var refinedCountries: List<String> = listOf()

override fun getCount(): Int = refinedCountries.size

override fun getItem(position: Int): String = refinedCountries[position]
Expand All @@ -75,4 +76,9 @@ class CountriesAdapter(
val locale = Locale("", COUNTRIES_MAP[countryIsoCode] ?: countryIsoCode)
textView.text = locale.displayCountry
}

fun update(list: List<String>) {
refinedCountries = list
notifyDataSetChanged()
}
}

0 comments on commit 9513880

Please sign in to comment.