move service to shared project.

This commit is contained in:
2020-05-11 01:16:33 +08:00
parent 98007c98e4
commit 134affddaa
11 changed files with 60 additions and 142 deletions

View File

@@ -18,22 +18,9 @@ namespace Pixiview
// public properties
public static AppTheme CurrentTheme { get; private set; }
public static PlatformCulture CurrentCulture { get; private set; }
public static Dictionary<string, object> ExtraResources { get; private set; }
private void InitResources(IEnvironmentService service)
private void InitResources()
{
var p = service.GetEnvironment();
ExtraResources = new Dictionary<string, object>
{
{ ThemeBase.IconLightFontFamily, p.IconLightFontFamily },
{ ThemeBase.IconRegularFontFamily, p.IconRegularFontFamily },
{ ThemeBase.IconSolidFontFamily, p.IconSolidFontFamily },
{ ThemeBase.IconLeft, p.IconLeft }
};
//var theme = service.GetApplicationTheme();
var theme = AppInfo.RequestedTheme;
SetTheme(theme, true);
}
@@ -61,10 +48,10 @@ namespace Pixiview
}
}
private void InitLanguage(IEnvironmentService service)
private void InitLanguage()
{
var ci = service.GetCurrentCultureInfo();
service.SetCultureInfo(ci);
var ci = EnvironmentService.GetCurrentCultureInfo();
EnvironmentService.SetCultureInfo(ci);
CurrentCulture = new PlatformCulture(ci.Name.ToLower());
}
@@ -88,19 +75,20 @@ namespace Pixiview
{
themeInstance = LightTheme.Instance;
}
//DependencyService.Get<IEnvironmentService>().SetStatusBarColor
#if __ANDROID__
EnvironmentService.SetStatusBarColor
#endif
var style = (StatusBarStyles)themeInstance[ThemeBase.StatusBarStyle];
DependencyService.Get<IEnvironmentService>().SetStatusBarStyle(style);
EnvironmentService.SetStatusBarStyle(style);
Resources = themeInstance;
}
protected override void OnStart()
{
var service = DependencyService.Get<IEnvironmentService>();
InitLanguage(service);
InitLanguage();
MainPage = new AppShell();
InitResources(service);
InitResources();
InitPreferences();
}
@@ -110,7 +98,6 @@ namespace Pixiview
protected override void OnResume()
{
//var theme = DependencyService.Get<IEnvironmentService>().GetApplicationTheme();
var theme = AppInfo.RequestedTheme;
SetTheme(theme);
}

View File

