| 1 | using Core.Librarys; |
| 2 | using Core.Servicers.Instances; |
| 3 | using Core.Servicers.Interfaces; |
| 4 | using Newtonsoft.Json; |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
| 7 | using System.Diagnostics; |
| 8 | using System.IO; |
| 9 | using System.Linq; |
| 10 | using System.Text; |
| 11 | using System.Threading.Tasks; |
| 12 | using System.Windows.Forms; |
| 13 | using UI.Controls; |
| 14 | using UI.Controls.Select; |
| 15 | using UI.Models; |
| 16 | using UI.Models.Category; |
| 17 | using UI.Servicers; |
| 18 | using UI.Views; |
| 19 | |
| 20 | namespace UI.ViewModels |
| 21 | { |
| 22 | public class CategoryPageVM : CategoryPageModel |
| 23 | { |
| 24 | private readonly ICategorys categorys; |
| 25 | private readonly MainViewModel mainVM; |
| 26 | private readonly IAppData appData; |
| 27 | private readonly IWebData _webData; |
| 28 | private readonly IUIServicer _uiServicer; |
| 29 | |
| 30 | public Command GotoListCommand { get; set; } |
| 31 | ///<summary> |
| 32 | /// |
| 33 | ///</summary> |
| 34 | public Command EditCommand { get; set; } |
| 35 | ///<summary> |
| 36 | /// |
| 37 | ///</summary> |
| 38 | public Command EditDoneCommand { get; set; } |
| 39 | ///<summary> |
| 40 | /// |
| 41 | ///</summary> |
| 42 | public Command EditCloseCommand { get; set; } |
| 43 | ///<summary> |
| 44 | /// |
| 45 | ///</summary> |
| 46 | public Command DelCommand { get; set; } |
| 47 | ///<summary> |
| 48 | /// |
| 49 | ///</summary> |
| 50 | public Command RefreshCommand { get; set; } |
| 51 | ///<summary> |
| 52 | /// |
| 53 | ///</summary> |
| 54 | public Command AddDirectoryCommand { get; set; } |
| 55 | ///<summary> |
| 56 | /// |
| 57 | ///</summary> |
| 58 | public Command DirectoriesCommand { get; set; } |
| 59 | |
| 60 | |
| 61 | public CategoryPageVM(ICategorys categorys, MainViewModel mainVM, IAppData appData, IWebData webData_, IUIServicer uIServicer_) |
| 62 | { |
| 63 | this.categorys = categorys; |
| 64 | this.mainVM = mainVM; |
| 65 | this.appData = appData; |
| 66 | _webData = webData_; |
| 67 | _uiServicer = uIServicer_; |
| 68 | |
| 69 | GotoListCommand = new Command(new Action<object>(OnGotoList)); |
| 70 | EditCommand = new Command(new Action<object>(OnEdit)); |
| 71 | EditDoneCommand = new Command(new Action<object>(OnEditDone)); |
| 72 | EditCloseCommand = new Command(new Action<object>(OnEditClose)); |
| 73 | DelCommand = new Command(new Action<object>(OnDel)); |
| 74 | RefreshCommand = new Command(new Action<object>(OnRefresh)); |
| 75 | AddDirectoryCommand = new Command(OnAddDirectory); |
| 76 | DirectoriesCommand = new Command(OnDirectoriesCommand); |
| 77 | |
| 78 | LoadData(); |
| 79 | |
| 80 | PropertyChanged += CategoryPageVM_PropertyChanged; |
| 81 | } |
| 82 | |
| 83 | private void OnRefresh(object obj) |
| 84 | { |
| 85 | LoadData(); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | private void OnDel(object obj) |
| 90 | { |
| 91 | if (ShowType.Id == 0) |
| 92 | { |
| 93 | DelAppCategory(); |
| 94 | } |
| 95 | else if (ShowType.Id == 1) |
| 96 | { |
| 97 | DelWebSiteCategory(); |
| 98 | } |
| 99 | |
| 100 | } |
| 101 | private void OnEditClose(object obj) |
| 102 | { |
| 103 | EditVisibility = System.Windows.Visibility.Collapsed; |
| 104 | } |
| 105 | private void OnEditDone(object obj) |
| 106 | { |
| 107 | if (ShowType.Id == 0) |
| 108 | { |
| 109 | EditAppCategoryAction(); |
| 110 | } |
| 111 | else if (ShowType.Id == 1) |
| 112 | { |
| 113 | EditWebSiteCategoryAction(); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | private void OnAddDirectory(object obj) |
| 118 | { |
| 119 | try |
| 120 | { |
| 121 | FolderBrowserDialog dialog = new FolderBrowserDialog(); |
| 122 | dialog.Description = "选择匹配目录"; |
| 123 | if (dialog.ShowDialog() == DialogResult.OK) |
| 124 | { |
| 125 | string path = dialog.SelectedPath; |
| 126 | if (path.Last() != '\\') |
| 127 | { |
| 128 | path += "\\"; |
| 129 | } |
| 130 | if (EditDirectories.Contains(path)) |
| 131 | { |
| 132 | mainVM.Toast("目录已存在", Controls.Window.ToastType.Error); |
| 133 | return; |
| 134 | } |
| 135 | EditDirectories.Add(path); |
| 136 | mainVM.Toast("已添加", Controls.Window.ToastType.Success); |
| 137 | } |
| 138 | } |
| 139 | catch (Exception ec) |
| 140 | { |
| 141 | Logger.Error(ec.ToString()); |
| 142 | mainVM.Toast("添加失败,请重试", Controls.Window.ToastType.Error); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | private void OnDirectoriesCommand(object obj) |
| 147 | { |
| 148 | string action = obj.ToString(); |
| 149 | switch (action) |
| 150 | { |
| 151 | case "remove": |
| 152 | if (string.IsNullOrEmpty(EditSelectedDirectory)) return; |
| 153 | |
| 154 | EditDirectories.Remove(EditSelectedDirectory); |
| 155 | break; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | #region 编辑校验 |
| 160 | ///<summary> |
| 161 | /// |
| 162 | ///</summary> |
| 163 | private bool IsEditVerify() |
| 164 | { |
| 165 | if (string.IsNullOrEmpty(EditName)) |
| 166 | { |
| 167 | IsEditError = true; |
| 168 | EditErrorText = "分类名称不能为空"; |
| 169 | return false; |
| 170 | } |
| 171 | if (string.IsNullOrEmpty(EditIconFile)) |
| 172 | { |
| 173 | mainVM.Toast("请选择一个分类图标", Controls.Window.ToastType.Error, Controls.Base.IconTypes.ImportantBadge12); |
| 174 | return false; |
| 175 | } |
| 176 | if (string.IsNullOrEmpty(EditColor)) |
| 177 | { |
| 178 | mainVM.Toast("请设置一个分类颜色", Controls.Window.ToastType.Error, Controls.Base.IconTypes.ImportantBadge12); |
| 179 | return false; |
| 180 | } |
| 181 | if (EditIconFile.IndexOf("pack://") == -1 && new FileInfo(EditIconFile).Length > 1000000) |
| 182 | { |
| 183 | mainVM.Toast("图标文件不能超过1MB", Controls.Window.ToastType.Error, Controls.Base.IconTypes.ImportantBadge12); |
| 184 | return false; |
| 185 | } |
| 186 | return true; |
| 187 | } |
| 188 | #endregion |
| 189 | |
| 190 | #region 应用分类操作 |
| 191 | #region 编辑应用分类 |
| 192 | private void EditAppCategoryAction() |
| 193 | { |
| 194 | try |
| 195 | { |
| 196 | if (!IsEditVerify()) return; |
| 197 | |
| 198 | string directoriesStr = JsonConvert.SerializeObject(EditDirectories.ToList()); |
| 199 | |
| 200 | if (IsCreate) |
| 201 | { |
| 202 | |
| 203 | |
| 204 | |
| 205 | if (Data.Where(m => m.Data.Name == EditName).Any()) |
| 206 | { |
| 207 | mainVM.Toast("分类名称已存在", Controls.Window.ToastType.Error, Controls.Base.IconTypes.ImportantBadge12); |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | mainVM.Toast("创建完成", Controls.Window.ToastType.Success); |
| 216 | |
| 217 | EditVisibility = System.Windows.Visibility.Collapsed; |
| 218 | |
| 219 | var res = categorys.Create(new Core.Models.CategoryModel() |
| 220 | { |
| 221 | Name = EditName, |
| 222 | IconFile = EditIconFile, |
| 223 | Color = EditColor, |
| 224 | IsDirectoryMath = EditIsDirectoryMath, |
| 225 | Directories = directoriesStr, |
| 226 | }); |
| 227 | |
| 228 | var item = new CategoryModel() |
| 229 | { |
| 230 | Data = res, |
| 231 | Count = 0 |
| 232 | }; |
| 233 | if (Data.Count == 0) |
| 234 | { |
| 235 | |
| 236 | var list = new System.Collections.ObjectModel.ObservableCollection<CategoryModel>(); |
| 237 | list.Add(item); |
| 238 | Data = list; |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | Data.Add(item); |
| 243 | } |
| 244 | |
| 245 | } |
| 246 | else |
| 247 | { |
| 248 | |
| 249 | |
| 250 | |
| 251 | if (Data.Where(m => m.Data.Name == EditName && m.Data.ID != SelectedAppCategoryItem.Data.ID).Any()) |
| 252 | { |
| 253 | mainVM.Toast("分类名称已存在", Controls.Window.ToastType.Error, Controls.Base.IconTypes.ImportantBadge12); |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | mainVM.Toast("已更新", Controls.Window.ToastType.Success); |
| 268 | var category = categorys.GetCategory(SelectedAppCategoryItem.Data.ID); |
| 269 | if (category != null) |
| 270 | { |
| 271 | category.Name = EditName; |
| 272 | category.IconFile = EditIconFile; |
| 273 | category.Color = EditColor; |
| 274 | category.IsDirectoryMath = EditIsDirectoryMath; |
| 275 | category.Directories = directoriesStr; |
| 276 | |
| 277 | categorys.Update(category); |
| 278 | } |
| 279 | |
| 280 | var item = Data.Where(m => m.Data.ID == SelectedAppCategoryItem.Data.ID).FirstOrDefault(); |
| 281 | |
| 282 | var changedItem = new CategoryModel() |
| 283 | { |
| 284 | Count = item.Count, |
| 285 | Data = new Core.Models.CategoryModel() |
| 286 | { |
| 287 | ID = item.Data.ID, |
| 288 | Name = EditName, |
| 289 | IconFile = EditIconFile, |
| 290 | Color = EditColor, |
| 291 | IsDirectoryMath = EditIsDirectoryMath, |
| 292 | Directories = directoriesStr |
| 293 | } |
| 294 | }; |
| 295 | |
| 296 | int editItemIndex = Data.IndexOf(item); |
| 297 | if (editItemIndex != -1) |
| 298 | { |
| 299 | Data[editItemIndex] = changedItem; |
| 300 | } |
| 301 | |
| 302 | EditVisibility = System.Windows.Visibility.Collapsed; |
| 303 | } |
| 304 | } |
| 305 | catch (Exception ec) |
| 306 | { |
| 307 | Logger.Error(ec.ToString()); |
| 308 | mainVM.Toast("操作失败,请重试", Controls.Window.ToastType.Error); |
| 309 | } |
| 310 | } |
| 311 | #endregion |
| 312 | |
| 313 | #region 删除分类 |
| 314 | private async void DelAppCategory() |
| 315 | { |
| 316 | if (SelectedAppCategoryItem == null) |
| 317 | { |
| 318 | return; |
| 319 | } |
| 320 | bool isConfirm = await _uiServicer.ShowConfirmDialogAsync("删除分类", "是否确认删除该分类?"); |
| 321 | if (isConfirm) |
| 322 | { |
| 323 | var category = categorys.GetCategory(SelectedAppCategoryItem.Data.ID); |
| 324 | if (category != null) |
| 325 | { |
| 326 | categorys.Delete(category); |
| 327 | var apps = appData.GetAppsByCategoryID(category.ID); |
| 328 | foreach (var app in apps) |
| 329 | { |
| 330 | app.CategoryID = 0; |
| 331 | app.Category = null; |
| 332 | appData.UpdateApp(app); |
| 333 | } |
| 334 | |
| 335 | |
| 336 | |
| 337 | Data.Remove(SelectedAppCategoryItem); |
| 338 | if (Data.Count == 0) |
| 339 | { |
| 340 | Data = new System.Collections.ObjectModel.ObservableCollection<CategoryModel>(); |
| 341 | } |
| 342 | mainVM.Toast("分类已删除", Controls.Window.ToastType.Success); |
| 343 | |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | #endregion |
| 348 | |
| 349 | #endregion |
| 350 | |
| 351 | #region 网站分类操作 |
| 352 | #region 编辑网站分类 |
| 353 | private void EditWebSiteCategoryAction() |
| 354 | { |
| 355 | if (!IsEditVerify()) return; |
| 356 | |
| 357 | if (IsCreate) |
| 358 | { |
| 359 | |
| 360 | |
| 361 | |
| 362 | if (WebCategoryData.Where(m => m.Data.Name == EditName).Any()) |
| 363 | { |
| 364 | mainVM.Toast("分类名称已存在", Controls.Window.ToastType.Error, Controls.Base.IconTypes.ImportantBadge12); |
| 365 | return; |
| 366 | } |
| 367 | if (WebCategoryData.Where(m => m.Data.Color == EditColor).Any()) |
| 368 | { |
| 369 | mainVM.Toast("颜色已存在,请重新选择", Controls.Window.ToastType.Error, Controls.Base.IconTypes.ImportantBadge12); |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | var category = _webData.CreateWebSiteCategory(new Core.Models.Db.WebSiteCategoryModel() |
| 374 | { |
| 375 | Color = EditColor, |
| 376 | IconFile = EditIconFile, |
| 377 | Name = EditName, |
| 378 | }); |
| 379 | |
| 380 | if (category != null) |
| 381 | { |
| 382 | mainVM.Toast("创建完成", Controls.Window.ToastType.Success); |
| 383 | EditVisibility = System.Windows.Visibility.Collapsed; |
| 384 | |
| 385 | var webCategory = new WebCategoryModel() |
| 386 | { |
| 387 | Count = 0, |
| 388 | Data = category, |
| 389 | }; |
| 390 | if (WebCategoryData.Count == 0) |
| 391 | { |
| 392 | WebCategoryData = new System.Collections.ObjectModel.ObservableCollection<WebCategoryModel>() |
| 393 | { |
| 394 | webCategory |
| 395 | }; |
| 396 | } |
| 397 | else |
| 398 | { |
| 399 | WebCategoryData.Add(webCategory); |
| 400 | } |
| 401 | |
| 402 | } |
| 403 | else |
| 404 | { |
| 405 | mainVM.Toast("创建失败", Controls.Window.ToastType.Error, Controls.Base.IconTypes.ImportantBadge12); |
| 406 | } |
| 407 | |
| 408 | |
| 409 | |
| 410 | } |
| 411 | else |
| 412 | { |
| 413 | |
| 414 | |
| 415 | |
| 416 | if (WebCategoryData.Where(m => m.Data.Name == EditName && m.Data.ID != SelectedWebCategoryItem.Data.ID).Any()) |
| 417 | { |
| 418 | mainVM.Toast("分类名称已存在", Controls.Window.ToastType.Error, Controls.Base.IconTypes.ImportantBadge12); |
| 419 | return; |
| 420 | } |
| 421 | if (WebCategoryData.Where(m => m.Data.Color == EditColor && m.Data.ID != SelectedWebCategoryItem.Data.ID).Any()) |
| 422 | { |
| 423 | mainVM.Toast("颜色已存在,请重新选择", Controls.Window.ToastType.Error, Controls.Base.IconTypes.ImportantBadge12); |
| 424 | return; |
| 425 | } |
| 426 | if (EditName == SelectedWebCategoryItem.Data.Name && EditIconFile == SelectedWebCategoryItem.Data.IconFile && EditColor == SelectedWebCategoryItem.Data.Color) |
| 427 | { |
| 428 | mainVM.Toast("没有修改"); |
| 429 | EditVisibility = System.Windows.Visibility.Collapsed; |
| 430 | return; |
| 431 | } |
| 432 | mainVM.Toast("已更新", Controls.Window.ToastType.Success); |
| 433 | |
| 434 | var category = SelectedWebCategoryItem.Data; |
| 435 | category.Name = EditName; |
| 436 | category.IconFile = EditIconFile; |
| 437 | category.Color = EditColor; |
| 438 | |
| 439 | _webData.UpdateWebSiteCategory(category); |
| 440 | |
| 441 | var item = WebCategoryData.Where(m => m.Data.ID == category.ID).FirstOrDefault(); |
| 442 | var index = WebCategoryData.IndexOf(item); |
| 443 | WebCategoryData[index] = new WebCategoryModel() |
| 444 | { |
| 445 | Count = SelectedWebCategoryItem.Count, |
| 446 | Data = category |
| 447 | }; |
| 448 | EditVisibility = System.Windows.Visibility.Collapsed; |
| 449 | } |
| 450 | } |
| 451 | #endregion |
| 452 | |
| 453 | #region 删除分类 |
| 454 | private async void DelWebSiteCategory() |
| 455 | { |
| 456 | if (SelectedWebCategoryItem == null) |
| 457 | { |
| 458 | return; |
| 459 | } |
| 460 | bool isConfirm = await _uiServicer.ShowConfirmDialogAsync("删除分类", "是否确认删除该分类?"); |
| 461 | if (isConfirm) |
| 462 | { |
| 463 | _webData.DeleteWebSiteCategory(SelectedWebCategoryItem.Data); |
| 464 | |
| 465 | |
| 466 | WebCategoryData.Remove(SelectedWebCategoryItem); |
| 467 | if (WebCategoryData.Count == 0) |
| 468 | { |
| 469 | WebCategoryData = new System.Collections.ObjectModel.ObservableCollection<WebCategoryModel>(); |
| 470 | } |
| 471 | mainVM.Toast("分类已删除", Controls.Window.ToastType.Success); |
| 472 | } |
| 473 | } |
| 474 | #endregion |
| 475 | #endregion |
| 476 | |
| 477 | private void OnEdit(object obj) |
| 478 | { |
| 479 | EditVisibility = System.Windows.Visibility.Visible; |
| 480 | IsCreate = obj == null; |
| 481 | |
| 482 | if (obj != null) |
| 483 | { |
| 484 | var appCategory = obj as CategoryModel; |
| 485 | var webCategory = obj as WebCategoryModel; |
| 486 | |
| 487 | EditName = appCategory == null ? webCategory.Data.Name : appCategory.Data.Name; |
| 488 | EditIconFile = appCategory == null ? webCategory.Data.IconFile : appCategory.Data.IconFile; |
| 489 | EditColor = string.IsNullOrEmpty(appCategory == null ? webCategory.Data.Color : appCategory.Data.Color) ? "#00FFAB" : appCategory == null ? webCategory.Data.Color : appCategory.Data.Color; |
| 490 | if (ShowType.Id == 0) |
| 491 | { |
| 492 | EditIsDirectoryMath = appCategory.Data.IsDirectoryMath; |
| 493 | if (appCategory.Data.Directories != null) |
| 494 | { |
| 495 | EditDirectories = new System.Collections.ObjectModel.ObservableCollection<string>(appCategory.Data.DirectoryList); |
| 496 | } |
| 497 | else |
| 498 | { |
| 499 | EditDirectories = new System.Collections.ObjectModel.ObservableCollection<string>(); |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | else |
| 504 | { |
| 505 | EditName = ""; |
| 506 | EditIconFile = "pack://application:,,,/Tai;component/Resources/Emoji/(1).png"; |
| 507 | EditColor = "#00FFAB"; |
| 508 | EditIsDirectoryMath = false; |
| 509 | EditDirectories = new System.Collections.ObjectModel.ObservableCollection<string>(); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | private void CategoryPageVM_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) |
| 514 | { |
| 515 | if (e.PropertyName == nameof(SelectedAppCategoryItem)) |
| 516 | { |
| 517 | |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | private void OnGotoList(object obj) |
| 522 | { |
| 523 | GotoList(); |
| 524 | } |
| 525 | |
| 526 | private void LoadData() |
| 527 | { |
| 528 | Task.Run(() => |
| 529 | { |
| 530 | Data = new System.Collections.ObjectModel.ObservableCollection<CategoryModel>(); |
| 531 | foreach (var item in categorys.GetCategories()) |
| 532 | { |
| 533 | Data.Add(new CategoryModel() |
| 534 | { |
| 535 | Count = appData.GetAppsByCategoryID(item.ID).Count, |
| 536 | Data = item |
| 537 | }); |
| 538 | } |
| 539 | |
| 540 | var webCategories = _webData.GetWebSiteCategories(); |
| 541 | var webCategoryData = new List<WebCategoryModel>(); |
| 542 | |
| 543 | foreach (var item in webCategories) |
| 544 | { |
| 545 | webCategoryData.Add(new WebCategoryModel() |
| 546 | { |
| 547 | Data = item, |
| 548 | Count = _webData.GetWebSitesCount(item.ID) |
| 549 | }); |
| 550 | } |
| 551 | WebCategoryData = new System.Collections.ObjectModel.ObservableCollection<WebCategoryModel>(webCategoryData); |
| 552 | }); |
| 553 | } |
| 554 | |
| 555 | private void GotoList() |
| 556 | { |
| 557 | if (ShowType.Id == 0 && SelectedAppCategoryItem != null) |
| 558 | { |
| 559 | mainVM.Data = SelectedAppCategoryItem; |
| 560 | mainVM.Uri = nameof(CategoryAppListPage); |
| 561 | SelectedAppCategoryItem = null; |
| 562 | } |
| 563 | else if (ShowType.Id == 1 && SelectedWebCategoryItem != null) |
| 564 | { |
| 565 | mainVM.Data = SelectedWebCategoryItem.Data; |
| 566 | mainVM.Uri = nameof(CategoryWebSiteListPage); |
| 567 | SelectedWebCategoryItem = null; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | public override void Dispose() |
| 572 | { |
| 573 | base.Dispose(); |
| 574 | PropertyChanged -= CategoryPageVM_PropertyChanged; |
| 575 | Data = null; |
| 576 | } |
| 577 | } |
| 578 | } |