From 4196d561f8ea274b762384914937583e161b6130 Mon Sep 17 00:00:00 2001 From: amazing-username Date: Tue, 11 Jun 2019 17:47:24 -0700 Subject: [PATCH] Added functionality to switch the color scheme. #5 #14 --- Mear/Mear.Android/Resources/values/colors.xml | 2 +- Mear/Mear.Android/Resources/values/styles.xml | 6 +- Mear/Mear/App.xaml | 83 +++++++++-------- Mear/Mear/Managers/ThemeManager.cs | 89 +++++++++++++++++++ .../Database/DBSettingsRepository.cs | 16 ++++ Mear/Mear/ViewModels/SettingsViewModel.cs | 6 ++ Mear/Mear/Views/MusicLibrary.xaml.cs | 3 + Mear/Mear/Views/SettingsPage.xaml | 11 ++- Mear/Mear/Views/SettingsPage.xaml.cs | 5 +- 9 files changed, 173 insertions(+), 48 deletions(-) create mode 100644 Mear/Mear/Managers/ThemeManager.cs diff --git a/Mear/Mear.Android/Resources/values/colors.xml b/Mear/Mear.Android/Resources/values/colors.xml index 4471e04..d4001b2 100644 --- a/Mear/Mear.Android/Resources/values/colors.xml +++ b/Mear/Mear.Android/Resources/values/colors.xml @@ -1,7 +1,7 @@ #000000 - #555555 + #360485 #000000 #bbaa11 diff --git a/Mear/Mear.Android/Resources/values/styles.xml b/Mear/Mear.Android/Resources/values/styles.xml index 9e6b4ee..0d9b925 100644 --- a/Mear/Mear.Android/Resources/values/styles.xml +++ b/Mear/Mear.Android/Resources/values/styles.xml @@ -14,12 +14,12 @@ - #222222 + #333333 #000000 - #FF4081 + #9955fa true @@ -28,6 +28,6 @@ diff --git a/Mear/Mear/App.xaml b/Mear/Mear/App.xaml index 012cfa8..ec1a171 100644 --- a/Mear/Mear/App.xaml +++ b/Mear/Mear/App.xaml @@ -3,72 +3,77 @@ xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Mear.App"> - #000010 + #101010 + #9955fa #ffffff - #000000 + #ffffff #ffffff #000000 #333333 - #660066 - #ffccff - #ffe6ff + #360485 + #ffffff + #9555fa - #ffffee - #aaaaaa + #ffffff + #333333 - #ffffee + #ffffff + #a300ff + #111111 - - #ffffff + #222222 + #915dfa + + #222222 + #9955fa + #eeffff + #ffffff + + - - - + + --> - \ No newline at end of file diff --git a/Mear/Mear/Managers/ThemeManager.cs b/Mear/Mear/Managers/ThemeManager.cs new file mode 100644 index 0000000..2f80550 --- /dev/null +++ b/Mear/Mear/Managers/ThemeManager.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Text; + +using Xamarin.Forms; + +using Mear.Models; +using Mear.Repositories.Database; + +namespace Mear.Managers +{ + public class ThemeManager + { + #region Methods + public void InitTheme() + { + var settingsRepo = new DBSettingsRepository(); + if (settingsRepo.IsDarkThemeOn()) + { + ApplyDarkTheme(); + } + else + { + ApplyLightTheme(); + } + } + public void ChangeTheme(bool? isDarkTheme = null) + { + var settingsRepo = new DBSettingsRepository(); + if (isDarkTheme == null) + { + settingsRepo.UpdateDarkTheme(); + } + else + { + settingsRepo.UpdateDarkTheme(isDarkTheme.Value); + } + + if (settingsRepo.IsDarkThemeOn()) + { + ApplyDarkTheme(); + } + else + { + ApplyLightTheme(); + } + } + + private void ApplyDarkTheme() + { + App.Current.Resources["NavigationPageColor"] = Color.FromHex("#101010"); + App.Current.Resources["BarTextColor"] = Color.FromHex("#ffffff"); + App.Current.Resources["ButtonColor"] = Color.FromHex("#360485"); + App.Current.Resources["ButtonTextColor"] = Color.FromHex("#ffffff"); + App.Current.Resources["ButtonBorderColor"] = Color.FromHex("#9955fa"); + App.Current.Resources["LabelTextColor"] = Color.FromHex("#ffffff"); + App.Current.Resources["LabelBackgroundColor"] = Color.FromHex("#333333"); + App.Current.Resources["EntryTextColor"] = Color.FromHex("#ffffff"); + App.Current.Resources["EntryPlaceholderTextColor"] = Color.FromHex("#a300ff"); + App.Current.Resources["EntryBackgroundColor"] = Color.FromHex("#111111"); + App.Current.Resources["SwitchBackgroundColor"] = Color.FromHex("#222222"); + App.Current.Resources["SwitchActiveColor"] = Color.FromHex("#915dfa"); + App.Current.Resources["SearchBarBackgroundColor"] = Color.FromHex("#222222"); + App.Current.Resources["SearchBarCancelButtonColor"] = Color.FromHex("#9955fa"); + App.Current.Resources["SearchBarPlaceholderTextColor"] = Color.FromHex("eeffff"); + App.Current.Resources["SearchBarTextColor"] = Color.FromHex("#ffffff"); + } + private void ApplyLightTheme() + { + App.Current.Resources["NavigationPageColor"] = Color.FromHex("#eeffff"); + App.Current.Resources["BarTextColor"] = Color.FromHex("#000000"); + App.Current.Resources["ButtonColor"] = Color.FromHex("#9955fa"); + App.Current.Resources["ButtonTextColor"] = Color.FromHex("#000000"); + App.Current.Resources["ButtonBorderColor"] = Color.FromHex("#360485"); + App.Current.Resources["LabelTextColor"] = Color.FromHex("#000000"); + App.Current.Resources["LabelBackgroundColor"] = Color.FromHex("efffff"); + App.Current.Resources["EntryTextColor"] = Color.FromHex("#000000"); + App.Current.Resources["EntryPlaceholderTextColor"] = Color.FromHex("#231412"); + App.Current.Resources["EntryBackgroundColor"] = Color.FromHex("#efffff"); + App.Current.Resources["SwitchBackgroundColor"] = Color.FromHex("#eeffff"); + App.Current.Resources["SwitchActiveColor"] = Color.FromHex("#360485"); + App.Current.Resources["SearchBarBackgroundColor"] = Color.FromHex("#eeffff"); + App.Current.Resources["SearchBarCancelButtonColor"] = Color.FromHex("#360485"); + App.Current.Resources["SearchBarPlaceholderTextColor"] = Color.FromHex("#111111"); + App.Current.Resources["SearchBarTextColor"] = Color.FromHex("#000000"); + } + #endregion + } +} diff --git a/Mear/Mear/Repositories/Database/DBSettingsRepository.cs b/Mear/Mear/Repositories/Database/DBSettingsRepository.cs index ace75a4..aded4b9 100644 --- a/Mear/Mear/Repositories/Database/DBSettingsRepository.cs +++ b/Mear/Mear/Repositories/Database/DBSettingsRepository.cs @@ -66,6 +66,22 @@ namespace Mear.Repositories.Database var msg = ex.Message; } } + public void UpdateDarkTheme(bool isDarkTheme) + { + try + { + if (DoesTableExist(_tableName)) + { + var settings = RetrieveSettings(); + settings.DarkTheme = isDarkTheme; + _Db.Update(settings); + } + } + catch (Exception ex) + { + var msg = ex.Message; + } + } private Settings RetrieveSettings() { diff --git a/Mear/Mear/ViewModels/SettingsViewModel.cs b/Mear/Mear/ViewModels/SettingsViewModel.cs index d42ddb5..46bd398 100644 --- a/Mear/Mear/ViewModels/SettingsViewModel.cs +++ b/Mear/Mear/ViewModels/SettingsViewModel.cs @@ -5,6 +5,7 @@ using System.Text; using Xamarin.Forms; +using Mear.Managers; using Mear.Repositories.Database; namespace Mear.ViewModels @@ -36,6 +37,11 @@ namespace Mear.ViewModels #region Methods + public void ToggleTheme(bool? isDarkTheme = null) + { + var themeMgr = new ThemeManager(); + themeMgr.ChangeTheme(isDarkTheme); + } private SwitchItem RetrieveColorSchemeSwitch() { var settingsRepo = new DBSettingsRepository(); diff --git a/Mear/Mear/Views/MusicLibrary.xaml.cs b/Mear/Mear/Views/MusicLibrary.xaml.cs index 3ff2190..d5bf7f1 100644 --- a/Mear/Mear/Views/MusicLibrary.xaml.cs +++ b/Mear/Mear/Views/MusicLibrary.xaml.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using Xamarin.Forms; using Xamarin.Forms.Xaml; +using Mear.Managers; using Mear.Models.Authentication; using Mear.Repositories.Database; @@ -27,6 +28,8 @@ namespace Mear.Views public MusicLibrary() { InitializeComponent(); + var themeMgr = new ThemeManager(); + themeMgr.InitTheme(); } #endregion diff --git a/Mear/Mear/Views/SettingsPage.xaml b/Mear/Mear/Views/SettingsPage.xaml index d4b8911..a04875d 100644 --- a/Mear/Mear/Views/SettingsPage.xaml +++ b/Mear/Mear/Views/SettingsPage.xaml @@ -23,15 +23,22 @@ + + HorizontalOptions="FillAndExpand" + MinimumWidthRequest="300" + Orientation="Vertical"> + diff --git a/Mear/Mear/Views/SettingsPage.xaml.cs b/Mear/Mear/Views/SettingsPage.xaml.cs index 286afce..7769549 100644 --- a/Mear/Mear/Views/SettingsPage.xaml.cs +++ b/Mear/Mear/Views/SettingsPage.xaml.cs @@ -33,9 +33,8 @@ namespace Mear.Views #region Events private void Switch_Toggled(object sender, ToggledEventArgs e) { - var val = e; - var valOne = sender; - var yp = ""; + bool? isDarkTheme = e.Value; + _viewModel.ToggleTheme(isDarkTheme); } #endregion #endregion