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;
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
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;
+ }
}
}
using SettingCore;
using SettingCore.Views;
using SettingView.TextResources;
+using System;
using System.Collections.Generic;
using System.Linq;
using Tizen.Applications;
{
if (mMainPage != null && mMainPage.AppBar != null)
{
+ MainMenuInfo.ClearCache();
+
mMainPage.AppBar.Title = Resources.IDS_ST_OPT_SETTINGS;
mMainPage.Content = CreateContent();
}
{
if (mMainPage != null && e.IsPlatformThemeChanged)
{
+ MainMenuInfo.ClearCache();
+
// recreate main page content just to apply new colors from gadgets
mMainPage.Content = CreateContent();
}
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);
});
}