Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- abstract class CoreBottomSheet<VB : ViewBinding, VM : CoreViewModel<*>>(private val isFullScreen: Boolean = true) :
- BottomSheetDialogFragment() {
- protected lateinit var binding: VB
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- retainInstance = true
- }
- override fun onCreateView(
- inflater: LayoutInflater,
- container: ViewGroup?,
- savedInstanceState: Bundle?
- ): View? {
- this.binding = onInflateViewBinding(inflater, container)
- return binding.root
- }
- override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
- super.onViewCreated(view, savedInstanceState)
- onSetupObservers(viewModel)
- setupCoreObservers()
- onViewReady(view, savedInstanceState)
- setupObserveConnection()
- }
- private fun setupCoreObservers() {
- viewModel.onNavigateAction().observe(viewLifecycleOwner) { navDir ->
- navDir?.let { findNavController().navigate(it) }
- }
- }
- private fun setupObserveConnection() {
- lifecycleScope.launch {
- sharedViewModel.onConnectedStateUpdate().collect { hasConnection ->
- withContext(Dispatchers.Main) {
- if (hasConnection) binding.root.snackbarOk(R.string.network_available)
- else binding.root.snackbarErr(R.string.no_connection)
- }
- }
- }
- }
- abstract val viewModel: VM
- abstract val sharedViewModel: MainViewModel
- abstract fun onInflateViewBinding(inflater: LayoutInflater, container: ViewGroup?): VB
- abstract fun onSetupObservers(viewModel: VM)
- abstract fun onViewReady(view: View, savedInstanceState: Bundle?)
- override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
- val bottomSheetDialog = BottomSheetDialog(
- requireContext(),
- R.style.AppBottomSheetDialogTheme
- )
- bottomSheetDialog.dismissWithAnimation = false
- bottomSheetDialog.behavior.isFitToContents = !isFullScreen
- bottomSheetDialog.behavior.skipCollapsed = isFullScreen
- bottomSheetDialog.behavior.state = if (isFullScreen) BottomSheetBehavior.STATE_EXPANDED
- else BottomSheetBehavior.STATE_COLLAPSED
- bottomSheetDialog.window?.statusBarColor = Color.TRANSPARENT
- bottomSheetDialog.window?.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
- bottomSheetDialog.behavior.addBottomSheetCallback(object :
- BottomSheetBehavior.BottomSheetCallback() {
- override fun onSlide(p0: View, p1: Float) {}
- override fun onStateChanged(p0: View, state: Int) {
- if (state == BottomSheetBehavior.STATE_HIDDEN) {
- dismiss()
- }
- }
- })
- return bottomSheetDialog
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement