Files
MercuryConverter/src/UI/Dialogs/DataScanning.axaml.cs
T

151 lines
4.6 KiB
C#
Raw Normal View History

2025-08-20 22:57:06 -07:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using MercuryConverter.Data;
2025-08-27 12:44:57 -07:00
using MercuryConverter.Utility;
namespace MercuryConverter.UI.Dialogs;
public partial class DataScanning : UserControl
{
2025-08-20 22:57:06 -07:00
private bool requiresUser;
2025-08-19 17:03:56 -07:00
2025-08-20 22:57:06 -07:00
public DataScanning(bool requiresUser = true)
{
2025-08-20 22:57:06 -07:00
this.requiresUser = requiresUser;
InitializeComponent();
if (!Design.IsDesignMode)
RunFlow();
}
public void RunFlow()
{
Task.Run(async () =>
{
2025-08-20 22:57:06 -07:00
if (Settings.I!.DataPath == "" || requiresUser) // no data path saved
{
2025-08-27 12:44:57 -07:00
UISelectMode();
var selectedPath = await Utils.BeginDirSelection("Locate Data Folder", Settings.I!.DataPath);
2025-08-20 22:57:06 -07:00
if (selectedPath == "") // cancelled opening folder
{
// TODO:
// return and go to completed mode if scan already completed
// continue if no scan has been completed
// break if we already have a path but somehow not scanned
Dispatcher.UIThread.Post(() => ScanPath.Text = "");
UISetError("No data folder provided.");
return;
}
Settings.I.DataPath = selectedPath;
}
2025-08-20 22:57:06 -07:00
Dispatcher.UIThread.Post(() => ScanPath.Text = Settings.I.DataPath);
if (!Directory.Exists(Settings.I.DataPath))
{
UISetError("Folder does not exist.");
return;
}
2025-08-20 22:57:06 -07:00
if (!(File.Exists(Path.Combine(Settings.I.DataPath, "MusicParameterTable.uasset"))
&& File.Exists(Path.Combine(Settings.I.DataPath, "MusicParameterTable.uexp"))))
{
2025-08-19 01:03:06 -07:00
UISetError("Missing MusicParameterTable asset files.\nPlease ensure you've set up your data folder properly!");
return;
}
2025-08-20 22:57:06 -07:00
UIScanningMode(Settings.I.DataPath);
Database.SetupNew(Settings.I.DataPath);
2025-08-20 22:57:06 -07:00
if (requiresUser) // TODO: or if there are warnings
UIScanCompletedMode();
else
Dispatcher.UIThread.Post(() => MainWindow.Instance!.Dialog.IsOpen = false);
});
}
private void UISelectMode()
{
UISetError();
Dispatcher.UIThread.Post(() =>
{
ScanStatus.Text = "select your data folder...";
2025-08-20 22:57:06 -07:00
ScanPath.IsVisible = false;
2025-08-19 01:03:06 -07:00
ScanInfo.IsVisible = false;
ButtonGroup.IsVisible = false;
ProgressAnimation.IsVisible = true;
});
}
private void UIScanningMode(string path)
{
UISetError();
Dispatcher.UIThread.Post(() =>
{
ScanStatus.Text = "scanning...";
ScanPath.IsVisible = true;
ScanPath.Text = path;
2025-08-19 01:03:06 -07:00
ScanInfo.IsVisible = false;
ButtonGroup.IsVisible = false;
ProgressAnimation.IsVisible = true;
});
}
private void UIScanCompletedMode()
{
Dispatcher.UIThread.Post(() =>
{
ScanStatus.Text = "scan complete";
ScanPath.IsVisible = true;
ScanInfo.IsVisible = true;
2025-08-19 01:03:06 -07:00
ScanInfoCountText.Text = Database.Songs.Count.ToString();
ButtonGroup.IsVisible = true;
ProgressAnimation.IsVisible = false;
});
}
/// <summary>
2025-08-19 01:03:06 -07:00
/// Use only when a scan is no longer running.
/// </summary>
/// <param name="error"></param>
private void UISetError(string? error = null)
{
Dispatcher.UIThread.Post(() =>
{
if (error == null)
{
ScanError.IsVisible = false;
return;
}
requiresUser = true;
ScanError.IsVisible = true;
ErrorText.Text = error;
2025-08-20 22:57:06 -07:00
ScanPath.IsVisible = ScanPath.Text == "" ? false : true;
ScanInfo.IsVisible = false;
ProgressAnimation.IsVisible = false;
ButtonGroup.IsVisible = true;
ScanStatus.Text = "an error has occurred";
});
}
private void CloseHandler(object sender, RoutedEventArgs args)
{
Dispatcher.UIThread.Post(() =>
{
MainWindow.Instance!.Dialog.IsOpen = false;
});
}
2025-08-20 22:57:06 -07:00
private void OpenFolderHandler(object sender, RoutedEventArgs args)
{
2025-08-20 22:57:06 -07:00
requiresUser = true;
RunFlow();
}
}