跳到主要内容

github/tai

公开

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

提交记录
修改UI/Controls/PageContainer.cs+80 −63看文件
@@ -7,6 +7,7 @@ using System.Threading.Tasks;
77using System.Windows;
88using System.Windows.Controls;
99using System.Windows.Input;
10新增:using UI.Models;
1011
1112namespace UI.Controls
1213{
@@ -116,7 +117,8 @@ namespace UI.Controls
116117 private readonly string ProjectName;
117118 private List<string> Historys;
118119 public int Index = 0, OldIndex = 0;
119删除: private Dictionary<string, object> PageCache;
120新增: private Dictionary<string, Page> PageCache;
121新增: private bool IsBack = false;
120122 public PageContainer()
121123 {
122124 DefaultStyleKey = typeof(PageContainer);
@@ -125,13 +127,25 @@ namespace UI.Controls
125127 BackCommand = new Command(new Action<object>(OnBackCommand));
126128 NavigationCommands.BrowseBack.InputGestures.Clear();
127129 NavigationCommands.BrowseForward.InputGestures.Clear();
128删除: PageCache = new Dictionary<string, object>();
130新增: PageCache = new Dictionary<string, Page>();
129131 }
132新增:
133新增:
130134 public override void OnApplyTemplate()
131135 {
132136 base.OnApplyTemplate();
133137
134138 Loaded += PageContainer_Loaded;
139新增: Unloaded += PageContainer_Unloaded;
140新增: }
141新增:
142新增: private void PageContainer_Unloaded(object sender, RoutedEventArgs e)
143新增: {
144新增: Loaded -= PageContainer_Loaded;
145新增: Unloaded -= PageContainer_Unloaded;
146新增: ClearCache();
147新增: Content = null;
148新增: DataContext = null;
135149 }
136150
137151 private void PageContainer_Loaded(object sender, RoutedEventArgs e)
@@ -153,26 +167,24 @@ namespace UI.Controls
153167 string uri = Historys[Index];
154168
155169 int preIndex = Index + 1;
156删除: int count = Historys.Count - (Index + 1);
157删除: // 从缓存中移除之前的页面
158删除: for (int i = preIndex; i < count; i++)
170新增:
171新增: // 从缓存中移除上一页
172新增:
173新增: var pageUri = Historys[preIndex];
174新增: if (PageCache.ContainsKey(pageUri))
159175 {
160删除: var pageUri = Historys[i];
161删除: if (PageCache.ContainsKey(pageUri))
162删除: {
163删除: var page = PageCache[pageUri];
164删除: page = null;
165删除: PageCache.Remove(pageUri);
166删除: }
176新增: var page = PageCache[pageUri];
177新增: var vm = page.DataContext as ModelBase;
178新增: vm?.Dispose();
179新增: page = null;
180新增: PageCache.Remove(pageUri);
167181 }
182新增: Historys.RemoveRange(preIndex, 1);
168183
169删除: // 历史记录中移除
170删除: Historys.RemoveRange(preIndex, count);
184新增: IsBack = true;
171185
172186 Uri = uri;
173187
174删除:
175删除:
176188 }
177189 }
178190
@@ -184,83 +196,88 @@ namespace UI.Controls
184196 {
185197 if (Uri != string.Empty)
186198 {
187删除: Page page = null;
188删除: var pageVM = GetPageVM(Uri);
189删除: Type pageType = Type.GetType(ProjectName + ".Views." + Uri);
190删除: if (pageType != null && ServiceProvider != null)
199新增: if (IndexUriList != null && IndexUriList.Contains(Uri))
191200 {
192删除: page = ServiceProvider.GetService(pageType) as Page;
193删除: if (page != null)
201新增: Historys.Clear();
202新增: Index = 0;
203新增: OldIndex = 0;
204新增: Historys.Add(Uri);
205新增: if (!IsBack)
194206 {
195删除:
196删除: page.DataContext = pageVM;
197删除:
207新增: ClearCache();
198208 }
199209
200210 }
211新增: else
212新增: {
213新增: //处理历史记录
214新增: if (OldIndex == Index)
215新增: {
216新增: //新开
217新增: Historys.Add(Uri);
218新增: Index++;
219新增: }
220新增: OldIndex = Index;
221新增: }
222新增: Page page = GetPage();
223新增:
201224
202225 if (page != null)
203226 {
204删除:
205227 Content = page;
206228
207删除:
208删除:
209删除: if (IndexUriList != null && IndexUriList.Contains(Uri))
210删除: {
211删除: Historys.Clear();
212删除: Index = 0;
213删除: OldIndex = 0;
214删除: Historys.Add(Uri);
215删除: PageCache.Clear();
216删除: }
217删除: else
229新增: // 加入缓存
230新增: if (!PageCache.ContainsKey(Uri))
218231 {
219删除: //处理历史记录
220删除: if (OldIndex == Index)
221删除: {
222删除: //新开
223删除: Historys.Add(Uri);
224删除: Index++;
225删除: }
226删除: OldIndex = Index;
232新增: PageCache.Add(Uri, page);
227233 }
228234
229删除: // 加入缓存
230删除: PageCache.Add(Uri, pageVM);
231删除:
232235 OnLoadPaged?.Invoke(this, EventArgs.Empty);
233236 }
234237 else
235238 {
236239 Debug.WriteLine("找不到Page:" + Uri + ",请确认已被注入");
237240 }
238删除:
239删除:
240241 }
242新增: IsBack = false;
241243 }
242244
243删除:
244删除: private object GetPageVM(string uri)
245新增: private Page GetPage()
245246 {
246删除: if (PageCache.ContainsKey(uri))
247新增: Page page = null;
248新增: if (PageCache.ContainsKey(Uri))
247249 {
248删除: return PageCache[uri];
250新增: return PageCache[Uri];
249251 }
250删除: else
252新增: Type pageType = Type.GetType(ProjectName + ".Views." + Uri);
253新增: if (pageType != null && ServiceProvider != null)
251254 {
252删除: Type pageVMType = Type.GetType(ProjectName + ".ViewModels." + Uri + "VM");
253删除: if (ServiceProvider != null && pageVMType != null)
255新增: page = ServiceProvider.GetService(pageType) as Page;
256新增: if (page != null)
254257 {
255删除:
256删除: var pageVM = ServiceProvider.GetService(pageVMType);
257删除:
258删除: return pageVM;
258新增: page.Unloaded += Page_Unloaded;
259259 }
260删除: else
260新增: }
261新增: return page;
262新增: }
263新增: private void Page_Unloaded(object sender, RoutedEventArgs e)
264新增: {
265新增: Page page = (Page)sender;
266新增:
267新增: page.Unloaded -= Page_Unloaded;
268新增: }
269新增:
270新增: private void ClearCache()
271新增: {
272新增: if (PageCache != null)
273新增: {
274新增: foreach (var key in PageCache.Keys)
261275 {
262删除: return null;
276新增: var page = PageCache[key];
277新增: var vm = page.DataContext as ModelBase;
278新增: vm?.Dispose();
263279 }
280新增: PageCache.Clear();
264281 }
265282 }
266283 }
修改UI/Models/IndexPageModel.cs+1 −1看文件
@@ -8,7 +8,7 @@ using UI.Controls.Charts.Model;
88
99namespace UI.Models
1010{
11删除: public class IndexPageModel : UINotifyPropertyChanged
11新增: public class IndexPageModel : ModelBase
1212 {
1313 private ObservableCollection<string> TabbarData_;
1414 /// <summary>
新增UI/Models/ModelBase.cs+13 −0看文件
@@ -0,0 +1,13 @@
1新增:using System;
2新增:using System.Collections.Generic;
3新增:using System.Linq;
4新增:using System.Text;
5新增:using System.Threading.Tasks;
6新增:
7新增:namespace UI.Models
8新增:{
9新增: public class ModelBase : UINotifyPropertyChanged
10新增: {
11新增: public virtual void Dispose() { }
12新增: }
13新增:}
修改UI/ViewModels/IndexPageVM.cs+13 −1看文件
@@ -8,6 +8,7 @@ using System.Drawing;
88using System.Linq;
99using System.Text;
1010using System.Threading.Tasks;
11新增:using System.Windows;
1112using System.Windows.Media.Imaging;
1213using UI.Controls;
1314using UI.Controls.Charts.Model;
@@ -23,6 +24,7 @@ namespace UI.ViewModels
2324 private readonly IData data;
2425 private readonly MainViewModel main;
2526 private readonly IMain mainServicer;
27新增:
2628 public IndexPageVM(
2729 IData data, MainViewModel main, IMain mainServicer)
2830 {
@@ -32,10 +34,17 @@ namespace UI.ViewModels
3234
3335 ToDetailCommand = new Command(new Action<object>(OnTodetailCommand));
3436
35删除: mainServicer.OnUpdateTime += MainServicer_OnUpdateTime;
3637 Init();
3738 }
3839
40新增:
41新增: public override void Dispose()
42新增: {
43新增: mainServicer.OnUpdateTime -= MainServicer_OnUpdateTime;
44新增: PropertyChanged -= IndexPageVM_PropertyChanged;
45新增: base.Dispose();
46新增: }
47新增:
3948 private void MainServicer_OnUpdateTime(object sender, EventArgs e)
4049 {
4150 if (TabbarSelectedIndex == 1)
@@ -52,7 +61,10 @@ namespace UI.ViewModels
5261 };
5362
5463 TabbarSelectedIndex = 1;
64新增:
5565 PropertyChanged += IndexPageVM_PropertyChanged;
66新增: mainServicer.OnUpdateTime += MainServicer_OnUpdateTime;
67新增:
5668 LoadThisWeekData();
5769 }
5870
修改UI/Views/DetailPage.xaml.cs+4 −1看文件
@@ -12,6 +12,7 @@ using System.Windows.Media;
1212using System.Windows.Media.Imaging;
1313using System.Windows.Navigation;
1414using System.Windows.Shapes;
15新增:using UI.ViewModels;
1516
1617namespace UI.Views
1718{
@@ -20,9 +21,11 @@ namespace UI.Views
2021 /// </summary>
2122 public partial class DetailPage : Page
2223 {
23删除: public DetailPage()
24新增: public DetailPage(DetailPageVM vm)
2425 {
2526 InitializeComponent();
27新增:
28新增: DataContext = vm;
2629 }
2730 }
2831}
修改UI/Views/IndexPage.xaml.cs+4 −2看文件
@@ -13,6 +13,7 @@ using System.Windows.Media.Imaging;
1313using System.Windows.Navigation;
1414using System.Windows.Shapes;
1515using UI.Controls.Charts.Model;
16新增:using UI.ViewModels;
1617
1718namespace UI.Views
1819{
@@ -21,10 +22,11 @@ namespace UI.Views
2122 /// </summary>
2223 public partial class IndexPage : Page
2324 {
24删除: public IndexPage()
25新增: public IndexPage(IndexPageVM vm)
2526 {
2627 InitializeComponent();
27删除:
28新增:
29新增: DataContext = vm;
2830 }
2931 }
3032}
修改UI/Views/SettingPage.xaml.cs+4 −1看文件
@@ -12,6 +12,7 @@ using System.Windows.Media;
1212using System.Windows.Media.Imaging;
1313using System.Windows.Navigation;
1414using System.Windows.Shapes;
15新增:using UI.ViewModels;
1516
1617namespace UI.Views
1718{
@@ -20,9 +21,11 @@ namespace UI.Views
2021 /// </summary>
2122 public partial class SettingPage : Page
2223 {
23删除: public SettingPage()
24新增: public SettingPage(SettingPageVM vm)
2425 {
2526 InitializeComponent();
27新增:
28新增: DataContext = vm;
2629 }
2730 }
2831}