Skip to content

Commit 76dff10

Browse files
committed
Optimize performance
1 parent 1af5ba4 commit 76dff10

File tree

5 files changed

+304
-156
lines changed

5 files changed

+304
-156
lines changed

Methods/MyDeviceHelper.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Text;
99
using System.Threading.Tasks;
1010
using System.Windows;
11+
using System.Windows.Threading;
1112

1213
namespace Frogy.Methods
1314
{
@@ -201,5 +202,20 @@ public static void PromotePermission()
201202
}
202203
catch { }
203204
}
205+
206+
public static void DoEvents()
207+
{
208+
DispatcherFrame frame = new DispatcherFrame();
209+
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
210+
new DispatcherOperationCallback(ExitFrames), frame);
211+
Dispatcher.PushFrame(frame);
212+
}
213+
214+
public static object ExitFrames(object f)
215+
{
216+
((DispatcherFrame)f).Continue = false;
217+
218+
return null;
219+
}
204220
}
205221
}

ViewModels/OverViewViewModel.cs

Lines changed: 156 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.WindowsAPICodePack.Dialogs;
99
using System;
1010
using System.Collections.Generic;
11+
using System.Collections.ObjectModel;
1112
using System.ComponentModel;
1213
using System.Linq;
1314
using System.Runtime.CompilerServices;
@@ -17,6 +18,7 @@
1718
using System.Windows;
1819
using System.Windows.Input;
1920
using System.Windows.Media.Animation;
21+
using System.Windows.Threading;
2022

2123
namespace Frogy.ViewModels
2224
{
@@ -27,112 +29,141 @@ class OverViewViewModel : INotifyPropertyChanged
2729
/// </summary>
2830
/// <param name="TodayOverview">今日OverView</param>
2931
/// <returns>List<OverViewItem></returns>
30-
private List<OverViewItem> PrintOverview(Dictionary<string, Software> TodayOverview)
32+
private Task<List<OverViewItem>> PrintOverview(Dictionary<string, Software> TodayOverview)
3133
{
32-
List<OverViewItem> result = new List<OverViewItem>();
33-
//排序
34-
var dicSort = from objDic in TodayOverview orderby objDic.Value.Duration descending select objDic;
35-
36-
foreach (KeyValuePair<string, Software> kvp in dicSort)
34+
return Task.Run(() =>
3735
{
38-
OverViewItem tmp =
39-
new OverViewItem()
40-
{
41-
AppName = kvp.Key,
42-
AppDuration = kvp.Value.Duration.ToString(),
43-
AppIcon = kvp.Value.Icon
44-
};
45-
result.Add(tmp);
46-
}
36+
List<OverViewItem> result = new List<OverViewItem>();
37+
//排序
38+
var dicSort = from objDic in TodayOverview orderby objDic.Value.Duration descending select objDic;
4739

48-
return result;
40+
foreach (KeyValuePair<string, Software> kvp in dicSort)
41+
{
42+
OverViewItem tmp =
43+
new OverViewItem()
44+
{
45+
AppName = kvp.Key,
46+
AppDuration = kvp.Value.Duration.ToString(),
47+
AppIcon = kvp.Value.Icon
48+
};
49+
result.Add(tmp);
50+
}
51+
52+
return result;
53+
});
4954
}
5055

