private static ApplicationState TimerReducer(ApplicationState previousState, TimerAction action)
        {
            //Debug.WriteLine($"TimerAction Now:{ ((TimerAction)action).Now}");
            var span       = action.Now - previousState.GetState <DateTime>(StartTime);
            var nextStates = previousState.States.SetItems(new Dictionary <string, object>()
            {
                { NowSpan, span },
                { Now, action.Now },
            });

            return(new ApplicationState(nextStates, previousState.TimerScheduler));
        }
Exemple #2
0
        static void Main()
        {
            var state = ApplicationState.GetState();

            state.LoginId = "kanasz";
            state.MaxSize = 1024;

            var state2 = ApplicationState.GetState();

            Console.WriteLine(state2.LoginId);
            Console.WriteLine(state2.MaxSize);
            Console.WriteLine(state == state2);
        }
        private static ApplicationState LapReducer(ApplicationState previousState, LapAction action)
        {
            var timerScheduler = previousState.TimerScheduler;
            var startTime      = previousState.GetState <DateTime>(StartTime);
            var lapTimeList    = previousState.GetState <ObservableCollection <LapTime> >(LapTimeList);
            var now            = previousState.GetState <DateTime>(Now);
            //Debug.WriteLine($"LapAction Now:{ now}");
            var prevLap = lapTimeList.Any() ? lapTimeList.Last().Time : startTime;

            lapTimeList.Add(new LapTime(time: now, span: timerScheduler.Now.DateTime.ToLocalTime() - prevLap));

            var max = lapTimeList.Max(s => s.Span);
            var min = lapTimeList.Min(s => s.Span);

            var nextStates = previousState.States.SetItems(new Dictionary <string, object>()
            {
                { LapTimeList, lapTimeList },
                { MaxLapTime, max },
                { MinLapTime, min },
            });

            return(new ApplicationState(nextStates, previousState.TimerScheduler));
        }
        private static ApplicationState ChangeModeReducer(ApplicationState previousState, ChangeModeAction action)
        {
            var           previousMode   = previousState.GetState <StopwatchMode>(Mode);
            var           timerScheduler = previousState.TimerScheduler;
            string        buttonLabel    = "";
            StopwatchMode nextMode;
            DateTime      startTime;
            var           lapTimeList = previousState.GetState <ObservableCollection <LapTime> >(LapTimeList);
            var           now = previousState.GetState <DateTime>(Now);
            var           l = new Dictionary <string, object>();
            TimeSpan      max, min;

            switch (previousMode)
            {
            case StopwatchMode.Init:
                nextMode    = StopwatchMode.Start;
                buttonLabel = Constants.StopLabel;
                startTime   = timerScheduler.Now.DateTime.ToLocalTime();
                //Debug.WriteLine($"startTime:{startTime}");
                l.Add(Mode, nextMode);
                l.Add(ButtonLabel, buttonLabel);
                l.Add(StartTime, startTime);
                break;

            case StopwatchMode.Start:
                //Debug.WriteLine($"ChangeModeAction Now:{ now}");
                nextMode    = StopwatchMode.Stop;
                buttonLabel = Constants.ResetLabel;
                startTime   = previousState.GetState <DateTime>(StartTime);
                var prevLap = lapTimeList.Any() ? lapTimeList.Last().Time : startTime;
                //Debug.WriteLine($"ChangeModeAction prevLap:{ prevLap}");
                lapTimeList.Add(new LapTime(time: now, span: timerScheduler.Now.DateTime.ToLocalTime() - prevLap));
                max = lapTimeList.Max(s => s.Span);
                min = lapTimeList.Min(s => s.Span);
                l.Add(LapTimeList, lapTimeList);
                l.Add(Mode, nextMode);
                l.Add(ButtonLabel, buttonLabel);
                l.Add(MaxLapTime, max);
                l.Add(MinLapTime, min);
                break;

            case StopwatchMode.Stop:
                nextMode    = StopwatchMode.Init;
                buttonLabel = Constants.StartLabel;
                startTime   = timerScheduler.Now.DateTime.ToLocalTime();
                lapTimeList.Clear();
                max = TimeSpan.Zero;
                min = TimeSpan.Zero;
                l.Add(Mode, nextMode);
                l.Add(ButtonLabel, buttonLabel);
                l.Add(StartTime, startTime);
                l.Add(NowSpan, TimeSpan.Zero);
                l.Add(LapTimeList, lapTimeList);
                l.Add(MaxLapTime, max);
                l.Add(MinLapTime, min);
                break;

            default:
                throw new InvalidOperationException();
            }

            var nextStates = previousState.States.SetItems(l);

            return(new ApplicationState(nextStates, previousState.TimerScheduler));
        }