mirror of
https://github.com/chylex/Discord-History-Tracker.git
synced 2025-06-10 04:12:10 +03:00
Update file dialogs and hide overwrite prompt when opening database files
Closes #198
This commit is contained in:
parent
3a6b83e0ba
commit
610516de1f
@ -3,6 +3,8 @@ using System.Collections.Generic;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Platform.Storage;
|
||||||
|
using DHT.Desktop.Dialogs.File;
|
||||||
using DHT.Desktop.Dialogs.Message;
|
using DHT.Desktop.Dialogs.Message;
|
||||||
using DHT.Server.Database;
|
using DHT.Server.Database;
|
||||||
using DHT.Server.Database.Exceptions;
|
using DHT.Server.Database.Exceptions;
|
||||||
@ -15,27 +17,27 @@ namespace DHT.Desktop.Common {
|
|||||||
|
|
||||||
private const string DatabaseFileInitialName = "archive.dht";
|
private const string DatabaseFileInitialName = "archive.dht";
|
||||||
|
|
||||||
private static readonly List<FileDialogFilter> DatabaseFileDialogFilter = new() {
|
private static readonly IReadOnlyList<FilePickerFileType> DatabaseFileDialogFilter = new List<FilePickerFileType> {
|
||||||
new FileDialogFilter {
|
FileDialogs.CreateFilter("Discord History Tracker Database", new [] { "dht" })
|
||||||
Name = "Discord History Tracker Database",
|
|
||||||
Extensions = { "dht" }
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
public static OpenFileDialog NewOpenDatabaseFileDialog() {
|
public static async Task<string[]> NewOpenDatabaseFilesDialog(Window window, string? suggestedDirectory) {
|
||||||
return new OpenFileDialog {
|
return await window.StorageProvider.OpenFiles(new FilePickerOpenOptions {
|
||||||
Title = "Open Database File",
|
Title = "Open Database File",
|
||||||
InitialFileName = DatabaseFileInitialName,
|
FileTypeFilter = DatabaseFileDialogFilter,
|
||||||
Filters = DatabaseFileDialogFilter
|
SuggestedStartLocation = await FileDialogs.GetSuggestedStartLocation(window, suggestedDirectory),
|
||||||
};
|
AllowMultiple = true
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SaveFileDialog NewOpenOrCreateDatabaseFileDialog() {
|
public static async Task<string?> NewOpenOrCreateDatabaseFileDialog(Window window, string? suggestedDirectory) {
|
||||||
return new SaveFileDialog {
|
return await window.StorageProvider.SaveFile(new FilePickerSaveOptions {
|
||||||
Title = "Open or Create Database File",
|
Title = "Open or Create Database File",
|
||||||
InitialFileName = DatabaseFileInitialName,
|
FileTypeChoices = DatabaseFileDialogFilter,
|
||||||
Filters = DatabaseFileDialogFilter
|
SuggestedFileName = DatabaseFileInitialName,
|
||||||
};
|
SuggestedStartLocation = await FileDialogs.GetSuggestedStartLocation(window, suggestedDirectory),
|
||||||
|
ShowOverwritePrompt = false
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<IDatabaseFile?> TryOpenOrCreateDatabaseFromPath(string path, Window window, Func<Task<bool>> checkCanUpgradeDatabase) {
|
public static async Task<IDatabaseFile?> TryOpenOrCreateDatabaseFromPath(string path, Window window, Func<Task<bool>> checkCanUpgradeDatabase) {
|
||||||
|
36
app/Desktop/Dialogs/File/FileDialogs.cs
Normal file
36
app/Desktop/Dialogs/File/FileDialogs.cs
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Platform.Storage;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.Dialogs.File {
|
||||||
|
static class FileDialogs {
|
||||||
|
public static async Task<string[]> OpenFiles(this IStorageProvider storageProvider, FilePickerOpenOptions options) {
|
||||||
|
return (await storageProvider.OpenFilePickerAsync(options)).ToLocalPaths();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<string?> SaveFile(this IStorageProvider storageProvider, FilePickerSaveOptions options) {
|
||||||
|
return (await storageProvider.SaveFilePickerAsync(options))?.ToLocalPath();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static FilePickerFileType CreateFilter(string name, string[] extensions) {
|
||||||
|
return new FilePickerFileType(name) {
|
||||||
|
Patterns = extensions.Select(static ext => "*." + ext).ToArray()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Task<IStorageFolder?> GetSuggestedStartLocation(Window window, string? suggestedDirectory) {
|
||||||
|
return suggestedDirectory == null ? Task.FromResult<IStorageFolder?>(null) : window.StorageProvider.TryGetFolderFromPathAsync(suggestedDirectory);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string ToLocalPath(this IStorageFile file) {
|
||||||
|
return file.TryGetLocalPath() ?? throw new NotSupportedException("Local filesystem is not supported.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string[] ToLocalPaths(this IReadOnlyList<IStorageFile> files) {
|
||||||
|
return files.Select(ToLocalPath).ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -7,8 +7,10 @@ using System.Text;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Platform.Storage;
|
||||||
using Avalonia.Threading;
|
using Avalonia.Threading;
|
||||||
using DHT.Desktop.Common;
|
using DHT.Desktop.Common;
|
||||||
|
using DHT.Desktop.Dialogs.File;
|
||||||
using DHT.Desktop.Dialogs.Message;
|
using DHT.Desktop.Dialogs.Message;
|
||||||
using DHT.Desktop.Dialogs.Progress;
|
using DHT.Desktop.Dialogs.Progress;
|
||||||
using DHT.Desktop.Dialogs.TextBox;
|
using DHT.Desktop.Dialogs.TextBox;
|
||||||
@ -68,12 +70,8 @@ namespace DHT.Desktop.Main.Pages {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async void MergeWithDatabase() {
|
public async void MergeWithDatabase() {
|
||||||
var fileDialog = DatabaseGui.NewOpenDatabaseFileDialog();
|
var paths = await DatabaseGui.NewOpenDatabaseFilesDialog(window, Path.GetDirectoryName(Db.Path));
|
||||||
fileDialog.Directory = Path.GetDirectoryName(Db.Path);
|
if (paths.Length == 0) {
|
||||||
fileDialog.AllowMultiple = true;
|
|
||||||
|
|
||||||
string[]? paths = await fileDialog.ShowAsync(window);
|
|
||||||
if (paths == null || paths.Length == 0) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,14 +116,13 @@ namespace DHT.Desktop.Main.Pages {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async void ImportLegacyArchive() {
|
public async void ImportLegacyArchive() {
|
||||||
var fileDialog = new OpenFileDialog {
|
var paths = await window.StorageProvider.OpenFiles(new FilePickerOpenOptions {
|
||||||
Title = "Open Legacy DHT Archive",
|
Title = "Open Legacy DHT Archive",
|
||||||
Directory = Path.GetDirectoryName(Db.Path),
|
SuggestedStartLocation = await FileDialogs.GetSuggestedStartLocation(window, Path.GetDirectoryName(Db.Path)),
|
||||||
AllowMultiple = true
|
AllowMultiple = true
|
||||||
};
|
});
|
||||||
|
|
||||||
string[]? paths = await fileDialog.ShowAsync(window);
|
if (paths.Length == 0) {
|
||||||
if (paths == null || paths.Length == 0) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
@ -8,7 +7,9 @@ using System.Text;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Web;
|
using System.Web;
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Platform.Storage;
|
||||||
using DHT.Desktop.Common;
|
using DHT.Desktop.Common;
|
||||||
|
using DHT.Desktop.Dialogs.File;
|
||||||
using DHT.Desktop.Dialogs.Message;
|
using DHT.Desktop.Dialogs.Message;
|
||||||
using DHT.Desktop.Main.Controls;
|
using DHT.Desktop.Main.Controls;
|
||||||
using DHT.Desktop.Server;
|
using DHT.Desktop.Server;
|
||||||
@ -114,20 +115,14 @@ namespace DHT.Desktop.Main.Pages {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async void OnClickSaveViewer() {
|
public async void OnClickSaveViewer() {
|
||||||
var dialog = new SaveFileDialog {
|
string? path = await window.StorageProvider.SaveFile(new FilePickerSaveOptions {
|
||||||
Title = "Save Viewer",
|
Title = "Save Viewer",
|
||||||
InitialFileName = Path.GetFileNameWithoutExtension(db.Path) + ".html",
|
FileTypeChoices = new [] { FileDialogs.CreateFilter("Discord History Viewer", new string[] { "html" }) },
|
||||||
Directory = Path.GetDirectoryName(db.Path),
|
SuggestedFileName = Path.GetFileNameWithoutExtension(db.Path) + ".html",
|
||||||
Filters = new List<FileDialogFilter> {
|
SuggestedStartLocation = await FileDialogs.GetSuggestedStartLocation(window, Path.GetDirectoryName(db.Path)),
|
||||||
new() {
|
});
|
||||||
Name = "Discord History Viewer",
|
|
||||||
Extensions = { "html" }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.ShowAsync(window);
|
|
||||||
|
|
||||||
string? path = await dialog;
|
if (path != null) {
|
||||||
if (!string.IsNullOrEmpty(path)) {
|
|
||||||
await WriteViewerFile(path, StandaloneViewerExportStrategy.Instance);
|
await WriteViewerFile(path, StandaloneViewerExportStrategy.Instance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,11 +26,8 @@ namespace DHT.Desktop.Main.Screens {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async void OpenOrCreateDatabase() {
|
public async void OpenOrCreateDatabase() {
|
||||||
var dialog = DatabaseGui.NewOpenOrCreateDatabaseFileDialog();
|
var path = await DatabaseGui.NewOpenOrCreateDatabaseFileDialog(window, Path.GetDirectoryName(dbFilePath));
|
||||||
dialog.Directory = Path.GetDirectoryName(dbFilePath);
|
if (path != null) {
|
||||||
|
|
||||||
string? path = await dialog.ShowAsync(window);
|
|
||||||
if (!string.IsNullOrWhiteSpace(path)) {
|
|
||||||
await OpenOrCreateDatabaseFromPath(path);
|
await OpenOrCreateDatabaseFromPath(path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user