129 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			129 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
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 List<IGallerySource> GallerySources { get; } = new()
 | 
						|
        {
 | 
						|
            new Yandere.GallerySource(),    // https://yande.re
 | 
						|
            new Danbooru.GallerySource(),   // https://danbooru.donmai.us
 | 
						|
            new Gelbooru.GallerySource()    // https://gelbooru.com
 | 
						|
        };
 | 
						|
 | 
						|
        public App()
 | 
						|
        {
 | 
						|
            Preferences.Set(Config.IsProxiedKey, true);
 | 
						|
            Preferences.Set(Config.ProxyHostKey, "192.168.25.9");
 | 
						|
            Preferences.Set(Config.ProxyPortKey, 1081);
 | 
						|
        }
 | 
						|
 | 
						|
        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;
 | 
						|
 | 
						|
            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);
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |