Search code examples
iosmvvmcrossbottom-sheet

Does Mvvmcross has support for IOS BottomSheetController


Mvvmcross has perfect support for BottomSheetDialogFragment, Android. It is possible to specify view model and display bottom sheet by navigating to it.

I didn't find something similar for ios. Does MVVMCross supports BottomSheetController? Links to Github repos or blogs are much appreciated.


Solution

  • I ended up with custom presentation attribute and following code in ios presenter, which works fine with navigation service.

        public override void RegisterAttributeTypes()
        {
            base.RegisterAttributeTypes();
    
            AttributeTypesToActionsDictionary.Register<BottomSheetPresentationAttribute>(
                ShowBottomSheet,
                (viewModel, attribute) =>
                {
                    if (_bottomSheetControllers.ContainsKey(viewModel))
                    {
                        var bottomSheet = _bottomSheetControllers[viewModel];
                        _bottomSheetControllers.Remove(bottomSheet);
    
                        bottomSheet.DismissViewController(true, null);
                    }
                    return Task.FromResult(true);
                });
        }
    
        private Task<bool> ShowBottomSheet(Type viewType, BottomSheetPresentationAttribute attribute, MvxViewModelRequest request)
        {
            var contentController = BottomSheetsHelper.GetContentController(viewType);
            contentController.OnViewCreate(() => (request as MvxViewModelInstanceRequest).ViewModelInstance);
    
            var bottomSheet = new BottomSheetController(contentController);
            _bottomSheetControllers.Add(contentController.ViewModel, bottomSheet);
    
            Window.RootViewController.PresentViewController(bottomSheet, true, null);
            return Task.FromResult(true);
        }
    
        private readonly Dictionary<object, BottomSheetController> _bottomSheetControllers = new Dictionary<object, BottomSheetController>();