跳到主要内容

github/tai

公开

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

浏览文件

DataPageVM.cs

287 行 · 9.7 KB历史下载
1using Core.Librarys;
2using Core.Librarys.Image;
3using Core.Models;
4using Core.Servicers.Interfaces;
5using System;
6using System.Collections.Generic;
7using System.Diagnostics;
8using System.Drawing;
9using System.Linq;
10using System.Text;
11using System.Threading.Tasks;
12using System.Windows.Media.Imaging;
13using UI.Controls;
14using UI.Controls.Charts.Model;
15using UI.Models;
16using UI.Servicers;
17using UI.Views;
18using static UI.Controls.SettingPanel.SettingPanel;
19
20namespace UI.ViewModels
21{
22 public class DataPageVM : DataPageModel
23 {
24 public Command ToDetailCommand { get; set; }
25 private readonly IData data;
26 private readonly MainViewModel main;
27 private readonly IAppContextMenuServicer appContextMenuServicer;
28 private readonly IAppConfig appConfig;
29 private readonly IWebData _webData;
30 private readonly IWebSiteContextMenuServicer _webSiteContextMenu;
31
32 public DataPageVM(IData data, MainViewModel main, IAppContextMenuServicer appContextMenuServicer, IAppConfig appConfig, IWebData webData, IWebSiteContextMenuServicer webSiteContextMenu)
33 {
34 this.data = data;
35 this.main = main;
36 this.appContextMenuServicer = appContextMenuServicer;
37 this.appConfig = appConfig;
38 _webData = webData;
39 _webSiteContextMenu = webSiteContextMenu;
40
41 ToDetailCommand = new Command(new Action<object>(OnTodetailCommand));
42
43 Init();
44 }
45
46 public override void Dispose()
47 {
48 PropertyChanged -= DataPageVM_PropertyChanged;
49
50 base.Dispose();
51 }
52 private void Init()
53 {
54 //LoadData(DateTime.Now.Date);
55 PropertyChanged += DataPageVM_PropertyChanged;
56
57 TabbarData = new System.Collections.ObjectModel.ObservableCollection<string>()
58 {
59 "按天","按月","按年"
60 };
61
62 TabbarSelectedIndex = 0;
63
64 AppContextMenu = appContextMenuServicer.GetContextMenu();
65 }
66
67 private void OnTodetailCommand(object obj)
68 {
69 var data = obj as ChartsDataModel;
70
71 if (data != null)
72 {
73 var model = data.Data as DailyLogModel;
74 if (model != null && model.AppModel != null)
75 {
76 main.Data = model.AppModel;
77 main.Uri = nameof(DetailPage);
78 }
79 else
80 {
81 main.Data = data.Data as Core.Models.Db.WebSiteModel;
82 main.Uri = nameof(WebSiteDetailPage);
83 }
84 }
85 }
86
87 private void DataPageVM_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
88 {
89
90 if (e.PropertyName == nameof(DayDate))
91 {
92 LoadData(DayDate);
93 }
94 else if (e.PropertyName == nameof(MonthDate))
95 {
96 LoadData(MonthDate);
97 }
98 else if (e.PropertyName == nameof(YearDate))
99 {
100 LoadData(YearDate);
101 }
102 else if (e.PropertyName == nameof(TabbarSelectedIndex))
103 {
104 if (TabbarSelectedIndex == 0)
105 {
106 if (DayDate == DateTime.MinValue)
107 {
108 DayDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
109 }
110
111 }
112 else if (TabbarSelectedIndex == 1)
113 {
114 if (MonthDate == DateTime.MinValue)
115 {
116 MonthDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
117 }
118 }
119 else if (TabbarSelectedIndex == 2)
120 {
121 if (YearDate == DateTime.MinValue)
122 {
123 YearDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
124 }
125 }
126 }
127 else if (e.PropertyName == nameof(ShowType))
128 {
129 LoadData(DayDate, 0);
130 LoadData(MonthDate, 1);
131 LoadData(YearDate, 2);
132 if (ShowType.Id == 0)
133 {
134 AppContextMenu = appContextMenuServicer.GetContextMenu();
135 }
136 else
137 {
138 AppContextMenu = _webSiteContextMenu.GetContextMenu();
139 }
140 }
141 }
142
143
144
145 #region 读取数据
146
147
148
149
150 private async void LoadData(DateTime date, int dataType_ = -1)
151 {
152 await Task.Run(() =>
153 {
154 DateTime dateStart = date, dateEnd = date;
155
156 dataType_ = dataType_ == -1 ? TabbarSelectedIndex : dataType_;
157
158 if (dataType_ == 1)
159 {
160 dateStart = new DateTime(date.Year, date.Month, 1);
161 dateEnd = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
162 }
163 else if (dataType_ == 0)
164 {
165 dateStart = new DateTime(date.Year, date.Month, date.Day, 0, 0, 0);
166 dateEnd = new DateTime(date.Year, date.Month, date.Day, 23, 59, 59);
167 }
168 else if (dataType_ == 2)
169 {
170 dateStart = new DateTime(date.Year, 1, 1, 0, 0, 0);
171 dateEnd = new DateTime(date.Year, 12, DateTime.DaysInMonth(date.Year, 12), 23, 59, 59);
172 }
173
174 List<ChartsDataModel> chartData = new List<ChartsDataModel>();
175 if (ShowType.Id == 0)
176 {
177 var result = data.GetDateRangelogList(dateStart, dateEnd);
178 chartData = MapToChartsData(result);
179 }
180 else
181 {
182 var result = _webData.GetWebSiteLogList(dateStart, dateEnd);
183 chartData = MapToChartsWebData(result);
184 }
185
186
187 if (dataType_ == 0)
188 {
189 Data = chartData;
190 }
191 else if (dataType_ == 1)
192 {
193 MonthData = chartData;
194 }
195 else
196 {
197 YearData = chartData;
198 }
199
200 });
201 }
202
203 #region 处理数据
204 private List<ChartsDataModel> MapToChartsData(IEnumerable<Core.Models.DailyLogModel> list)
205 {
206 var resData = new List<ChartsDataModel>();
207 try
208 {
209 var config = appConfig.GetConfig();
210
211 foreach (var item in list)
212 {
213 var bindModel = new ChartsDataModel();
214 bindModel.Data = item;
215 bindModel.Name = !string.IsNullOrEmpty(item.AppModel?.Alias) ? item.AppModel.Alias : string.IsNullOrEmpty(item.AppModel?.Description) ? item.AppModel.Name : item.AppModel.Description;
216 bindModel.Value = item.Time;
217 bindModel.Tag = Time.ToString(item.Time);
218 bindModel.PopupText = item.AppModel?.File;
219 bindModel.Icon = item.AppModel?.IconFile;
220 bindModel.BadgeList = new List<ChartBadgeModel>();
221 if (item.AppModel.Category != null)
222 {
223 bindModel.BadgeList.Add(new ChartBadgeModel()
224 {
225 Name = item.AppModel.Category.Name,
226 Color = item.AppModel.Category.Color,
227 Type = ChartBadgeType.Category
228 });
229 }
230 if (config.Behavior.IgnoreProcessList.Contains(item.AppModel.Name))
231 {
232 bindModel.BadgeList.Add(ChartBadgeModel.IgnoreBadge);
233 }
234 resData.Add(bindModel);
235 }
236 }
237 catch (Exception e)
238 {
239 Logger.Error(e.ToString());
240 }
241
242 return resData;
243 }
244
245 private List<ChartsDataModel> MapToChartsWebData(IEnumerable<Core.Models.Db.WebSiteModel> list)
246 {
247 var resData = new List<ChartsDataModel>();
248 try
249 {
250 var config = appConfig.GetConfig();
251
252 foreach (var item in list)
253 {
254 var bindModel = new ChartsDataModel();
255 bindModel.Data = item;
256 bindModel.Name = !string.IsNullOrEmpty(item.Alias) ? item.Alias : item.Title;
257 bindModel.Value = item.Duration;
258 bindModel.Tag = Time.ToString(item.Duration);
259 bindModel.PopupText = item.Domain;
260 bindModel.Icon = item.IconFile;
261 bindModel.BadgeList = new List<ChartBadgeModel>();
262 if (item.Category != null)
263 {
264 bindModel.BadgeList.Add(new ChartBadgeModel()
265 {
266 Name = item.Category.Name,
267 Color = item.Category.Color,
268 Type = ChartBadgeType.Category
269 });
270 }
271 if (config.Behavior.IgnoreURLList.Contains(item.Domain))
272 {
273 bindModel.BadgeList.Add(ChartBadgeModel.IgnoreBadge);
274 }
275 resData.Add(bindModel);
276 }
277 }
278 catch (Exception e)
279 {
280 Logger.Error(e.ToString());
281 }
282 return resData;
283 }
284 #endregion
285 #endregion
286 }
287}