/
StopwatchViewModel.cs
107 lines (89 loc) · 3.93 KB
/
StopwatchViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Reactive.Linq;
using Microsoft.Practices.Unity;
using Reactive.Bindings;
using Reactive.Bindings.Extensions;
using Reactive;
using SwLib.Pcl.Controllers;
using Xamarin.Forms;
namespace StopWatch
{
public class StopwatchViewModel : IDisposable
{
public StopwatchViewModel(IStopWatchController stopWatchController)
{
var elapsedtime =
stopWatchController.StopWatchModel.ObserveProperty(x => x.ElapsedTime).ToReadOnlyReactiveProperty();
Rotation = elapsedtime.Select(x => (double) (x/1000*6%360) - 90).ToReadOnlyReactiveProperty();
Time = elapsedtime.Select(x =>x.ToTimeFormat() + "->")
.ToReadOnlyReactiveProperty();
StartCommand = IsRuning.Inverse().ToReactiveCommand();
StartCommand.Subscribe(x =>
{
IsRuning.Value = true;
if (IsStart.Value)
{
stopWatchController.Restart();
}
else
{
stopWatchController.Start();
}
IsStart.Value = true;
});
StopCommand = IsRuning.ToReactiveCommand();
StopCommand.Subscribe(x =>
{
IsRuning.Value = false;
stopWatchController.Stop();
});
ClearCommand = IsRuning.CombineLatest(elapsedtime, (run, t) => t > 0 && !run).ToReactiveCommand();
ClearCommand.Subscribe(x =>
{
IsRuning.Value = false;
IsStart.Value = false;
stopWatchController.Clear();
});
LapCommand = IsRuning.ToReactiveCommand();
LapCommand.Subscribe(x => stopWatchController.Lap());
stopWatchController.StopWatchModel.Laps
.CollectionChangedAsObservable()
.Subscribe(x =>
{
if (x.Action == NotifyCollectionChangedAction.Add)
{
var newitem = Convert.ToInt64(x.NewItems[0]);
Laps.Insert(0, $"Lap:{Laps.Count + 1} {newitem.ToTimeFormat()}" );
}
else if (x.Action == NotifyCollectionChangedAction.Reset)
{
Laps.Clear();
}
});
StartCommandTitle = IsStart.Select(x => x ? "ReStart" : "Start").ToReadOnlyReactiveProperty();
ToSaveCommand = IsStart.CombineLatest(IsRuning, (start, run) => start && !run).ToReactiveCommand();
ToSaveCommand.Subscribe(
x => MessagingCenter.Send(new NavigationMessage {IsModal = true}, NavigationMessage.ToSavePage));
MessagingCenter.Subscribe<SaveViewModel>(this, Clear, savevm => ClearCommand.Execute());
}
public const string Clear = "Clear";
public ReactiveCommand StartCommand { get; }
public ReactiveCommand StopCommand { get; }
public ReactiveCommand ClearCommand { get; }
public ReactiveCommand LapCommand { get; }
public ReactiveCommand ToSaveCommand { get; }
public ReactiveProperty<bool> IsStart { get; } = new ReactiveProperty<bool>(false);
public ReactiveProperty<bool> IsRuning { get; } = new ReactiveProperty<bool>(false);
public ReadOnlyReactiveProperty<string> StartCommandTitle { get; }
public ObservableCollection<string> Laps { get; } = new ObservableCollection<string>();
public ReadOnlyReactiveProperty<string> Time { get; }
public ReadOnlyReactiveProperty<double> Rotation { get; }
public void Dispose()
{
MessagingCenter.Unsubscribe<SaveViewModel>(this, Clear);
}
}
}