跳到主要内容

github/tai

公开

Tai · Windows 软件与网站使用时长统计。上游 Planshit/Tai 的源码镜像,非灵栈原创;MIT 许可。来源:https://github.com/Planshit/Tai 。本次为手动同步,暂不提供安装包。

浏览文件

IndexPageVM.cs

307 行 · 9.6 KB历史下载
1using Core.Librarys;
2using Core.Librarys.Image;
3using Core.Models;
4using Core.Models.Db;
5using Core.Servicers.Interfaces;
6using System;
7using System.Collections.Generic;
8using System.Diagnostics;
9using System.Drawing;
10using System.Linq;
11using System.Text;
12using System.Threading.Tasks;
13using System.Windows;
14using System.Windows.Media.Imaging;
15using UI.Controls;
16using UI.Controls.Charts.Model;
17using UI.Controls.Select;
18using UI.Models;
19using UI.Servicers;
20using UI.Views;
21
22namespace UI.ViewModels
23{
24 public class IndexPageVM : IndexPageModel
25 {
26 public Command ToDetailCommand { get; set; }
27 public Command RefreshCommand { get; set; }
28 private readonly IData data;
29 private readonly MainViewModel main;
30 private readonly IMain mainServicer;
31 private readonly IAppContextMenuServicer appContextMenuServicer;
32 private readonly IInputServicer inputServicer;
33 private readonly IAppConfig appConfig;
34 private readonly IWebData _webData;
35 private readonly IWebSiteContextMenuServicer _webSiteContextMenu;
36 public List<SelectItemModel> MoreTypeOptions { get; set; }
37 public IndexPageVM(
38 IData data,
39 MainViewModel main,
40 IMain mainServicer,
41 IAppContextMenuServicer appContextMenuServicer,
42 IInputServicer inputServicer,
43 IAppConfig appConfig,
44 IWebData webData_,
45 IWebSiteContextMenuServicer webSiteContext_)
46 {
47 this.data = data;
48 this.main = main;
49 this.mainServicer = mainServicer;
50 this.appContextMenuServicer = appContextMenuServicer;
51 this.inputServicer = inputServicer;
52 this.appConfig = appConfig;
53 _webData = webData_;
54 _webSiteContextMenu = webSiteContext_;
55
56 ToDetailCommand = new Command(new Action<object>(OnTodetailCommand));
57 RefreshCommand = new Command(new Action<object>(OnRefreshCommand));
58
59 Init();
60 }
61
62 private void OnRefreshCommand(object obj)
63 {
64 LoadData();
65 }
66
67 public override void Dispose()
68 {
69 PropertyChanged -= IndexPageVM_PropertyChanged;
70 base.Dispose();
71 }
72
73 private void Init()
74 {
75 TabbarData = new System.Collections.ObjectModel.ObservableCollection<string>()
76 {
77 "今日","本周"
78 };
79
80 TabbarSelectedIndex = 0;
81 AppContextMenu = appContextMenuServicer.GetContextMenu();
82 WebSiteContextMenu = _webSiteContextMenu.GetContextMenu();
83 PropertyChanged += IndexPageVM_PropertyChanged;
84 inputServicer.OnKeyUpInput += InputServicer_OnKeyUpInput;
85
86 LoadData();
87
88 MoreTypeOptions = new List<SelectItemModel>()
89 {
90 new SelectItemModel()
91 {
92 Id=0,
93 Name="应用"
94 },
95 new SelectItemModel()
96 {
97 Id=1,
98 Name="网站"
99 }
100 };
101 MoreType = MoreTypeOptions[0];
102 }
103
104 private void InputServicer_OnKeyUpInput(object sender, System.Windows.Forms.Keys key)
105 {
106 if (key == System.Windows.Forms.Keys.F5)
107 {
108 // 重新加载数据
109 LoadData();
110 }
111 }
112
113 private void OnTodetailCommand(object obj)
114 {
115 var data = obj as ChartsDataModel;
116
117 if (data != null)
118 {
119 if (data.Data is DailyLogModel)
120 {
121 var model = data.Data as DailyLogModel;
122 if (model != null && model.AppModel != null)
123 {
124 main.Data = model.AppModel;
125 main.Uri = nameof(DetailPage);
126 }
127 }
128 else if (data.Data is WebSiteModel)
129 {
130 main.Data = data.Data;
131 main.Uri = nameof(WebSiteDetailPage);
132 }
133
134 }
135 }
136
137 private void IndexPageVM_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
138 {
139 if (e.PropertyName == nameof(TabbarSelectedIndex))
140 {
141 LoadData();
142 }
143 }
144
145 #region 读取数据
146
147 private void LoadData()
148 {
149 if (IsLoading)
150 {
151 return;
152 }
153
154 FrequentUseNum = appConfig.GetConfig().General.IndexPageFrequentUseNum + 1;
155 MoreNum = appConfig.GetConfig().General.IndexPageMoreNum + 1;
156
157 if (TabbarSelectedIndex == 0)
158 {
159 LoadTodayData();
160 LoadTodayMoreData();
161 }
162 else if (TabbarSelectedIndex == 1)
163 {
164 LoadThisWeekData();
165 LoadThisWeekMoreData();
166 }
167 //else
168 //{
169 // LoadLastWeekData();
170 //}
171 }
172
173 #region 今日数据
174 private void LoadTodayData()
175 {
176 IsLoading = true;
177
178 Task.Run(() =>
179 {
180 var list = data.GetDateRangelogList(DateTime.Now.Date, DateTime.Now.Date);
181 var res = MapToChartsData(list);
182 var topWebList = _webData.GetDateRangeWebSiteList(DateTime.Now, DateTime.Now, FrequentUseNum);
183
184 IsLoading = false;
185 WeekData = res;
186 WebFrequentUseData = MapToChartsData(topWebList);
187 });
188 }
189
190 private void LoadTodayMoreData()
191 {
192 IsLoading = true;
193 Task.Run(() =>
194 {
195 var appMoreData = data.GetDateRangelogList(DateTime.Now.Date, DateTime.Now.Date, MoreNum, FrequentUseNum);
196 var webMoreData = _webData.GetDateRangeWebSiteList(DateTime.Now.Date, DateTime.Now.Date, MoreNum, FrequentUseNum);
197
198 IsLoading = false;
199 AppMoreData = MapToChartsData(appMoreData);
200 WebMoreData = MapToChartsData(webMoreData);
201 });
202 }
203 #endregion
204
205 #region 本周数据
206 private void LoadThisWeekData()
207 {
208 IsLoading = true;
209
210 Task.Run(() =>
211 {
212 var list = data.GetThisWeeklogList();
213 var res = MapToChartsData(list);
214
215 var week = Time.GetThisWeekDate();
216 var topWebList = _webData.GetDateRangeWebSiteList(week[0], week[1], FrequentUseNum);
217
218 IsLoading = false;
219 WeekData = res;
220 WebFrequentUseData = MapToChartsData(topWebList);
221 });
222
223
224
225 }
226 private void LoadThisWeekMoreData()
227 {
228 IsLoading = true;
229 Task.Run(() =>
230 {
231 var week = Time.GetThisWeekDate();
232 var appMoreData = data.GetDateRangelogList(week[0], week[1], MoreNum, FrequentUseNum);
233 var webMoreData = _webData.GetDateRangeWebSiteList(week[0], week[1], MoreNum, FrequentUseNum);
234
235 IsLoading = false;
236 AppMoreData = MapToChartsData(appMoreData);
237 WebMoreData = MapToChartsData(webMoreData);
238 });
239 }
240 #endregion
241
242 #region 上周数据
243 private void LoadLastWeekData()
244 {
245 //IsLoading = true;
246 //var task = Task.Run(() =>
247 // {
248 // var list = data.GetLastWeeklogList();
249 // var res = MapToChartsData(list);
250 // IsLoading = false;
251 // return res;
252 // });
253 //WeekData = await task;
254 IsLoading = true;
255 Task.Run(() =>
256 {
257 var list = data.GetLastWeeklogList();
258 var res = MapToChartsData(list);
259 IsLoading = false;
260 WeekData = res;
261 });
262
263 }
264 #endregion
265
266 #region 处理数据
267 private List<ChartsDataModel> MapToChartsData(IEnumerable<Core.Models.DailyLogModel> list)
268 {
269 var resData = new List<ChartsDataModel>();
270
271 foreach (var item in list)
272 {
273 var bindModel = new ChartsDataModel();
274 bindModel.Data = item;
275 bindModel.Name = !string.IsNullOrEmpty(item.AppModel?.Alias) ? item.AppModel.Alias : string.IsNullOrEmpty(item.AppModel?.Description) ? item.AppModel.Name : item.AppModel.Description;
276 bindModel.Value = item.Time;
277 bindModel.Tag = Time.ToString(item.Time);
278 bindModel.PopupText = item.AppModel?.File;
279 bindModel.Icon = item.AppModel?.IconFile;
280 bindModel.DateTime = item.Date;
281 resData.Add(bindModel);
282 }
283
284 return resData;
285 }
286 private List<ChartsDataModel> MapToChartsData(IEnumerable<Core.Models.Db.WebSiteModel> list)
287 {
288 var resData = new List<ChartsDataModel>();
289
290 foreach (var item in list)
291 {
292 var bindModel = new ChartsDataModel();
293 bindModel.Data = item;
294 bindModel.Name = !string.IsNullOrEmpty(item.Alias) ? item.Alias : item.Title;
295 bindModel.Value = item.Duration;
296 bindModel.Tag = Time.ToString(item.Duration);
297 bindModel.PopupText = item.Domain;
298 bindModel.Icon = item.IconFile;
299 resData.Add(bindModel);
300 }
301
302 return resData;
303 }
304 #endregion
305 #endregion
306 }
307}