Get main menu items from cache.
authorPiotr Czaja <p.czaja@samsung.com>
Mon, 6 Nov 2023 13:27:46 +0000 (14:27 +0100)
committerPiotr Czaja/Tizen Services & IoT (PLT) /SRPOL/Engineer/Samsung Electronics <p.czaja@samsung.com>
Wed, 8 Nov 2023 13:11:03 +0000 (14:11 +0100)
Change-Id: If6beccfa855b622d25959af8e922951cb3597037

SettingCore/GadgetManager.cs
SettingCore/MainMenuInfo.cs
SettingView/SettingView.cs

index 5eddcb9cd5651de5c5548162b36034917e3e838f..94082b1c9e057b4633ab384db6a92dcf5bd6e4b3 100644 (file)
@@ -17,8 +17,7 @@ namespace SettingCore
         {
             try
             {
-                // get all installed gadgets for Settings
-                installedGadgets = GadgetProvider.Gadgets.ToList();
+                GetInstalledGadgets();
 
                 // get initial customization from file
                 var initCust = FileStorage.ReadFromFile(FileStorage.InitialFilePath);
@@ -35,19 +34,6 @@ namespace SettingCore
                     _ = UpdateCustomization(backupCust);
                 }
 
-                return true;
-            }
-            catch (Exception e)
-            {
-                Logger.Error($"{e}");
-                return false;
-            }
-        }
-
-        public void SaveCustomizationToFiles()
-        {
-            try
-            {
                 // save current customization to both files (current and backup)
                 var menuCustItems = installedGadgets.Select(x => new MenuCustomizationItem(x.Path, x.Order));
                 FileStorage.WriteToFiles(menuCustItems);
@@ -56,10 +42,22 @@ namespace SettingCore
                 FileStorage.Instance.Changed += CustFileChanged;
                 FileStorage.Instance.Lost += CustFileLost;
                 FileStorage.Instance.StartMonitoring();
+
+                return true;
             }
             catch (Exception e)
             {
                 Logger.Error($"{e}");
+                return false;
+            }
+        }
+
+        private void GetInstalledGadgets()
+        {
+            // get all installed gadgets for Settings
+            if (!installedGadgets.Any())
+            {
+                installedGadgets = GadgetProvider.Gadgets.ToList();
             }
         }
 
@@ -166,6 +164,7 @@ namespace SettingCore
 
         public IEnumerable<SettingGadgetInfo> GetMainWithCurrentOrder()
         {
+            GetInstalledGadgets();
             var main = installedGadgets
                 .Where(info => info.IsMainMenu);
 
index ead3ed408e9dedca95b77c29fbe0fcb1c604e8a5..c8ffb8e42af7e9ba3b685cd8ce8df2c600cf0f50 100644 (file)
@@ -15,6 +15,7 @@ namespace SettingCore
         public string Path { get; set; }
 
         private static string CachePath => System.IO.Path.Combine(Tizen.Applications.Application.Current.DirectoryInfo.Data, "main-menu.cache");
+        public static List<MainMenuInfo> CacheMenu { get => cache; }
         private static List<MainMenuInfo> cache = new List<MainMenuInfo>();
         private const string metadataNamePrefix = "http://tizen.org/metadata/ui-gadget/menu";
         private const string iconPathMetadata = "icon-path";
@@ -40,6 +41,10 @@ namespace SettingCore
 
         public static void UpdateCache(IEnumerable<MainMenuInfo> infos)
         {
+            if (infos == cache)
+            {
+                return;
+            }
             try
             {
                 cache.Clear();
index f0575e17cf9a7d91231b7120cc6420563eb22142..08dd0f03ad0ddf5b60cd90715bf11f37ac477595 100644 (file)
@@ -34,6 +34,7 @@ namespace SettingView
         private static SettingViewBorder appCustomBorder;
         private ContentPage mMainPage;
         private static Task rowsCreated;
+        private static Task gadgetManagerInitialized;
 
         public Program(Size2D windowSize, Position2D windowPosition, ThemeOptions themeOptions, IBorderInterface borderInterface)
             : base(windowSize, windowPosition, themeOptions, borderInterface)
@@ -46,9 +47,9 @@ namespace SettingView
 
             mMainPage = CreateMainPage();
 
-            bool initilized = GadgetManager.Instance.Init();
-            mMainPage.Content = initilized ? CreateContent() : GetTextNotice("Failed to initialize GadgetManager.\nPlease check error logs for more information.", Color.Red);
-            _ = SaveCustomization();
+            mMainPage.Content = CreateContent();
+            gadgetManagerInitialized = InitGadgetManager();
+            _ = CheckCustomization();
 
             var navigator = new SettingNavigation();
             navigator.WidthResizePolicy = ResizePolicyType.FillToParent;
@@ -94,12 +95,38 @@ namespace SettingView
             LogScalableInfo();
         }
 
-        private async Task SaveCustomization()
+        private async Task InitGadgetManager()
         {
             await rowsCreated;
             await Task.Run(() =>
             {
-                GadgetManager.Instance.SaveCustomizationToFiles();
+                GadgetManager.Instance.Init();
+                return true;
+            });
+        }
+
+        private async Task CheckCustomization()
+        {
+            await gadgetManagerInitialized;
+            await Task.Run(async () =>
+            {
+                var customizationMainMenus = GadgetManager.Instance.GetMainWithCurrentOrder();
+
+                var customizationMainMenusStr = customizationMainMenus.Where(i => i.IsVisible).Select(x => new string(x.Path));
+                var cacheMainMenuStr = MainMenuInfo.CacheMenu.Select(x => new string(x.Path));
+
+                if (!customizationMainMenusStr.SequenceEqual(cacheMainMenuStr))
+                {
+                    Logger.Verbose($"customization has changed. Reload main view.");
+                    if (mMainPage != null)
+                    {
+                        await Post(() =>
+                        {
+                            mMainPage.Content = CreateContent(true);
+                            return true;
+                        });
+                    }
+                }
                 return true;
             });
         }
@@ -162,7 +189,7 @@ namespace SettingView
 
             if (mMainPage != null && items.Any())
             {
-                mMainPage.Content = CreateContent();
+                mMainPage.Content = CreateContent(true);
             }
         }
 
@@ -245,20 +272,8 @@ namespace SettingView
             });
         }
 
