Beispiel #1
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            _viewModel = AppDelegate.SampleViewModel;

            timerStartButton.AddGestureRecognizer(new UITapGestureRecognizer(() => _viewModel?.StartClock()));
            timerStopButton.AddGestureRecognizer(new UITapGestureRecognizer(() => _viewModel?.StopClock()));
            networkButton.AddGestureRecognizer(new UITapGestureRecognizer(() => _viewModel?.GetFakeNetworking()));

            //Observe LiveData streams with LifecycleManager
            //Use NSLog or other error logging lib in real app
            _viewModel?.ClockLiveData.Observe(
                LifecycleManager, OnNextClockData, e => Console.WriteLine($"SampleViewController {e.ToString()}"));

            _viewModel?.FakeNetworkingLiveData.Observe(
                LifecycleManager, OnNextNetworkData, e => Console.WriteLine($"SampleViewController {e.ToString()}"));
        }
        /// <summary>
        /// Get ViewModel dependency from parent Activity, and observe LiveData streams
        /// </summary>
        /// <param name="savedInstanceState"></param>
        public override void OnActivityCreated(Bundle savedInstanceState)
        {
            base.OnActivityCreated(savedInstanceState);

            if (Activity is ISampleFragmentContract activity)
            {
                if (_viewModel == null)
                {
                    _viewModel = activity.GetSampleViewModel();

                    //Observe LiveData streams with LifecycleManager
                    _viewModel?.ClockLiveData.Observe(
                        LifecycleManager, OnNextClockData, e => Log.Error("SampleFragment", e.ToString()));

                    _viewModel?.FakeNetworkingLiveData.Observe(
                        LifecycleManager, OnNextNetworkData, e => Log.Error("SampleFragment", e.ToString()));
                }
            }
            else
            {
                throw new Exception("Activity not implementing ISampleFragmentContract");
            }
        }