5156
/// <summary>
5257
/// 打印表格
5358
/// </summary>
5459
/// <param name="mies"></param>
55-
private SeriesCollection PrintOverviewChart(List<MyTimeDuration> tmp)
60+
private Task<SeriesCollection> PrintOverviewChart(List<MyTimeDuration> tmp)
5661
{
57-
SeriesCollection result = new SeriesCollection { };
58-
Dictionary<string, ChartValues<double>> tmpdic = new Dictionary<string, ChartValues<double>>();
59-
foreach (MyTimeDuration duration in tmp)
62+
return Task.Run(() =>
6063
{
61-
string appName = duration.TimeDurationTask.ApplicationName;
62-
if (string.IsNullOrEmpty(appName) || appName == "Frogy") continue;
64+
SeriesCollection result = new SeriesCollection { };
65+
Dictionary<string, ChartValues<double>> tmpdic = new Dictionary<string, ChartValues<double>>();
66+
foreach (MyTimeDuration duration in tmp)
67+
{
68+
string appName = duration.TimeDurationTask.ApplicationName;
69+
if (string.IsNullOrEmpty(appName) || appName == "Frogy") continue;
6370

64-
if (!tmpdic.ContainsKey(appName)) tmpdic.Add(appName,
65-
new ChartValues<double> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
71+
if (!tmpdic.ContainsKey(appName)) tmpdic.Add(appName,
72+
new ChartValues<double> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
6673

67-
TimeSpan start = duration.StartTime;
68-
TimeSpan stop = duration.StopTime;
69-
TimeSpan spent = duration.Duration;
74+
TimeSpan start = duration.StartTime;
75+
TimeSpan stop = duration.StopTime;
76+
TimeSpan spent = duration.Duration;
7077

71-
if (start.Hours == stop.Hours)
72-
tmpdic[appName][start.Hours] += Math.Round(spent.TotalMinutes, 2);
73-
else
74-
{
75-
if (stop.Hours - start.Hours == 1)
76-
{
77-
tmpdic[appName][start.Hours] += Math.Round((59.59d - start.Minutes), 2);
78-
tmpdic[appName][stop.Hours] += stop.Minutes;
79-
}
78+
if (start.Hours == stop.Hours)
79+
tmpdic[appName][start.Hours] += Math.Round(spent.TotalMinutes, 2);
8080
else
8181
{
82-
if (stop.Hours - start.Hours > 1)
82+
if (stop.Hours - start.Hours == 1)
8383
{
84-
int tmpint = stop.Hours - start.Hours;
85-
8684
tmpdic[appName][start.Hours] += Math.Round((59.59d - start.Minutes), 2);
8785
tmpdic[appName][stop.Hours] += stop.Minutes;
86+
}
87+
else
88+
{
89+
if (stop.Hours - start.Hours > 1)
90+
{
91+
int tmpint = stop.Hours - start.Hours;
8892

89-
for (int i = 1; i <= tmpint; i++)
90-
tmpdic[appName][start.Hours + i] = 59.59d;
93+
tmpdic[appName][start.Hours] += Math.Round((59.59d - start.Minutes), 2);
94+
tmpdic[appName][stop.Hours] += stop.Minutes;
95+
96+
for (int i = 1; i < tmpint; i++)
97+
tmpdic[appName][start.Hours + i] = 59.59d;
98+
}
9199
}
92100
}
93101
}
94-
}
95102

96-
Application.Current.Dispatcher.Invoke(delegate
97-
{
103+
ChartValues<double> sumTime = new ChartValues<double> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
98104
foreach (string key in tmpdic.Keys)
99105
{
100-
result.Add(new StackedColumnSeries
106+
for(int i = 0; i < tmpdic[key].Count(); i++)
101107
{
102-
Values = tmpdic[key],
103-
DataLabels = false,
104-
Title = key,
105-
IsHitTestVisible = false
106-
});
108+
sumTime[i] += tmpdic[key][i];
109+
}
107110
}
108-
});
109111

110-
return result;
112+
foreach (string key in tmpdic.Keys)
113+
{
114+
for(int j = 0; j < tmpdic[key].Count(); j++)
115+
{
116+
if (tmpdic[key][j] / sumTime[j] < 0.01)
117+
{
118+
tmpdic[key][j] = 0;
119+
}
120+
}
121+
}
122+
123+
Application.Current.Dispatcher.Invoke(delegate
124+
{
125+
foreach (string key in tmpdic.Keys)
126+
{
127+
result.Add(new StackedColumnSeries
128+
{
129+
Values = tmpdic[key],
130+
DataLabels = false,
131+
Title = key,
132+
IsHitTestVisible = false
133+
});
134+
}
135+
});
136+
137+
return result;
138+
});
111139
}
112140

113141
/// <summary>
114142
/// 打印SummaryView
115143
/// </summary>
116144
/// <returns></returns>
117-
private List<DetailViewItem> PrintSummaryView(List<MyTimeDuration> durations)
145+
private Task<List<DetailViewItem>> PrintSummaryView(List<MyTimeDuration> durations)
118146
{
119-
List<DetailViewItem> result = new List<DetailViewItem>();
120-
121-
foreach (MyTimeDuration timeSpan in durations)
147+
return Task.Run(() =>
122148
{
123-
DetailViewItem tmp = new DetailViewItem()
149+
List<DetailViewItem> result = new List<DetailViewItem>();
150+
151+
foreach (MyTimeDuration timeSpan in durations)
124152
{
125-
StartTime = timeSpan.StartTime.ToString(),
126-
StopTime = timeSpan.StopTime.ToString(),
127-
AppDuration = timeSpan.Duration.ToString(),
128-
AppIcon = MyDataHelper.BitmapToBitmapImage(MyDataHelper.Base64StringToImage(timeSpan.TimeDurationTask.ApplicationIcon_Base64)),
129-
AppName = timeSpan.TimeDurationTask.ApplicationName,
130-
WindowTitle = timeSpan.TimeDurationTask.ApplicationTitle,
131-
SystemState = timeSpan.TimeDurationTask.ComputerStatus.ToString()
132-
};
133-
result.Add(tmp);
134-
}
135-
return result;
153+
DetailViewItem tmp = new DetailViewItem()
154+
{
155+
StartTime = timeSpan.StartTime.ToString(),
156+
StopTime = timeSpan.StopTime.ToString(),
157+
AppDuration = timeSpan.Duration.ToString(),
158+
AppIcon = MyDataHelper.BitmapToBitmapImage(MyDataHelper.Base64StringToImage(timeSpan.TimeDurationTask.ApplicationIcon_Base64)),
159+
AppName = timeSpan.TimeDurationTask.ApplicationName,
160+
WindowTitle = timeSpan.TimeDurationTask.ApplicationTitle,
161+
SystemState = timeSpan.TimeDurationTask.ComputerStatus.ToString()
162+
};
163+
result.Add(tmp);
164+
}
165+
return result;
166+
});
136167
}
137168

