示例#1
0
        } = new ReactiveCommand();                                                          // いつでも実行可能

        public MainViewModel(INavigationService navigationService,
                             IPageDialogService dialogService, IStopWatchModel stopWatch)
        {
            // ■プロパティの実装
            // StopWatchModel の各プロパティをそのまま公開してるだけ
            IsRunning       = stopWatch.IsRunning;
            FormattedLaps   = stopWatch.FormattedLaps;
            IsVisibleMillis = stopWatch.IsVisibleMillis;

            // 表示用にthrottleで20ms毎に間引き。View側でやってもよいかも。
            FormattedTime = stopWatch.FormattedTime
                            //.Do(x=> Debug.WriteLine($"Throttled:{x}"))
                            .ToReadOnlyReactiveProperty();

            //// STOP されたら、最速/最遅ラップを表示して、LapActivity へ遷移
            IsRunning.Buffer(2, 1).Where(x => x[0] && !x[1])
            .Subscribe(async _ =>
            {
                // Alert を表示させる
                await dialogService.DisplayAlertAsync(
                    "Fastest/Worst Lap",
                    $"Fastest:{stopWatch.FormattedFastestLap.Value}\n" +
                    $"Worst:{stopWatch.FormattedWorstLap.Value}",
                    "Close");

                // LapActivity へ遷移させる
                await navigationService.NavigateAsync("LapPage");
            })
            .AddTo(_subscriptions);


            // ■コマンドの実装
            // 開始 or 終了
            StartOrStopCommand = new ReactiveCommand(); // いつでも実行可能
            StartOrStopCommand.Subscribe(_ =>
            {
                stopWatch.StartOrStop();
            });

            // 経過時間の記録
            LapCommand = IsRunning.ToReactiveCommand(); // 実行中のみ記録可能
            LapCommand.Subscribe(_ =>
            {
                stopWatch.Lap();
            });

            // ミリ秒以下表示の切り替え
            ToggleVisibleMillisCommand = new ReactiveCommand(); // いつでも実行可能
            ToggleVisibleMillisCommand.Subscribe(_ =>
            {
                stopWatch.ToggleVisibleMillis();
            });
        }
        public MainViewModel(INavigationService navigationService,
                             IPageDialogService dialogService, LocationUseCase locationUseCase)
        {
            // ■プロパティの実装
            // LocationUseCase の各プロパティを必要なら加工して公開
            IsRunning = locationUseCase.IsRunning.ToReadOnlyReactiveProperty();

            // Location の時刻をフォーマットして公開
            FormattedTime = locationUseCase.Location
                            .Select(l => l.Time.ToString("HH:mm:ss"))
                            .ToReadOnlyReactiveProperty();

            // Location の緯度を度分秒または度にフォーマットして公開
            FormattedLatitude = IsDmsFormat.CombineLatest(
                locationUseCase.Location.Select(l => l.Latitude),
                (isDms, lat) => lat.Format(isDms))
                                .ToReadOnlyReactiveProperty();

            // Location の経度を度分秒または度にフォーマットして公開
            FormattedLongitude = IsDmsFormat.CombineLatest(
                locationUseCase.Location.Select(l => l.Longitude),
                (isDms, lon) => lon.Format(isDms))
                                 .ToReadOnlyReactiveProperty();

            // 記録されたレコード群を件数として公開
            RecordCount = locationUseCase.Records
                          .ToCollectionChanged()
                          .Select(_ => locationUseCase.Records.Count)
                          .ToReadOnlyReactiveProperty();

            //// STOP されたら、最も精度のよい位置情報を表示して、RecordsPage へ遷移
            IsRunning
            .Buffer(2, 1)
            .Where(x => x[0] && !x[1])
            .Subscribe(async _ =>
            {
                // 最も精度のよい緯度経度を得る
                //  返値がメソッドは、その時点の情報でしかない(Reactiveではない)ので注意すること
                var bestLocation = locationUseCase.GetBestLocation();

                var message = bestLocation.HasValue ?
                              $"{bestLocation.Value.Latitude.Format(IsDmsFormat.Value)}/" +
                              $"{bestLocation.Value.Longitude.Format(IsDmsFormat.Value)} です。" :
                              "記録されてません";

                //// Alert を表示させる
                await dialogService.DisplayAlertAsync("最も精度の良い位置は", message, "Close");

                // RecordPage へ遷移させる
                await navigationService.NavigateAsync("RecordsPage");
            });


            // ■コマンドの実装
            // 開始 or 終了
            StartOrStopCommand = new ReactiveCommand(); // いつでも実行可能
            StartOrStopCommand.Subscribe(_ =>
            {
                locationUseCase.StartOrStop();
            });

            // 位置情報の記録
            RecordCommand = IsRunning.ToReactiveCommand(); // 実行中のみ記録可能
            RecordCommand.Subscribe(_ =>
            {
                locationUseCase.Record();
            });
        }