Skip to content

Commit

Permalink
feat: preview markdown, images, and media resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
aiguoli committed Oct 17, 2023
1 parent 9be49b6 commit 5048578
Show file tree
Hide file tree
Showing 21 changed files with 345 additions and 17 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,6 @@ MigrationBackup/
.ionide/

# Fody - auto-generated XML schema
FodyWeavers.xsd
FodyWeavers.xsd

SimepleList/SimpleList - Backup.csproj
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Unzip and then double click
- [x] Index
- [x] Download
- [x] Share
- [ ] Preview
- [x] Preview
- [x] Download progress
- [x] Upload
- [ ] Automatic synchronization
Expand Down
2 changes: 1 addition & 1 deletion README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ OneDrive
- [x] 列表
- [x] 下载
- [x] 共享
- [ ] 预览
- [x] 预览
- [x] 下载进度
- [x] 上传
- [ ] 自动同步
Expand Down
13 changes: 13 additions & 0 deletions SimpleList/Models/FileType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace SimpleList.Models
{
public enum FileType
{
Text,
Markdown,
Code,
Image,
Media,
Office,
Unknown
}
}
19 changes: 18 additions & 1 deletion SimpleList/Services/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using SimpleList.Models;
using System;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage;

Expand Down Expand Up @@ -60,5 +62,20 @@ public static string ReadableFileSize(long? bytes)
}
return "0";
}

public static FileType GetFileType(string ext)
{
return ext switch
{
".txt" => FileType.Text,
".md" => FileType.Markdown,
string _ when ImageType.Contains(ext) => FileType.Image,// https://learn.microsoft.com/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.image?view=windows-app-sdk-1.4#image-file-formats
string _ when MediaType.Contains(ext) => FileType.Media,
_ => FileType.Unknown,
};
}

public static readonly string[] ImageType = { ".png", ".jpg", ".jpeg", ".bmp", ".gif", ".tiff", ".ico", ".svg" };
public static readonly string[] MediaType = { "mp3", ".mp4", ".wma", ".3gp", ".aac", ".flac", ".wax", ".wav", ".wmx", ".wpl", ".avi" };
}
}
15 changes: 14 additions & 1 deletion SimpleList/SimpleList.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ApplicationManifest>app.manifest</ApplicationManifest>
<Platforms>x86;x64;ARM64</Platforms>
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
<PublishProfile>Properties\PublishProfiles\win10-$(Platform).pubxml</PublishProfile>
<PublishProfile>win10-$(Platform).pubxml</PublishProfile>
<UseWinUI>true</UseWinUI>
<EnableMsixTooling>true</EnableMsixTooling>

Expand Down Expand Up @@ -58,6 +58,9 @@
<None Remove="Views\Layout\GridCloudView.xaml" />
<None Remove="Views\Layout\GridFileView.xaml" />
<None Remove="Views\Layout\ImageCloudView.xaml" />
<None Remove="Views\Preview\ImagePreviewView.xaml" />
<None Remove="Views\Preview\MarkdownPreviewView.xaml" />
<None Remove="Views\Preview\MediaPreviewView.xaml" />
<None Remove="Views\PropertyView.xaml" />
<None Remove="Views\RenameFileView.xaml" />
<None Remove="Views\ShareFileView.xaml" />
Expand All @@ -77,6 +80,7 @@
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" />
<PackageReference Include="CommunityToolkit.WinUI.Controls.SettingsControls" Version="8.0.230907" />
<PackageReference Include="CommunityToolkit.WinUI.UI" Version="7.1.2" />
<PackageReference Include="CommunityToolkit.WinUI.UI.Controls.Markdown" Version="7.1.2" />
<PackageReference Include="Downloader" Version="3.0.6" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
Expand Down Expand Up @@ -111,6 +115,15 @@
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<Page Update="Views\Preview\MediaPreviewView.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Update="Views\Preview\ImagePreviewView.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Update="Views\Preview\MarkdownPreviewView.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Update="Views\CreateDrive.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
Expand Down
21 changes: 21 additions & 0 deletions SimpleList/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,27 @@
<data name="CloudPage_Title.Text" xml:space="preserve">
<value>Your drive</value>
</data>
<data name="FileView_Flyout_Convert.Text" xml:space="preserve">
<value>Convert</value>
</data>
<data name="FileView_Flyout_Delete.Text" xml:space="preserve">
<value>Delete</value>
</data>
<data name="FileView_Flyout_Download.Text" xml:space="preserve">
<value>Download</value>
</data>
<data name="FileView_Flyout_Open.Text" xml:space="preserve">
<value>Open</value>
</data>
<data name="FileView_Flyout_Property.Text" xml:space="preserve">
<value>Property</value>
</data>
<data name="FileView_Flyout_Rename.Text" xml:space="preserve">
<value>Rename</value>
</data>
<data name="FileView_Flyout_Share.Text" xml:space="preserve">
<value>Share</value>
</data>
<data name="HomePage_Capacity" xml:space="preserve">
<value>Capacity:</value>
</data>
Expand Down
21 changes: 21 additions & 0 deletions SimpleList/Strings/zh-CN/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,27 @@
<data name="CloudPage_Title.Text" xml:space="preserve">
<value>你的网盘</value>
</data>
<data name="FileView_Flyout_Convert.Text" xml:space="preserve">
<value>转换</value>
</data>
<data name="FileView_Flyout_Delete.Text" xml:space="preserve">
<value>删除</value>
</data>
<data name="FileView_Flyout_Download.Text" xml:space="preserve">
<value>下载</value>
</data>
<data name="FileView_Flyout_Open.Text" xml:space="preserve">
<value>打开</value>
</data>
<data name="FileView_Flyout_Property.Text" xml:space="preserve">
<value>属性</value>
</data>
<data name="FileView_Flyout_Rename.Text" xml:space="preserve">
<value>重命名</value>
</data>
<data name="FileView_Flyout_Share.Text" xml:space="preserve">
<value>分享</value>
</data>
<data name="HomePage_Capacity" xml:space="preserve">
<value>容量: </value>
</data>
Expand Down
4 changes: 0 additions & 4 deletions SimpleList/ViewModels/CloudViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Graph.Models;
using SimpleList.Services;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;

