initial push
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Input;
|
||||
using Photos.Models;
|
||||
using Avalonia.Platform.Storage;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia.Threading;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
|
||||
namespace Photos.ViewModels;
|
||||
|
||||
public partial class MainWindowViewModel : ViewModelBase
|
||||
{
|
||||
public ObservableCollection<Photo> Photos { get; private set; } = [];
|
||||
|
||||
[ObservableProperty] private Photo? _selectedPhoto;
|
||||
|
||||
public string LibraryPath { get; private set; } = string.Empty;
|
||||
|
||||
public MainWindowViewModel()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void OpenDirectory()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void SanitizeFiles(List<string> files, IEnumerable<string> extensions)
|
||||
=> files.RemoveAll(x => !extensions.Any(ex => x.EndsWith(ex, StringComparison.OrdinalIgnoreCase)));
|
||||
|
||||
private async Task AddPhotosAsync(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
var files = await Task.Run(() =>
|
||||
{
|
||||
var files = Directory.GetFiles(path, "*", SearchOption.AllDirectories).ToList();
|
||||
SanitizeFiles(files, [".png", ".jpg", ".jpeg"]);
|
||||
return files;
|
||||
});
|
||||
|
||||
var photosToAdd = await Task.Run(() =>
|
||||
{
|
||||
var photos = new List<Photo>();
|
||||
foreach (var file in files)
|
||||
{
|
||||
photos.Add(new Photo(file, LibraryPath));
|
||||
}
|
||||
return photos;
|
||||
});
|
||||
|
||||
await Dispatcher.UIThread.InvokeAsync(() =>
|
||||
{
|
||||
foreach (var photo in photosToAdd) Photos.Add(photo);
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async void WindowDropHandler(DragEventArgs e)
|
||||
{
|
||||
var files = e.DataTransfer.TryGetFiles();
|
||||
if (files is null || files.Length == 0) return;
|
||||
|
||||
var localPath = Path.GetDirectoryName(files[0].TryGetLocalPath());
|
||||
if (String.IsNullOrWhiteSpace(localPath)) return;
|
||||
|
||||
LibraryPath = localPath;
|
||||
Photos.Clear();
|
||||
|
||||
await AddPhotosAsync(localPath);
|
||||
}
|
||||
|
||||
public void PhotoClicked(PointerReleasedEventArgs e)
|
||||
{
|
||||
if(e.Source is not Image img) return;
|
||||
SelectedPhoto = Photos.FirstOrDefault(x => x.Source == img.Source);
|
||||
}
|
||||
|
||||
public void SelectedPhotoClicked()
|
||||
{
|
||||
SelectedPhoto = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user