From fd0300fccb43198c931fa3d6cb31202956d4c214 Mon Sep 17 00:00:00 2001 From: Gordon Pedersen Date: Tue, 2 Jul 2024 10:01:06 +1000 Subject: [PATCH] More reorg, cleaning up some warnings --- Classes/CustomAuthenticationStateProvider.cs | 3 -- Classes/FeatureFlags.cs | 5 +++ Classes/NavigatorService.cs | 7 +--- Classes/State.cs | 40 ++++++++++++-------- Classes/Utilities.cs | 5 --- Components/Layout/AvatarMenuLinks.razor | 2 +- Components/Layout/NavLinks.razor | 2 +- Components/Pages/Person.razor | 3 +- MauiProgram.cs | 7 +--- Platforms/Android/MainActivity.cs | 24 +++++++----- 10 files changed, 50 insertions(+), 48 deletions(-) create mode 100644 Classes/FeatureFlags.cs diff --git a/Classes/CustomAuthenticationStateProvider.cs b/Classes/CustomAuthenticationStateProvider.cs index 48d0cc0..80f4969 100644 --- a/Classes/CustomAuthenticationStateProvider.cs +++ b/Classes/CustomAuthenticationStateProvider.cs @@ -1,8 +1,5 @@ using Microsoft.AspNetCore.Components.Authorization; -using Neighbourhood.omg.lol.Models; using System.Security.Claims; -using System.Text.Json; -using System.Text.Json.Serialization; namespace Neighbourhood.omg.lol { public class CustomAuthenticationStateProvider : AuthenticationStateProvider { diff --git a/Classes/FeatureFlags.cs b/Classes/FeatureFlags.cs new file mode 100644 index 0000000..a0c24b5 --- /dev/null +++ b/Classes/FeatureFlags.cs @@ -0,0 +1,5 @@ +namespace Neighbourhood.omg.lol { + public static class FeatureFlags { + public static bool Following { get; } = false; + } +} diff --git a/Classes/NavigatorService.cs b/Classes/NavigatorService.cs index e0f368b..3099c22 100644 --- a/Classes/NavigatorService.cs +++ b/Classes/NavigatorService.cs @@ -1,12 +1,7 @@ using Microsoft.AspNetCore.Components; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Neighbourhood.omg.lol { public class NavigatorService { - internal NavigationManager NavigationManager { get; set; } + internal NavigationManager? NavigationManager { get; set; } } } diff --git a/Classes/State.cs b/Classes/State.cs index fceddda..abae1f6 100644 --- a/Classes/State.cs +++ b/Classes/State.cs @@ -3,11 +3,20 @@ using System.ComponentModel; using System.Globalization; using System.Text.Json; using Neighbourhood.omg.lol.Models; +using System.Runtime.CompilerServices; namespace Neighbourhood.omg.lol { public class State : INotifyPropertyChanged { - // Feature flags - public bool FeatureFollowing { get; } = false; + + // Events + public event EventHandler? IntentReceived; + public event PropertyChangedEventHandler? PropertyChanged; + + // Create the OnPropertyChanged method to raise the event + // The calling member's name will be used as the parameter. + protected void OnPropertyChanged([CallerMemberName] string? name = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); + protected void OnIntentRecieved() => IntentReceived?.Invoke(this, EventArgs.Empty); + // Main data lists public List? Statuses { get; set; } public List? Pics { get; set; } @@ -43,6 +52,7 @@ namespace Neighbourhood.omg.lol { else { string selectedAddressJson = JsonSerializer.Serialize(_selectedAddress); Preferences.Default.Set("selectedaddress", selectedAddressJson); + OnPropertyChanged(); } } } @@ -69,13 +79,12 @@ namespace Neighbourhood.omg.lol { } // share intent stuff - public event EventHandler? IntentReceived; private string? _shareString; public string? ShareString { get => _shareString; set { _shareString = value; - IntentReceived?.Invoke(this, EventArgs.Empty); + OnIntentRecieved(); } } public string? ShareStringSubject { get; set; } @@ -85,7 +94,7 @@ namespace Neighbourhood.omg.lol { get => _sharePhoto; set { _sharePhoto = value; - IntentReceived?.Invoke(this, EventArgs.Empty); + OnIntentRecieved(); } } public long? SharePhotoSize { get; set; } @@ -93,13 +102,12 @@ namespace Neighbourhood.omg.lol { public string? SharePhotoText { get; set; } // refreshing - public event PropertyChangedEventHandler? PropertyChanged; private bool _isRefreshing; public bool IsRefreshing { get => _isRefreshing; private set { _isRefreshing = value; - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsRefreshing))); + OnPropertyChanged(); } } @@ -133,7 +141,7 @@ namespace Neighbourhood.omg.lol { get => _canRefresh; set { _canRefresh = value; - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CanRefresh))); + OnPropertyChanged(); } } @@ -179,12 +187,14 @@ namespace Neighbourhood.omg.lol { } public async Task RemoveAccountDetails() { - Preferences.Default.Clear(); - AccountInfo = null; - AddressList = null; - SelectedAddress = null; - Following = null; - api.RemoveToken(); + await Task.Run(() => { + Preferences.Default.Clear(); + AccountInfo = null; + AddressList = null; + SelectedAddress = null; + Following = null; + api.RemoveToken(); + }); } public bool IsFollowing(string address) => Following?.Contains(address) ?? false; @@ -218,7 +228,7 @@ namespace Neighbourhood.omg.lol { public async Task?> GetDirectory(bool forceRefresh = false) { if (forceRefresh || this.AddressDirectory == null || this.AddressDirectory.Count == 0) { IdnMapping idn = new IdnMapping(); - this.AddressDirectory = (await api.Directory()).Select(s => { + this.AddressDirectory = (await api.Directory())?.Select(s => { if (s.StartsWith("xn--")) return idn.GetUnicode(s); else return s; }).ToList(); diff --git a/Classes/Utilities.cs b/Classes/Utilities.cs index 496ea60..1a9860a 100644 --- a/Classes/Utilities.cs +++ b/Classes/Utilities.cs @@ -1,10 +1,5 @@ using Markdig; using Microsoft.AspNetCore.Components; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Neighbourhood.omg.lol { diff --git a/Components/Layout/AvatarMenuLinks.razor b/Components/Layout/AvatarMenuLinks.razor index 913fb6c..19df29f 100644 --- a/Components/Layout/AvatarMenuLinks.razor +++ b/Components/Layout/AvatarMenuLinks.razor @@ -32,7 +32,7 @@ } } -@if (State.FeatureFollowing && State.IsAuthorized) { +@if (FeatureFlags.Following && State.IsAuthorized) { Feed diff --git a/Components/Layout/NavLinks.razor b/Components/Layout/NavLinks.razor index 974e8f6..7361d7e 100644 --- a/Components/Layout/NavLinks.razor +++ b/Components/Layout/NavLinks.razor @@ -21,7 +21,7 @@
Address Directory
-@if (State.FeatureFollowing) { +@if (FeatureFlags.Following) { diff --git a/Components/Pages/Person.razor b/Components/Pages/Person.razor index b60e1ba..8057a82 100644 --- a/Components/Pages/Person.razor +++ b/Components/Pages/Person.razor @@ -11,8 +11,7 @@
@Address
-@if (State.FeatureFollowing) -{ +@if (FeatureFlags.Following) {
@if (State.IsFollowing(Address)) {