Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TrainingsFragment : BaseFragment(R.layout.fragment_trainings) {
- private val viewModel by inject<TrainingsVM>()
- private val simulatorsAdapter = DelegateAdapter(SimulatorItemDelegate { lessons ->
- val bundle = bundleOf(
- KEY_SIMULATOR to lessons,
- KEY_LABEL to lessons.name
- )
- navigateTo(R.id.lessonsFragment, bundle)
- })
- override fun setupUI(v: View) {
- FragmentTrainingsBinding.bind(v).apply {
- recyclerViewSimulators.setRecyclerView(root, simulatorsAdapter)
- swipeRefresh.setOnRefreshListener { refreshListener(this) }
- with(viewModel) {
- progress.observe(viewLifecycleOwner) { progressBar.showProgressBar(it) }
- errors.observe(viewLifecycleOwner) {
- root.showToast(getString(R.string.fragment_trainings_load_simulators_error))
- }
- }
- lifecycleScope.launch {
- launch { viewModel.watchState.collect { onStateChanged(it, this@apply) } }
- }
- }
- }
- override fun setupObserver() {
- lifecycleScope.launch {
- launch { viewModel.watchTrainings.collect { model ->
- model?.let { simulator -> renderSimulators(
- simulator.map {
- SimulatorsModel(
- id = it.id,
- name = it.name,
- description = it.description,
- image = it.image,
- level = it.level,
- progress = it.progress,
- available = it.available,
- status = it.status,
- buttonName = it.buttonName,
- lessonsCount = it.lessonsCount
- )
- }
- ) }
- } }
- }
- }
- private fun renderSimulators(simulator: List<SimulatorsModel>) {
- simulatorsAdapter.apply {
- items.clear()
- items.addAll(simulator.map { SimulatorsElement.SimulatorItem(it) })
- notifyDataSetChanged()
- }
- }
- private fun refreshListener(binding: FragmentTrainingsBinding) {
- viewModel.fetchSimulators()
- binding.swipeRefresh.isRefreshing = false
- }
- private fun onStateChanged(state: TrainingsState, binding: FragmentTrainingsBinding) {
- when (state) {
- TrainingsState.LogoutState -> logout(binding)
- TrainingsState.NeutralState -> Unit
- }
- viewModel.onSetState(TrainingsState.NeutralState)
- }
- private fun logout(binding: FragmentTrainingsBinding) {
- viewModel.onLogout()
- binding.root.context.navigateToActivity<AuthActivity> { }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement