feat(Permission) Added start of the Storage feature. Detailed/Plain default toggle

This commit is contained in:
2022-04-24 19:33:18 -04:00
parent 8a4d00054a
commit afaafbe713
20 changed files with 538 additions and 65 deletions
+27 -1
View File
@@ -1,4 +1,5 @@
using Model.BuildOrders;
using Microsoft.AspNetCore.Components.ProtectedBrowserStorage;
using Model.BuildOrders;
using Model.Doc;
using Model.Economy;
using Model.Entity;
@@ -10,6 +11,7 @@ using Model.Website;
using Model.Website.Enums;
using Model.Work.Tasks;
using Services.Immortal;
using Services.Website;
namespace Services;
@@ -25,6 +27,30 @@ public interface IToastService
void ClearAllToasts();
}
public interface IStorageService
{
public void Subscribe(Action action);
public void Unsubscribe(Action action);
T GetValue<T>(string forKey);
void SetValue<T>(string key, T value);
Task Load();
}
public interface IPermissionService
{
public void Subscribe(Action action);
public void Unsubscribe(Action action);
public bool GetIsStorageEnabled();
public bool GetIsDataCollectionEnabled();
public void SetIsStorageEnabled(bool isEnabled);
public void SetIsDataCollectionEnabled(bool isEnabled);
Task Load();
}
public interface ISearchService
{
public List<SearchPointModel> SearchPoints { get; set; }
+21 -5
View File
@@ -1,12 +1,28 @@
namespace Services.Immortal;
using Services.Website;
namespace Services.Immortal;
public class EntityViewType
{
public static string Detailed = "Detailed";
public static string Plain = "Plain";
}
public class EntityDisplayService : IEntityDisplayService
{
private string displayType = "Detailed";
private string _displayType;
public EntityDisplayService(IStorageService storageService)
{
_displayType = storageService.GetValue<bool>(StorageKeys.IsPlainView)
? EntityViewType.Plain : EntityViewType.Detailed;
}
public List<string> DefaultChoices()
{
return new List<string> { "Detailed", "Plain" };
return new List<string> { EntityViewType.Detailed, EntityViewType.Plain };
}
public void Subscribe(Action action)
@@ -21,12 +37,12 @@ public class EntityDisplayService : IEntityDisplayService
public string GetDisplayType()
{
return displayType;
return _displayType;
}
public void SetDisplayType(string displayType)
{
this.displayType = displayType;
this._displayType = displayType;
NotifyDataChanged();
}
+5 -2
View File
@@ -15,11 +15,14 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="YamlDotNet" Version="11.2.1"/>
<PackageReference Include="Blazored.LocalStorage" Version="4.3.0-preview.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.ProtectedBrowserStorage" Version="5.0.0-rc.1.20451.17" />
<PackageReference Include="Microsoft.JSInterop" Version="7.0.0-preview.2.22153.2" />
<PackageReference Include="YamlDotNet" Version="11.2.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Model\Model.csproj"/>
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>
</Project>
+64
View File
@@ -0,0 +1,64 @@
using Blazored.LocalStorage;
using Microsoft.JSInterop;
namespace Services.Website;
public class PermissionService : IPermissionService
{
private bool isLoaded;
private IJSRuntime _jsRuntime;
private bool isStorageEnabled = false;
private IToastService _toastService;
private IStorageService _storageService;
public PermissionService(IJSRuntime jsRuntime, IToastService toastService, IStorageService storageService)
{
_jsRuntime = jsRuntime;
_toastService = toastService;
_storageService = storageService;
}
public void Subscribe(Action action)
{
OnChange += action;
}
public void Unsubscribe(Action action)
{
OnChange += action;
}
public bool GetIsStorageEnabled()
{
return _storageService.GetValue<bool>(StorageKeys.EnabledStorage);
}
public bool GetIsDataCollectionEnabled()
{
return _storageService.GetValue<bool>(StorageKeys.EnabledDataCollection);
}
public void SetIsStorageEnabled(bool isEnabled)
{
_storageService.SetValue(StorageKeys.EnabledStorage, isEnabled);
}
public void SetIsDataCollectionEnabled(bool isEnabled)
{
_storageService.SetValue(StorageKeys.EnabledDataCollection, isEnabled);
}
public Task Load()
{
throw new NotImplementedException();
}
private event Action OnChange = null!;
private void NotifyDataChanged()
{
OnChange();
}
}
+106
View File
@@ -0,0 +1,106 @@
using Blazored.LocalStorage;
using Microsoft.JSInterop;
using Model.Feedback;
namespace Services.Website;
public class StorageKeys
{
public static string EnabledStorage = "StorageEnabled";
public static string EnabledDataCollection = "StorageDataCollection";
public static string IsPlainView { get; set; } = "IsPlainView";
}
public class StorageService : IStorageService
{
private readonly ISyncLocalStorageService _localStorageService;
private IJSRuntime _jsRuntime;
private readonly IToastService _toastService;
private bool isLoaded;
private bool isStorageEnabled;
public StorageService(IJSRuntime jsRuntime, IToastService toastService,
ISyncLocalStorageService localStorageService)
{
_jsRuntime = jsRuntime;
_toastService = toastService;
_localStorageService = localStorageService;
}
private string enabledKey => StorageKeys.EnabledStorage;
public void Subscribe(Action action)
{
OnChange += action;
}
public void Unsubscribe(Action action)
{
OnChange += action;
}
public T GetValue<T>(string forKey)
{
return _localStorageService.GetItem<T>(forKey);
}
public void SetValue<T>(string key, T value)
{
if (key.Equals(StorageKeys.EnabledStorage) && value.Equals(true))
{
_localStorageService.SetItem(key, value);
NotifyDataChanged();
return;
}
if (key.Equals(StorageKeys.EnabledStorage))
{
_localStorageService.Clear();
NotifyDataChanged();
return;
}
var isEnabled = GetValue<bool>(StorageKeys.EnabledStorage);
if (!isEnabled)
{
_toastService.AddToast(new ToastModel
{
Title = "Permission Error",
SeverityType = SeverityType.Error,
Message = "Storage must be enabled before Storage can be used."
});
NotifyDataChanged();
return;
}
_localStorageService.SetItem(key, value);
NotifyDataChanged();
}
public async Task Load()
{
if (!isLoaded) return;
isLoaded = true;
isStorageEnabled = GetValue<bool>(enabledKey);
NotifyDataChanged();
}
private event Action OnChange = null!;
private void NotifyDataChanged()
{
try
{
OnChange();
}
catch (Exception e)
{
}
}
}