Last active
May 11, 2020 04:49
-
-
Save ahinchman1/11340be89c42dd82bcbfc36240fc5ed9 to your computer and use it in GitHub Desktop.
This file contains 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
sealed class ErrorViewState { | |
object Content : ErrorViewState() | |
} | |
class ErrorViewModel : ViewModel() { | |
private val _viewState = MutableLiveData<ErrorViewState>() | |
val viewState: LiveData<ErrorViewState> = _viewState | |
init { | |
_viewState.value = ErrorViewState.Content | |
} | |
} | |
class ErrorFragment: Fragment() { | |
private val navigator by lazy { (requireActivity() as MainActivity).navigator } | |
private val viewModel by viewModels<ErrorViewModel>() | |
override fun onCreateView( | |
inflater: LayoutInflater, | |
container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? = inflater.inflate(R.layout.fragment_error, container, false) | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setHasOptionsMenu(true) | |
activity?.title = requireArguments().getString(PREVIOUS_DESTINATION_LABEL, "") | |
viewModel.viewState.observe(this, Observer<ErrorViewState> { viewState -> | |
viewState?.let { render(it) } | |
}) | |
} | |
private fun render(viewState: ErrorViewState) { | |
when (viewState) { | |
is ErrorViewState.Content -> { | |
error_button.setOnClickListener { | |
activity?.title?.let { _ -> | |
when (requireArguments().getInt(PREVIOUS_DESTINATION_ID, 0)) { | |
R.id.invasiveSpeciesMapFragment -> navigator.navigateToInvasiveSpeciesMap() | |
R.id.treeSpotterFragment -> navigator.navigateToTreeSpotter() | |
else -> navigator.navigateToError(from = Screen.ERROR) | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment