using Xamarin.Forms; using Gallery.Services; using Xamarin.Essentials; using Gallery.Resources; using Gallery.Util; using Gallery.Resources.Theme; using System.Collections.Generic; using Gallery.Util.Interface; using Gallery.Resources.UI; namespace Gallery { public partial class App : Application { public static AppTheme CurrentTheme { get; private set; } public static PlatformCulture CurrentCulture { get; private set; } public static Dictionary RefreshTimes { get; } = new(); public static List GallerySources { get; } = new() { new Sources.Yandere.GallerySource(), // https://yande.re new Sources.Danbooru.GallerySource(), // https://danbooru.donmai.us new Sources.Gelbooru.GallerySource() // https://gelbooru.com }; public App() { //Device.SetFlags(new string[0]); } private void InitResource() { foreach (var source in GallerySources) { source.InitDynamicResources(Definition.IconSolidFamily, LightTheme.Instance, DarkTheme.Instance); } var theme = AppInfo.RequestedTheme; SetTheme(theme, true); } private void InitPreference() { Config.Proxy = null; Config.DownloadThreads = Preferences.Get(Config.DownloadThreadsKey, 1); var isProxied = Preferences.Get(Config.IsProxiedKey, false); if (isProxied) { var host = Preferences.Get(Config.ProxyHostKey, null); var port = Preferences.Get(Config.ProxyPortKey, 0); if (!string.IsNullOrEmpty(host) && port > 0) { try { if (host.IndexOf(':') >= 0) { host = $"[{host}]"; } var uri = new System.Uri($"http://{host}:{port}"); Config.Proxy = new System.Net.WebProxy(uri, true); #if DEBUG Log.Print($"load proxy: {uri}"); #endif } catch (System.Exception ex) { Log.Error("preferences.init", $"failed to parse proxy: {host}:{port}, error: {ex.Message}"); } } } } private void InitLanguage() { var ci = Environment.GetCurrentCultureInfo(); Environment.SetCultureInfo(ci); CurrentCulture = new PlatformCulture(ci.Name.ToLower()); } private void SetTheme(AppTheme theme, bool force = false) { if (force || theme != CurrentTheme) { CurrentTheme = theme; } else { return; } #if DEBUG Log.Print($"application theme: {theme}"); #endif Theme themeInstance = theme switch { AppTheme.Dark => DarkTheme.Instance, _ => LightTheme.Instance }; #if __IOS__ var style = (StatusBarStyles)themeInstance[Theme.StatusBarStyle]; Environment.SetStatusBarStyle(style); #elif __ANDROID__ var color = (Color)themeInstance[Theme.NavigationColor]; Environment.SetStatusBarColor(color); #endif Resources = themeInstance; } protected override void OnStart() { InitLanguage(); MainPage = new AppShell(); InitResource(); InitPreference(); } protected override void OnSleep() { } protected override void OnResume() { var theme = AppInfo.RequestedTheme; SetTheme(theme); } } }