| 1 | using Core.Models; |
| 2 | using Core.Models.Db; |
| 3 | using Core.Servicers.Instances; |
| 4 | using Core.Servicers.Interfaces; |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
| 7 | using System.Diagnostics; |
| 8 | using System.Linq; |
| 9 | using System.Text; |
| 10 | using System.Threading.Tasks; |
| 11 | using UI.Controls; |
| 12 | using UI.Models; |
| 13 | using UI.Models.Category; |
| 14 | using UI.Models.CategoryAppList; |
| 15 | using UI.Views; |
| 16 | |
| 17 | namespace UI.ViewModels |
| 18 | { |
| 19 | public class CategoryWebSiteListPageVM : CategoryWebSiteListPageModel |
| 20 | { |
| 21 | private readonly MainViewModel _mainVM; |
| 22 | private readonly IWebData _webData; |
| 23 | public Command ShowChooseCommand { get; set; } |
| 24 | public Command ChoosedCommand { get; set; } |
| 25 | public Command GotoDetailCommand { get; set; } |
| 26 | public Command SearchCommand { get; set; } |
| 27 | public Command ChooseCloseCommand { get; set; } |
| 28 | public Command DelCommand { get; set; } |
| 29 | private List<OptionModel> _webSiteOptionsTemp; |
| 30 | public CategoryWebSiteListPageVM(MainViewModel mainVM_, IWebData webData_) |
| 31 | { |
| 32 | _mainVM = mainVM_; |
| 33 | _webData = webData_; |
| 34 | Category = mainVM_.Data as WebSiteCategoryModel; |
| 35 | |
| 36 | ShowChooseCommand = new Command(new Action<object>(OnShowChoose)); |
| 37 | ChoosedCommand = new Command(new Action<object>(OnChoosed)); |
| 38 | GotoDetailCommand = new Command(new Action<object>(OnGotoDetail)); |
| 39 | SearchCommand = new Command(new Action<object>(OnSearch)); |
| 40 | ChooseCloseCommand = new Command(new Action<object>(OnChooseClose)); |
| 41 | DelCommand = new Command(new Action<object>(OnDel)); |
| 42 | |
| 43 | LoadWebsiteList(); |
| 44 | } |
| 45 | |
| 46 | private void LoadWebsiteList() |
| 47 | { |
| 48 | if (Category == null) |
| 49 | { |
| 50 | _mainVM.Toast("参数错误", Controls.Window.ToastType.Error, Controls.Base.IconTypes.Error); |
| 51 | return; |
| 52 | } |
| 53 | Task.Run(() => |
| 54 | { |
| 55 | var list = _webData.GetWebSites(Category.ID); |
| 56 | CategoryWebSiteList = new System.Collections.ObjectModel.ObservableCollection<Core.Models.Db.WebSiteModel>(list); |
| 57 | |
| 58 | LoadWebSiteOptionList(); |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | ///<summary> |
| 63 | /// |
| 64 | ///</summary> |
| 65 | private void LoadWebSiteOptionList() |
| 66 | { |
| 67 | var list = _webData.GetUnSetCategoryWebSites(); |
| 68 | list = list.Concat(CategoryWebSiteList).OrderBy(m => m.Title).ToList(); |
| 69 | |
| 70 | var optionList = new List<OptionModel>(); |
| 71 | foreach (var site in list) |
| 72 | { |
| 73 | optionList.Add(new OptionModel() |
| 74 | { |
| 75 | IsChecked = site.CategoryID == Category.ID, |
| 76 | OptionValue = new Controls.Select.SelectItemModel |
| 77 | { |
| 78 | Name = $"{site.Title} - {site.Domain}", |
| 79 | Img = site.IconFile |
| 80 | }, |
| 81 | WebSite = site |
| 82 | }); |
| 83 | } |
| 84 | WebSiteOptionList = optionList; |
| 85 | _webSiteOptionsTemp = new List<OptionModel>(WebSiteOptionList); |
| 86 | } |
| 87 | private void OnDel(object obj) |
| 88 | { |
| 89 | if (SelectedItem != null) |
| 90 | { |
| 91 | _webData.UpdateWebSitesCategory(new int[] { SelectedItem.ID }, 0); |
| 92 | CategoryWebSiteList.Remove(SelectedItem); |
| 93 | if (CategoryWebSiteList.Count == 0) |
| 94 | { |
| 95 | CategoryWebSiteList = new System.Collections.ObjectModel.ObservableCollection<WebSiteModel>(); |
| 96 | } |
| 97 | |
| 98 | Task.Run(() => |
| 99 | { |
| 100 | LoadWebSiteOptionList(); |
| 101 | }); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | private void OnChooseClose(object obj) |
| 106 | { |
| 107 | ChooseVisibility = System.Windows.Visibility.Collapsed; |
| 108 | SearchInput = ""; |
| 109 | } |
| 110 | |
| 111 | private void OnSearch(object obj) |
| 112 | { |
| 113 | string keyword = obj.ToString(); |
| 114 | if (string.IsNullOrEmpty(keyword)) |
| 115 | { |
| 116 | WebSiteOptionList = new List<OptionModel>(_webSiteOptionsTemp); |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | WebSiteOptionList = _webSiteOptionsTemp.Where(m => m.WebSite.Title.Contains(keyword) || m.WebSite.Domain.Contains(keyword)).ToList(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | |
| 125 | |
| 126 | private void OnGotoDetail(object obj) |
| 127 | { |
| 128 | if (SelectedItem != null) |
| 129 | { |
| 130 | _mainVM.Data = SelectedItem; |
| 131 | _mainVM.Uri = nameof(WebSiteDetailPage); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | private void OnChoosed(object obj) |
| 136 | { |
| 137 | ChooseVisibility = System.Windows.Visibility.Collapsed; |
| 138 | SearchInput = ""; |
| 139 | _webSiteOptionsTemp = new List<OptionModel>(WebSiteOptionList); |
| 140 | UpdateCategory(); |
| 141 | } |
| 142 | |
| 143 | private void UpdateCategory() |
| 144 | { |
| 145 | var removeSiteList = WebSiteOptionList.Where(m => m.IsChecked == false && CategoryWebSiteList.Where(s => s.ID == m.WebSite.ID).Any()).Select(m => m.WebSite.ID).ToList(); |
| 146 | var addSiteList = WebSiteOptionList.Where(m => m.IsChecked == true && !CategoryWebSiteList.Where(s => s.ID == m.WebSite.ID).Any()).Select(m => m.WebSite).ToList(); |
| 147 | |
| 148 | foreach (var id in removeSiteList) |
| 149 | { |
| 150 | var item = CategoryWebSiteList.Where(m => m.ID == id).FirstOrDefault(); |
| 151 | if (item != null) |
| 152 | { |
| 153 | CategoryWebSiteList.Remove(item); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | if (CategoryWebSiteList.Count == 0) |
| 158 | { |
| 159 | CategoryWebSiteList = new System.Collections.ObjectModel.ObservableCollection<WebSiteModel>(addSiteList); |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | foreach (var item in addSiteList) |
| 164 | { |
| 165 | CategoryWebSiteList.Add(item); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if (removeSiteList.Count > 0) |
| 170 | { |
| 171 | _webData.UpdateWebSitesCategory(removeSiteList.ToArray(), 0); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | if (addSiteList.Count > 0) |
| 176 | { |
| 177 | _webData.UpdateWebSitesCategory(addSiteList.Select(m => m.ID).ToArray(), Category.ID); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | private void OnShowChoose(object obj) |
| 182 | { |
| 183 | ChooseVisibility = System.Windows.Visibility.Visible; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | |
| 188 | } |
| 189 | } |