Optimalization, file name&extension

This commit is contained in:
2022-02-08 09:53:20 +01:00
parent 2b5ab06b57
commit 755793c85c
22 changed files with 268 additions and 85 deletions

View File

@@ -132,6 +132,8 @@
<converters:DateTimeConverter x:Key="DateTimeConverter"/>
<converters:IsTypeConverter x:Key="IsTypeConverter"/>
<converters:ItemViewModelIsAttibuteTypeConverter x:Key="ItemViewModelIsAttibuteTypeConverter"/>
<converters:ItemViewModelIsAttibuteTypeConverter x:Key="ItemViewModelIsNotAttibuteTypeConverter" Invert="true"/>
<converters:GetFileExtensionConverter x:Key="GetFileExtensionConverter"/>
</ResourceDictionary>
</Application.Resources>

View File

@@ -21,6 +21,8 @@ namespace FileTime.Avalonia.Application
public partial class TabContainer : INewItemProcessor
{
private bool _updateFromCode;
private CancellationTokenSource? _moveCancellationTokenSource;
[Property]
private TabState _tabState;
@@ -70,6 +72,10 @@ namespace FileTime.Avalonia.Application
{
if (_selectedItem != value)
{
if(_selectedItem is ContainerViewModel containerVM)
{
containerVM.Unload(unloadParent: false);
}
_selectedItem = value;
if (value is ElementViewModel elementViewModel)
@@ -83,8 +89,10 @@ namespace FileTime.Avalonia.Application
ElementPreview = null;
}
await Tab.SetCurrentSelectedItem(SelectedItem?.Item, fromDataBinding);
OnPropertyChanged(nameof(SelectedItem));
if (await Tab.SetCurrentSelectedItem(SelectedItem?.Item, fromDataBinding))
{
OnPropertyChanged(nameof(SelectedItem));
}
}
}
@@ -131,6 +139,7 @@ namespace FileTime.Avalonia.Application
private async Task Tab_CurrentLocationChanged(object? sender, AsyncEventArgs e, CancellationToken token = default)
{
CurrentLocation.Unload(true);
var currentLocation = await Tab.GetCurrentLocation(token);
var parent = GenerateParent(currentLocation);
CurrentLocation = new ContainerViewModel(this, parent, currentLocation, ItemNameConverterService);
@@ -270,6 +279,13 @@ namespace FileTime.Avalonia.Application
}
}
private CancellationToken CancelAndGenerateNextMovementToken()
{
if(_moveCancellationTokenSource != null) _moveCancellationTokenSource.Cancel();
_moveCancellationTokenSource = new CancellationTokenSource();
return _moveCancellationTokenSource.Token;
}
public async Task Open()
{
if (ChildContainer != null)
@@ -285,32 +301,32 @@ namespace FileTime.Avalonia.Application
public async Task MoveCursorDown()
{
await RunFromCode(async () => await Tab.SelectNextItem());
await RunFromCode(async () => await Tab.SelectNextItem(token: CancelAndGenerateNextMovementToken()));
}
public async Task MoveCursorDownPage()
{
await RunFromCode(async () => await Tab.SelectNextItem(10));
await RunFromCode(async () => await Tab.SelectNextItem(10, token: CancelAndGenerateNextMovementToken()));
}
public async Task MoveCursorUp()
{
await RunFromCode(async () => await Tab.SelectPreviousItem());
await RunFromCode(async () => await Tab.SelectPreviousItem(token: CancelAndGenerateNextMovementToken()));
}
public async Task MoveCursorUpPage()
{
await RunFromCode(async () => await Tab.SelectPreviousItem(10));
await RunFromCode(async () => await Tab.SelectPreviousItem(10, token: CancelAndGenerateNextMovementToken()));
}
public async Task MoveCursorToFirst()
{
await RunFromCode(Tab.SelectFirstItem);
await RunFromCode(async () => await Tab.SelectFirstItem(token: CancelAndGenerateNextMovementToken()));
}
public async Task MoveCursorToLast()
{
await RunFromCode(Tab.SelectLastItem);
await RunFromCode(async () => await Tab.SelectLastItem(token: CancelAndGenerateNextMovementToken()));
}
public async Task GotToProvider()

View File

@@ -10,14 +10,12 @@ namespace FileTime.Avalonia.Converters
{
public class ContextMenuGenerator : IValueConverter
{
private readonly IContextMenuProvider _contextMenuProvider;
private IContextMenuProvider? _contextMenuProvider;
public ContextMenuGenerator()
{
_contextMenuProvider = App.ServiceProvider.GetService<IContextMenuProvider>() ?? throw new Exception($"No {nameof(IContextMenuProvider)} is registered.");
}
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
_contextMenuProvider ??= App.ServiceProvider.GetService<IContextMenuProvider>() ?? throw new Exception($"No {nameof(IContextMenuProvider)} is registered.");
if (value is ContainerViewModel containerViewModel)
{
return _contextMenuProvider.GetContextMenuForFolder(containerViewModel.Container);

View File

@@ -0,0 +1,25 @@
using System;
using System.Globalization;
using Avalonia.Data.Converters;
using FileTime.Avalonia.Services;
using Microsoft.Extensions.DependencyInjection;
namespace FileTime.Avalonia.Converters
{
public class GetFileExtensionConverter : IValueConverter
{
private ItemNameConverterService? _itemNameConverterService;
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is not string fullName) return value;
_itemNameConverterService ??= App.ServiceProvider.GetService<ItemNameConverterService>() ?? throw new Exception($"No {nameof(ItemNameConverterService)} is registered.");;
return _itemNameConverterService.GetFileExtension(fullName);
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

View File

@@ -9,9 +9,12 @@ namespace FileTime.Avalonia.Converters
{
public class ItemViewModelIsAttibuteTypeConverter : IValueConverter
{
public bool Invert { get; set; }
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
return parameter is AttibuteType targetAttribute && GetAttibuteType(value) == targetAttribute;
var result = parameter is AttibuteType targetAttribute && GetAttibuteType(value) == targetAttribute;
if (Invert && parameter is AttibuteType) result = !result;
return result;
}
private static AttibuteType? GetAttibuteType(object? value)

View File

@@ -2,6 +2,7 @@
using FileTime.Avalonia.Application;
using FileTime.Avalonia.Models;
using FileTime.Avalonia.ViewModels;
using FileTime.Core.Models;
using MvvmGen;
using System;
using System.Collections.Generic;
@@ -17,9 +18,10 @@ namespace FileTime.Avalonia.Services
var nameParts = new List<ItemNamePart>();
var rapidTravelText = AppState.RapidTravelText.ToLower();
var name = itemViewModel.Item is IElement ? GetFileName(itemViewModel.Item.Name) : itemViewModel.Item.Name;
if (AppState.ViewMode == ViewMode.RapidTravel && rapidTravelText.Length > 0)
{
var nameLeft = itemViewModel.Item.Name;
var nameLeft = name;
while (nameLeft.ToLower().IndexOf(rapidTravelText, StringComparison.Ordinal) is int rapidTextStart && rapidTextStart != -1)
{
@@ -43,9 +45,22 @@ namespace FileTime.Avalonia.Services
}
else
{
nameParts.Add(new ItemNamePart(itemViewModel.Item.Name));
nameParts.Add(new ItemNamePart(name));
}
return nameParts;
}
public string GetFileName(string fullName)
{
var parts = fullName.Split('.');
var fileName = string.Join('.', parts[..^1]);
return fileName == "." ? fullName : fileName;
}
public string GetFileExtension(string fullName)
{
var parts = fullName.Split('.');
return parts.Length > 1 ? parts[^1] : "";
}
}
}

View File

@@ -72,7 +72,7 @@ namespace FileTime.Avalonia.ViewModels
{
get
{
if (!_isInitialized) Task.Run(Refresh);
if (!_isInitialized) Task.Run(Refresh).Wait();
return _containers;
}
set
@@ -90,7 +90,7 @@ namespace FileTime.Avalonia.ViewModels
{
get
{
if (!_isInitialized) Task.Run(Refresh);
if (!_isInitialized) Task.Run(Refresh).Wait();
return _elements;
}
set
@@ -107,7 +107,7 @@ namespace FileTime.Avalonia.ViewModels
{
get
{
if (!_isInitialized) Task.Run(Refresh);
if (!_isInitialized) Task.Run(Refresh).Wait();
return _items;
}
set
@@ -139,15 +139,15 @@ namespace FileTime.Avalonia.ViewModels
private async Task Container_Refreshed(object? sender, AsyncEventArgs e, CancellationToken token = default)
{
await Refresh(false, false, token);
await Refresh(false, false, token: token);
}
[Obsolete($"Use the parametrizable version of {nameof(Refresh)}.")]
private async Task Refresh()
{
await Refresh(true);
await Refresh(true, silent: true);
}
private async Task Refresh(bool initializeChildren, bool alloweReuse = true, CancellationToken token = default)
private async Task Refresh(bool initializeChildren, bool alloweReuse = true, bool silent = false, CancellationToken token = default)
{
if (_isRefreshing) return;
@@ -169,9 +169,9 @@ namespace FileTime.Avalonia.ViewModels
}
}
if(await _container.GetElements() is IReadOnlyList<IElement> elements)
if (await _container.GetElements() is IReadOnlyList<IElement> elements)
{
foreach(var element in elements)
foreach (var element in elements)
{
var generator = async (IElement e) =>
{
@@ -203,9 +203,18 @@ namespace FileTime.Avalonia.ViewModels
containerToRemove?.Dispose();
}
Containers = new ObservableCollection<ContainerViewModel>(newContainers);
Elements = new ObservableCollection<ElementViewModel>(newElements);
Items = new ObservableCollection<IItemViewModel>(newContainers.Cast<IItemViewModel>().Concat(newElements));
if (silent)
{
_containers = new ObservableCollection<ContainerViewModel>(newContainers);
_elements = new ObservableCollection<ElementViewModel>(newElements);
_items = new ObservableCollection<IItemViewModel>(newContainers.Cast<IItemViewModel>().Concat(newElements));
}
else
{
Containers = new ObservableCollection<ContainerViewModel>(newContainers);
Elements = new ObservableCollection<ElementViewModel>(newElements);
Items = new ObservableCollection<IItemViewModel>(newContainers.Cast<IItemViewModel>().Concat(newElements));
}
for (var i = 0; i < Items.Count; i++)
{
@@ -260,19 +269,35 @@ namespace FileTime.Avalonia.ViewModels
return await generator(item);
}
public void Unload(bool recursive = true)
public void Unload(bool recursive = true, bool unloadParent = true, bool unloadEvents = false)
{
_isInitialized = false;
if (recursive)
{
foreach (var container in _containers)
{
container.Unload(true);
container.Unload(true, false, true);
container.Dispose();
container.ChildrenToAdopt.Clear();
}
}
if (unloadParent)
{
var parent = Parent;
while (parent != null)
{
var lastParent = parent;
parent = parent.Parent;
lastParent.Unload();
}
}
if(unloadEvents)
{
Container.Refreshed.Remove(Container_Refreshed);
}
_containers.Clear();
_elements.Clear();
_items.Clear();

View File

@@ -34,7 +34,14 @@ namespace FileTime.Avalonia.ViewModels
}
else if (elementSize < MAXTEXTPREVIEWSIZE)
{
TextContent = await element.GetContent();
try
{
TextContent = await element.GetContent();
}
catch(Exception e)
{
TextContent = $"Error while getting content of {element.FullName}. " + e.ToString();
}
Mode = ElementPreviewMode.Text;
}
else

View File

@@ -202,7 +202,9 @@ namespace FileTime.Avalonia.ViewModels
}
places.Add(new PlaceInfo(name, container));
}
}
LocalContentProvider.Unload();
}
else
{
@@ -972,6 +974,22 @@ namespace FileTime.Avalonia.ViewModels
}
}
private Task ToggleAutoRefresh()
{
var tab = AppState.SelectedTab.TabState.Tab;
tab.AutoRefresh = !tab.AutoRefresh;
var text = "Auto refresh is: " + (tab.AutoRefresh ? "ON" : "OFF");
_popupTexts.Add(text);
Task.Run(async () =>
{
await Task.Delay(5000);
await Dispatcher.UIThread.InvokeAsync(() => _popupTexts.Remove(text));
});
return Task.CompletedTask;
}
[Command]
public async void ProcessInputs()
{
@@ -1438,6 +1456,11 @@ namespace FileTime.Avalonia.ViewModels
FileTime.App.Core.Command.Commands.Dummy,
new KeyWithModifiers[] { new KeyWithModifiers(Key.T), new KeyWithModifiers(Key.M) },
ChangeTimelineMode),
new CommandBinding(
"toggle auto refresh",
FileTime.App.Core.Command.Commands.Dummy,
new KeyWithModifiers[] { new KeyWithModifiers(Key.R, shift: true) },
ToggleAutoRefresh),
//TODO REMOVE
new CommandBinding(
"open in default file browser",

View File

@@ -21,6 +21,7 @@
Source="{Binding Converter={StaticResource ItemToImageConverter}}" />
<ItemsControl
Margin="5,0,0,0"
Grid.Column="1"
VerticalAlignment="Center"
Items="{Binding DisplayName}">
@@ -41,13 +42,14 @@
</ItemsControl>
<Grid Grid.Column="2" IsVisible="{Binding ShowAttributes,ElementName=ItemRoot}">
<Grid ColumnDefinitions="90,90,40,45"
<Grid ColumnDefinitions="30,50,90,40,45"
IsVisible="{Binding Converter={StaticResource ItemViewModelIsAttibuteTypeConverter},ConverterParameter={x:Static models:AttibuteType.LocalFile}}">
<TextBlock HorizontalAlignment="Right" Classes="SmallText" Text="{Binding Size, Converter={StaticResource FormatSizeConverter}, ConverterParameter=0}"/>
<TextBlock HorizontalAlignment="Right" Classes="SmallText" Grid.Column="1" Text="{Binding Item.CreatedAt, Converter={StaticResource DateTimeConverter}, ConverterParameter=yyyy-MM-dd}"/>
<TextBlock HorizontalAlignment="Right" Classes="SmallText" Grid.Column="2" Text="{Binding Item.CreatedAt, Converter={StaticResource DateTimeConverter}, ConverterParameter=hh:mm}"/>
<TextBlock HorizontalAlignment="Right" Classes="SmallText" Grid.Column="3" Text="{Binding Item.Attributes}"/>
<TextBlock HorizontalAlignment="Right" Classes="SmallText" Text="{Binding Item.Name, Converter={StaticResource GetFileExtensionConverter}}"/>
<TextBlock HorizontalAlignment="Right" Classes="SmallText" Grid.Column="1" Text="{Binding Size, Converter={StaticResource FormatSizeConverter}, ConverterParameter=0}"/>
<TextBlock HorizontalAlignment="Right" Classes="SmallText" Grid.Column="2" Text="{Binding Item.CreatedAt, Converter={StaticResource DateTimeConverter}, ConverterParameter=yyyy-MM-dd}"/>
<TextBlock HorizontalAlignment="Right" Classes="SmallText" Grid.Column="3" Text="{Binding Item.CreatedAt, Converter={StaticResource DateTimeConverter}, ConverterParameter=hh:mm}"/>
<TextBlock HorizontalAlignment="Right" Classes="SmallText" Grid.Column="4" Text="{Binding Item.Attributes}"/>
</Grid>
<Grid ColumnDefinitions="90,40,45"
IsVisible="{Binding Converter={StaticResource ItemViewModelIsAttibuteTypeConverter},ConverterParameter={x:Static models:AttibuteType.Container}}">