Commit e43b5229 authored by HankG's avatar HankG

Add menu commands, Posts repository usage, and external Post JSON import

parent 19f58f21
using System;

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using DynamicData;
using LiteDB;
using MySocialPortalDesktop.Factory;
using MySocialPortalLib.Model;
using MySocialPortalLib.Service;
using MySocialPortalLib.Service.SocialMediaConnectors;
using MySocialPortalLib.Util;
using ReactiveUI;
namespace MySocialPortalDesktop.ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
private const int MaxPosts = 5;
private const int MaxPostsQuery = 25;
private const int MaxPostsFromRepo = 200;
public ObservableCollection<PostViewModel> PostViewModels { get; }
public MainWindowViewModel()
{
PostViewModels = new ObservableCollection<PostViewModel>();
TwitterConnector = SocialMediaConnectorFactory.GetNewTwitterConnector();
ImportedPosts = new List<Post>();
LoadInitialPosts();
}
public void DoExit()
{
var app = Application.Current.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime;
app?.Shutdown();
}
public void LoadNewer()
{
var posts = TwitterConnector.GetNewerHomeTimeline(MaxPosts).ToList();
var posts = TwitterConnector.GetNewerHomeTimeline(MaxPostsQuery).ToList();
ProcessPosts(posts, true);
}
public void LoadOlder()
{
var posts = TwitterConnector.GetOlderHomeTimeline(MaxPosts).ToList();
if (ImportedPosts.Count > 0)
{
ProcessImported();
return;
}
var posts = TwitterConnector.GetOlderHomeTimeline(MaxPostsQuery).ToList();
ProcessPosts(posts, false);
}
public async void ImportPosts()
{
var app = Application.Current.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime;
var window = app.MainWindow;
var fileDialog = new OpenFileDialog
{
Directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
Title = "Import Posts Database",
AllowMultiple = false,
Filters = new List<FileDialogFilter>
{
new FileDialogFilter
{
Name = "Posts JSON",
Extensions = new List<string>{"json"}
}
}
};
var result = await fileDialog.ShowAsync(window).ConfigureAwait(false);
if (result != null)
{
await ImportPostsDatabase(result.First()).ConfigureAwait(false);
}
}
private bool ProcessImported()
{
foreach (var post in ImportedPosts)
{
try
{
PostViewModels.Add(new PostViewModel(post));
}
catch (Exception e1)
{
Console.WriteLine($"Error processing post {post?.Id}: {e1}");
}
}
return true;
}
private TwitterConnector TwitterConnector { get; }
private List<Post> ImportedPosts { get; }
private TwitterConnector TwitterConnector { get; }
private async Task<bool> ImportPostsDatabase(string jsonFile)
{
Console.WriteLine("Importing Posts");
try
{
var postsToImport =
System.Text.Json.JsonSerializer.Deserialize<List<Post>>(System.IO.File.ReadAllText(jsonFile));
ImportedPosts.AddRange(postsToImport);
}
catch (Exception e2)
{
Console.WriteLine($"Error reading file '{jsonFile}': {e2}");
}
Console.WriteLine("Import Complete");
return true;
}
private void LoadInitialPosts()
{
var posts = RepositoryFactory.Instance.AllPostsRepository.GetPosts(MaxPostsFromRepo);
if (posts == null)
{
return;
}
var postsList = posts.ToList();
postsList.Sort(new PostComparisonDescending());
postsList.ForEach(p => PostViewModels.Add(new PostViewModel(p)));
}
private void ProcessPosts(List<Post> posts, bool insertTop)
{
if (posts.Count == 0)
......@@ -45,14 +141,36 @@ namespace MySocialPortalDesktop.ViewModels
return;
}
RepositoryFactory.Instance.AllPostsRepository.AddPosts(posts);
if (insertTop)
{
posts.Sort((i1,i2) => i1.PostDateTime.CompareTo(i2.PostDateTime));
posts.Sort(new PostComparisonAscending());
posts.ForEach(p => PostViewModels.Insert(0, new PostViewModel(p)));
}
else
{
posts.Sort((i1,i2) => i2.PostDateTime.CompareTo(i1.PostDateTime));
posts.Sort(new PostComparisonDescending());
var currentGuessIndex = 0;
foreach (var post in posts)
{
var postDate = post.PostDateTime.LocalDateTime;
var guessDate = PostViewModels[currentGuessIndex].Date;
if (postDate >= guessDate)
{
PostViewModels.Insert(currentGuessIndex, new PostViewModel(post));
continue;
}
if (currentGuessIndex == PostViewModels.Count - 1)
{
PostViewModels.Insert(currentGuessIndex, new PostViewModel(post));
continue;
}
currentGuessIndex++;
}
PostViewModels.AddRange(posts.Select(p => new PostViewModel(p)));
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment