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
{
///
/// An empty page that can be used on its own or navigated to within a Frame.
///
public sealed partial class FeelsFeedPage : Page
{
private readonly Feels Feels = new Feels();
private readonly ObservableCollection LiveFeelsPosts = [];
private readonly List> AllFeels;
private int CurrentChunk = 0;
public FeelsFeedPage()
{
this.InitializeComponent();
var feels = Feels.AllFeels();
AllFeels = (List>)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();
}
}
}
}
}