Commit f21ebba4 authored by HankG's avatar HankG

Add getting newer/older tweets but returns null and destabilizes

parent 6a26c389
......@@ -14,17 +14,48 @@ namespace MySocialPortalDesktop.ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
private const int MaxPosts = 5;
public ObservableCollection<PostViewModel> PostViewModels { get; }
public MainWindowViewModel()
{
PostViewModels = new ObservableCollection<PostViewModel>();
TwitterConnector = SocialMediaConnectorFactory.GetNewTwitterConnector();
}
public void Load()
public void LoadNewer()
{
var posts = new SocialMediaConnectorFactory().GetNewTwitterConnector().GetHomeTimeline(10);
PostViewModels.AddRange(posts.Select(p => new PostViewModel(p)));
var posts = TwitterConnector.GetNewerHomeTimeline(MaxPosts).ToList();
ProcessPosts(posts, true);
}
public void LoadOlder()
{
var posts = TwitterConnector.GetOlderHomeTimeline(MaxPosts).ToList();
ProcessPosts(posts, false);
}
private TwitterConnector TwitterConnector { get; }
private void ProcessPosts(List<Post> posts, bool insertTop)
{
if (posts.Count == 0)
{
Console.WriteLine("No posts returned");
return;
}
if (insertTop)
{
posts.Sort((i1,i2) => i1.PostDateTime.CompareTo(i2.PostDateTime));
posts.ForEach(p => PostViewModels.Insert(0, new PostViewModel(p)));
}
else
{
posts.Sort((i1,i2) => i2.PostDateTime.CompareTo(i1.PostDateTime));
PostViewModels.AddRange(posts.Select(p => new PostViewModel(p)));
}
}
}
}
......@@ -7,6 +7,7 @@ using ReactiveUI;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using LinqToTwitter.Net;
namespace MySocialPortalDesktop.ViewModels
{
......@@ -16,6 +17,7 @@ namespace MySocialPortalDesktop.ViewModels
private static Bitmap DefaultImage = BuildDefaultProfileImage();
private string _body;
private DateTimeOffset _date;
private string _title;
private string _originalSocialMediaSystem;
private Bitmap _profileImage;
......@@ -32,6 +34,12 @@ namespace MySocialPortalDesktop.ViewModels
set => this.RaiseAndSetIfChanged(ref _body, value);
}
public DateTimeOffset Date
{
get => _date;
set => _date = this.RaiseAndSetIfChanged(ref _date, value);
}
public bool HasLinks => Links.Count > 0;
public ObservableCollection<ExternalLinkViewModel> Links { get; private set; }
......@@ -59,7 +67,8 @@ namespace MySocialPortalDesktop.ViewModels
Title = "Unknown Author";
Body = post.Body;
ProfileImage = DefaultImage;
Date = post.PostDateTime;
var author = RepositoryFactory.Instance.MainPeopleRepository.FindById(post.UserId);
if (author != null)
{
......@@ -84,9 +93,17 @@ namespace MySocialPortalDesktop.ViewModels
foreach (var link in Post.Links)
{
var preview = await lps.GetPreview(link.Url).ConfigureAwait(false);
Links.Add(ExternalLinkViewModel.FromLinkPreview(preview, lps.ImageCacheFolderPath));
}
try
{
var preview = await lps.GetPreview(link.Url).ConfigureAwait(false);
Links.Add(ExternalLinkViewModel.FromLinkPreview(preview, lps.ImageCacheFolderPath));
}
catch (Exception e)
{
Console.WriteLine($"Error building VM for link: {link.Url}");
Console.WriteLine(e);
}
}
}
protected async void LoadProfileImage(Person author)
......
......@@ -14,8 +14,8 @@
<vm:MainWindowViewModel/>
</Design.DataContext>
<Grid RowDefinitions="Auto,*" ColumnDefinitions="*">
<Button Grid.Row="0" Grid.Column="0" Width="100" Content="Load" Command="{Binding Load}"/>
<Grid RowDefinitions="Auto,*,Auto" ColumnDefinitions="*">
<Button Grid.Row="0" Grid.Column="0" Width="100" Content="Load Newer" Command="{Binding LoadNewer}"/>
<ListBox Grid.Row="1" Grid.Column="0" Items="{Binding PostViewModels}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
......@@ -23,5 +23,6 @@
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Grid.Row="2" Grid.Column="0" Width="100" Content="Load Older" Command="{Binding LoadOlder}"/>
</Grid>
</Window>
......@@ -29,9 +29,10 @@
<Image Grid.Row="0" Grid.Column="0" Source="{Binding ProfileImage}" Width="30" Height="30" Stretch="UniformToFill" VerticalAlignment="Top"/>
<Grid Grid.Row="0" Grid.Column="1" RowDefinitions="Auto,Auto,Auto,Auto,Auto,Auto" ColumnDefinitions="*">
<TextBlock Grid.Row="0" Grid.Column="0" Name="Title" Classes="Title" Text="{Binding Title}" />
<TextBlock Grid.Row="1" Grid.Column="0" Name="Body" Classes="Body" Text="{Binding Body}" TextWrapping="Wrap" />
<views:ExternalLinkView Grid.Row="2" Grid.Column="0" Name="Links" DataContext="{Binding Links[0]}" Margin="3" IsVisible="{Binding HasLinks}" />
<TextBlock Grid.Row="3" Grid.Column="0" Name="Count" Classes="Body" Text="{Binding Links.Count}" TextWrapping="Wrap" />
<TextBlock Grid.Row="1" Grid.Column="0" Name="Date" Classes="SocialMediaName" Text="{Binding Date}" HorizontalAlignment="Right" />
<TextBlock Grid.Row="2" Grid.Column="0" Name="Body" Classes="Body" Text="{Binding Body}" TextWrapping="Wrap" />
<views:ExternalLinkView Grid.Row="3" Grid.Column="0" Name="Links" DataContext="{Binding Links[0]}" Margin="3" IsVisible="{Binding HasLinks}" />
<TextBlock Grid.Row="4" Grid.Column="0" Name="Count" Classes="Body" Text="{Binding Links.Count}" TextWrapping="Wrap" />
<TextBlock Grid.Row="5" Grid.Column="0" Name="Source" Classes="SocialMediaName" Text="{Binding OriginalSocialMediaSystem}" HorizontalAlignment="Right" />
</Grid>
</Grid>
......
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