mirror of
https://github.com/ultrasn0w/app.git
synced 2025-12-13 10:29:53 +01:00
167 lines
4.4 KiB
C#
167 lines
4.4 KiB
C#
using System.Diagnostics;
|
|
using APP.DTO;
|
|
using APP.Utility;
|
|
|
|
namespace APP;
|
|
|
|
public partial class Main : Form
|
|
{
|
|
private readonly ImageList _imageListSeason;
|
|
private Auth? _auth;
|
|
private bool _authSucc;
|
|
private List<Anime>? _seasonAnime;
|
|
private User? _user;
|
|
|
|
public Main()
|
|
{
|
|
InitializeComponent();
|
|
_imageListSeason = new ImageList();
|
|
_imageListSeason.ImageSize = new Size(128, 200);
|
|
Load += OnLoadEv;
|
|
LogBoy.AddToLog("Hi");
|
|
}
|
|
|
|
private async void OnLoadEv(object? sender, EventArgs e)
|
|
{
|
|
_auth = await SaveBoy.ReadAuth();
|
|
if (_auth != null)
|
|
{
|
|
// check if we exist
|
|
var users = await BackendComms.GetUser();
|
|
if (users == null || users.All(u => u.Username != _auth.Username))
|
|
{
|
|
// We dont exist
|
|
SaveBoy.DeleteAuth();
|
|
_auth = null;
|
|
}
|
|
else
|
|
{
|
|
// try auth
|
|
if (await BackendComms.Auth(_auth.Username, _auth.Secret))
|
|
{
|
|
// Login successful
|
|
loginToolStripMenuItem.Visible = false;
|
|
_authSucc = true;
|
|
await UpdateUser();
|
|
}
|
|
else
|
|
{
|
|
_authSucc = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
await UpdateSeason();
|
|
}
|
|
|
|
private async Task UpdateSeason()
|
|
{
|
|
_seasonAnime = await BackendComms.GetSeason();
|
|
if (_seasonAnime == null)
|
|
{
|
|
ShowError("Fehler beim Season abrufen");
|
|
}
|
|
else
|
|
{
|
|
var count = 0;
|
|
foreach (var anime in _seasonAnime)
|
|
{
|
|
_imageListSeason.Images.Add(await WebBoy.DownloadImage(anime.ImageLarge));
|
|
listViewSeason.Items.Add(new ListViewItem
|
|
{
|
|
Text = anime.Title,
|
|
ImageIndex = count++
|
|
});
|
|
}
|
|
|
|
listViewSeason.LargeImageList = _imageListSeason;
|
|
}
|
|
}
|
|
|
|
private async Task UpdateUser()
|
|
{
|
|
_user = await BackendComms.GetUser(_auth?.Username);
|
|
if (_user == null)
|
|
{
|
|
ShowError("Fehler beim User abrufen");
|
|
}
|
|
else
|
|
{
|
|
tabUser.Text = _user.Username;
|
|
userLabel.Text = _user.Username;
|
|
userLabel.Visible = true;
|
|
userLinkLabel.Text = _user.Url;
|
|
userLinkLabel.Visible = true;
|
|
userLabelInfo.Text = StringAssemble.UserInfo(_user);
|
|
userLabelInfo.Visible = true;
|
|
userLabelInfoData.Text = StringAssemble.UserData(_user);
|
|
userLabelInfoData.Visible = true;
|
|
if (!string.IsNullOrWhiteSpace(_user.ImageUrl))
|
|
{
|
|
userImage.LoadAsync(_user.ImageUrl);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void ShowError(string text)
|
|
{
|
|
MessageBox.Show(text, @"FEHLER", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
|
|
private async void loginToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
if (_authSucc) return;
|
|
var users = await BackendComms.GetUser();
|
|
if (DoRegister(users))
|
|
{
|
|
await UpdateUser();
|
|
}
|
|
}
|
|
|
|
private bool DoRegister(List<User>? users)
|
|
{
|
|
// Register promt
|
|
var regWin = new RegisterWin(_auth, users);
|
|
regWin.ShowDialog();
|
|
if (regWin.Cancelled)
|
|
{
|
|
regWin.Dispose();
|
|
return false;
|
|
}
|
|
|
|
_auth = new Auth
|
|
{
|
|
Username = regWin.Auth.Username,
|
|
Secret = regWin.Auth.Secret
|
|
};
|
|
loginToolStripMenuItem.Visible = false;
|
|
regWin.Dispose();
|
|
return true;
|
|
}
|
|
|
|
private async void userLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(_user?.Url)) return;
|
|
try
|
|
{
|
|
var url = new Uri(_user.Url).ToString();
|
|
var sInfo = new ProcessStartInfo(url)
|
|
{
|
|
UseShellExecute = true
|
|
};
|
|
Process.Start(sInfo);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await Console.Error.WriteLineAsync(ex.Message);
|
|
}
|
|
}
|
|
|
|
private void logToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
var logWin = new LogWin(LogBoy.GetLog());
|
|
logWin.ShowDialog();
|
|
logWin.Dispose();
|
|
}
|
|
}
|