-        private static View CreateContent()
+        private static View CreateContent(bool customizationChanged = false)
         {
-            var mainMenus = GadgetManager.Instance.GetMainWithCurrentOrder();
-            if (!mainMenus.Any())
-            {
-                return GetTextNotice("There is no setting menus installed.", Color.Orange);
-            }
-
-            var visibleMenus = mainMenus.Where(i => i.IsVisible);
-            if (!visibleMenus.Any())
-            {
-                return GetTextNotice("There is no setting menus visible.", Color.Gray);
-            }
-
             var content = new ScrollableBase()
             {
                 WidthSpecification = LayoutParamPolicies.MatchParent,
@@ -271,25 +286,54 @@ namespace SettingView
                 },
             };
 
-            rowsCreated = CreateContentRows(visibleMenus, content);
+            rowsCreated = CreateContentRows(content, customizationChanged);
 
             return content;
         }
 
-        private static System.Threading.Tasks.Task CreateContentRows(IEnumerable<SettingGadgetInfo> visibleMenus, View content)
+        private static System.Threading.Tasks.Task CreateContentRows(View content, bool customizationChanged = false)
         {
             return System.Threading.Tasks.Task.Run(async () =>
             {
-                var menus = new List<MainMenuInfo>();
                 System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
                 stopwatch.Start();
-                foreach (var gadgetInfo in visibleMenus)
+                var menus = MainMenuInfo.CacheMenu;
+                if (menus.Count == 0 || customizationChanged)
                 {
-                    if (MainMenuInfo.Create(gadgetInfo) is MainMenuInfo menu)
+                    var mainMenus = GadgetManager.Instance.GetMainWithCurrentOrder();
+                    if (!mainMenus.Any())
+                    {
+                        await Post(() =>
+                        {
+                            var textLabel = GetTextNotice("There is no setting menus installed.", Color.Orange);
+                            content.Add(textLabel);
+                            return true;
+                        });
+                        return;
+                    }
+
+                    var visibleMenus = mainMenus.Where(i => i.IsVisible);
+                    if (!visibleMenus.Any())
+                    {
+                        await Post(() =>
+                        {
+                            var textLabel = GetTextNotice("There is no setting menus visible.", Color.Gray);
+                            content.Add(textLabel);
+                            return true;
+                        });
+                        return;
+                    }
+
+                    menus = new List<MainMenuInfo>();
+                    foreach (var gadgetInfo in visibleMenus)
                     {
-                        menus.Add(menu);
+                        if (MainMenuInfo.Create(gadgetInfo) is MainMenuInfo menu)
+                        {
+                            menus.Add(menu);
+                        }
                     }
                 }
+
                 stopwatch.Stop();
                 Logger.Debug($"MEASURE loaded all MainMenuInfos, total time: {stopwatch.Elapsed}");