Advertisement
daniil_fb

CoreBottomSheet (MVVM)

Feb 15th, 2021
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.89 KB | None | 0 0
  1. abstract class CoreBottomSheet<VB : ViewBinding, VM : CoreViewModel<*>>(private val isFullScreen: Boolean = true) :
  2.     BottomSheetDialogFragment() {
  3.  
  4.     protected lateinit var binding: VB
  5.  
  6.     override fun onCreate(savedInstanceState: Bundle?) {
  7.         super.onCreate(savedInstanceState)
  8.         retainInstance = true
  9.     }
  10.  
  11.     override fun onCreateView(
  12.         inflater: LayoutInflater,
  13.         container: ViewGroup?,
  14.         savedInstanceState: Bundle?
  15.     ): View? {
  16.         this.binding = onInflateViewBinding(inflater, container)
  17.         return binding.root
  18.     }
  19.  
  20.     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  21.         super.onViewCreated(view, savedInstanceState)
  22.         onSetupObservers(viewModel)
  23.         setupCoreObservers()
  24.         onViewReady(view, savedInstanceState)
  25.         setupObserveConnection()
  26.     }
  27.  
  28.     private fun setupCoreObservers() {
  29.         viewModel.onNavigateAction().observe(viewLifecycleOwner) { navDir ->
  30.             navDir?.let { findNavController().navigate(it) }
  31.         }
  32.     }
  33.  
  34.     private fun setupObserveConnection() {
  35.         lifecycleScope.launch {
  36.             sharedViewModel.onConnectedStateUpdate().collect { hasConnection ->
  37.                 withContext(Dispatchers.Main) {
  38.                     if (hasConnection) binding.root.snackbarOk(R.string.network_available)
  39.                     else binding.root.snackbarErr(R.string.no_connection)
  40.                 }
  41.             }
  42.         }
  43.     }
  44.  
  45.     abstract val viewModel: VM
  46.     abstract val sharedViewModel: MainViewModel
  47.  
  48.     abstract fun onInflateViewBinding(inflater: LayoutInflater, container: ViewGroup?): VB
  49.     abstract fun onSetupObservers(viewModel: VM)
  50.     abstract fun onViewReady(view: View, savedInstanceState: Bundle?)
  51.  
  52.     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
  53.         val bottomSheetDialog = BottomSheetDialog(
  54.             requireContext(),
  55.             R.style.AppBottomSheetDialogTheme
  56.         )
  57.  
  58.         bottomSheetDialog.dismissWithAnimation = false
  59.         bottomSheetDialog.behavior.isFitToContents = !isFullScreen
  60.         bottomSheetDialog.behavior.skipCollapsed = isFullScreen
  61.         bottomSheetDialog.behavior.state = if (isFullScreen) BottomSheetBehavior.STATE_EXPANDED
  62.         else BottomSheetBehavior.STATE_COLLAPSED
  63.         bottomSheetDialog.window?.statusBarColor = Color.TRANSPARENT
  64.         bottomSheetDialog.window?.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
  65.  
  66.         bottomSheetDialog.behavior.addBottomSheetCallback(object :
  67.             BottomSheetBehavior.BottomSheetCallback() {
  68.             override fun onSlide(p0: View, p1: Float) {}
  69.  
  70.             override fun onStateChanged(p0: View, state: Int) {
  71.                 if (state == BottomSheetBehavior.STATE_HIDDEN) {
  72.                     dismiss()
  73.                 }
  74.             }
  75.         })
  76.  
  77.         return bottomSheetDialog
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement