diff --git a/Samples/MaterialMvvmSample/App.xaml b/Samples/MaterialMvvmSample/App.xaml index f1403db6..b21309ca 100644 --- a/Samples/MaterialMvvmSample/App.xaml +++ b/Samples/MaterialMvvmSample/App.xaml @@ -7,6 +7,7 @@ xmlns:ui="clr-namespace:XF.Material.Forms.UI;assembly=XF.Material"> + + diff --git a/Samples/MaterialMvvmSample/App.xaml.cs b/Samples/MaterialMvvmSample/App.xaml.cs index 49389861..5d224d92 100644 --- a/Samples/MaterialMvvmSample/App.xaml.cs +++ b/Samples/MaterialMvvmSample/App.xaml.cs @@ -10,10 +10,9 @@ public partial class App : Application { public App(INavigationService navigationService) { + XF.Material.Forms.Material.Init(this); InitializeComponent(); - - XF.Material.Forms.Material.Init(this, "Material.Style"); - XamSvg.Shared.Config.ResourceAssembly = typeof(App).Assembly; + XF.Material.Forms.Material.Use("Material.Style"); navigationService.SetRootView(ViewNames.LandingView); } diff --git a/XF.Material/Material.cs b/XF.Material/Material.cs index b72188f3..63974c91 100644 --- a/XF.Material/Material.cs +++ b/XF.Material/Material.cs @@ -12,27 +12,32 @@ namespace XF.Material.Forms public class Material { private static readonly Lazy MaterialUtilityInstance = new Lazy(() => DependencyService.Get()); - private readonly MaterialConfiguration _config; - private static ResourceDictionary _res; + private static Material Instance; - internal Material(Application app, MaterialConfiguration materialResource) : this(app) - { - _config = materialResource; - } + private ResourceDictionary _res; - internal Material(Application app, string key) : this(app) + internal Material(Application app, MaterialConfiguration config = null) { - _config = GetResource(key); + _res = app.Resources; + Instance = this; + MergeMaterialDictionaries(config, config == null); } - internal Material(Application app) + // internal Material(Application app, string key) : this(app) + // { + // _res = app.Resources; + // Instance = this; + // var config = GetResource(key); + // MergeMaterialDictionaries(config, config == null); + // } + + public static void Use(string key) { - _res = app.Resources; - _config = new MaterialConfiguration - { - ColorConfiguration = new MaterialColorConfiguration(), - FontConfiguration = new MaterialFontConfiguration() - }; + //Replace instance + //new Material(Application.Current, GetResource(key)); + var config = GetResource(key); + if(config != null) + Instance.MergeMaterialDictionaries(config, false); } /// @@ -49,17 +54,15 @@ internal Material(Application app) /// public static T GetResource(string key) { - _res.TryGetValue(key ?? throw new ArgumentNullException(nameof(key)), out var value); + if(Instance._res == null) + throw new Exception("You must call one of the Init() methods in App.xaml.cs before InitializeComponent()"); - if (value is T resource) - { + if (Instance._res.TryGetValue(key ?? throw new ArgumentNullException(nameof(key)), out var value) + && value is T resource) return resource; - } if (value != null) - { throw new InvalidCastException($"The resource retrieved was not of the type {typeof(T)}. Use {value.GetType()} instead."); - } return default; } @@ -72,21 +75,20 @@ public static T GetResource(string key) /// public static void Init(Application app, MaterialConfiguration materialResource) { - var material = new Material(app ?? throw new ArgumentNullException(nameof(app)), materialResource ?? throw new ArgumentNullException(nameof(materialResource))); - material.MergeMaterialDictionaries(); + new Material(app ?? throw new ArgumentNullException(nameof(app)), materialResource ?? throw new ArgumentNullException(nameof(materialResource))); } - /// - /// Configure's the current app's resources by merging pre-defined Material resources and creating new resources based on the 's properties. - /// - /// The cross-platform mobile application that is running. - /// The key of the object in the current app's resource dictionary. - /// - public static void Init(Application app, string key) - { - var material = new Material(app ?? throw new ArgumentNullException(nameof(app)), key ?? throw new ArgumentNullException(nameof(key))); - material.MergeMaterialDictionaries(); - } + ///// + ///// Configure's the current app's resources by merging pre-defined Material resources and creating new resources based on the 's properties. + ///// + ///// The cross-platform mobile application that is running. + ///// The key of the object in the current app's resource dictionary. + ///// + //public static void Init(Application app, string key) + //{ + // var material = new Material(app ?? throw new ArgumentNullException(nameof(app)), key ?? throw new ArgumentNullException(nameof(key))); + // material.MergeMaterialDictionaries(); + //} /// /// Configure's the current app's resources by merging pre-defined Material resources. @@ -95,15 +97,47 @@ public static void Init(Application app, string key) /// public static void Init(Application app) { - var material = new Material(app ?? throw new ArgumentNullException(nameof(app))); - material.MergeMaterialDictionaries(); + new Material(app ?? throw new ArgumentNullException(nameof(app))); } - private void MergeMaterialDictionaries() + Lazy defaultColors = new Lazy(() => new MaterialColors(new MaterialColorConfiguration())); + Lazy defaultTypos = new Lazy(() => new MaterialTypography(new MaterialFontConfiguration())); + Lazy defaultSizes = new Lazy(() => new MaterialSizes()); + + private void MergeMaterialDictionaries(MaterialConfiguration config, bool addDefaults) { - _res.MergedDictionaries.Add(new MaterialColors(_config.ColorConfiguration ?? new MaterialColorConfiguration())); - _res.MergedDictionaries.Add(new MaterialTypography(_config.FontConfiguration ?? new MaterialFontConfiguration())); - _res.MergedDictionaries.Add(new MaterialSizes()); + if(config != null) + { + if (config.ColorConfiguration != null) + { + _res.MergedDictionaries.Remove(defaultColors.Value); + _res.MergedDictionaries.Add(new MaterialColors(config.ColorConfiguration)); + } + else if(addDefaults &&!_res.MergedDictionaries.Contains((defaultColors.Value))) + _res.MergedDictionaries.Add(defaultColors.Value); + + if (config.FontConfiguration != null) + { + _res.MergedDictionaries.Remove(defaultTypos.Value); + _res.MergedDictionaries.Add(new MaterialTypography(config.FontConfiguration)); + } + else if(addDefaults &&!_res.MergedDictionaries.Contains((defaultTypos.Value))) + _res.MergedDictionaries.Add(defaultTypos.Value); + } + + if (addDefaults) + { + if (config == null) + { + if(!_res.MergedDictionaries.Contains((defaultColors.Value))) + _res.MergedDictionaries.Add(defaultColors.Value); + if(!_res.MergedDictionaries.Contains((defaultTypos.Value))) + _res.MergedDictionaries.Add(defaultTypos.Value); + } + + if(!_res.MergedDictionaries.Contains((defaultSizes.Value))) + _res.MergedDictionaries.Add(defaultSizes.Value); + } } /// @@ -185,7 +219,7 @@ private static Xamarin.Forms.Color GetSecondaryColor() /// /// Static class that contains the current Material font family values. /// - public static class FontFamily + public static class FontFamily { /// /// Body 1 font family, used for long-form writing and small text sizes.