namespace SimpleList.ViewModels
{
Expand Down
1 change: 1 addition & 0 deletions SimpleList/ViewModels/DriveViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public DriveViewModel(OneDrive provider, string displayName = null)
BreadcrumbItems.Add(new BreadcrumbItem { Name = "RootFileName".GetLocalized(), ItemId = "Root" });
}

[RelayCommand]
public async Task GetFiles(string itemId = "Root")
{
IsLoading = Visibility.Visible;
Expand Down
14 changes: 14 additions & 0 deletions SimpleList/ViewModels/FileViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Microsoft.Graph.Models;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media.Imaging;
using SimpleList.Models;
using SimpleList.Services;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -58,7 +60,17 @@ public async Task LoadImage()
}
}

[RelayCommand]
public async Task LoadContent()
{
if (IsFile)
{
Content = (await Drive.Provider.GetItemContent(Id)).ToString();
}
}

[ObservableProperty] private BitmapImage _image;
[ObservableProperty] private string _content;
private readonly DriveItem _file;

public string Id { get => _file.Id; }
Expand All @@ -70,5 +82,7 @@ public async Task LoadImage()
public int? ChildrenCount { get => _file.Folder?.ChildCount; }
public DriveViewModel Drive { get; }
public string ItemType { get; }
public string DownloadUrl { get => _file.AdditionalData["@microsoft.graph.downloadUrl"].ToString(); }
public bool CanPreview { get => IsFile && Utils.GetFileType(Path.GetExtension(Name)) != FileType.Unknown; }
}
}
59 changes: 59 additions & 0 deletions SimpleList/ViewModels/PreviewViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Graph.Models;
using Microsoft.UI.Xaml.Media.Imaging;
using System;
using System.IO;
using System.Threading.Tasks;
using Windows.Media.Core;
using Windows.Storage.Streams;

namespace SimpleList.ViewModels
{
public partial class PreviewViewModel : ObservableObject
{
public PreviewViewModel(FileViewModel file)
{
_file = file;
}

[RelayCommand]
public async Task LoadTextContent()
{
IsLoading = true;
Stream stram = await _file.Drive.Provider.GetItemContent(_file.Id);
using StreamReader reader = new(stram);
Text = await reader.ReadToEndAsync();
IsLoading = false;
}

[RelayCommand]
public async Task LoadImageContent()
{
IsLoading = true;
Stream stream = await _file.Drive.Provider.GetItemContent(_file.Id);
InMemoryRandomAccessStream randomAccessStream = new();
await RandomAccessStream.CopyAsync(stream.AsInputStream(), randomAccessStream);
randomAccessStream.Seek(0);
BitmapImage img = new();
await img.SetSourceAsync(randomAccessStream);
Image = img;
IsLoading = false;
}

public void LoadMediaSource()
{
IsLoading = true;
string downloadUri = _file.DownloadUrl;
MediaSource mediaSource = MediaSource.CreateFromUri(new Uri(downloadUri));
Media = mediaSource;
IsLoading = false;
}

private readonly FileViewModel _file;
[ObservableProperty] private bool _isLoading = false;
[ObservableProperty] private string _text;
[ObservableProperty] private BitmapImage _image;
[ObservableProperty] private MediaSource _media;
}
}
2 changes: 1 addition & 1 deletion SimpleList/ViewModels/ShareFileViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private async Task<string> ShareFile()
}

