-
-
Save stankinzl/d98a77008b0b5b2931d970d86704b0e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PropertySelectionFragment : BaseFragment(), PropertyItemOnClickListener { | |
companion object { | |
val TAG: String = PropertySelectionFragment::class.java.name | |
fun newInstance() = PropertySelectionFragment() | |
} | |
@Inject | |
lateinit var propertyListAdapter: PropertyListAdapter | |
@Inject | |
lateinit var appFragmentFlowRouter: AppFragmentFlowRouter | |
@Inject | |
lateinit var linearLayoutManager: RecyclerView.LayoutManager | |
@Inject | |
lateinit var propertySelectionViewModel: PropertySelectionViewModel | |
override val layout: Int | |
get() = R.layout.fragment_property_selection | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
setUpRecyclerView() | |
displayFirstScreen() | |
observeViewModelState() | |
disableInteractivityBeforePropertiesFetched() | |
propertySelectionViewModel.fetchProperties() | |
} | |
private fun setUpRecyclerView() { | |
propertyListAdapter.setOnClickListener(this@PropertySelectionFragment) | |
property_list_recyclerview.apply { | |
layoutManager = linearLayoutManager | |
adapter = propertyListAdapter | |
} | |
} | |
fun observeViewModelState() { | |
propertySelectionViewModel.state.observe(this, Observer { | |
handleState(it) | |
}) | |
} | |
fun handleState(state: PropertySelectionState) { | |
when (state) { | |
is PropertySelectionState.NetworkError -> displayErrorLoadingPropertiesView() | |
is PropertySelectionState.GenericError -> displayErrorLoadingPropertiesView() | |
is PropertySelectionState.PropertiesLoaded -> { | |
propertyListAdapter.updateItems(state.properties) | |
select_property_title.setOnClickListener { displaySecondScreen() } | |
select_property_icon.visibility = View.VISIBLE | |
} | |
is PropertySelectionState.Loading -> displayLoadingView() | |
is PropertySelectionState.EmptyPropertyList -> displayEmptyView() | |
is PropertySelectionState.SuccessfullySavedSelectedProperty -> { | |
appFragmentFlowRouter.removeAllFragmentsFromBackStack(requireActivity()) | |
appFragmentFlowRouter.goToHomeScreen(requireActivity()) | |
} | |
is PropertySelectionState.ErrorSavingSelectedProperty -> displayErrorSavingSelectedPropertyView() | |
} | |
} | |
private fun displayErrorSavingSelectedPropertyView() { | |
println("error saving selected property") | |
} | |
private fun displayEmptyView() { | |
println("property list is empty") | |
} | |
private fun displayLoadingView() { | |
println("property list loading") | |
} | |
fun displayErrorLoadingPropertiesView() { | |
println("property list error loading properties") | |
} | |
private fun disableInteractivityBeforePropertiesFetched() { | |
select_property_icon.visibility = View.GONE | |
} | |
private fun displayFirstScreen() { | |
property_list_recyclerview.visibility = View.GONE | |
property_list_icon.visibility = View.GONE | |
} | |
private fun displaySecondScreen() { | |
mini_living_logo.visibility = View.GONE | |
select_property_title.visibility = View.GONE | |
select_property_icon.visibility = View.GONE | |
property_list_recyclerview.visibility = View.VISIBLE | |
property_list_icon.visibility = View.VISIBLE | |
} | |
override fun onPropertyItemClicked(property: Property) { | |
propertySelectionViewModel.saveSelectedProperty(property.id) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment