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

60 lines
2.0 KiB
C#

using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
// 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 FeelsFeedPage : Page
{
private readonly Feels Feels = new Feels();
private readonly ObservableCollection<FeelsPost> LiveFeelsPosts = [];
private readonly List<List<FeelsPost>> AllFeels;
private int CurrentChunk = 0;
public FeelsFeedPage()
{
this.InitializeComponent();
var feels = Feels.AllFeels();
AllFeels = (List<List<FeelsPost>>)feels.Chunk(4).Select(chunk => chunk.ToList()).ToList();
LoadFeelsChunk();
feelsListView.ItemsSource = LiveFeelsPosts;
}
private void LoadFeelsChunk()
{
var chunk = AllFeels[CurrentChunk];
foreach (var feel in chunk)
{
var date = DateTimeOffset.FromUnixTimeSeconds(feel.M_Time).DateTime.ToLocalTime();
feel.TimeString = date.ToString("HH:mm (dddd, MMMM dd, yyyy)");
var body = Feels.GetFeelsBody(feel);
char[] delimiters = new char[] { ' ', '\n' };
feel.WordCount = body.Split(delimiters, StringSplitOptions.RemoveEmptyEntries).Length;
feel.Body = body;
LiveFeelsPosts.Add(feel);
}
CurrentChunk++;
}
private void ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
var scrollViewer = sender as ScrollViewer;
if (scrollViewer != null)
{
if (scrollViewer.VerticalOffset >= scrollViewer.ScrollableHeight)
{
LoadFeelsChunk();
}
}
}
}
}