2024-06-06 05:20:09 +00:00
|
|
|
|
using Microsoft.AspNetCore.Components;
|
2024-06-18 01:44:18 +00:00
|
|
|
|
using System.ComponentModel;
|
2024-06-20 04:25:48 +00:00
|
|
|
|
using System.Globalization;
|
2024-06-01 04:38:12 +00:00
|
|
|
|
using System.Text.Json;
|
|
|
|
|
|
|
|
|
|
namespace Neighbourhood.omg.lol.Models {
|
2024-06-18 01:44:18 +00:00
|
|
|
|
public class State : INotifyPropertyChanged {
|
2024-06-14 07:20:04 +00:00
|
|
|
|
// Main data lists
|
|
|
|
|
public List<Status>? Statuses { get; set; }
|
|
|
|
|
public List<Pic>? Pics { get; set; }
|
|
|
|
|
public List<NowData>? NowGarden { get; set; }
|
|
|
|
|
public List<MarkupString>? EphemeralMessages { get; set; }
|
2024-06-18 07:11:28 +00:00
|
|
|
|
public List<string>? AddressDirectory { get; set; }
|
2024-06-13 00:26:43 +00:00
|
|
|
|
|
2024-06-14 07:20:04 +00:00
|
|
|
|
// Account data
|
2024-06-01 04:38:12 +00:00
|
|
|
|
public AccountResponseData? AccountInfo { get; set; }
|
|
|
|
|
public AddressResponseList? AddressList { get; set; }
|
2024-06-20 06:46:12 +00:00
|
|
|
|
|
2024-06-27 07:26:43 +00:00
|
|
|
|
public bool IsAuthorized { get => AccountInfo != null; }
|
2024-06-01 04:38:12 +00:00
|
|
|
|
public string? Name { get => AccountInfo?.Name; }
|
|
|
|
|
public string? Email { get => AccountInfo?.Email; }
|
|
|
|
|
public IEnumerable<string>? AddressNames { get => AddressList?.Select(a => a.Address); }
|
2024-06-14 07:20:04 +00:00
|
|
|
|
|
|
|
|
|
// Selected Address
|
2024-06-27 07:26:43 +00:00
|
|
|
|
private AddressResponseData? _selectedAddress;
|
|
|
|
|
public AddressResponseData? SelectedAddress {
|
|
|
|
|
get {
|
|
|
|
|
if (_selectedAddress == null) {
|
|
|
|
|
string selectedAddressJson = Preferences.Default.Get("selectedaddress", string.Empty);
|
|
|
|
|
if (!string.IsNullOrEmpty(selectedAddressJson)) _selectedAddress = JsonSerializer.Deserialize<AddressResponseData>(selectedAddressJson);
|
|
|
|
|
}
|
|
|
|
|
return _selectedAddress;
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
if (_selectedAddress != value) {
|
|
|
|
|
_selectedAddress = value;
|
|
|
|
|
if (_selectedAddress == null) Preferences.Default.Remove("selectedaddress");
|
|
|
|
|
else {
|
|
|
|
|
string selectedAddressJson = JsonSerializer.Serialize(_selectedAddress);
|
|
|
|
|
Preferences.Default.Set("selectedaddress", selectedAddressJson);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-01 04:38:12 +00:00
|
|
|
|
public string? SelectedAddressName { get => SelectedAddress?.Address; }
|
|
|
|
|
|
2024-06-14 07:20:04 +00:00
|
|
|
|
// data for selected address
|
2024-06-06 05:20:09 +00:00
|
|
|
|
public List<Status>? CachedAddressStatuses { get; set; }
|
|
|
|
|
public List<Pic>? CachedAddressPics { get; set; }
|
|
|
|
|
public MarkupString? CachedAddressBio { get; set; }
|
|
|
|
|
private string? _cachedAddress;
|
|
|
|
|
public string? CachedAddress {
|
|
|
|
|
get => _cachedAddress;
|
|
|
|
|
set {
|
|
|
|
|
if (_cachedAddress != value) {
|
|
|
|
|
_cachedAddress = value;
|
|
|
|
|
CachedAddressStatuses = new List<Status>();
|
|
|
|
|
CachedAddressPics = new List<Pic>();
|
|
|
|
|
CachedAddressBio = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-05 12:41:08 +00:00
|
|
|
|
|
2024-06-14 07:20:04 +00:00
|
|
|
|
// share intent stuff
|
|
|
|
|
public event EventHandler<EventArgs>? IntentReceived;
|
|
|
|
|
private string? _shareString;
|
2024-06-20 06:46:12 +00:00
|
|
|
|
public string? ShareString {
|
2024-06-14 07:20:04 +00:00
|
|
|
|
get => _shareString;
|
|
|
|
|
set {
|
|
|
|
|
_shareString = value;
|
|
|
|
|
IntentReceived?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-18 06:23:03 +00:00
|
|
|
|
public string? ShareStringSubject { get; set; }
|
|
|
|
|
|
2024-06-14 07:20:04 +00:00
|
|
|
|
private string? _sharePhoto;
|
|
|
|
|
public string? SharePhoto {
|
|
|
|
|
get => _sharePhoto;
|
|
|
|
|
set {
|
|
|
|
|
_sharePhoto = value;
|
|
|
|
|
IntentReceived?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-18 06:23:03 +00:00
|
|
|
|
public long? SharePhotoSize { get; set; }
|
|
|
|
|
public string? SharePhotoContentType { get; set; }
|
|
|
|
|
public string? SharePhotoText { get; set; }
|
2024-06-14 07:20:04 +00:00
|
|
|
|
|
2024-06-18 01:44:18 +00:00
|
|
|
|
// refreshing
|
|
|
|
|
public event PropertyChangedEventHandler? PropertyChanged;
|
|
|
|
|
private bool _isRefreshing;
|
|
|
|
|
public bool IsRefreshing {
|
|
|
|
|
get => _isRefreshing;
|
2024-06-24 04:52:45 +00:00
|
|
|
|
private set {
|
2024-06-18 01:44:18 +00:00
|
|
|
|
_isRefreshing = value;
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsRefreshing)));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-14 07:20:04 +00:00
|
|
|
|
|
2024-06-24 04:52:45 +00:00
|
|
|
|
public void SendRefresh() => IsRefreshing = true;
|
|
|
|
|
|
|
|
|
|
private static int _refresherCount = 0;
|
|
|
|
|
private static Mutex mutex = new Mutex();
|
|
|
|
|
|
|
|
|
|
public class RefreshToken : IDisposable {
|
|
|
|
|
public event EventHandler? Disposed;
|
|
|
|
|
public void Dispose() => Disposed?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
}
|
|
|
|
|
public RefreshToken GetRefreshToken() {
|
|
|
|
|
mutex.WaitOne();
|
|
|
|
|
_refresherCount++;
|
|
|
|
|
mutex.ReleaseMutex();
|
|
|
|
|
RefreshToken token = new RefreshToken();
|
|
|
|
|
token.Disposed += RefreshToken_Disposed;
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RefreshToken_Disposed(object? sender, EventArgs e) {
|
|
|
|
|
mutex.WaitOne();
|
|
|
|
|
_refresherCount--;
|
|
|
|
|
if (_refresherCount == 0) IsRefreshing = false;
|
|
|
|
|
mutex.ReleaseMutex();
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-18 01:44:18 +00:00
|
|
|
|
private bool _canRefresh;
|
|
|
|
|
public bool CanRefresh {
|
|
|
|
|
get => _canRefresh;
|
|
|
|
|
set {
|
|
|
|
|
_canRefresh = value;
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CanRefresh)));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-14 07:20:04 +00:00
|
|
|
|
|
|
|
|
|
// api service
|
|
|
|
|
private RestService api { get; set; }
|
|
|
|
|
|
|
|
|
|
public State(RestService restService) {
|
|
|
|
|
api = restService;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-01 04:38:12 +00:00
|
|
|
|
public async Task PopulateAccountDetails(string token) {
|
2024-06-14 07:20:04 +00:00
|
|
|
|
api.AddToken(token);
|
2024-06-01 04:38:12 +00:00
|
|
|
|
|
|
|
|
|
string accountJson = Preferences.Default.Get("accountdetails", string.Empty);
|
|
|
|
|
string addressJson = Preferences.Default.Get("accountaddresses", string.Empty);
|
|
|
|
|
string selectedAddressJson = Preferences.Default.Get("selectedaddress", string.Empty);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(accountJson)) AccountInfo = JsonSerializer.Deserialize<AccountResponseData>(accountJson);
|
|
|
|
|
if (!string.IsNullOrEmpty(addressJson)) AddressList = JsonSerializer.Deserialize<AddressResponseList>(addressJson);
|
|
|
|
|
if (!string.IsNullOrEmpty(selectedAddressJson)) SelectedAddress = JsonSerializer.Deserialize<AddressResponseData>(selectedAddressJson);
|
|
|
|
|
|
|
|
|
|
// if we haven't got account info, attempt to retrieve it.
|
|
|
|
|
if (AccountInfo == null) {
|
|
|
|
|
AccountInfo = await api.AccountInfo();
|
|
|
|
|
if (AccountInfo != null) {
|
2024-06-27 23:36:04 +00:00
|
|
|
|
// quick fix for users without names (such as the review account)
|
|
|
|
|
if(AccountInfo.Name == null) AccountInfo.Name = AccountInfo.Email.Split('@').FirstOrDefault() ?? "person";
|
2024-06-01 04:38:12 +00:00
|
|
|
|
Preferences.Default.Set("accountdetails", JsonSerializer.Serialize(AccountInfo));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if we don't have the list of addresses, attempt to retrieve that.
|
|
|
|
|
if (AddressList == null) {
|
|
|
|
|
AddressList = await api.Addresses();
|
|
|
|
|
if (AddressList != null) {
|
|
|
|
|
Preferences.Default.Set("accountaddresses", JsonSerializer.Serialize(AddressList));
|
|
|
|
|
SelectedAddress = AddressList.FirstOrDefault();
|
|
|
|
|
Preferences.Default.Set("selectedaddress", JsonSerializer.Serialize(SelectedAddress));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-05 12:41:08 +00:00
|
|
|
|
|
|
|
|
|
public async Task RemoveAccountDetails() {
|
|
|
|
|
Preferences.Default.Clear();
|
|
|
|
|
AccountInfo = null;
|
|
|
|
|
AddressList = null;
|
|
|
|
|
SelectedAddress = null;
|
2024-06-14 07:20:04 +00:00
|
|
|
|
api.RemoveToken();
|
2024-06-06 05:20:09 +00:00
|
|
|
|
}
|
2024-06-05 12:41:08 +00:00
|
|
|
|
|
2024-06-06 05:20:09 +00:00
|
|
|
|
public async Task<MarkupString?> GetBio(string address, bool forceRefresh = false) {
|
|
|
|
|
CachedAddress = address;
|
|
|
|
|
if (forceRefresh || CachedAddressBio == null) {
|
|
|
|
|
CachedAddressBio = await api.StatuslogBio(address);
|
|
|
|
|
}
|
|
|
|
|
return CachedAddressBio;
|
2024-06-05 12:41:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-13 04:46:24 +00:00
|
|
|
|
public async Task<List<MarkupString>?> GetEphemeralMessages(bool forceRefresh = false) {
|
2024-06-20 06:46:12 +00:00
|
|
|
|
if (forceRefresh || this.EphemeralMessages == null || this.EphemeralMessages.Count == 0) {
|
2024-06-13 04:46:24 +00:00
|
|
|
|
this.EphemeralMessages = await api.Ephemeral();
|
|
|
|
|
}
|
|
|
|
|
return this.EphemeralMessages;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-18 07:11:28 +00:00
|
|
|
|
public async Task<List<string>?> GetDirectory(bool forceRefresh = false) {
|
|
|
|
|
if (forceRefresh || this.AddressDirectory == null || this.AddressDirectory.Count == 0) {
|
2024-06-20 04:25:48 +00:00
|
|
|
|
IdnMapping idn = new IdnMapping();
|
|
|
|
|
this.AddressDirectory = (await api.Directory()).Select(s => {
|
|
|
|
|
if (s.StartsWith("xn--")) return idn.GetUnicode(s);
|
|
|
|
|
else return s;
|
|
|
|
|
}).ToList();
|
2024-06-18 07:11:28 +00:00
|
|
|
|
}
|
|
|
|
|
return this.AddressDirectory;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-06 05:20:09 +00:00
|
|
|
|
public async Task<List<Status>?> GetStatuses(bool forceRefresh = false) {
|
2024-06-05 12:41:08 +00:00
|
|
|
|
if (forceRefresh || this.Statuses == null || this.Statuses.Count == 0) {
|
|
|
|
|
this.Statuses = await api.StatuslogLatest();
|
|
|
|
|
}
|
2024-06-06 05:20:09 +00:00
|
|
|
|
return this.Statuses;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-13 04:46:24 +00:00
|
|
|
|
public async Task<List<Status>?> GetStatuses(string address, bool forceRefresh = false) {
|
|
|
|
|
this.CachedAddress = address;
|
|
|
|
|
if (forceRefresh || this.CachedAddressStatuses == null || this.CachedAddressStatuses.Count == 0) {
|
|
|
|
|
this.CachedAddressStatuses = await api.Statuslog(address);
|
2024-06-06 05:20:09 +00:00
|
|
|
|
}
|
2024-06-13 04:46:24 +00:00
|
|
|
|
return this.CachedAddressStatuses;
|
2024-06-06 05:20:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-11 00:36:48 +00:00
|
|
|
|
public async Task<List<NowData>?> GetNowGarden(bool forceRefresh = false) {
|
|
|
|
|
if (forceRefresh || this.NowGarden == null || this.NowGarden.Count == 0) {
|
|
|
|
|
this.NowGarden = await api.NowGarden();
|
|
|
|
|
}
|
|
|
|
|
return this.NowGarden;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-06 05:20:09 +00:00
|
|
|
|
public async Task<List<Pic>?> GetPics(bool forceRefresh = false) {
|
2024-06-20 06:46:12 +00:00
|
|
|
|
if (forceRefresh || this.Pics == null || this.Pics.Count == 0) {
|
2024-06-06 05:20:09 +00:00
|
|
|
|
this.Pics = await api.SomePics();
|
|
|
|
|
}
|
|
|
|
|
return this.Pics;
|
2024-06-05 12:41:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-11 07:24:52 +00:00
|
|
|
|
public async Task<List<Pic>?> GetPics(string address, bool forceRefresh = false) {
|
|
|
|
|
CachedAddress = address;
|
2024-06-13 04:46:24 +00:00
|
|
|
|
if (forceRefresh || this.CachedAddressPics == null || this.CachedAddressPics.Count == 0) {
|
2024-06-11 07:24:52 +00:00
|
|
|
|
CachedAddressPics = (await api.SomePics(address)) ?? new List<Pic>();
|
|
|
|
|
}
|
|
|
|
|
return CachedAddressPics;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-20 06:46:12 +00:00
|
|
|
|
public async Task RefreshStatuses() {
|
|
|
|
|
await GetStatuses(forceRefresh: true);
|
|
|
|
|
if(SelectedAddressName != null)
|
|
|
|
|
await GetStatuses(SelectedAddressName, forceRefresh: true);
|
|
|
|
|
}
|
|
|
|
|
public async Task RefreshPics() {
|
|
|
|
|
await GetPics(forceRefresh: true);
|
|
|
|
|
if (SelectedAddressName != null)
|
|
|
|
|
await GetPics(SelectedAddressName, forceRefresh: true );
|
|
|
|
|
}
|
2024-06-11 00:36:48 +00:00
|
|
|
|
public async Task RefreshNow() => await GetNowGarden(forceRefresh: true);
|
|
|
|
|
|
2024-06-13 00:26:43 +00:00
|
|
|
|
}
|
2024-06-01 04:38:12 +00:00
|
|
|
|
}
|