Skip to content

Commit

Permalink
profile renaming/deleting
Browse files Browse the repository at this point in the history
  • Loading branch information
rrelyea committed Jun 23, 2023
1 parent 06ee3eb commit 58a3b1f
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 54 deletions.
88 changes: 81 additions & 7 deletions Pages/Profiles.razor
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,94 @@

Click on profile name to make it the active profile. You'll be navigated back to your prior page.<br/>

<ul>

<table>
<tr>
<th></th>
<th>Name</th>
<th>Action</th>
</tr>
@foreach(var profileName in appData.ProfileNames)
{
<li><a href=/portfolio @onclick="@(e=>makeProfileActive(e,profileName))" @onclick:preventDefault>@profileName</a> @if(profileName==appData.CurrentProfileName){<b>&nbsp;[active]</b>}</li>
<tr>
<td>
@if(profileName==appData.CurrentProfileName){<b>&nbsp;[active]&nbsp;</b>}
</td>
<td>
@if (renameProfileName == profileName)
{
<EditForm Model="appData">
<InputText @bind-Value=newName />
</EditForm>
}
else
{
<a href=/portfolio @onclick="@(e=>makeProfileActive(e,profileName))" @onclick:preventDefault>@profileName</a><span>&nbsp;</span>
}
</td>
<td>
@if (renameProfileName == null || renameProfileName == profileName)
{
<button @onclick="@(e=>renameProfile(e,profileName))">Rename</button>
}
@if (renameProfileName == null)
{
<button @onclick="@(e=>deleteProfile(e,profileName))">Del</button>
}
</td>
</tr>
}

</ul>

</table>
<br/>
<label>Create new profile:</label><br/>
<InputText placeholder="name" @bind-Value="@newProfileName" /><button @onclick="createProfile">create</button>
<InputText placeholder="name" @bind-Value="@newProfileName" /><button @onclick="createProfile">create</button>

@code {
private ElementReference renameBox;

private async Task makeProfileActive(EventArgs e, string profileName)
{
appData.CurrentProfileName = profileName;
await LocalStorageAccessor.SetValueAsync("CurrentProfileName", profileName);
await ProfileUtilities.Load(appData);
Navigation.NavigateTo(appData.LastPageUri);
if (appData.LastPageUri != null)
{
Navigation.NavigateTo(appData.LastPageUri);
}
}
private async Task renameProfile(EventArgs e, string profileName)
{
if (renameProfileName == null)
{
renameProfileName = profileName;
newName = profileName;
}
else
{
if (newName != renameProfileName && !string.IsNullOrEmpty(newName) && !appData.ProfileNames.Contains(newName))
{
await LocalStorageAccessor.RenameKey(renameProfileName, newName);
await ProfileUtilities.SetProfileNames(appData);

if (appData.CurrentProfileName == renameProfileName)
{
appData.CurrentProfileName = newName;
}

renameProfileName = null;
}
}
}
private async Task deleteProfile(EventArgs e, string profileName)
{
await LocalStorageAccessor.RemoveAsync(profileName);
if (appData.CurrentProfileName == profileName)
{
appData.CurrentProfileName = null;
appData.FamilyData = null;
}

await ProfileUtilities.SetProfileNames(appData);
}

private async Task createProfile()
Expand All @@ -38,9 +108,13 @@ Click on profile name to make it the active profile. You'll be navigated back to
newProfileName = "";
}
}

protected override async Task OnInitializedAsync()
{

}

private string newProfileName { get; set; }
private string? renameProfileName { get; set; }
private string? newName {get; set;}
}
14 changes: 11 additions & 3 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddScoped<ISearchModel>(sp => SearchModel.Create());

builder.Services.AddScoped<LocalStorageAccessor>();
var lsa = builder.Services.BuildServiceProvider().GetService<LocalStorageAccessor>();
string? CurrentProfileName;
try {
CurrentProfileName = await lsa.GetValueAsync<string>("CurrentProfileName");
}
catch (Exception ex) {
CurrentProfileName = null;
}

IRSData? irsData = await IRSData.Create(httpClient);
if (irsData != null) {
builder.Services.AddSingleton<IRSData>(irsData);
var appData = new AppData(new FamilyData(irsData));
appData.CurrentProfileName = "primary";
appData.CurrentProfileName = CurrentProfileName;
builder.Services.AddSingleton<IAppData>(appData);
} else {
throw new Exception("irsData is null");
Expand All @@ -41,6 +51,4 @@
Funds.AddRange(Stocks);
builder.Services.AddSingleton<IList<Fund>>(Funds);

builder.Services.AddScoped<LocalStorageAccessor>();

await builder.Build().RunAsync();
8 changes: 8 additions & 0 deletions Shared/LocalStorageAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,12 @@ public async Task RemoveAsync(string key)
await WaitForReference();
await _accessorJsRef.Value.InvokeVoidAsync("remove", key);
}

