diff --git a/PomoTime/MainPage.xaml b/PomoTime/MainPage.xaml index 6a1b350..9e5bf2d 100644 --- a/PomoTime/MainPage.xaml +++ b/PomoTime/MainPage.xaml @@ -13,10 +13,7 @@ - + @@ -65,13 +62,14 @@ VerticalAlignment="Top" FontSize="30" FontWeight="Light" - Text="{x:Bind runningState.OnRest, Converter={StaticResource BoolToWorkRestConverter}, Mode=OneWay}" /> + Text="{x:Bind runningState.CurrentPeriod, Converter={StaticResource PeriodToStringConverter}, Mode=OneWay}" /> + @@ -89,17 +87,29 @@ SpinButtonPlacementMode="Inline" Value="{x:Bind WorkMinutes, Mode=TwoWay}" /> + Value="{x:Bind BreakMinutes, Mode=TwoWay}" /> + diff --git a/PomoTime/MainPage.xaml.cs b/PomoTime/MainPage.xaml.cs index 58815c3..d430050 100644 --- a/PomoTime/MainPage.xaml.cs +++ b/PomoTime/MainPage.xaml.cs @@ -15,6 +15,8 @@ using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; using Windows.Storage; +using Windows.System.Threading; +using Windows.UI.Core; // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 @@ -26,11 +28,16 @@ namespace PomoTime /// public sealed partial class MainPage : Page { - private DispatcherTimer dispatcherTimer; public RunningState runningState = new RunningState(); + private const int DefaultBreakMinutes = 5; + private const int DefaultWorkMinutes = 25; + private const int DefaultLongBreakMinutes = 15; - private int RestMinutes { get; set; } + + private int BreakMinutes { get; set; } private int WorkMinutes { get; set; } + private int LongBreakMinutes { get; set; } + public MainPage() { @@ -50,44 +57,84 @@ namespace PomoTime Windows.Storage.ApplicationDataCompositeValue minutes = (ApplicationDataCompositeValue)roamingSettings.Values["Minutes"]; if (minutes != null) { - WorkMinutes = (int)minutes["WorkMinutes"]; - RestMinutes = (int)minutes["RestMinutes"]; + if (minutes["WorkMinutes"] != null) + { + WorkMinutes = (int)minutes["WorkMinutes"]; + } + else + { + WorkMinutes = DefaultWorkMinutes; + } + if (minutes["BreakMinutes"] != null) + { + BreakMinutes = (int)minutes["BreakMinutes"]; + } + else + { + BreakMinutes = DefaultBreakMinutes; + } + if (minutes["LongBreakMinutes"] != null) + { + LongBreakMinutes = (int)minutes["LongBreakMinutes"]; + } + else + { + LongBreakMinutes = DefaultLongBreakMinutes; + } } else { // Some maigc defualt numbers - WorkMinutes = 40; - RestMinutes = 5; + WorkMinutes = DefaultWorkMinutes; + BreakMinutes = DefaultBreakMinutes; + LongBreakMinutes = DefaultLongBreakMinutes; } runningState.MinutesLeft = WorkMinutes; runningState.SecondsLeft = 0; } - public void DispatcherTimerSetup() - { - dispatcherTimer = new DispatcherTimer(); - dispatcherTimer.Tick += dispatcherTimer_Tick; - dispatcherTimer.Interval = new TimeSpan(0, 0, 1); - } - void dispatcherTimer_Tick(object sender, object e) + void timer_Tick() { + if (!runningState.IsRunning) + { + return; + } if (runningState.SecondsLeft == 0) { runningState.SecondsLeft = 59; if (runningState.MinutesLeft == 0) { - if (runningState.OnRest) + switch (runningState.CurrentPeriod) { - runningState.OnRest = false; - runningState.MinutesLeft = WorkMinutes; - runningState.SecondsLeft = 0; - } - else - { - runningState.OnRest = true; - runningState.MinutesLeft = RestMinutes; - runningState.SecondsLeft = 0; + case Period.Work: + if (runningState.PreviousShortBreaks != 4) + { + runningState.CurrentPeriod = Period.ShortBreak; + runningState.MinutesLeft = BreakMinutes; + } + else + { + runningState.CurrentPeriod = Period.LongBreak; + runningState.MinutesLeft = LongBreakMinutes; + } + runningState.SecondsLeft = 0; + + break; + case Period.ShortBreak: + runningState.CurrentPeriod = Period.Work; + runningState.PreviousShortBreaks += 1; + runningState.MinutesLeft = WorkMinutes; + runningState.SecondsLeft = 0; + break; + case Period.LongBreak: + runningState.CurrentPeriod = Period.Work; + runningState.PreviousShortBreaks = 0; + runningState.MinutesLeft = WorkMinutes; + runningState.SecondsLeft = 0; + break; + default: + throw new ArgumentOutOfRangeException(); } } else @@ -105,24 +152,21 @@ namespace PomoTime private void PlayButton_Click(object sender, RoutedEventArgs e) { AppBarButton b = sender as AppBarButton; - dispatcherTimer.Start(); runningState.IsRunning = true; } private void PauseButton_Click(object sender, RoutedEventArgs e) { AppBarButton b = sender as AppBarButton; - dispatcherTimer.Stop(); runningState.IsRunning = false; } private void ResetButton_Click(object sender, RoutedEventArgs e) { AppBarButton b = sender as AppBarButton; - dispatcherTimer.Stop(); runningState.IsRunning = false; - runningState.OnRest = false; + runningState.CurrentPeriod = Period.Work; runningState.MinutesLeft = WorkMinutes; runningState.SecondsLeft = 0; } @@ -150,14 +194,22 @@ namespace PomoTime ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings; Windows.Storage.ApplicationDataCompositeValue minutes = new Windows.Storage.ApplicationDataCompositeValue(); minutes["WorkMinutes"] = WorkMinutes; - minutes["RestMinutes"] = RestMinutes; + minutes["BreakMinutes"] = BreakMinutes; + minutes["LongBreakMinutes"] = LongBreakMinutes; roamingSettings.Values["Minutes"] = minutes; deferral.Complete(); } private void MainPageLoaded(object sender, RoutedEventArgs e) { - DispatcherTimerSetup(); + ThreadPoolTimer timer = ThreadPoolTimer.CreatePeriodicTimer(async (t) => + { + await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, + () => + { + timer_Tick(); + }); + }, TimeSpan.FromSeconds(1)); } } } diff --git a/PomoTime/Package.appxmanifest b/PomoTime/Package.appxmanifest index 049d317..4870482 100644 --- a/PomoTime/Package.appxmanifest +++ b/PomoTime/Package.appxmanifest @@ -35,10 +35,11 @@ DisplayName="PomoTime" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" - Description="PomoTime" + Description="A qute little pomodoro technique timer, which does exactly what you think it does - counts (by default) 40 minutes of work time and then 5 minutes of rest time. And repeats until you stop it." BackgroundColor="transparent"> - + + diff --git a/PomoTime/PeriodToStringConverter.cs b/PomoTime/PeriodToStringConverter.cs new file mode 100644 index 0000000..3a36fa5 --- /dev/null +++ b/PomoTime/PeriodToStringConverter.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Windows.UI.Xaml.Data; + +namespace PomoTime +{ + public class PeriodToStringConverter : IValueConverter + { + + #region IValueConverter Members + + // Define the Convert method to change a DateTime object to + // a month string. + public object Convert(object value, Type targetType, + object parameter, string language) + { + // The value parameter is the data from the source object. + Period period = (Period)value; + string output; + switch (period) + { + case Period.LongBreak: + output = "Long break"; + break; + case Period.ShortBreak: + output = "Short break"; + break; + case Period.Work: + output = "Work"; + break; + default: + output = "Something wrong"; + break; + } + + // Return the month value to pass to the target. + return output; + } + + // ConvertBack is not implemented for a OneWay binding. + public object ConvertBack(object value, Type targetType, + object parameter, string language) + { + throw new NotImplementedException(); + } + + #endregion + } + +} diff --git a/PomoTime/PomoTime.csproj b/PomoTime/PomoTime.csproj index 68e5563..223ae79 100644 --- a/PomoTime/PomoTime.csproj +++ b/PomoTime/PomoTime.csproj @@ -122,6 +122,7 @@ MainPage.xaml + @@ -131,13 +132,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - @@ -152,7 +202,10 @@ - 6.2.9 + 6.2.10 + + + 6.0.0 6.0.0 diff --git a/PomoTime/RunningState.cs b/PomoTime/RunningState.cs index eacdd8c..3008363 100644 --- a/PomoTime/RunningState.cs +++ b/PomoTime/RunningState.cs @@ -8,6 +8,10 @@ using System.Threading.Tasks; namespace PomoTime { + public enum Period + { + Work, ShortBreak, LongBreak + } public class RunningState : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; @@ -52,17 +56,31 @@ namespace PomoTime } - public bool _on_rest; + public Period _period; - public bool OnRest + public Period CurrentPeriod { - get { return _on_rest; } + get { return _period; } set { - _on_rest = value; - NotifyPropertyChanged("OnRest"); + _period = value; + NotifyPropertyChanged("CurrentPeriod"); } } + + public int _previous_short_breaks; + + public int PreviousShortBreaks + { + get { return _previous_short_breaks; } + set + { + _previous_short_breaks = value; + NotifyPropertyChanged("PreviousShortBreaks"); + } + } } + + }