| 1 | using Core.Servicers.Interfaces; |
| 2 | using System; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Linq; |
| 5 | using System.Text; |
| 6 | using System.Threading.Tasks; |
| 7 | using UI.Controls; |
| 8 | using UI.Controls.Base; |
| 9 | using UI.Controls.Navigation.Models; |
| 10 | using UI.Controls.Window; |
| 11 | using UI.Models; |
| 12 | using UI.Views; |
| 13 | |
| 14 | namespace UI.ViewModels |
| 15 | { |
| 16 | public class MainViewModel : MainWindowModel |
| 17 | { |
| 18 | private readonly IServiceProvider serviceProvider; |
| 19 | private readonly IAppConfig appConfig; |
| 20 | public Command OnSelectedCommand { get; set; } |
| 21 | public Command GotoPageCommand { get; set; } |
| 22 | |
| 23 | private string[] pages = { nameof(IndexPage), nameof(ChartPage), nameof(DataPage), nameof(CategoryPage) }; |
| 24 | public MainViewModel( |
| 25 | IServiceProvider serviceProvider, |
| 26 | IAppConfig appConfig, |
| 27 | IMain main |
| 28 | ) |
| 29 | { |
| 30 | this.serviceProvider = serviceProvider; |
| 31 | this.appConfig = appConfig; |
| 32 | |
| 33 | ServiceProvider = serviceProvider; |
| 34 | |
| 35 | OnSelectedCommand = new Command(new Action<object>(OnSelectedCommandHandle)); |
| 36 | GotoPageCommand = new Command(new Action<object>(OnGotoPageCommand)); |
| 37 | |
| 38 | |
| 39 | Items = new System.Collections.ObjectModel.ObservableCollection<Controls.Navigation.Models.NavigationItemModel>(); |
| 40 | |
| 41 | PropertyChanged += MainViewModel_PropertyChanged; |
| 42 | |
| 43 | InitNavigation(); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | |
| 48 | private void MainViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) |
| 49 | { |
| 50 | if (e.PropertyName == nameof(Uri)) |
| 51 | { |
| 52 | if (Uri == nameof(IndexPage)) |
| 53 | { |
| 54 | Data = null; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | public void LoadDefaultPage() |
| 60 | { |
| 61 | int startPageIndex = appConfig.GetConfig().General.StartPage; |
| 62 | NavSelectedItem = Items[startPageIndex]; |
| 63 | Uri = NavSelectedItem.Uri; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | private void OnGotoPageCommand(object obj) |
| 68 | { |
| 69 | Uri = obj.ToString(); |
| 70 | } |
| 71 | |
| 72 | private void OnSelectedCommandHandle(object obj) |
| 73 | { |
| 74 | if (!string.IsNullOrEmpty(NavSelectedItem.Uri)) |
| 75 | { |
| 76 | Uri = NavSelectedItem.Uri; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | private void InitNavigation() |
| 81 | { |
| 82 | IndexUriList = new List<string>(); |
| 83 | IndexUriList.AddRange(pages); |
| 84 | |
| 85 | Items.Add(new Controls.Navigation.Models.NavigationItemModel() |
| 86 | { |
| 87 | UnSelectedIcon = Controls.Base.IconTypes.Home, |
| 88 | SelectedIcon = IconTypes.HomeSolid, |
| 89 | Title = "概览", |
| 90 | Uri = nameof(IndexPage), |
| 91 | ID = -1 |
| 92 | |
| 93 | }); |
| 94 | Items.Add(new Controls.Navigation.Models.NavigationItemModel() |
| 95 | { |
| 96 | UnSelectedIcon = Controls.Base.IconTypes.ZeroBars, |
| 97 | SelectedIcon = IconTypes.FourBars, |
| 98 | Title = "统计", |
| 99 | ID = 1, |
| 100 | Uri = nameof(ChartPage), |
| 101 | |
| 102 | }); |
| 103 | Items.Add(new Controls.Navigation.Models.NavigationItemModel() |
| 104 | { |
| 105 | UnSelectedIcon = Controls.Base.IconTypes.Calendar, |
| 106 | SelectedIcon = IconTypes.CalendarSolid, |
| 107 | |
| 108 | Title = "详细", |
| 109 | ID = 2, |
| 110 | Uri = nameof(DataPage), |
| 111 | |
| 112 | }); |
| 113 | Items.Add(new Controls.Navigation.Models.NavigationItemModel() |
| 114 | { |
| 115 | UnSelectedIcon = Controls.Base.IconTypes.EndPoint, |
| 116 | SelectedIcon = IconTypes.EndPointSolid, |
| 117 | Title = "分类", |
| 118 | ID = 3, |
| 119 | Uri = nameof(CategoryPage), |
| 120 | |
| 121 | }); |
| 122 | } |
| 123 | |
| 124 | public void Toast(string content, ToastType type = ToastType.Info, IconTypes icon = IconTypes.Accept) |
| 125 | { |
| 126 | ToastContent = content; |
| 127 | ToastIcon = icon; |
| 128 | ToastType = type; |
| 129 | IsShowToast = true; |
| 130 | |
| 131 | } |
| 132 | |
| 133 | ///<summary> |
| 134 | /// |
| 135 | ///</summary> |
| 136 | ///<param name="message_"></param> |
| 137 | public void Error(string message_) |
| 138 | { |
| 139 | Toast(message_, ToastType.Error, IconTypes.Error); |
| 140 | } |
| 141 | ///<summary> |
| 142 | /// |
| 143 | ///</summary> |
| 144 | ///<param name="message_"></param> |
| 145 | public void Info(string message_) |
| 146 | { |
| 147 | Toast(message_, ToastType.Info, IconTypes.Info); |
| 148 | } |
| 149 | ///<summary> |
| 150 | /// |
| 151 | ///</summary> |
| 152 | ///<param name="message_"></param> |
| 153 | public void Success(string message_) |
| 154 | { |
| 155 | Toast(message_, ToastType.Success, IconTypes.Accept); |
| 156 | } |
| 157 | } |
| 158 | } |