@@ -345,8 +345,7 @@ namespace Pixiview.Illust
{
MainThread.BeginInvokeOnMainThread(async () =>
{
var service = DependencyService.Get<IFileStore>();
var result = await service.SaveImageToGalleryAsync(image);
var result = await FileStore.SaveImageToGalleryAsync(image);
string message = result ?? ResourceHelper.SaveSuccess;
await DisplayAlert(ResourceHelper.Title, message, ResourceHelper.Ok);

View File

@@ -73,8 +73,8 @@
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Utils\Converters.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\Extensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\IEnvironmentService.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\IFileStore.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\EnvironmentService.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\FileStore.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\IllustData.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\LongPressEffect.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\Stores.cs" />

View File

@@ -7,13 +7,8 @@ namespace Pixiview.UI
public static class StyleDefinition
{
public const double FontSizeTitle = 18.0;
public const double FontSizeTitleIcon = 24.0;
public static readonly Thickness ScreenBottomPadding;
public static readonly Thickness HorizonLeft10 = new Thickness(10, 0, 0, 0);
public static readonly Thickness HorizonRight10 = new Thickness(0, 0, 10, 0);
public static readonly Thickness LeftBottom10 = new Thickness(10, 0, 0, 10);
public static readonly GridLength TitleIconWidth = 40;
public static readonly Color ColorLightShadow = Color.FromRgba(0, 0, 0, 0x20);
public static readonly Color ColorDeepShadow = Color.FromRgba(0, 0, 0, 0x50);
public static readonly Color ColorRedBackground = Color.FromRgb(0xfd, 0x43, 0x63);
@@ -22,6 +17,16 @@ namespace Pixiview.UI
public static readonly double FontSizeMicro = Device.GetNamedSize(NamedSize.Micro, typeof(Label));
public static readonly double FontSizeSmall = Device.GetNamedSize(NamedSize.Small, typeof(Label));
#if __IOS__
public const string IconLightFontFamily = "FontAwesome5Pro-Light";
public const string IconRegularFontFamily = "FontAwesome5Pro-Regular";
public const string IconSolidFontFamily = "FontAwesome5Pro-Solid";
public const string IconLeft = "\uf104";
#else
// TODO
#endif
public const string IconUser = "\uf007";
public const string IconSparkles = "\uf890";
public const string IconOrder = "\uf88f";
@@ -29,7 +34,6 @@ namespace Pixiview.UI
public const string IconRefresh = "\uf2f1";
public const string IconLove = "\uf004";
public const string IconOption = "\uf013";
public const string IconDownload = "\uf019";
public const string IconFavorite = "\uf02e";
public const string IconShare = "\uf35d";

View File

@@ -11,7 +11,6 @@ namespace Pixiview.UI.Theme
public const string FontIconLove = nameof(FontIconLove);
public const string FontIconNotLove = nameof(FontIconNotLove);
public const string FontIconOption = nameof(FontIconOption);
public const string FontIconDownload = nameof(FontIconDownload);
public const string FontIconFavorite = nameof(FontIconFavorite);
public const string FontIconShare = nameof(FontIconShare);
@@ -29,36 +28,20 @@ namespace Pixiview.UI.Theme
public const string IconLightFontFamily = nameof(IconLightFontFamily);
public const string IconRegularFontFamily = nameof(IconRegularFontFamily);
public const string IconSolidFontFamily = nameof(IconSolidFontFamily);
public const string IconLeft = nameof(IconLeft);
public const string FontSizeTitle = nameof(FontSizeTitle);
public const string FontSizeTitleIcon = nameof(FontSizeTitleIcon);
//public const string Horizon10 = nameof(Horizon10);
public const string ScreenBottomPadding = nameof(ScreenBottomPadding);
public const string NavigationBarHeight = nameof(NavigationBarHeight);
public const string IconRefresh = nameof(IconRefresh);
public const string IconOption = nameof(IconOption);
public const string IconDownload = nameof(IconDownload);
protected void InitResources()
{
Add(FontSizeTitle, StyleDefinition.FontSizeTitle);
Add(FontSizeTitleIcon, StyleDefinition.FontSizeTitleIcon);
//Add(Horizon10, StyleDefinition.Horizon10);
Add(ScreenBottomPadding, StyleDefinition.ScreenBottomPadding);
Add(IconRefresh, StyleDefinition.IconRefresh);
Add(IconOption, StyleDefinition.IconOption);
Add(IconDownload, StyleDefinition.IconDownload);
if (App.ExtraResources != null)
{
foreach (var kv in App.ExtraResources)
{
Add(kv.Key, kv.Value);
}
}
Add(IconLightFontFamily, StyleDefinition.IconLightFontFamily);
Add(IconRegularFontFamily, StyleDefinition.IconRegularFontFamily);
Add(IconSolidFontFamily, StyleDefinition.IconSolidFontFamily);
var regularFontFamily = (string)this[IconRegularFontFamily];
var solidFontFamily = (string)this[IconSolidFontFamily];
var regularFontFamily = StyleDefinition.IconRegularFontFamily;
var solidFontFamily = StyleDefinition.IconSolidFontFamily;
Add(FontIconLove, GetSolidIcon(StyleDefinition.IconLove, solidFontFamily, StyleDefinition.ColorRedBackground));
Add(FontIconUser, GetSolidIcon(StyleDefinition.IconUser, solidFontFamily));
@@ -67,7 +50,6 @@ namespace Pixiview.UI.Theme
Add(FontIconRefresh, GetSolidIcon(StyleDefinition.IconRefresh, solidFontFamily));
Add(FontIconNotLove, GetSolidIcon(StyleDefinition.IconLove, regularFontFamily));
Add(FontIconOption, GetSolidIcon(StyleDefinition.IconOption, solidFontFamily));
Add(FontIconDownload, GetSolidIcon(StyleDefinition.IconDownload, solidFontFamily));
Add(FontIconFavorite, GetSolidIcon(StyleDefinition.IconFavorite, solidFontFamily));
Add(FontIconShare, GetSolidIcon(StyleDefinition.IconShare, solidFontFamily));
}

View File

@@ -0,0 +1,183 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Threading;
using Pixiview.Resources;
#if __IOS__
using Foundation;
using UIKit;
#endif
namespace Pixiview.Utils
{
public class EnvironmentService
{
#region - Theme -
/*
[SuppressMessage("Code Notifications", "XI0002:Notifies you from using newer Apple APIs when targeting an older OS version", Justification = "<Pending>")]
public AppTheme GetApplicationTheme()
{
if (UIDevice.CurrentDevice.CheckSystemVersion(12, 0))
{
var currentController = Platform.GetCurrentUIViewController();
if (currentController == null)
{
return AppTheme.Unspecified;
}
var style = currentController.TraitCollection.UserInterfaceStyle;
if (style == UIUserInterfaceStyle.Dark)
{
return AppTheme.Dark;
}
else if (style == UIUserInterfaceStyle.Light)
{
return AppTheme.Light;
}
}
return AppTheme.Unspecified;
}
//*/
public static void SetStatusBarStyle(StatusBarStyles style)
{
SetStatusBarStyle(ConvertStyle(style));
}
public static void SetStatusBarStyle(UIStatusBarStyle style)
{
if (UIApplication.SharedApplication.StatusBarStyle == style)
{
return;
}
if (style == UIStatusBarStyle.BlackOpaque)
{
UIApplication.SharedApplication.SetStatusBarHidden(true, true);
}
else
{
UIApplication.SharedApplication.SetStatusBarStyle(style, true);
UIApplication.SharedApplication.SetStatusBarHidden(false, true);
}
}
[SuppressMessage("Code Notifications", "XI0002:Notifies you from using newer Apple APIs when targeting an older OS version", Justification = "<Pending>")]
public static UIStatusBarStyle ConvertStyle(StatusBarStyles style)
{
switch (style)
{
case StatusBarStyles.DarkText:
return UIStatusBarStyle.DarkContent;
case StatusBarStyles.WhiteText:
return UIStatusBarStyle.LightContent;
case StatusBarStyles.Hidden:
return UIStatusBarStyle.BlackOpaque;
case StatusBarStyles.Default:
default:
return UIStatusBarStyle.Default;
}
}
#endregion
#region - Culture Info -
public static void SetCultureInfo(CultureInfo ci)
{
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
App.DebugPrint($"CurrentCulture set: {ci.Name}");
}
public static CultureInfo GetCurrentCultureInfo()
{
string lang;
if (NSLocale.PreferredLanguages.Length > 0)
{
var pref = NSLocale.PreferredLanguages[0];
lang = iOSToDotnetLanguage(pref);
}
else
{
lang = "zh-CN";
}
CultureInfo ci;
var platform = new PlatformCulture(lang);
try
{
ci = new CultureInfo(platform.Language);
}
catch (CultureNotFoundException e)
{
try
{
var fallback = ToDotnetFallbackLanguage(platform);
App.DebugPrint($"{lang} failed, trying {fallback} ({e.Message})");
ci = new CultureInfo(fallback);
}
catch (CultureNotFoundException e1)
{
App.DebugError("culture.get", $"{lang} couldn't be set, using 'zh-CN' ({e1.Message})");
ci = new CultureInfo("zh-CN");
}
}
return ci;
}
[SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "<Pending>")]
private static string iOSToDotnetLanguage(string iOSLanguage)
{
string netLanguage;
//certain languages need to be converted to CultureInfo equivalent
switch (iOSLanguage)
{
case "ms-MY": // "Malaysian (Malaysia)" not supported .NET culture
case "ms-SG": // "Malaysian (Singapore)" not supported .NET culture
netLanguage = "ms"; // closest supported
break;
case "gsw-CH": // "Schwiizertüütsch (Swiss German)" not supported .NET culture
netLanguage = "de-CH"; // closest supported
break;
// add more application-specific cases here (if required)
// ONLY use cultures that have been tested and known to work
default:
netLanguage = iOSLanguage;
break;
}
App.DebugPrint($"iOS Language: {iOSLanguage}, .NET Language/Locale: {netLanguage}");
return netLanguage;
}
private static string ToDotnetFallbackLanguage(PlatformCulture platCulture)
{
string netLanguage;
switch (platCulture.LanguageCode)
{
//
case "pt":
netLanguage = "pt-PT"; // fallback to Portuguese (Portugal)
break;
case "gsw":
netLanguage = "de-CH"; // equivalent to German (Switzerland) for this app
break;
// add more application-specific cases here (if required)
// ONLY use cultures that have been tested and known to work
default:
netLanguage = platCulture.LanguageCode; // use the first part of the identifier (two chars, usually);
break;
}
App.DebugPrint($".NET Fallback Language/Locale: {platCulture.LanguageCode} to {netLanguage} (application-specific)");
return netLanguage;
}
#endregion
}
}

View File

@@ -0,0 +1,49 @@
using System.Threading.Tasks;
using Xamarin.Forms;
#if __IOS__
using Xamarin.Forms.Platform.iOS;
#else
using Xamarin.Forms.Platform.Android;
#endif
namespace Pixiview.Utils
{
public class FileStore
{
public static Task<string> SaveImageToGalleryAsync(ImageSource image)
{
#if __IOS__
IImageSourceHandler renderer;
if (image is UriImageSource)
{
renderer = new ImageLoaderSourceHandler();
}
else if (image is FileImageSource)
{
renderer = new FileImageSourceHandler();
}
else
{
renderer = new StreamImagesourceHandler();
}
var photo = renderer.LoadImageAsync(image).Result;
var task = new TaskCompletionSource<string>();
if (photo == null)
{
task.SetResult(null);
}
else
{
photo.SaveToPhotosAlbum((img, error) =>
{
task.SetResult(error?.ToString());
});
}
return task.Task;
#else
// TODO
#endif
}
}
}

View File

@@ -1,25 +0,0 @@
using System.Globalization;
using Xamarin.Forms;
namespace Pixiview.Utils
{
public interface IEnvironmentService
{
EnvironmentParameter GetEnvironment();
//AppTheme GetApplicationTheme();
void SetStatusBarStyle(StatusBarStyles style);
void SetStatusBarColor(Color color);
CultureInfo GetCurrentCultureInfo();
void SetCultureInfo(CultureInfo ci);
}
public class EnvironmentParameter
{
public string IconLightFontFamily { get; set; }
public string IconRegularFontFamily { get; set; }
public string IconSolidFontFamily { get; set; }
public string IconLeft { get; set; }
}
}

View File

@@ -1,10 +0,0 @@
using System.Threading.Tasks;
using Xamarin.Forms;
namespace Pixiview.Utils
{
public interface IFileStore
{
Task<string> SaveImageToGalleryAsync(ImageSource image);
}
}