Skip to content

Instantly share code, notes, and snippets.

@stankinzl
Created February 12, 2019 14:58
Show Gist options
  • Save stankinzl/31b30086d4691cbc27c8361714cf969b to your computer and use it in GitHub Desktop.
Save stankinzl/31b30086d4691cbc27c8361714cf969b to your computer and use it in GitHub Desktop.
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