Commit e015cbd9 authored by HankG's avatar HankG

Updates to get profile photo preview working

parent c1eaf869
using MySocialPortalDesktop.Services;
using MySocialPortalDesktop.Factory;
using MySocialPortalDesktop.ViewModels;
using MySocialPortalDesktop.Views;
namespace MySocialPortalDesktop.DesignData
{
......
......@@ -4,7 +4,6 @@
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="Factory" />
<Folder Include="Models\" />
<Compile Update="**\*.xaml.cs">
<DependentUpon>%(Filename)</DependentUpon>
......@@ -13,6 +12,7 @@
<SubType>Designer</SubType>
</AvaloniaResource>
<AvaloniaResource Include="Assets\**" />
<Folder Include="Services" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="0.9.0-preview7" />
......
using System;
using MySocialPortalDesktop.Factory;
using MySocialPortalLib.Service;
namespace MySocialPortalDesktop.Services
{
public class ServiceFactory
{
private static readonly Lazy<ServiceFactory> Singleton =
new Lazy<ServiceFactory>(() => new ServiceFactory());
public static readonly ServiceFactory Instance = Singleton.Value;
public LinkPreviewService LinkPreviewService { get; }
public ImageService ImageService { get; }
private ServiceFactory()
{
var imagePreviewCacheFolder = DirectoryServices.Instance.OpenGraphCacheDirectory();
var linkPreviewImageService = new ImageService(imagePreviewCacheFolder);
LinkPreviewService = new LinkPreviewService(RepositoryFactory.Instance.LinkPreviewRepository, imagePreviewCacheFolder);
}
}
}
\ No newline at end of file
using System;
using System.IO;
using Avalonia.Media.Imaging;
using MySocialPortalDesktop.Factory;
using MySocialPortalDesktop.Services;
using MySocialPortalLib.Model;
using ReactiveUI;
......@@ -58,7 +56,11 @@ namespace MySocialPortalDesktop.ViewModels
if (item != null && item.UrlFound)
{
vm.Title = item.Title;
vm.Description = item.Description.Length > MaxDescriptionLenth ? item.Description.Substring(0, MaxDescriptionLenth) + "..." : item.Description;
if (item.Description != null)
{
vm.Description = item.Description.Length > MaxDescriptionLenth ? item.Description.Substring(0, MaxDescriptionLenth) + "..." : item.Description;
}
vm.HasPreview = true;
vm.Url = item.RequestUrl;
if (!item.HasPreviewImage)
......
......@@ -14,7 +14,7 @@ namespace MySocialPortalDesktop.ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
public ObservableCollection<PostViewModel> PostViewModels { get; set; }
public ObservableCollection<PostViewModel> PostViewModels { get; }
public MainWindowViewModel()
{
PostViewModels = new ObservableCollection<PostViewModel>();
......
using System;
using Avalonia.Media.Imaging;
using MySocialPortalDesktop.Factory;
using MySocialPortalDesktop.Services;
using MySocialPortalLib.Model;
using MySocialPortalLib.Service;
using ReactiveUI;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
namespace MySocialPortalDesktop.ViewModels
{
public class PostViewModel : ViewModelBase
{
private const string DefaultProfileImageName = "default_profile_icon.png";
private static Bitmap DefaultImage = BuildDefaultProfileImage();
private string _body;
private string _title;
private string _originalSocialMediaSystem;
......@@ -45,7 +49,7 @@ namespace MySocialPortalDesktop.ViewModels
public PostViewModel():this(new Post())
{
}
public PostViewModel(Post post)
......@@ -54,14 +58,15 @@ namespace MySocialPortalDesktop.ViewModels
Post = post;
Title = "Unknown Author";
Body = post.Body;
var defaultImageName = "default_profile_icon.png";
string directory = DirectoryServices.Instance.ProfileImagesDirectory();
ProfileImage = new Bitmap(Path.Combine(directory, defaultImageName));
ProfileImage = DefaultImage;
var author = RepositoryFactory.Instance.MainPeopleRepository.FindById(post.UserId);
if (author != null)
{
Title = author.Name;
LoadProfileImage(author);
}
OriginalSocialMediaSystem = post.OriginalSocialMediaSystem;
FillLinkVMs();
}
......@@ -83,5 +88,39 @@ namespace MySocialPortalDesktop.ViewModels
Links.Add(ExternalLinkViewModel.FromLinkPreview(preview, lps.ImageCacheFolderPath));
}
}
protected async void LoadProfileImage(Person author)
{
var profileImageUrl = author.SocialMediaAccounts.First().Value.ProfilePhotoPath;
try
{
if (!string.IsNullOrWhiteSpace(profileImageUrl))
{
var (found, imageFileInfo) = await ServiceFactory.Instance.ProfileImageService
.GetRemoteImage(new Uri(profileImageUrl)).ConfigureAwait(false);
if (found)
{
ProfileImage = new Bitmap(imageFileInfo.FullName);
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
private static Bitmap BuildDefaultProfileImage()
{
try
{
string directory = DirectoryServices.Instance.ProfileImagesDirectory();
return new Bitmap(Path.Combine(directory, DefaultProfileImageName));
}
catch
{
return null;
}
}
}
}
\ No newline at end of file
......@@ -7,9 +7,9 @@ namespace MySocialPortalDesktop.Factory
{
public class RepositoryFactory : IDisposable
{
private const string AllPostsRepositoryDbName = "all_people.db";
private const string AllPostsRepositoryDbName = "all_posts.db";
private const string LinkPreviewRepositoryDbName = "link_preview.db";
private const string MainPeopleRepositoryDbName = "all_posts.db";
private const string MainPeopleRepositoryDbName = "all_people.db";
private const string ProfileImageRepositoryDbName = "profile_images.db";
private static readonly Lazy<RepositoryFactory> Singleton =
......
......@@ -37,7 +37,11 @@ namespace MySocialPortalLib.Service
try
{
var imageUrl = imageUri.AbsoluteUri;
var imageExtension = new FileInfo(imageUri.AbsolutePath).Extension ?? DefaultExtension;
var imageExtension = new FileInfo(imageUri.AbsolutePath).Extension;
if (string.IsNullOrWhiteSpace(imageExtension))
{
imageExtension = DefaultExtension;
}
var imageCacheItem = CacheRepository.FindByUrl(imageUrl);
var imageFileName = imageCacheItem?.LocalFileName ?? GetRandomName(imageExtension);
imageFilePath = Path.Combine(LocalImageFolderPath, imageFileName);
......@@ -56,7 +60,7 @@ namespace MySocialPortalLib.Service
}
using var client = new WebClient();
await client.DownloadFileTaskAsync(imageUrl, imageFilePath).ConfigureAwait(true);
await client.DownloadFileTaskAsync(imageUrl, imageFilePath).ConfigureAwait(false);
if (!File.Exists(imageFilePath))
{
if (imageCacheItem != null)
......
......@@ -52,7 +52,7 @@ namespace MySocialPortalLib.Service
var hasImagePreview = false;
if (!string.IsNullOrEmpty(imageUrl?.AbsolutePath.Trim()))
{
var (success, imageInfo) = await ImageService.GetRemoteImage(imageUrl).ConfigureAwait(true);
var (success, imageInfo) = await ImageService.GetRemoteImage(imageUrl).ConfigureAwait(false);
hasImagePreview = success;
imageName = imageInfo?.Name ?? "";
}
......
......@@ -64,6 +64,14 @@ namespace MySocialPortalLibTest.Service
Assert.False(success1);
Assert.Null(file1);
}
[Fact]
public void TesNullUri()
{
var (success, file) = GetImageService().GetRemoteImage(null).Result;
Assert.False(success);
Assert.Null(file);
}
private ImageService GetImageService()
{
......
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