跳到主要内容

github/tai

公开

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

浏览文件

CategoryAppListPageVM.cs

248 行 · 7.2 KB历史下载
1using Core.Models;
2using Core.Servicers.Interfaces;
3using System;
4using System.Collections.Generic;
5using System.Diagnostics;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9using UI.Controls;
10using UI.Models;
11using UI.Models.Category;
12using UI.Models.CategoryAppList;
13using UI.Views;
14
15namespace UI.ViewModels
16{
17 public class CategoryAppListPageVM : CategoryAppListPageModel
18 {
19 private readonly IAppData appData;
20 private readonly MainViewModel mainVM;
21 private List<ChooseAppModel> appList;
22 public Command ShowChooseCommand { get; set; }
23 public Command ChoosedCommand { get; set; }
24 public Command GotoDetailCommand { get; set; }
25 public Command SearchCommand { get; set; }
26 public Command ChooseCloseCommand { get; set; }
27 public Command DelCommand { get; set; }
28 public CategoryAppListPageVM(IAppData appData, MainViewModel mainVM)
29 {
30 this.appData = appData;
31 this.mainVM = mainVM;
32
33
34 ShowChooseCommand = new Command(new Action<object>(OnShowChoose));
35 ChoosedCommand = new Command(new Action<object>(OnChoosed));
36 GotoDetailCommand = new Command(new Action<object>(OnGotoDetail));
37 SearchCommand = new Command(new Action<object>(OnSearch));
38 ChooseCloseCommand = new Command(new Action<object>(OnChooseClose));
39 DelCommand = new Command(new Action<object>(OnDel));
40
41 LoadData();
42
43 }
44 public override void Dispose()
45 {
46 base.Dispose();
47 Data = null;
48 AppList = null;
49 }
50 private void OnDel(object obj)
51 {
52 if (SelectedItem != null)
53 {
54 var list = Data.ToList();
55
56 list.Remove(SelectedItem);
57
58 var app = appData.GetApp(SelectedItem.ID);
59 if (app != null)
60 {
61 app.CategoryID = 0;
62 app.Category = null;
63 appData.UpdateApp(app);
64 }
65
66 Data = list;
67 }
68 }
69
70 private void OnChooseClose(object obj)
71 {
72 ChooseVisibility = System.Windows.Visibility.Collapsed;
73 SearchInput = "";
74 }
75
76 private void OnSearch(object obj)
77 {
78 string keyword = obj.ToString();
79
80 if (keyword == "vscode")
81 {
82 keyword = "Visual Studio Code";
83 }
84 else if (keyword == "ps")
85 {
86 keyword = "Photoshop";
87 }
88 Search(keyword);
89 }
90
91
92
93 private void OnGotoDetail(object obj)
94 {
95 if (SelectedItem != null)
96 {
97 mainVM.Data = SelectedItem;
98 mainVM.Uri = nameof(DetailPage);
99 }
100 }
101
102 private void OnChoosed(object obj)
103 {
104 ChooseVisibility = System.Windows.Visibility.Collapsed;
105
106 SearchInput = "";
107
108 var data = new List<AppModel>();
109
110 foreach (var item in AppList)
111 {
112
113 var app = appData.GetApp(item.App.ID);
114
115 if (item.IsChoosed)
116 {
117 // 处理选中
118 if (app.CategoryID != Category.Data.ID)
119 {
120 app.CategoryID = Category.Data.ID;
121 app.Category = Category.Data;
122 appData.UpdateApp(app);
123 }
124
125 data.Add(app);
126 }
127 else
128 {
129 // 处理取选
130 bool isHas = Data.Where(m => m.ID == item.App.ID).Any();
131 if (isHas)
132 {
133 app.CategoryID = 0;
134 app.Category = null;
135 appData.UpdateApp(app);
136 }
137 }
138 }
139 Data = data;
140
141 //LoadData();
142 }
143
144 private void OnShowChoose(object obj)
145 {
146 ChooseVisibility = System.Windows.Visibility.Visible;
147
148
149 LoadApps();
150
151 }
152
153
154 private void LoadData()
155 {
156 Task.Run(() =>
157 {
158 Category = mainVM.Data as UI.Models.Category.CategoryModel;
159 Data = appData.GetAppsByCategoryID(Category.Data.ID);
160
161 //var appList = new List<ChooseAppModel>();
162 //foreach (var item in appData.GetAllApps())
163 //{
164 // var app = new ChooseAppModel();
165 // app.App = item;
166 // app.IsChoosed = item.CategoryID == Category.Data.ID;
167 // app.Value.Name = item.Name;
168 // app.Value.Img = item.IconFile;
169
170 // appList.Add(app);
171 //}
172
173 //AppList = appList;
174 });
175 }
176
177 private void LoadApps()
178 {
179 Task.Run(() =>
180 {
181 appList = new List<ChooseAppModel>();
182
183 if (Category == null)
184 {
185 return;
186 }
187 foreach (var item in appData.GetAllApps())
188 {
189 var app = new ChooseAppModel();
190 app.App = item;
191 app.IsChoosed = item.CategoryID == Category.Data.ID;
192 app.Value.Name = String.IsNullOrEmpty(item.Description) ? item.Name : item.Description;
193 app.Value.Img = item.IconFile;
194
195 if (app.IsChoosed || item.CategoryID == 0)
196 {
197 appList.Add(app);
198 }
199 }
200 appList = appList.OrderBy(m => m.App.Description).ToList();
201
202 AppList = appList;
203 });
204 }
205
206 private void Search(string keyword)
207 {
208 Task.Run(() =>
209 {
210 if (!string.IsNullOrEmpty(keyword))
211 {
212 Debug.WriteLine(keyword);
213 keyword = keyword.ToLower();
214
215 //var list = appList.Where(m => m.App.Description.ToLower().IndexOf(keyword) != -1 || m.App.Name.ToLower().IndexOf(keyword) != -1).ToList();
216 var list = AppList.ToList();
217
218 foreach (var item in list)
219 {
220 item.Visibility = item.App.Description != null && item.App.Description.ToLower().IndexOf(keyword) != -1 || item.App.Name != null && item.App.Name.ToLower().IndexOf(keyword) != -1 ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
221 }
222
223 return list;
224
225 }
226 else
227 {
228 var list = AppList.ToList();
229
230 foreach (var item in list)
231 {
232 item.Visibility = System.Windows.Visibility.Visible;
233 }
234
235 return list;
236
237 }
238 })
239
240 .ContinueWith(task =>
241 {
242 AppList = task.Result;
243
244 }, TaskScheduler.FromCurrentSynchronizationContext());
245
246 }
247 }
248}