add cache for main menus; clear cache when theme or lang changes
authorMarcin Romaniuk <m.romaniuk@samsung.com>
Fri, 13 Oct 2023 21:36:32 +0000 (23:36 +0200)
committerPiotr Czaja/Tizen Services & IoT (PLT) /SRPOL/Engineer/Samsung Electronics <p.czaja@samsung.com>
Thu, 26 Oct 2023 07:33:54 +0000 (09:33 +0200)
SettingCore/MainMenuInfo.cs
SettingView/SettingView.cs

index b947360b8ddfbca1d3bc4bed64003a57912306f8..3c4633c310d01917778a34638e917faddd4d0ac2 100644 (file)
@@ -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<MainMenuInfo> cache = new List<MainMenuInfo>();
+
+        static MainMenuInfo()
+        {
+            ReadCache();
+        }
+
+        private static void ReadCache()
+        {
+            try
+            {
+                string text = System.IO.File.ReadAllText(CachePath);
+                cache = JsonSerializer.Deserialize<List<MainMenuInfo>>(text);
+            }
+            catch
+            {
+                Logger.Warn($"Could not read main menu cache file.");
+            }
+        }
+
+        public static void UpdateCache(IEnumerable<MainMenuInfo> 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<MainMenuInfo>());
+        }
+
+        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;
+        }
     }
 }
index 2c86f9600f2eadc67d8af7ef165855a37071c25a..34eae8f3fe0fc6f44dea24a6023f01ecb7980120 100644 (file)
@@ -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<SettingGadgetInfo> visibleMenus, View content)
         {
-            return System.Threading.Tasks.Task.Run(() =>
+            return System.Threading.Tasks.Task.Run(async () =>
             {
-                Post(() =>
+                var menus = new List<MainMenuInfo>();
+                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);
             });
         }