public void Init() { backgroundWorker.AddAction(() => { //创建本月数据 CreateMonthlyData(); //获取设置今日数据 tomatoDataToday = FindCreateTodayData(); }); backgroundWorker.Run(); config.Changed += Config_Changed; tray.MouseClickTrayIcon += Tray_MouseClickTrayIcon; tray.MouseMoveTrayIcon += Tray_MouseMoveTrayIcon; //工作计时器 workTimer = new DispatcherTimer(); workTimer.Tick += WorkTimer_Tick; workTimer.Interval = new TimeSpan(0, config.options.Tomato.WorkMinutes, 0); //休息计时器 restTimer = new DispatcherTimer(); restTimer.Tick += RestTimer_Tick;; restTimer.Interval = new TimeSpan(0, config.options.Tomato.ShortRestMinutes, 0); icorefreshTimer = new DispatcherTimer(); icorefreshTimer.Tick += icorefreshTimer_Tick;; /****调试模式代码****/ #if DEBUG workTimer.Interval = new TimeSpan(0, 0, 25); restTimer.Interval = new TimeSpan(0, 0, 5); #endif main.OnLeaveEvent += Main_OnLeaveEvent; }
/// <summary> /// 查找日期数据,如果不存在则创建 /// </summary> /// <param name="date"></param> /// <returns></returns> public Models.Statistic.TomatoModel FindCreateData(DateTime date) { if (date.Date == DateTime.Now.Date && tomatoDataToday != null && tomatoDataToday.Date == date.Date) { //当日 return(tomatoDataToday); } else { //非当日从数据库中查找 using (var db = new StatisticContext()) { var res = db.Tomatos.Where(m => m.Date == date.Date); if (res.Count() == 0) { //数据库中没有时则创建 var dateData = new Models.Statistic.TomatoModel() { Date = date.Date, RestartCount = 0, TomatoCount = 0 }; db.Tomatos.Add(dateData); db.SaveChanges(); return(dateData); } else { return(res.ToList().FirstOrDefault()); } } } }
/// <summary> /// 数据持久化 /// </summary> public void SaveData() { backgroundWorker.AddAction(() => { if (tomatoDataToday == null) { tomatoDataToday = FindCreateTodayData(); } using (var db = new StatisticContext()) { var item = (from c in db.Tomatos where c.Date == tomatoDataToday.Date select c).FirstOrDefault(); item.TomatoCount = tomatoDataToday.TomatoCount; item.RestartCount = tomatoDataToday.RestartCount; db.SaveChanges(); } }); backgroundWorker.Run(); }
private void WorkTimer_Tick(object sender, EventArgs e) { //工作时间已完成 //string tip = $"已结束本次工作时间,请休息{config.options.Tomato.ShortRestMinutes}分钟,我会在下一次工作开始时再次提醒您。"; string tip = Application.Current.Resources["Lang_TomatoWorkfinishTip2"].ToString().Replace("{x}", config.options.Tomato.ShortRestMinutes.ToString()); string subtitle = ""; //停止工作 workTimer.Stop(); timerWatcher.Stop(); //计次 workCount++; if (workCount == 4) { //第四次工作完成,长休息 restTimer.Interval = new TimeSpan(0, config.options.Tomato.LongRestMinutes, 0); #if DEBUG restTimer.Interval = new TimeSpan(0, 0, 10); #endif //tip = $"获得一个番茄!完成了一组工作,请休息{config.options.Tomato.LongRestMinutes}分钟,我会在下一次工作开始时再次提醒您。"; string tip1 = Application.Current.Resources["Lang_TomatoWorkfinishTip1"].ToString().Replace("{x}", config.options.Tomato.LongRestMinutes.ToString()); tip = $"{Application.Current.Resources["Lang_Getatomato"]}{tip1}"; subtitle = $"{Application.Current.Resources["Lang_Great"]}"; //数据记录 // 判断时间更新 if (tomatoDataToday.Date != DateTime.Now.Date) { // 先保存未更新日期的数据 SaveData(); // 更新日期 tomatoDataToday = FindCreateTodayData(); } tomatoDataToday.TomatoCount++; SaveData(); } //进入休息时间 restTimer.Start(); timerWatcher.Restart(); // 启动图标更新 tray.UpdateIcon("green-tomato-1"); icorefreshTimer.Interval = new TimeSpan(0, 0, (int)restTimer.Interval.TotalMinutes * 60 / 10); icorefreshTimer.Start(); if (config.options.Tomato.IsEnabledInteractiveModel) { Dialog($"{Application.Current.Resources["Lang_TomatoTimer"]}", tip, subtitle); } else { tray.BalloonTipIcon($"Tomato:{workCount}/4", tip); } }