wakka/BinkPage.xaml.cs
2025-05-12 02:41:58 -05:00

106 lines
3.2 KiB
C#

using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Navigation;
using Org.BouncyCastle.Tls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Threading;
using Microsoft.UI.Xaml;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace wakka
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class BinkPage : Page
{
private DispatcherTimer timer;
private readonly ObservableCollection<BinkPost> Binks = [];
private static Bink Bink { get; set; } = new Bink();
public BinkPage()
{
this.InitializeComponent();
StartTimer();
var BinkList = Bink.AllBinks();
if (BinkList != null)
{
foreach (var bink in BinkList)
{
SetBinkDateString(bink);
Binks.Add(bink);
}
}
binksListView.ItemsSource = Binks;
}
public void SetBinkDateString (BinkPost bink)
{
var date = DateTimeOffset.FromUnixTimeSeconds(bink.Time / 1000000000).DateTime.ToLocalTime();
bink.TimeString = date.ToString("HH:mm (dddd, MMMM dd, yyyy)");
}
public void GetNewBinks()
{
var BinkList = Bink.BinksAfter(Binks[0].Time);
if (BinkList != null)
{
BinkList.Reverse();
foreach (var bink in BinkList)
{
SetBinkDateString(bink);
Binks.Insert(0, bink);
}
}
}
private void BinkSubmit(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
var message = binkComposeBox.Text;
if (!string.IsNullOrEmpty(message))
{
Bink.PostBink(message);
binkComposeBox.Text = string.Empty;
GetNewBinks();
}
}
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
if (string.IsNullOrEmpty(binkComposeBox.Text))
{
binkComposeDialog.IsPrimaryButtonEnabled = false;
}
else
{
binkComposeDialog.IsPrimaryButtonEnabled = true;
}
}
private async void OnBinkCreate(object sender, RoutedEventArgs e)
{
binkComposeDialog.IsPrimaryButtonEnabled = false;
await binkComposeDialog.ShowAsync();
}
public void GetNewBinksButton(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
GetNewBinks();
}
private void StartTimer()
{
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(20);
timer.Tick += Timer_Tick;
timer.Start();
}
private void Timer_Tick(object? sender, object? e)
{
GetNewBinks();
}
}
}