From: Marcin Romaniuk Date: Fri, 13 Oct 2023 21:36:32 +0000 (+0200) Subject: add cache for main menus; clear cache when theme or lang changes X-Git-Tag: accepted/tizen/unified/20231120.023124~13 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=70a2fa25e0655418c233ef3c93f148d1072f911a;p=profile%2Fiot%2Fapps%2Fdotnet%2Fsettings.git add cache for main menus; clear cache when theme or lang changes --- diff --git a/SettingCore/MainMenuInfo.cs b/SettingCore/MainMenuInfo.cs index b947360..3c4633c 100644 --- a/SettingCore/MainMenuInfo.cs +++ b/SettingCore/MainMenuInfo.cs @@ -1,18 +1,61 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Reflection; +using System.Text.Json; using Tizen.NUI; namespace SettingCore { public class MainMenuInfo { - public string IconPath { get; private set; } - public Color IconColor { get; private set; } - public string Title { get; private set; } - public Action Action { get; private set; } + public string IconPath { get; set; } + public Color IconColor { get; set; } + public string Title { get; set; } + public string Path { get; set; } - public static MainMenuInfo Create(SettingGadgetInfo info) + private static string CachePath => System.IO.Path.Combine(Tizen.Applications.Application.Current.DirectoryInfo.Data, "main-menu.cache"); + private static List cache = new List(); + + static MainMenuInfo() + { + ReadCache(); + } + + private static void ReadCache() + { + try + { + string text = System.IO.File.ReadAllText(CachePath); + cache = JsonSerializer.Deserialize>(text); + } + catch + { + Logger.Warn($"Could not read main menu cache file."); + } + } + + public static void UpdateCache(IEnumerable infos) + { + try + { + cache.Clear(); + cache.AddRange(infos); + string text = JsonSerializer.Serialize(cache); + System.IO.File.WriteAllText(CachePath, text); + } + catch (Exception ex) + { + Logger.Warn($"{ex}"); + } + } + + public static void ClearCache() + { + UpdateCache(new List()); + } + + private static MainMenuInfo FromGadget(SettingGadgetInfo info) { string assemblyPath = System.IO.Path.Combine(info.Pkg.ResourcePath, info.Pkg.ExecutableFile); System.Reflection.Assembly assembly = null; @@ -45,11 +88,7 @@ namespace SettingCore string iconPath = mainMenu.ProvideIconPath(); Color iconColor = mainMenu.ProvideIconColor(); string title = mainMenu.ProvideTitle(); - Action action = () => - { - Logger.Debug($"navigating to menupath {info.Path}, title: {title}"); - GadgetNavigation.NavigateTo(info.Path); - }; + NUIGadgetManager.Remove(mainMenu); return new MainMenuInfo @@ -57,8 +96,54 @@ namespace SettingCore IconPath = iconPath, IconColor = iconColor, Title = title, - Action = action, + Path = info.Path, + }; + } + + private static MainMenuInfo FromCache(SettingGadgetInfo info) + { + var cached = cache.SingleOrDefault(x => x.Path == info.Path); + if (cached == null) + { + return null; + } + + return new MainMenuInfo() + { + Title = cached.Title, + IconPath = cached.IconPath, + IconColor = cached.IconColor, + Path = cached.Path, }; } + + public static MainMenuInfo Create(SettingGadgetInfo info) + { + TimeSpan total = TimeSpan.Zero; + System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); + + stopwatch.Start(); + var menu = FromCache(info); + stopwatch.Stop(); + total += stopwatch.Elapsed; + if (menu != null) + { + Logger.Debug($"MEASURE loaded MainMenuInfo from Cache, path: {info.Path}, time: {stopwatch.Elapsed}"); + return menu; + } + + stopwatch.Restart(); + menu = FromGadget(info); + stopwatch.Stop(); + total += stopwatch.Elapsed; + if (menu != null) + { + Logger.Debug($"MEASURE loaded MainMenuInfo from Gadget, path: {info.Path}, time: {stopwatch.Elapsed}"); + return menu; + } + + Logger.Debug($"MEASURE could NOT load MainMenuInfo, path: {info.Path}, time: {total}"); + return null; + } } } diff --git a/SettingView/SettingView.cs b/SettingView/SettingView.cs index 2c86f96..34eae8f 100644 --- a/SettingView/SettingView.cs +++ b/SettingView/SettingView.cs @@ -17,6 +17,7 @@ using SettingCore; using SettingCore.Views; using SettingView.TextResources; +using System; using System.Collections.Generic; using System.Linq; using Tizen.Applications; @@ -182,6 +183,8 @@ namespace SettingView { if (mMainPage != null && mMainPage.AppBar != null) { + MainMenuInfo.ClearCache(); + mMainPage.AppBar.Title = Resources.IDS_ST_OPT_SETTINGS; mMainPage.Content = CreateContent(); } @@ -191,6 +194,8 @@ namespace SettingView { if (mMainPage != null && e.IsPlatformThemeChanged) { + MainMenuInfo.ClearCache(); + // recreate main page content just to apply new colors from gadgets mMainPage.Content = CreateContent(); } @@ -247,22 +252,44 @@ namespace SettingView private static System.Threading.Tasks.Task CreateContentRows(IEnumerable visibleMenus, View content) { - return System.Threading.Tasks.Task.Run(() => + return System.Threading.Tasks.Task.Run(async () => { - Post(() => + var menus = new List(); + System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); + stopwatch.Start(); + foreach (var gadgetInfo in visibleMenus) { - foreach (var gadgetInfo in visibleMenus) + await Post(() => { - var info = MainMenuInfo.Create(gadgetInfo); - if (info != null) + if (MainMenuInfo.Create(gadgetInfo) is MainMenuInfo menu) { - var row = new SettingCore.Views.MainMenuItem(info.IconPath, info.IconColor, info.Title); - row.Clicked += (s, e) => { info.Action?.Invoke(); }; - - content.Add(row); + menus.Add(menu); } - } - }); + return true; + }); + } + stopwatch.Stop(); + Logger.Debug($"MEASURE loaded all MainMenuInfos, total time: {stopwatch.Elapsed}"); + + stopwatch.Restart(); + foreach (var menu in menus) + { + await Post(() => + { + var row = new SettingCore.Views.MainMenuItem(menu.IconPath, menu.IconColor, menu.Title); + row.Clicked += (s, e) => + { + Logger.Debug($"navigating to menupath {menu.Path}, title: {menu.Title}"); + GadgetNavigation.NavigateTo(menu.Path); + }; + content.Add(row); + return true; + }); + } + stopwatch.Stop(); + Logger.Debug($"MEASURE created UI main menu rows, total time: {stopwatch.Elapsed}"); + + MainMenuInfo.UpdateCache(menus); }); }