Neighbourhood.omg.lol/Classes/State.cs

340 lines
12 KiB
C#
Raw Permalink Normal View History

2024-06-06 05:20:09 +00:00
using Microsoft.AspNetCore.Components;
using System.ComponentModel;
2024-06-20 04:25:48 +00:00
using System.Globalization;
using System.Text.Json;
2024-07-01 23:19:37 +00:00
using Neighbourhood.omg.lol.Models;
2024-07-02 00:01:06 +00:00
using System.Runtime.CompilerServices;
2024-07-01 23:19:37 +00:00
namespace Neighbourhood.omg.lol {
public class State : INotifyPropertyChanged {
2024-07-02 00:01:06 +00:00
// Events
public event EventHandler<EventArgs>? 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<Status>? Statuses { get; set; }
public List<Pic>? Pics { get; set; }
public List<NowData>? NowGarden { get; set; }
public List<MarkupString>? EphemeralMessages { get; set; }
public List<string>? AddressDirectory { get; set; }
2024-06-13 00:26:43 +00:00
2024-07-25 03:39:36 +00:00
public List<FeedItem>? Feed { get; set; }
2024-06-28 07:08:12 +00:00
2024-07-11 05:57:53 +00:00
public Dictionary<string, Theme>? Themes { get; set; }
// Account data
private AccountResponseData? _accountInfo;
public AccountResponseData? AccountInfo {
get => _accountInfo;
set {
_accountInfo = value;
OnPropertyChanged(nameof(AccountInfo));
}
}
public AddressResponseList? AddressList { get; set; }
2024-06-20 06:46:12 +00:00
public bool IsAuthorized { get => AccountInfo != null; }
public string? Name { get => AccountInfo?.Name; }
public string? Email { get => AccountInfo?.Email; }
public IEnumerable<string>? AddressNames { get => AddressList?.Select(a => a.Address); }
// Selected Address
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-07-02 00:01:06 +00:00
OnPropertyChanged();
}
}
}
}
2024-06-28 07:08:12 +00:00
public List<string>? Following { get; private set; }
public string? SelectedAddressName { get => SelectedAddress?.Address; }
// 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 List<Paste>? CachedAddressPastes { get; set; }
2024-06-06 05:20:09 +00:00
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>();
CachedAddressPastes = new List<Paste>();
2024-06-06 05:20:09 +00:00
CachedAddressBio = null;
}
}
}
2024-06-05 12:41:08 +00:00
// share intent stuff
private string? _shareString;
2024-06-20 06:46:12 +00:00
public string? ShareString {
get => _shareString;
set {
_shareString = value;
2024-07-02 00:01:06 +00:00
OnIntentRecieved();
}
}
public string? ShareStringSubject { get; set; }
private string? _sharePhoto;
public string? SharePhoto {
get => _sharePhoto;
set {
_sharePhoto = value;
2024-07-02 00:01:06 +00:00
OnIntentRecieved();
}
}
public long? SharePhotoSize { get; set; }
public string? SharePhotoContentType { get; set; }
public string? SharePhotoText { get; set; }
// refreshing
private bool _isRefreshing;
public bool IsRefreshing {
get => _isRefreshing;
2024-06-24 04:52:45 +00:00
private set {
_isRefreshing = value;
2024-07-02 00:01:06 +00:00
OnPropertyChanged();
}
}
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();
}
private bool _canRefresh;
public bool CanRefresh {
get => _canRefresh;
set {
_canRefresh = value;
2024-07-02 00:01:06 +00:00
OnPropertyChanged();
}
}
// api service
2024-07-16 00:45:33 +00:00
private ApiService api { get; set; }
2024-07-16 00:45:33 +00:00
public State(ApiService restService) {
api = restService;
}
public async Task PopulateAccountDetails(string token) {
api.AddToken(token);
string accountJson = Preferences.Default.Get("accountdetails", string.Empty);
string addressJson = Preferences.Default.Get("accountaddresses", string.Empty);
string selectedAddressJson = Preferences.Default.Get("selectedaddress", string.Empty);
2024-06-28 07:08:12 +00:00
string followingJson = Preferences.Default.Get("following", 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);
2024-06-28 07:08:12 +00:00
if (!string.IsNullOrEmpty(followingJson)) Following = JsonSerializer.Deserialize<List<string>>(followingJson);
// if we haven't got account info, attempt to retrieve it.
if (AccountInfo == null) {
AccountInfo = await api.AccountInfo();
if (AccountInfo != null) {
// quick fix for users without names (such as the review account)
if(AccountInfo.Name == null) AccountInfo.Name = AccountInfo.Email.Split('@').FirstOrDefault() ?? "person";
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() {
2024-07-02 00:01:06 +00:00
await Task.Run(() => {
Preferences.Default.Clear();
AccountInfo = null;
AddressList = null;
SelectedAddress = null;
Following = null;
api.RemoveToken();
});
2024-06-06 05:20:09 +00:00
}
2024-06-05 12:41:08 +00:00
2024-06-28 07:08:12 +00:00
public bool IsFollowing(string address) => Following?.Contains(address) ?? false;
2024-07-02 02:55:07 +00:00
public async Task Follow(string address) {
2024-06-28 07:08:12 +00:00
if (Following == null) Following = new List<string>();
Following.Add(address);
Preferences.Default.Set("following", JsonSerializer.Serialize(Following));
2024-07-02 02:55:07 +00:00
await GetFeed(forceRefresh: true);
2024-06-28 07:08:12 +00:00
}
2024-07-02 02:55:07 +00:00
public async Task Unfollow(string address) {
2024-06-28 07:08:12 +00:00
if (Following == null) Following = new List<string>();
Following.Remove(address);
Preferences.Default.Set("following", JsonSerializer.Serialize(Following));
2024-07-02 02:55:07 +00:00
await GetFeed(forceRefresh: true);
2024-06-28 07:08:12 +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) {
2024-07-23 01:12:52 +00:00
CachedAddressBio = Utilities.MdToHtmlMarkup(await api.StatuslogBio(address));
2024-06-06 05:20:09 +00:00
}
return CachedAddressBio;
2024-06-05 12:41:08 +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) {
this.EphemeralMessages = await api.Ephemeral();
}
return this.EphemeralMessages;
}
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();
2024-07-02 00:01:06 +00:00
this.AddressDirectory = (await api.Directory())?.Select(s => {
2024-06-20 04:25:48 +00:00
if (s.StartsWith("xn--")) return idn.GetUnicode(s);
else return s;
}).ToList();
}
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;
}
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
}
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;
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;
}
public async Task<List<Paste>?> GetPastes(string address, bool forceRefresh = false) {
CachedAddress = address;
if (forceRefresh || this.CachedAddressPastes == null || this.CachedAddressPastes.Count == 0) {
if (AddressNames?.Contains(address) ?? false) {
CachedAddressPastes = (await api.GetMyPastes(address)) ?? new List<Paste>();
}
else {
CachedAddressPastes = (await api.GetPastes(address)) ?? new List<Paste>();
}
}
return CachedAddressPastes;
}
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);
public async Task RefreshPastes() {
if (SelectedAddressName != null)
await GetPastes(SelectedAddressName, forceRefresh: true);
}
2024-07-25 03:39:36 +00:00
public async Task<IOrderedEnumerable<FeedItem>> GetFeed(bool forceRefresh = false) {
2024-06-28 07:08:12 +00:00
if(forceRefresh || Feed == null || Feed.Count == 0) {
2024-07-25 03:39:36 +00:00
Feed = new List<FeedItem>();
2024-06-28 07:08:12 +00:00
foreach(string address in Following ?? new List<string>()) {
2024-07-25 03:39:36 +00:00
Feed.AddRange((await GetStatuses(address, forceRefresh))?.Select(s => new FeedItem { Status = s }) ?? new List<FeedItem>());
Feed.AddRange((await GetPics(address, forceRefresh))?.Select(p => new FeedItem { Pic = p }) ?? new List<FeedItem>());
Feed.AddRange((await GetPastes(address, forceRefresh))?.Select(p => new FeedItem { Paste = p }) ?? new List<FeedItem>());
2024-06-28 07:08:12 +00:00
}
}
return Feed.OrderByDescending(s => s.CreatedTime);
}
2024-07-11 05:57:53 +00:00
public async Task<Dictionary<string, Theme>?> GetThemes(bool forceRefresh = false) {
if (forceRefresh || this.Themes == null || this.Themes.Count == 0) {
this.Themes = await api.GetThemes();
}
return this.Themes;
}
2024-06-13 00:26:43 +00:00
}
}