138169
public OverViewViewModel()
@@ -149,37 +180,46 @@ public OverViewViewModel()
149180

150181
private async void Update()
151182
{
183+
DateChangeable = false;
184+
Loading = Visibility.Visible;
185+
152186
((App)Application.Current).appData.Load(displayDate);
153187
MyDay today = ((App)Application.Current).appData.AllDays[displayDate];
154188

155-
await Task.Run(() =>
156-
{
157-
Overview = PrintOverview(today.GetOverView());
189+
var a = await PrintOverview(today.GetOverView());
190+
var b = await PrintOverviewChart(today.GetTimeline());
191+
#if DEBUG
192+
var c = await PrintSummaryView(today.GetTimeline());
193+
#endif
158194

159-
#if DEBUG
160-
DetailView = PrintSummaryView(today.GetTimeline());
161-
#endif
162-
});
195+
Overview.Clear();
196+
DetailView.Clear();
197+
OverviewChart = b;
163198

164-
OverviewChart.Clear();
165-
await Task.Run(() =>
199+
foreach (OverViewItem d in a)
166200
{
167-
SeriesCollection OverviewChart_tmp = PrintOverviewChart(today.GetTimeline());
168-
foreach (StackedColumnSeries i in OverviewChart_tmp)
169-
{
170-
OverviewChart.Add(i);
171-
//Thread.Sleep(20);
172-
}
173-
});
201+
Overview.Add(d);
202+
MyDeviceHelper.DoEvents();
203+
}
174204

205+
#if DEBUG
206+
foreach (DetailViewItem f in c)
207+
{
208+
DetailView.Add(f);
209+
MyDeviceHelper.DoEvents();
210+
}
211+
#endif
175212
UpdateTime = LanguageHelper.InquireLocalizedWord("General_LastUpdate") + DateTime.Now.ToString("H:mm");
213+
214+
DateChangeable = true;
215+
Loading = Visibility.Hidden;
176216
}
177217

178218
/// <summary>
179219
/// 详细数据
180220
/// </summary>
181-
private List<DetailViewItem> detailView;
182-
public List<DetailViewItem> DetailView
221+
private ObservableCollection<DetailViewItem> detailView = new ObservableCollection<DetailViewItem>();
222+
public ObservableCollection<DetailViewItem> DetailView
183223
{
184224
get
185225
{
@@ -195,8 +235,8 @@ public List<DetailViewItem> DetailView
195235
/// <summary>
196236
/// 整体视图
197237
/// </summary>
198-
private List<OverViewItem> overview;
199-
public List<OverViewItem> Overview
238+
private ObservableCollection<OverViewItem> overview = new ObservableCollection<OverViewItem>();
239+
public ObservableCollection<OverViewItem> Overview
200240
{
201241
get
202242
{
@@ -241,6 +281,34 @@ public string UpdateTime
241281
}
242282
}
243283

284+
private bool dateChangeable = true;
285+
public bool DateChangeable
286+
{
287+
get
288+
{
289+
return dateChangeable;
290+
}
291+
set
292+
{
293+
dateChangeable = value;
294+
OnPropertyChanged();
295+
}
296+
}
297+
298+
private Visibility loading = Visibility.Hidden;
299+
public Visibility Loading
300+
{
301+
get
302+
{
303+
return loading;
304+
}
305+
set
306+
{
307+
loading = value;
308+
OnPropertyChanged();
309+
}
310+
}
311+
244312
#region 表格
245313
/// <summary>
246314
/// 表格

0 commit comments

Comments
 (0)