Skip to content

Commit

Permalink
♻️ refactor: Most of the content of SettingService has been moved to …
Browse files Browse the repository at this point in the history
…the interface
  • Loading branch information
Yu-Core committed Oct 10, 2024
1 parent 2e57bcc commit fa02b2a
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public static partial class ServiceCollectionExtensions
public static IServiceCollection AddDependencyInjection(this IServiceCollection services)
{
services.AddSingleton<IStaticWebAssets, Essentials.StaticWebAssets>();
services.AddSingleton<Rcl.Essentials.IPreferences, Essentials.Preferences>();
services.AddSingleton<ISettingService, Services.SettingService>();
services.AddSingleton<INavigateController, Essentials.NavigateController>();
services.AddSingleton<IGlobalConfiguration, GlobalConfiguration>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
namespace SwashbucklerDiary.Maui.Services
using SwashbucklerDiary.Rcl.Services;

namespace SwashbucklerDiary.Maui.Services
{
public class SettingService : Rcl.Services.SettingService
public class SettingService : Essentials.Preferences, ISettingService
{
public SettingService(Rcl.Essentials.IPreferences preferences) :
base(preferences)
{
}
public Dictionary<string, object> DefalutSettings { get; set; } = [];

public Dictionary<string, object> TempSettings { get; set; } = [];

public override T Get<T>(string key)
public T Get<T>(string key)
{
if (_defalutSettings.TryGetValue(key, out var defaultValue))
if (DefalutSettings.TryGetValue(key, out var defaultValue))
{
return Get(key, (T)defaultValue);
}

return default!;
}

public override T Get<T>(string key, T defaultValue)
public T Get<T>(string key, T defaultValue)
{
return Preferences.Default.Get(key, defaultValue);
}
Expand Down
136 changes: 120 additions & 16 deletions src/SwashbucklerDiary.Rcl/Services/SettingService/ISettingService.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,142 @@
using SwashbucklerDiary.Rcl.Essentials;
using Masa.Blazor;
using SwashbucklerDiary.Rcl.Essentials;
using System.Linq.Expressions;

namespace SwashbucklerDiary.Rcl.Services
{
public interface ISettingService : IPreferences
{
Task InitializeAsync();
protected Dictionary<string, object> DefalutSettings { get; set; }

T Get<T>(string key);
protected Dictionary<string, object> TempSettings { get; set; }

T Get<T>(string key, T defaultValue);
public Task InitializeAsync() => DefalutInitializeAsync(DefalutSettings);

T Get<T>(Expression<Func<Setting, T>> expr);
protected static Task DefalutInitializeAsync(Dictionary<string, object> dictionary)
{
var defaultSettings = new Setting().ToParameters().ToDictionary();
var defaultTempSettings = new TempSetting().ToParameters().ToDictionary();

T Get<T>(Expression<Func<Setting, T>> expr, T defaultValue);
AddDefaultSettings(dictionary, defaultSettings);
AddDefaultSettings(dictionary, defaultTempSettings);

Task SetAsync<T>(Expression<Func<Setting, T>> expr, T value);
return Task.CompletedTask;
}

Task RemoveAsync<T>(Expression<Func<Setting, T>> expr);
private static void AddDefaultSettings(Dictionary<string, object> dictionary, Dictionary<string, object?> dictionary2)
{
foreach (var item in dictionary2)
{
if (item.Value is not null)
{
dictionary.Add(item.Key, item.Value);
}
}
}

T GetTemp<T>(string key);
public T Get<T>(string key);

T GetTemp<T>(Expression<Func<TempSetting, T>> expr);
public T Get<T>(string key, T defaultValue);

T GetTemp<T>(string key, T defaultValue);
public T GetTemp<T>(string key)
{
if (TempSettings.TryGetValue(key, out var value))
{
return (T)value;
}

T GetTemp<T>(Expression<Func<TempSetting, T>> expr, T defaultValue);
if (DefalutSettings.TryGetValue(key, out var defaulValue))
{
return (T)defaulValue;
}

void SetTemp<T>(string key, T value);
return default!;
}

void SetTemp<T>(Expression<Func<TempSetting, T>> expr, T value);
public T GetTemp<T>(string key, T defaultValue)
{
if (TempSettings.TryGetValue(key, out var value))
{
return (T)value;
}

void RemoveTemp(string key);
return defaultValue;
}

void RemoveTemp<T>(Expression<Func<TempSetting, T>> expr);
public void SetTemp<T>(string key, T value)
{
TempSettings[key] = value;
}

public void RemoveTemp(string key)
{
TempSettings.Remove(key);
}

public T Get<T>(Expression<Func<Setting, T>> expr)
{
if (expr.Body is MemberExpression memberExpr)
{
return Get<T>(memberExpr.Member.Name);
}

throw new ArgumentException("Expression does not contain a member access.");
}

public Task SetAsync<T>(Expression<Func<Setting, T>> expr, T value)
{
var key = GetSettingKey(expr);
return SetAsync(key, value);
}

private static string GetSettingKey<T>(Expression<Func<Setting, T>> expr) => GetExpressionMemberName(expr);

private static string GetTempSettingKey<T>(Expression<Func<TempSetting, T>> expr) => GetExpressionMemberName(expr);

private static string GetExpressionMemberName<T, TResult>(Expression<Func<T, TResult>> expr)
{
if (expr.Body is MemberExpression memberExpr)
{
return memberExpr.Member.Name;
}

throw new ArgumentException("Expression does not contain a member access.");
}

public T GetTemp<T>(Expression<Func<TempSetting, T>> expr)
{
var key = GetTempSettingKey(expr);
return GetTemp<T>(key);
}

public T GetTemp<T>(Expression<Func<TempSetting, T>> expr, T defaultValue)
{
var key = GetTempSettingKey(expr);
return GetTemp<T>(key, defaultValue);
}

public void SetTemp<T>(Expression<Func<TempSetting, T>> expr, T value)
{
var key = GetTempSettingKey(expr);
SetTemp<T>(key, value);
}

public void RemoveTemp<T>(Expression<Func<TempSetting, T>> expr)
{
var key = GetTempSettingKey(expr);
RemoveTemp(key);
}

public T Get<T>(Expression<Func<Setting, T>> expr, T defaultValue)
{
var key = GetSettingKey(expr);
return GetTemp(key, defaultValue);
}

public Task RemoveAsync<T>(Expression<Func<Setting, T>> expr)
{
var key = GetSettingKey(expr);
return RemoveAsync(key);
}
}
}
165 changes: 0 additions & 165 deletions src/SwashbucklerDiary.Rcl/Services/SettingService/SettingService.cs

This file was deleted.

Loading

0 comments on commit fa02b2a

Please sign in to comment.