public async Task RenameKey(string oldName, string newName)
{
await WaitForReference();
var value = await GetValueAsync<string>(oldName);
await SetValueAsync(newName, value);
await RemoveAsync(oldName);
}
}
13 changes: 1 addition & 12 deletions Shared/Models/FamilyData/AppData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@ public AppData(FamilyData familyData) {

public FamilyData FamilyData { get; set; }
public List<string> ProfileNames {get; set;}
public string CurrentProfileName {get; set;}
public string? CurrentProfileName {get; set;}
public string LastPageUri {get; set;}
public string CurrentProfileKey
{
get {
switch (CurrentProfileName) {
case "primary":
return "localSave";
default:
return CurrentProfileName;
}
}
}
}
5 changes: 1 addition & 4 deletions Shared/Models/FamilyData/IAppData.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
public interface IAppData {
public FamilyData FamilyData { get; set; }
public List<string> ProfileNames {get; set;}
public string CurrentProfileName {get; set;}
public string CurrentProfileKey { get; }
public string? CurrentProfileName {get; set;}
public string LastPageUri {get; set;}


}
29 changes: 25 additions & 4 deletions Shared/Models/ProfileUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

public static class ProfileUtilities
{
public static string Key { get; set; } = "localSave";
public static string Value { get; set; } = "";
public static string storedJson { get; set; } = "";

Expand All @@ -30,7 +29,7 @@ public static async Task Save(string key, FamilyData familyData)
public static async Task Load(IAppData appData)
{
try {
storedJson = await LocalStorageAccessor.GetValueAsync<string>(appData.CurrentProfileKey);
storedJson = await LocalStorageAccessor.GetValueAsync<string>(appData.CurrentProfileName);
var options = new JsonSerializerOptions()
{
Converters =
Expand All @@ -48,14 +47,36 @@ public static async Task Load(IAppData appData)
}
}

public static async Task Clear(IAppData appData, IRSData irsData)
public static async Task Clear(IAppData appData, string key, IRSData irsData)
{
appData.FamilyData = new FamilyData(irsData);
await LocalStorageAccessor.RemoveAsync(Key);
await LocalStorageAccessor.RemoveAsync(key);
}

public static async Task ClearAllAsync()
{
await LocalStorageAccessor.Clear();
}

public static async Task SetProfileNames(IAppData appData)
{
var keys = new List<string>();
var keysJsonElement = await LocalStorageAccessor.GetKeys();
foreach (var el in keysJsonElement.EnumerateArray())
{
string? value = el.GetString();
if (value != null) {
switch (value) {
case "CurrentProfileName":
case "i18nextLng":
break;
default:
keys.Add(value);
break;
}
}
}

appData.ProfileNames = keys;
}
}
37 changes: 13 additions & 24 deletions Shared/TopLine.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,30 @@
<button @onclick=@Clear>Erase</button>
</div>
@code{

private async Task Save() {
await ProfileUtilities.Save(appData.CurrentProfileKey, appData.FamilyData);
}
await ProfileUtilities.Save(appData.CurrentProfileName, appData.FamilyData);
}

private async Task Clear() {
ProfileUtilities.Clear(appData, irsData);
ProfileUtilities.Clear(appData, appData.CurrentProfileName, irsData);
Navigation.NavigateTo("/");
}

protected override async Task OnInitializedAsync()
{
ProfileUtilities.LocalStorageAccessor = LocalStorageAccessor;
appData.CurrentProfileName = "primary";
await ProfileUtilities.Load(appData);
await ProfileUtilities.SetProfileNames(appData);

var keys = new List<string>();
var keysJsonElement = await LocalStorageAccessor.GetKeys();
foreach (var el in keysJsonElement.EnumerateArray())
if (appData.CurrentProfileName != null && appData.ProfileNames.Contains(appData.CurrentProfileName))
{
string? value = el.GetString();
if (value != null) {
switch (value) {
case "i18nextLng":
break;
case "localSave":
keys.Add("primary");
break;
default:
keys.Add(value);
break;
}
}
await ProfileUtilities.Load(appData);
}
else
{
appData.CurrentProfileName = null;
await ProfileUtilities.SetProfileNames(appData);
appData.LastPageUri = Navigation.Uri;
Navigation.NavigateTo("/profiles");
}

appData.ProfileNames = keys;
}
}

0 comments on commit 58a3b1f

Please sign in to comment.