Advertisement
BlackZerg

Untitled

Mar 17th, 2021
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.84 KB | None | 0 0
  1. class TrainingsFragment : BaseFragment(R.layout.fragment_trainings) {
  2.  
  3.     private val viewModel by inject<TrainingsVM>()
  4.  
  5.     private val simulatorsAdapter = DelegateAdapter(SimulatorItemDelegate { lessons ->
  6.         val bundle = bundleOf(
  7.             KEY_SIMULATOR to lessons,
  8.             KEY_LABEL to lessons.name
  9.         )
  10.         navigateTo(R.id.lessonsFragment, bundle)
  11.     })
  12.  
  13.     override fun setupUI(v: View) {
  14.         FragmentTrainingsBinding.bind(v).apply {
  15.             recyclerViewSimulators.setRecyclerView(root, simulatorsAdapter)
  16.             swipeRefresh.setOnRefreshListener { refreshListener(this) }
  17.  
  18.             with(viewModel) {
  19.                 progress.observe(viewLifecycleOwner) { progressBar.showProgressBar(it) }
  20.                 errors.observe(viewLifecycleOwner) {
  21.                     root.showToast(getString(R.string.fragment_trainings_load_simulators_error))
  22.                 }
  23.             }
  24.             lifecycleScope.launch {
  25.                 launch { viewModel.watchState.collect { onStateChanged(it, this@apply) } }
  26.             }
  27.         }
  28.     }
  29.  
  30.     override fun setupObserver() {
  31.         lifecycleScope.launch {
  32.             launch { viewModel.watchTrainings.collect { model ->
  33.                 model?.let { simulator -> renderSimulators(
  34.                     simulator.map {
  35.                         SimulatorsModel(
  36.                             id = it.id,
  37.                             name = it.name,
  38.                             description = it.description,
  39.                             image = it.image,
  40.                             level = it.level,
  41.                             progress = it.progress,
  42.                             available = it.available,
  43.                             status = it.status,
  44.                             buttonName = it.buttonName,
  45.                             lessonsCount = it.lessonsCount
  46.                         )
  47.                     }
  48.                 ) }
  49.             } }
  50.         }
  51.     }
  52.  
  53.     private fun renderSimulators(simulator: List<SimulatorsModel>) {
  54.         simulatorsAdapter.apply {
  55.             items.clear()
  56.             items.addAll(simulator.map { SimulatorsElement.SimulatorItem(it) })
  57.             notifyDataSetChanged()
  58.         }
  59.     }
  60.  
  61.     private fun refreshListener(binding: FragmentTrainingsBinding) {
  62.         viewModel.fetchSimulators()
  63.         binding.swipeRefresh.isRefreshing = false
  64.     }
  65.  
  66.     private fun onStateChanged(state: TrainingsState, binding: FragmentTrainingsBinding) {
  67.         when (state) {
  68.             TrainingsState.LogoutState -> logout(binding)
  69.             TrainingsState.NeutralState -> Unit
  70.         }
  71.         viewModel.onSetState(TrainingsState.NeutralState)
  72.     }
  73.  
  74.     private fun logout(binding: FragmentTrainingsBinding) {
  75.         viewModel.onLogout()
  76.         binding.root.context.navigateToActivity<AuthActivity> {  }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement