Skip to content

Commit

Permalink
Merge pull request #43 from Planshit/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
noberumotto authored Apr 30, 2020
2 parents 6e698f4 + d2cc86a commit 8b93e79
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 66 deletions.
23 changes: 20 additions & 3 deletions src/Local/Project1.UI/Controls/Project1UIWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,18 @@ public bool IsThruWindow
private Rectangle ScreenArea;
#endregion

#region custom events
/// <summary>
/// 当窗口调用wshow完成时
/// </summary>
public event EventHandler OnWShow;
/// <summary>
/// 当窗口调用whide完成时
/// </summary>
public event EventHandler OnWHide;

#endregion

#region 3.初始化
public Project1UIWindow()
{
Expand Down Expand Up @@ -255,9 +267,6 @@ public Project1UIWindow()
var intPtr = new WindowInteropHelper(this).Handle;//获取当前窗口的句柄
var screen = System.Windows.Forms.Screen.FromHandle(intPtr);//获取当前屏幕
ScreenArea = screen.Bounds;



}

///// <summary>
Expand Down Expand Up @@ -658,13 +667,17 @@ private void AnimationHide(CompletedActionType completedAction)
{
case AnimationType.None:
CompletedAction(completedAction);
OnWHide?.Invoke(this, null);

break;
case AnimationType.RightBottomScale:
case AnimationType.Opacity:
closeWindowStoryboard.Completed += (e, c) =>
{
Opacity = 0;
CompletedAction(completedAction);
OnWHide?.Invoke(this, null);

};
closeWindowStoryboard.Begin();
break;
Expand All @@ -678,6 +691,7 @@ private void AnimationHide(CompletedActionType completedAction)
}
Opacity = 0;
CompletedAction(completedAction);
OnWHide?.Invoke(this, null);
}
}
#endregion
Expand Down Expand Up @@ -716,6 +730,7 @@ private void AnimationShow(CompletedActionType completedAction)
Opacity = 1;
CompletedAction(completedAction);
}
OnWShow?.Invoke(this, null);
}
#endregion

Expand All @@ -730,6 +745,8 @@ private void CompletedAction(CompletedActionType completedAction)
Hide();
break;
case CompletedActionType.Show:
//如果开启了鼠标穿透则不抢占焦点
ShowActivated = !IsThruWindow;
Show();
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
for (int i = 0; i < actions.Count; i++)
{
actions[i]();
actions.RemoveAt(i);
}
}

Expand Down
37 changes: 16 additions & 21 deletions src/Local/ProjectEye/Core/Service/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class MainService : IService
/// <summary>
/// 用眼计时器
/// </summary>
private DispatcherTimer timer;
private DispatcherTimer work_timer;
System.Diagnostics.Stopwatch workTimerStopwatch;
/// <summary>
/// 离开检测计时器
Expand Down Expand Up @@ -109,9 +109,9 @@ public MainService(App app,
public void Init()
{
//初始化用眼计时器
timer = new DispatcherTimer();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = new TimeSpan(0, config.options.General.WarnTime, 0);
work_timer = new DispatcherTimer();
work_timer.Tick += new EventHandler(timer_Tick);
work_timer.Interval = new TimeSpan(0, config.options.General.WarnTime, 0);
workTimerStopwatch = new Stopwatch();
//初始化离开检测计时器
leave_timer = new DispatcherTimer();
Expand All @@ -132,7 +132,7 @@ public void Init()
/****调试模式代码****/
#if DEBUG
//30秒提示休息
timer.Interval = new TimeSpan(0, 0, 30);
work_timer.Interval = new TimeSpan(0, 0, 30);
//20秒表示离开
leave_timer.Interval = new TimeSpan(0, 0, 20);
//每10秒检测回来
Expand Down Expand Up @@ -213,14 +213,14 @@ private void leave_timer_Tick(object sender, EventArgs e)
#region 获取下一次休息剩余分钟数
public double GetRestCountdownMinutes()
{
return timer.Interval.TotalMinutes - workTimerStopwatch.Elapsed.TotalMinutes;
return work_timer.Interval.TotalMinutes - workTimerStopwatch.Elapsed.TotalMinutes;
}
#endregion

#region 获取提醒计时是否在运行
public bool IsWorkTimerRun()
{
return timer.IsEnabled && !config.options.General.Noreset;
return work_timer.IsEnabled && !config.options.General.Noreset;
}
#endregion

Expand All @@ -241,7 +241,7 @@ private void StatisticData()
//更新用眼时长
statistic.StatisticUseEyeData();
//数据持久化
statistic.Save().Wait();
statistic.Save();
}
}
#endregion
Expand Down Expand Up @@ -287,7 +287,7 @@ public void Exit()
//更新用眼时长
statistic.StatisticUseEyeData();
//数据持久化
statistic.Save().Wait();
statistic.Save();
}