[RelayCommand]
private async Task CopyToClipboard()
private void CopyToClipboard()
{
DataPackage package = new();
package.SetText(ShareLink);
Expand Down
24 changes: 17 additions & 7 deletions SimpleList/Views/Layout/ColumnFileView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,23 @@
<Grid DoubleTapped="OpenFolder">
<Grid.ContextFlyout>
<MenuFlyout>
<MenuFlyoutItem Text="Open" Command="{Binding Cloud.OpenFolderCommand}" CommandParameter="{Binding}" IsEnabled="{Binding IsFolder}" />
<MenuFlyoutItem Text="Download" Command="{Binding DownloadFileCommand}" CommandParameter="{Binding Id}" IsEnabled="{Binding IsFile}" />
<MenuFlyoutItem Text="Delete" Click="ShowDeleteFileDialogAsync" />
<MenuFlyoutItem Text="Convert" Click="ShowConverFiletDialogAsync" Visibility="{Binding Name, Converter={StaticResource FileNameToCanConvertCommandVisible}}" />
<MenuFlyoutItem Text="Share" Click="ShowShareFileDialogAsync" />
<MenuFlyoutItem Text="Rename" Click="ShowRenameFileDialogAsync" />
<MenuFlyoutItem Text="Property" Click="ShowPropertyDialogAsync" />
<MenuFlyoutItem
x:Uid="FileView_Flyout_Open"
Command="{Binding Drive.OpenFolderCommand}"
CommandParameter="{Binding}"
IsEnabled="{Binding IsFolder}"
Visibility="{Binding IsFolder, Converter={StaticResource BoolToVisibilityConverter}}" />
<MenuFlyoutItem
x:Uid="FileView_Flyout_Open"
Click="ShowPreviewDialogAsync"
IsEnabled="{Binding CanPreview}"
Visibility="{Binding CanPreview, Converter={StaticResource BoolToVisibilityConverter}}"/>
<MenuFlyoutItem x:Uid="FileView_Flyout_Download" Command="{Binding DownloadFileCommand}" CommandParameter="{Binding Id}" IsEnabled="{Binding IsFile}" />
<MenuFlyoutItem x:Uid="FileView_Flyout_Delete" Click="ShowDeleteFileDialogAsync" />
<MenuFlyoutItem x:Uid="FileView_Flyout_Convert" Click="ShowConverFiletDialogAsync" Visibility="{Binding Name, Converter={StaticResource FileNameToCanConvertCommandVisible}}" />
<MenuFlyoutItem x:Uid="FileView_Flyout_Share" Click="ShowShareFileDialogAsync" />
<MenuFlyoutItem x:Uid="FileView_Flyout_Rename" Click="ShowRenameFileDialogAsync" />
<MenuFlyoutItem x:Uid="FileView_Flyout_Property" Click="ShowPropertyDialogAsync" />
</MenuFlyout>
</Grid.ContextFlyout>
<Grid.ColumnDefinitions>
Expand Down
43 changes: 43 additions & 0 deletions SimpleList/Views/Layout/ColumnFileView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using SimpleList.Models;
using SimpleList.Services;
using SimpleList.ViewModels;
using SimpleList.Views.Preview;
using System;
using System.IO;

namespace SimpleList.Views.Layout
{
Expand Down Expand Up @@ -78,6 +82,45 @@ private async void ShowDeleteFileDialogAsync(object sender, RoutedEventArgs e)
};
await dialog.ShowAsync();
}

private async void ShowPreviewDialogAsync(object sender, RoutedEventArgs e)
{
FileViewModel viewModel = DataContext as FileViewModel;
switch (Utils.GetFileType(Path.GetExtension(viewModel.Name).ToLower()))
{
case FileType.Markdown:
{
MarkdownPreviewView dialog = new()
{
XamlRoot = XamlRoot,
DataContext = new PreviewViewModel(viewModel)
};
await dialog.ShowAsync();
break;
}
case FileType.Image:
{
ImagePreviewView dialog = new()
{
XamlRoot = XamlRoot,
DataContext = new PreviewViewModel(viewModel)
};
await dialog.ShowAsync();
break;
}
case FileType.Media:
{
MediaPreviewView dialog = new()
{
XamlRoot = XamlRoot,
DataContext = new PreviewViewModel(viewModel)
};
await dialog.ShowAsync();
break;
}
}

}
}

}
Loading

0 comments on commit 5048578

Please sign in to comment.