settings as XLabs config manager

main
Paul Schneider 9 years ago
parent f42b66383a
commit 10f8e9a7f5
3 changed files with 152 additions and 104 deletions

@ -39,7 +39,8 @@ namespace BookAStar
// by Views resolution, and then, drop it // by Views resolution, and then, drop it
public static App CurrentApp { get { return Current as App; } } public static App CurrentApp { get { return Current as App; } }
public static bool MasterPresented { public static bool MasterPresented
{
get get
{ return CurrentApp.masterDetail.IsPresented; } { return CurrentApp.masterDetail.IsPresented; }
internal set internal set
@ -74,9 +75,9 @@ namespace BookAStar
ViewFactory.Register<BookQueriesPage, BookQueriesViewModel>(); ViewFactory.Register<BookQueriesPage, BookQueriesViewModel>();
ViewFactory.Register<EditBillingLinePage, BillingLineViewModel>(); ViewFactory.Register<EditBillingLinePage, BillingLineViewModel>();
ViewFactory.Register<EditEstimatePage, EstimateViewModel>(); ViewFactory.Register<EditEstimatePage, EstimateViewModel>();
ConfigManager = new XLabs.Settings.GenericConfigSettingsMgr(s => ConfigManager = new XLabs.Settings.GenericConfigSettingsMgr(s =>
Settings.AppSettings.GetValueOrDefault<string>(s, Settings.SettingsDefault), null); MainSettings.AppSettings.GetValueOrDefault<string>(s, MainSettings.SettingsDefault), null);
} }
ExtendedMasterDetailPage masterDetail; ExtendedMasterDetailPage masterDetail;
@ -111,13 +112,16 @@ namespace BookAStar
// var mainPage = new NavigationPage(bQueriesPage); // var mainPage = new NavigationPage(bQueriesPage);
masterDetail = new ExtendedMasterDetailPage() { masterDetail = new ExtendedMasterDetailPage()
{
Title = "MainPage" Title = "MainPage"
}; };
masterDetail.Master = new DashboardPage { masterDetail.Master = new DashboardPage
{
Title = "Bookingstar", Title = "Bookingstar",
BindingContext = new DashboardViewModel() }; BindingContext = new DashboardViewModel()
};
// masterDetail.Detail = home; // masterDetail.Detail = home;

@ -4,6 +4,7 @@ using BookAStar.Model.Auth.Account;
using Newtonsoft.Json; using Newtonsoft.Json;
using Plugin.Settings; using Plugin.Settings;
using Plugin.Settings.Abstractions; using Plugin.Settings.Abstractions;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
@ -21,14 +22,18 @@ namespace BookAStar
public static class MainSettings public static class MainSettings
{ {
private static ISettings AppSettings { public static ISettings AppSettings
get { {
get
{
return CrossSettings.Current; return CrossSettings.Current;
} }
} }
#region Setting Constants #region Setting Constants
public static readonly string SettingsDefault = string.Empty;
public static readonly string EntityDataSettingsPrefix = "Ed";
public static readonly string EntityCursorSettingsPrefix = "Ec";
private const string userNameKey = "user_id"; private const string userNameKey = "user_id";
private const string PushNotificationsKey = "pushNotifs"; private const string PushNotificationsKey = "pushNotifs";
private const string AllowGPSUsageKey = "allowGPSUsage"; private const string AllowGPSUsageKey = "allowGPSUsage";
@ -65,7 +70,8 @@ namespace BookAStar
public static string UserName public static string UserName
{ {
get { get
{
return AppSettings.GetValueOrDefault<string>(userNameKey, null); return AppSettings.GetValueOrDefault<string>(userNameKey, null);
} }
} }
@ -91,7 +97,8 @@ namespace BookAStar
public static string GoogleRegId public static string GoogleRegId
{ {
set { set
{
var oldregid = GoogleRegId; var oldregid = GoogleRegId;
AppSettings.AddOrUpdateValue<string>(GoogleRegIdKey, value); AppSettings.AddOrUpdateValue<string>(GoogleRegIdKey, value);
// TODO If it changed, and there's an identified user, // TODO If it changed, and there's an identified user,
@ -128,7 +135,9 @@ namespace BookAStar
} }
} }
public static User CurrentUser { get public static User CurrentUser
{
get
{ {
var uname = UserName; var uname = UserName;
if (uname == null) return null; if (uname == null) return null;
@ -169,13 +178,16 @@ namespace BookAStar
return AccountList.FirstOrDefault(a => a.UserName == username); return AccountList.FirstOrDefault(a => a.UserName == username);
} }
public static bool PushNotifications { public static bool PushNotifications
get { {
get
{
return AppSettings.GetValueOrDefault<bool>( return AppSettings.GetValueOrDefault<bool>(
PushNotificationsKey, PushNotificationsKey,
PushNotificationsDefault); PushNotificationsDefault);
} }
set { set
{
// TODO Stop Broadcast receiver // TODO Stop Broadcast receiver
AppSettings.AddOrUpdateValue<bool>( AppSettings.AddOrUpdateValue<bool>(
PushNotificationsKey, PushNotificationsKey,
@ -224,8 +236,10 @@ namespace BookAStar
return AppSettings.GetValueOrDefault<double>(MusicalKey + key, MusicalDefault[key]); return AppSettings.GetValueOrDefault<double>(MusicalKey + key, MusicalDefault[key]);
} }
public static Dictionary<string,double> Musical { public static Dictionary<string, double> Musical
get { {
get
{
return musical; return musical;
} }
} }
@ -237,5 +251,36 @@ namespace BookAStar
return environ; return environ;
} }
} }
/// <summary>
/// Use a sub-key to make persist different tables with the same definition.
/// </summary>
/// <typeparam name="V"></typeparam>
/// <param name="collection"></param>
/// <param name="subKey"></param>
public static void Populate<V>(this IList<V> collection, string subKey = null)
{
var key = $"{EntityDataSettingsPrefix}/{subKey}/{typeof(V).FullName}";
var data = AppSettings.GetValueOrDefault<string>(key, null);
if (data != null)
{
var items = JsonConvert.DeserializeObject<IList<V>>(data);
if (items != null)
foreach (var item in items)
collection.Add(item);
}
}
/// <summary>
/// Saves a list
/// </summary>
/// <typeparam name="V"></typeparam>
/// <param name="collection"></param>
/// <param name="subKey"></param>
public static void SaveCollection<V>(this IList<V> collection, string subKey=null)
{
if (collection == null) return;
var key = $"{EntityDataSettingsPrefix}/{subKey}/{typeof(V).FullName}";
AppSettings.AddOrUpdateValue(key, JsonConvert.SerializeObject(collection));
}
} }
} }

@ -1,6 +1,5 @@
// Helpers/Settings.cs // Helpers/Settings.cs
using Plugin.Settings; #if USELESS_but_makes_the_component_removable // this file must exist ^^
using Plugin.Settings.Abstractions;
namespace BookAStar.Helpers namespace BookAStar.Helpers
{ {
@ -23,7 +22,6 @@ namespace BookAStar.Helpers
private const string SettingsKey = "settings_key"; private const string SettingsKey = "settings_key";
public static readonly string SettingsDefault = string.Empty; public static readonly string SettingsDefault = string.Empty;
public static readonly string XLabsSettingsPrefix = "_XLABS_";
#endregion #endregion
@ -39,6 +37,7 @@ namespace BookAStar.Helpers
AppSettings.AddOrUpdateValue<string>(SettingsKey, value); AppSettings.AddOrUpdateValue<string>(SettingsKey, value);
} }
} }
} }
} }
#endif
Loading…