screen.Dispose();
Expand Down Expand Up @@ -323,10 +323,10 @@ public void Pause(bool isStopStatistic = true)
/// <returns>重启时返回TRUE</returns>
public bool SetWarnTime(int minutes)
{
if (timer.Interval.TotalMinutes != minutes)
if (work_timer.Interval.TotalMinutes != minutes)
{
Debug.WriteLine(timer.Interval.TotalMinutes + "," + minutes);
timer.Interval = new TimeSpan(0, minutes, 0);
Debug.WriteLine(work_timer.Interval.TotalMinutes + "," + minutes);
work_timer.Interval = new TimeSpan(0, minutes, 0);
if (!config.options.General.Noreset)
{
ReStart();
Expand Down Expand Up @@ -356,7 +356,7 @@ public void ReStart()
private void DoStart()
{
//休息提醒
timer.Start();
work_timer.Start();
workTimerStopwatch.Restart();
//离开监听
leave_timer.Start();
Expand All @@ -375,21 +375,16 @@ private void DoStart()
#region 停止计时实际操作
private void DoStop(bool isStopStatistic = true)
{

//统计数据
StatisticData();

timer.Stop();
work_timer.Stop();
workTimerStopwatch.Stop();
leave_timer.Stop();

back_timer.Stop();

if (isStopStatistic)
{
useeye_timer.Stop();
}

busy_timer.Stop();
}
#endregion
Expand Down Expand Up @@ -429,13 +424,13 @@ private void isVisibleChanged(object sender, DependencyPropertyChangedEventArgs
if (window.IsVisible)
{
//显示提示窗口时停止计时
timer.Stop();
work_timer.Stop();
workTimerStopwatch.Stop();
}
else
{
//隐藏时继续计时
timer.Start();
work_timer.Start();
workTimerStopwatch.Restart();
}
}
Expand Down
88 changes: 52 additions & 36 deletions src/Local/ProjectEye/Core/Service/StatisticService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public class StatisticService : IService
/// 用眼开始时间
/// </summary>
private DateTime useEyeStartTime { get; set; }
/// <summary>
/// 工作时间总分钟临时变量
/// </summary>
private double cacheWorkTotalMinutes;
public StatisticService(
App app,
BackgroundWorkerService backgroundWorker)
Expand All @@ -84,7 +88,7 @@ public StatisticService(

private void app_Exit(object sender, ExitEventArgs e)
{
Save().Wait();
Save();
}

public void Init()
Expand Down Expand Up @@ -159,18 +163,22 @@ public void MigrateDone()
#endregion

#region 创建本月数据
private void CreateMonthlyItems()
{
CreateMonthlyItems(DateTime.Now);
}
/// <summary>
/// 创建本月的数据
/// </summary>
private void CreateMonthlyItems()
private void CreateMonthlyItems(DateTime dateTime)
{
int days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
int today = DateTime.Now.Day;
int days = DateTime.DaysInMonth(dateTime.Year, dateTime.Month);
int today = dateTime.Day;
using (var db = new StatisticContext())
{
for (int i = 0; i < days; i++)
{
var date = DateTime.Now.AddDays(-today + (i + 1)).Date;
var date = dateTime.AddDays(-today + (i + 1)).Date;
if (db.Statistics.Where(m => m.Date == date).Count() == 0)
{
//补上缺少的日期
Expand Down Expand Up @@ -200,18 +208,18 @@ public void Add(StatisticType type, double value)
todayStatistic.Date.Date != DateTime.Now.Date)
{
//保存之前的数据
Save().Wait();
Save();
//更新当日数据
todayStatistic = FindCreate(DateTime.Now.Date);
}

switch (type)
{
case StatisticType.WorkingTime:
todayStatistic.WorkingTime = Math.Round(todayStatistic.WorkingTime + value / 60, 1);
todayStatistic.WorkingTime += value;
break;
case StatisticType.ResetTime:
todayStatistic.ResetTime = Math.Round(todayStatistic.ResetTime + value / 60, 1);
todayStatistic.ResetTime = Math.Round(todayStatistic.ResetTime + value / 60, 2);
break;
case StatisticType.SkipCount:
todayStatistic.SkipCount += (int)value;
Expand Down Expand Up @@ -259,12 +267,6 @@ public StatisticModel FindCreate(DateTime date)
}
}
}

//var data = statisticList.Data.Where(m => m.Date == date);
//if (data.Count() == 1)
//{
// return data.Single();
//}
}

#endregion
Expand All @@ -291,24 +293,25 @@ private StatisticModel GetNewdayData(DateTime date = default)
/// <summary>
/// 数据持久化
/// </summary>
public async Task Save()
public void Save()
{
if (todayStatistic == null)
{
todayStatistic = FindCreate();
}
using (var db = new StatisticContext())
backgroundWorker.AddAction(() =>
{
//var item = db.Statistics.Where(m => m.Date == todayStatistic.Date).Single();
var item = await (from c in db.Statistics where c.Date == todayStatistic.Date select c).FirstOrDefaultAsync();
item.ResetTime = todayStatistic.ResetTime;
item.SkipCount = todayStatistic.SkipCount;
item.WorkingTime = todayStatistic.WorkingTime;

await db.SaveChangesAsync();
}
//xml.Save(statisticList);

if (todayStatistic == null)
{
todayStatistic = FindCreate();
}
using (var db = new StatisticContext())
{
//var item = db.Statistics.Where(m => m.Date == todayStatistic.Date).Single();
var item = (from c in db.Statistics where c.Date == todayStatistic.Date select c).FirstOrDefault();
item.ResetTime = todayStatistic.ResetTime;
item.SkipCount = todayStatistic.SkipCount;
item.WorkingTime = todayStatistic.WorkingTime;
db.SaveChanges();
}
});
backgroundWorker.Run();
}
#endregion

Expand All @@ -333,15 +336,24 @@ public double GetCalculateUseEyeMinutes()
/// </summary>
public void StatisticUseEyeData()
{
double use = GetCalculateUseEyeMinutes();
if (use > 0)
double use = GetCalculateUseEyeMinutes() + cacheWorkTotalMinutes;
//bool issave = false;

double workTotalHours = use / 60;
if (workTotalHours < 0.1)
{
cacheWorkTotalMinutes = use;
}
else
{
Debug.WriteLine("用眼时长 +" + use + " 分钟");
//增加统计
Add(StatisticType.WorkingTime, use);
//重置统计时间
ResetStatisticTime();
Add(StatisticType.WorkingTime, Math.Round(workTotalHours, 2));
cacheWorkTotalMinutes = 0;
//issave = true;
}
//LogHelper.Debug("[" + (issave ? "saved:" + Math.Round(workTotalHours, 2) + "小时" : "-") + "]用眼+" + use + "分钟,工作开始时间:" + useEyeStartTime.ToString() + ",统计时间:" + DateTime.Now.ToString(), true);
//重置统计时间
ResetStatisticTime();
}
#endregion

Expand Down Expand Up @@ -401,6 +413,10 @@ public List<StatisticModel> GetData(DateTime startDate, DateTime endDate)
startDate = startDate.AddDays(-1);
using (var db = new StatisticContext())
{
if (db.Statistics.Where(m => m.Date == endDate.Date).Count() == 0)
{
CreateMonthlyItems(endDate);
}
result = db.Statistics.Where(m => m.Date > startDate && m.Date <= endDate).OrderBy(m => m.Date).ToList();
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion src/Local/ProjectEye/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.4")]
[assembly: AssemblyVersion("1.2.6")]
[assembly: AssemblyFileVersion("1.0.0.1")]
Loading

0 comments on commit 8b93e79

Please sign in to comment.