| 1 | using Core.Librarys; |
| 2 | using Core.Librarys.Image; |
| 3 | using Core.Models; |
| 4 | using Core.Models.Db; |
| 5 | using Core.Servicers.Interfaces; |
| 6 | using System; |
| 7 | using System.Collections.Generic; |
| 8 | using System.Diagnostics; |
| 9 | using System.Drawing; |
| 10 | using System.Linq; |
| 11 | using System.Text; |
| 12 | using System.Threading.Tasks; |
| 13 | using System.Windows; |
| 14 | using System.Windows.Media.Imaging; |
| 15 | using UI.Controls; |
| 16 | using UI.Controls.Charts.Model; |
| 17 | using UI.Controls.Select; |
| 18 | using UI.Models; |
| 19 | using UI.Servicers; |
| 20 | using UI.Views; |
| 21 | |
| 22 | namespace 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 | |
| 168 | |
| 169 | |
| 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 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 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 | } |