mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-02-23 01:34:42 +00:00
Migrate to Stylet and refactor view/view-model framework
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using DiscordChatExporter.Core.Models;
|
||||
using DiscordChatExporter.Core.Services;
|
||||
using DiscordChatExporter.Gui.ViewModels.Framework;
|
||||
using Tyrrrz.Extensions;
|
||||
|
||||
namespace DiscordChatExporter.Gui.ViewModels.Dialogs
|
||||
{
|
||||
public class ExportSetupViewModel : DialogScreen
|
||||
{
|
||||
private readonly DialogManager _dialogManager;
|
||||
private readonly SettingsService _settingsService;
|
||||
|
||||
public Guild Guild { get; set; }
|
||||
|
||||
public Channel Channel { get; set; }
|
||||
|
||||
public string FilePath { get; set; }
|
||||
|
||||
public IReadOnlyList<ExportFormat> AvailableFormats =>
|
||||
Enum.GetValues(typeof(ExportFormat)).Cast<ExportFormat>().ToArray();
|
||||
|
||||
public ExportFormat SelectedFormat { get; set; } = ExportFormat.HtmlDark;
|
||||
|
||||
public DateTime? From { get; set; }
|
||||
|
||||
public DateTime? To { get; set; }
|
||||
|
||||
public int? PartitionLimit { get; set; }
|
||||
|
||||
public ExportSetupViewModel(DialogManager dialogManager, SettingsService settingsService)
|
||||
{
|
||||
_dialogManager = dialogManager;
|
||||
_settingsService = settingsService;
|
||||
}
|
||||
|
||||
protected override void OnViewLoaded()
|
||||
{
|
||||
base.OnViewLoaded();
|
||||
|
||||
// Persist preferences
|
||||
SelectedFormat = _settingsService.LastExportFormat;
|
||||
PartitionLimit = _settingsService.LastPartitionLimit;
|
||||
}
|
||||
|
||||
public void Confirm()
|
||||
{
|
||||
// Persist preferences
|
||||
_settingsService.LastExportFormat = SelectedFormat;
|
||||
_settingsService.LastPartitionLimit = PartitionLimit;
|
||||
|
||||
// Clamp 'from' and 'to' values
|
||||
if (From > To)
|
||||
From = To;
|
||||
if (To < From)
|
||||
To = From;
|
||||
|
||||
// Generate default file name
|
||||
var ext = SelectedFormat.GetFileExtension();
|
||||
var defaultFileName = $"{Guild.Name} - {Channel.Name}.{ext}".Replace(Path.GetInvalidFileNameChars(), '_');
|
||||
|
||||
// Prompt for output file path
|
||||
var filter = $"{ext.ToUpperInvariant()} files|*.{ext}";
|
||||
FilePath = _dialogManager.PromptSaveFilePath(filter, defaultFileName);
|
||||
|
||||
// If canceled - return
|
||||
if (FilePath.IsBlank())
|
||||
return;
|
||||
|
||||
// Close dialog
|
||||
Close(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
using DiscordChatExporter.Core.Services;
|
||||
using GalaSoft.MvvmLight;
|
||||
using DiscordChatExporter.Gui.ViewModels.Framework;
|
||||
using Tyrrrz.Extensions;
|
||||
|
||||
namespace DiscordChatExporter.Gui.ViewModels
|
||||
namespace DiscordChatExporter.Gui.ViewModels.Dialogs
|
||||
{
|
||||
public class SettingsViewModel : ViewModelBase, ISettingsViewModel
|
||||
public class SettingsViewModel : DialogScreen
|
||||
{
|
||||
private readonly ISettingsService _settingsService;
|
||||
private readonly SettingsService _settingsService;
|
||||
|
||||
public bool IsAutoUpdateEnabled
|
||||
{
|
||||
@@ -26,7 +26,7 @@ namespace DiscordChatExporter.Gui.ViewModels
|
||||
set => _settingsService.MessageGroupLimit = value.ClampMin(0);
|
||||
}
|
||||
|
||||
public SettingsViewModel(ISettingsService settingsService)
|
||||
public SettingsViewModel(SettingsService settingsService)
|
||||
{
|
||||
_settingsService = settingsService;
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using DiscordChatExporter.Core.Models;
|
||||
using DiscordChatExporter.Core.Services;
|
||||
using DiscordChatExporter.Gui.Messages;
|
||||
using GalaSoft.MvvmLight;
|
||||
using GalaSoft.MvvmLight.CommandWpf;
|
||||
using Tyrrrz.Extensions;
|
||||
|
||||
namespace DiscordChatExporter.Gui.ViewModels
|
||||
{
|
||||
public class ExportSetupViewModel : ViewModelBase, IExportSetupViewModel
|
||||
{
|
||||
private readonly ISettingsService _settingsService;
|
||||
|
||||
private string _filePath;
|
||||
private ExportFormat _format;
|
||||
private DateTime? _from;
|
||||
private DateTime? _to;
|
||||
private int? _partitionLimit;
|
||||
|
||||
public Guild Guild { get; private set; }
|
||||
|
||||
public Channel Channel { get; private set; }
|
||||
|
||||
public string FilePath
|
||||
{
|
||||
get => _filePath;
|
||||
set
|
||||
{
|
||||
Set(ref _filePath, value);
|
||||
ExportCommand.RaiseCanExecuteChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public IReadOnlyList<ExportFormat> AvailableFormats =>
|
||||
Enum.GetValues(typeof(ExportFormat)).Cast<ExportFormat>().ToArray();
|
||||
|
||||
public ExportFormat SelectedFormat
|
||||
{
|
||||
get => _format;
|
||||
set
|
||||
{
|
||||
Set(ref _format, value);
|
||||
|
||||
// Replace extension in path
|
||||
var ext = value.GetFileExtension();
|
||||
if (FilePath != null)
|
||||
FilePath = Path.ChangeExtension(FilePath, ext);
|
||||
}
|
||||
}
|
||||
|
||||
public DateTime? From
|
||||
{
|
||||
get => _from;
|
||||
set => Set(ref _from, value);
|
||||
}
|
||||
|
||||
public DateTime? To
|
||||
{
|
||||
get => _to;
|
||||
set => Set(ref _to, value);
|
||||
}
|
||||
|
||||
public int? PartitionLimit
|
||||
{
|
||||
get => _partitionLimit;
|
||||
set => Set(ref _partitionLimit, value);
|
||||
}
|
||||
|
||||
// Commands
|
||||
public RelayCommand ExportCommand { get; }
|
||||
|
||||
public ExportSetupViewModel(ISettingsService settingsService)
|
||||
{
|
||||
_settingsService = settingsService;
|
||||
|
||||
// Commands
|
||||
ExportCommand = new RelayCommand(Export, () => FilePath.IsNotBlank());
|
||||
|
||||
// Messages
|
||||
MessengerInstance.Register<ShowExportSetupMessage>(this, m =>
|
||||
{
|
||||
Guild = m.Guild;
|
||||
Channel = m.Channel;
|
||||
SelectedFormat = _settingsService.LastExportFormat;
|
||||
FilePath = $"{Guild.Name} - {Channel.Name}.{SelectedFormat.GetFileExtension()}"
|
||||
.Replace(Path.GetInvalidFileNameChars(), '_');
|
||||
From = null;
|
||||
To = null;
|
||||
PartitionLimit = _settingsService.LastPartitionLimit;
|
||||
});
|
||||
}
|
||||
|
||||
private void Export()
|
||||
{
|
||||
// Persist preferences
|
||||
_settingsService.LastExportFormat = SelectedFormat;
|
||||
_settingsService.LastPartitionLimit = PartitionLimit;
|
||||
|
||||
// Clamp 'from' and 'to' values
|
||||
if (From > To)
|
||||
From = To;
|
||||
if (To < From)
|
||||
To = From;
|
||||
|
||||
// Start export
|
||||
MessengerInstance.Send(new StartExportMessage(Channel, FilePath, SelectedFormat, From, To, PartitionLimit));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using MaterialDesignThemes.Wpf;
|
||||
using Microsoft.Win32;
|
||||
using Stylet;
|
||||
|
||||
namespace DiscordChatExporter.Gui.ViewModels.Framework
|
||||
{
|
||||
public class DialogManager
|
||||
{
|
||||
private readonly IViewManager _viewManager;
|
||||
|
||||
public DialogManager(IViewManager viewManager)
|
||||
{
|
||||
_viewManager = viewManager;
|
||||
}
|
||||
|
||||
public async Task<T> ShowDialogAsync<T>(DialogScreen<T> dialogScreen)
|
||||
{
|
||||
// Get the view that renders this viewmodel
|
||||
var view = _viewManager.CreateAndBindViewForModelIfNecessary(dialogScreen);
|
||||
|
||||
// Set up event routing that will close the view when called from viewmodel
|
||||
DialogOpenedEventHandler onDialogOpened = (sender, e) =>
|
||||
{
|
||||
// Delegate to close the dialog and unregister event handler
|
||||
void OnScreenClosed(object o, CloseEventArgs args)
|
||||
{
|
||||
e.Session.Close();
|
||||
dialogScreen.Closed -= OnScreenClosed;
|
||||
}
|
||||
|
||||
dialogScreen.Closed += OnScreenClosed;
|
||||
};
|
||||
|
||||
// Show view
|
||||
await DialogHost.Show(view, onDialogOpened);
|
||||
|
||||
// Return the result
|
||||
return dialogScreen.DialogResult;
|
||||
}
|
||||
|
||||
public string PromptSaveFilePath(string filter = "All files|*.*", string initialFilePath = "")
|
||||
{
|
||||
// Create dialog
|
||||
var dialog = new SaveFileDialog
|
||||
{
|
||||
Filter = filter,
|
||||
AddExtension = true,
|
||||
FileName = initialFilePath,
|
||||
DefaultExt = Path.GetExtension(initialFilePath) ?? ""
|
||||
};
|
||||
|
||||
// Show dialog and return result
|
||||
return dialog.ShowDialog() == true ? dialog.FileName : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
26
DiscordChatExporter.Gui/ViewModels/Framework/DialogScreen.cs
Normal file
26
DiscordChatExporter.Gui/ViewModels/Framework/DialogScreen.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using Stylet;
|
||||
|
||||
namespace DiscordChatExporter.Gui.ViewModels.Framework
|
||||
{
|
||||
public abstract class DialogScreen<T> : Screen
|
||||
{
|
||||
public T DialogResult { get; private set; }
|
||||
|
||||
public void Close(T dialogResult = default(T))
|
||||
{
|
||||
// Set the result
|
||||
DialogResult = dialogResult;
|
||||
|
||||
// If there is a parent - ask them to close this dialog
|
||||
if (Parent != null)
|
||||
RequestClose(Equals(dialogResult, default(T)));
|
||||
// Otherwise close ourselves
|
||||
else
|
||||
((IScreenState) this).Close();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class DialogScreen : DialogScreen<bool?>
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using DiscordChatExporter.Gui.ViewModels.Dialogs;
|
||||
|
||||
namespace DiscordChatExporter.Gui.ViewModels.Framework
|
||||
{
|
||||
// Used to instantiate new view models while making use of dependency injection
|
||||
public interface IViewModelFactory
|
||||
{
|
||||
ExportSetupViewModel CreateExportSetupViewModel();
|
||||
|
||||
SettingsViewModel CreateSettingsViewModel();
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using DiscordChatExporter.Core.Models;
|
||||
using GalaSoft.MvvmLight.CommandWpf;
|
||||
|
||||
namespace DiscordChatExporter.Gui.ViewModels
|
||||
{
|
||||
public interface IExportSetupViewModel
|
||||
{
|
||||
Guild Guild { get; }
|
||||
Channel Channel { get; }
|
||||
string FilePath { get; set; }
|
||||
IReadOnlyList<ExportFormat> AvailableFormats { get; }
|
||||
ExportFormat SelectedFormat { get; set; }
|
||||
DateTime? From { get; set; }
|
||||
DateTime? To { get; set; }
|
||||
int? PartitionLimit { get; set; }
|
||||
|
||||
RelayCommand ExportCommand { get; }
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using DiscordChatExporter.Core.Models;
|
||||
using GalaSoft.MvvmLight.CommandWpf;
|
||||
|
||||
namespace DiscordChatExporter.Gui.ViewModels
|
||||
{
|
||||
public interface IMainViewModel
|
||||
{
|
||||
bool IsBusy { get; }
|
||||
bool IsDataAvailable { get; }
|
||||
|
||||
bool IsProgressIndeterminate { get; }
|
||||
double Progress { get; }
|
||||
|
||||
bool IsBotToken { get; set; }
|
||||
string TokenValue { get; set; }
|
||||
|
||||
IReadOnlyList<Guild> AvailableGuilds { get; }
|
||||
Guild SelectedGuild { get; set; }
|
||||
IReadOnlyList<Channel> AvailableChannels { get; }
|
||||
|
||||
RelayCommand ViewLoadedCommand { get; }
|
||||
RelayCommand ViewClosedCommand { get; }
|
||||
RelayCommand PullDataCommand { get; }
|
||||
RelayCommand ShowSettingsCommand { get; }
|
||||
RelayCommand ShowAboutCommand { get; }
|
||||
RelayCommand<Channel> ShowExportSetupCommand { get; }
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace DiscordChatExporter.Gui.ViewModels
|
||||
{
|
||||
public interface ISettingsViewModel
|
||||
{
|
||||
bool IsAutoUpdateEnabled { get; set; }
|
||||
|
||||
string DateFormat { get; set; }
|
||||
int MessageGroupLimit { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,280 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Windows;
|
||||
using DiscordChatExporter.Core.Exceptions;
|
||||
using DiscordChatExporter.Core.Models;
|
||||
using DiscordChatExporter.Core.Services;
|
||||
using DiscordChatExporter.Gui.Messages;
|
||||
using GalaSoft.MvvmLight;
|
||||
using GalaSoft.MvvmLight.CommandWpf;
|
||||
using Tyrrrz.Extensions;
|
||||
|
||||
namespace DiscordChatExporter.Gui.ViewModels
|
||||
{
|
||||
public class MainViewModel : ViewModelBase, IMainViewModel
|
||||
{
|
||||
private readonly ISettingsService _settingsService;
|
||||
private readonly IUpdateService _updateService;
|
||||
private readonly IDataService _dataService;
|
||||
private readonly IExportService _exportService;
|
||||
|
||||
private readonly Dictionary<Guild, IReadOnlyList<Channel>> _guildChannelsMap;
|
||||
|
||||
private bool _isBusy;
|
||||
private double _progress;
|
||||
private bool _isBotToken;
|
||||
private string _tokenValue;
|
||||
private IReadOnlyList<Guild> _availableGuilds;
|
||||
private Guild _selectedGuild;
|
||||
private IReadOnlyList<Channel> _availableChannels;
|
||||
|
||||
public bool IsBusy
|
||||
{
|
||||
get => _isBusy;
|
||||
private set
|
||||
{
|
||||
Set(ref _isBusy, value);
|
||||
PullDataCommand.RaiseCanExecuteChanged();
|
||||
ShowExportSetupCommand.RaiseCanExecuteChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsDataAvailable => AvailableGuilds.NotNullAndAny();
|
||||
|
||||
public bool IsProgressIndeterminate => Progress <= 0;
|
||||
|
||||
public double Progress
|
||||
{
|
||||
get => _progress;
|
||||
private set
|
||||
{
|
||||
Set(ref _progress, value);
|
||||
RaisePropertyChanged(() => IsProgressIndeterminate);
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsBotToken
|
||||
{
|
||||
get => _isBotToken;
|
||||
set => Set(ref _isBotToken, value);
|
||||
}
|
||||
|
||||
public string TokenValue
|
||||
{
|
||||
get => _tokenValue;
|
||||
set
|
||||
{
|
||||
// Remove invalid chars
|
||||
value = value?.Trim('"');
|
||||
|
||||
Set(ref _tokenValue, value);
|
||||
PullDataCommand.RaiseCanExecuteChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public IReadOnlyList<Guild> AvailableGuilds
|
||||
{
|
||||
get => _availableGuilds;
|
||||
private set
|
||||
{
|
||||
Set(ref _availableGuilds, value);
|
||||
RaisePropertyChanged(() => IsDataAvailable);
|
||||
}
|
||||
}
|
||||
|
||||
public Guild SelectedGuild
|
||||
{
|
||||
get => _selectedGuild;
|
||||
set
|
||||
{
|
||||
Set(ref _selectedGuild, value);
|
||||
AvailableChannels = value != null ? _guildChannelsMap[value] : Array.Empty<Channel>();
|
||||
ShowExportSetupCommand.RaiseCanExecuteChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public IReadOnlyList<Channel> AvailableChannels
|
||||
{
|
||||
get => _availableChannels;
|
||||
private set => Set(ref _availableChannels, value);
|
||||
}
|
||||
|
||||
public RelayCommand ViewLoadedCommand { get; }
|
||||
public RelayCommand ViewClosedCommand { get; }
|
||||
public RelayCommand PullDataCommand { get; }
|
||||
public RelayCommand ShowSettingsCommand { get; }
|
||||
public RelayCommand ShowAboutCommand { get; }
|
||||
public RelayCommand<Channel> ShowExportSetupCommand { get; }
|
||||
|
||||
public MainViewModel(ISettingsService settingsService, IUpdateService updateService, IDataService dataService,
|
||||
IExportService exportService)
|
||||
{
|
||||
_settingsService = settingsService;
|
||||
_updateService = updateService;
|
||||
_dataService = dataService;
|
||||
_exportService = exportService;
|
||||
|
||||
_guildChannelsMap = new Dictionary<Guild, IReadOnlyList<Channel>>();
|
||||
|
||||
// Commands
|
||||
ViewLoadedCommand = new RelayCommand(ViewLoaded);
|
||||
ViewClosedCommand = new RelayCommand(ViewClosed);
|
||||
PullDataCommand = new RelayCommand(PullData, () => TokenValue.IsNotBlank() && !IsBusy);
|
||||
ShowSettingsCommand = new RelayCommand(ShowSettings);
|
||||
ShowAboutCommand = new RelayCommand(ShowAbout);
|
||||
ShowExportSetupCommand = new RelayCommand<Channel>(ShowExportSetup, _ => !IsBusy);
|
||||
|
||||
// Messages
|
||||
MessengerInstance.Register<StartExportMessage>(this,
|
||||
m => Export(m.Channel, m.FilePath, m.Format, m.From, m.To, m.PartitionLimit));
|
||||
}
|
||||
|
||||
private async void ViewLoaded()
|
||||
{
|
||||
// Load settings
|
||||
_settingsService.Load();
|
||||
|
||||
// Get last token
|
||||
if (_settingsService.LastToken != null)
|
||||
{
|
||||
IsBotToken = _settingsService.LastToken.Type == AuthTokenType.Bot;
|
||||
TokenValue = _settingsService.LastToken.Value;
|
||||
}
|
||||
|
||||
// Check and prepare update
|
||||
try
|
||||
{
|
||||
var updateVersion = await _updateService.CheckPrepareUpdateAsync();
|
||||
if (updateVersion != null)
|
||||
{
|
||||
MessengerInstance.Send(new ShowNotificationMessage(
|
||||
$"Update to DiscordChatExporter v{updateVersion} will be installed when you exit",
|
||||
"INSTALL NOW", () =>
|
||||
{
|
||||
_updateService.NeedRestart = true;
|
||||
Application.Current.Shutdown();
|
||||
}));
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
MessengerInstance.Send(new ShowNotificationMessage("Failed to perform application auto-update"));
|
||||
}
|
||||
}
|
||||
|
||||
private void ViewClosed()
|
||||
{
|
||||
// Save settings
|
||||
_settingsService.Save();
|
||||
|
||||
// Finalize updates if available
|
||||
_updateService.FinalizeUpdate();
|
||||
}
|
||||
|
||||
private async void PullData()
|
||||
{
|
||||
IsBusy = true;
|
||||
|
||||
// Create token
|
||||
var token = new AuthToken(
|
||||
IsBotToken ? AuthTokenType.Bot : AuthTokenType.User,
|
||||
TokenValue);
|
||||
|
||||
// Save token
|
||||
_settingsService.LastToken = token;
|
||||
|
||||
// Clear existing
|
||||
_guildChannelsMap.Clear();
|
||||
|
||||
try
|
||||
{
|
||||
// Get DM channels
|
||||
{
|
||||
var channels = await _dataService.GetDirectMessageChannelsAsync(token);
|
||||
var guild = Guild.DirectMessages;
|
||||
_guildChannelsMap[guild] = channels.OrderBy(c => c.Name).ToArray();
|
||||
}
|
||||
|
||||
// Get guild channels
|
||||
{
|
||||
var guilds = await _dataService.GetUserGuildsAsync(token);
|
||||
foreach (var guild in guilds)
|
||||
{
|
||||
var channels = await _dataService.GetGuildChannelsAsync(token, guild.Id);
|
||||
_guildChannelsMap[guild] = channels.Where(c => c.Type == ChannelType.GuildTextChat)
|
||||
.OrderBy(c => c.Name)
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
MessengerInstance.Send(new ShowNotificationMessage("Unauthorized – make sure the token is valid"));
|
||||
}
|
||||
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden)
|
||||
{
|
||||
MessengerInstance.Send(new ShowNotificationMessage("Forbidden – account may be locked by 2FA"));
|
||||
}
|
||||
|
||||
AvailableGuilds = _guildChannelsMap.Keys.ToArray();
|
||||
SelectedGuild = AvailableGuilds.FirstOrDefault();
|
||||
IsBusy = false;
|
||||
}
|
||||
|
||||
private void ShowSettings()
|
||||
{
|
||||
MessengerInstance.Send(new ShowSettingsMessage());
|
||||
}
|
||||
|
||||
private void ShowAbout()
|
||||
{
|
||||
Process.Start("https://github.com/Tyrrrz/DiscordChatExporter");
|
||||
}
|
||||
|
||||
private void ShowExportSetup(Channel channel)
|
||||
{
|
||||
MessengerInstance.Send(new ShowExportSetupMessage(SelectedGuild, channel));
|
||||
}
|
||||
|
||||
private async void Export(Channel channel, string filePath, ExportFormat format,
|
||||
DateTime? from, DateTime? to, int? partitionLimit)
|
||||
{
|
||||
IsBusy = true;
|
||||
|
||||
// Get last used token
|
||||
var token = _settingsService.LastToken;
|
||||
|
||||
// Get guild
|
||||
var guild = SelectedGuild;
|
||||
|
||||
// Create progress handler
|
||||
var progressHandler = new Progress<double>(p => Progress = p);
|
||||
|
||||
try
|
||||
{
|
||||
// Get chat log
|
||||
var chatLog = await _dataService.GetChatLogAsync(token, guild, channel, from, to, progressHandler);
|
||||
|
||||
// Export
|
||||
_exportService.ExportChatLog(chatLog, filePath, format, partitionLimit);
|
||||
|
||||
// Notify completion
|
||||
MessengerInstance.Send(new ShowNotificationMessage("Export complete"));
|
||||
}
|
||||
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden)
|
||||
{
|
||||
MessengerInstance.Send(new ShowNotificationMessage("You don't have access to this channel"));
|
||||
}
|
||||
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
|
||||
{
|
||||
MessengerInstance.Send(new ShowNotificationMessage("This channel doesn't exist"));
|
||||
}
|
||||
|
||||
Progress = 0;
|
||||
IsBusy = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
227
DiscordChatExporter.Gui/ViewModels/RootViewModel.cs
Normal file
227
DiscordChatExporter.Gui/ViewModels/RootViewModel.cs
Normal file
@@ -0,0 +1,227 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
using DiscordChatExporter.Core.Exceptions;
|
||||
using DiscordChatExporter.Core.Models;
|
||||
using DiscordChatExporter.Core.Services;
|
||||
using DiscordChatExporter.Gui.ViewModels.Framework;
|
||||
using MaterialDesignThemes.Wpf;
|
||||
using Stylet;
|
||||
using Tyrrrz.Extensions;
|
||||
|
||||
namespace DiscordChatExporter.Gui.ViewModels
|
||||
{
|
||||
public class RootViewModel : Screen
|
||||
{
|
||||
private readonly IViewModelFactory _viewModelFactory;
|
||||
private readonly DialogManager _dialogManager;
|
||||
private readonly SettingsService _settingsService;
|
||||
private readonly UpdateService _updateService;
|
||||
private readonly DataService _dataService;
|
||||
private readonly ExportService _exportService;
|
||||
|
||||
private readonly Dictionary<Guild, IReadOnlyList<Channel>> _guildChannelsMap =
|
||||
new Dictionary<Guild, IReadOnlyList<Channel>>();
|
||||
|
||||
public SnackbarMessageQueue Notifications { get; } = new SnackbarMessageQueue(TimeSpan.FromSeconds(5));
|
||||
|
||||
public bool IsEnabled { get; private set; } = true;
|
||||
|
||||
public bool IsProgressIndeterminate => Progress < 0;
|
||||
|
||||
public double Progress { get; private set; }
|
||||
|
||||
public bool IsBotToken { get; set; }
|
||||
|
||||
public string TokenValue { get; set; }
|
||||
|
||||
public IReadOnlyList<Guild> AvailableGuilds { get; private set; }
|
||||
|
||||
public Guild SelectedGuild { get; set; }
|
||||
|
||||
public IReadOnlyList<Channel> AvailableChannels =>
|
||||
SelectedGuild != null ? _guildChannelsMap[SelectedGuild] : Array.Empty<Channel>();
|
||||
|
||||
public RootViewModel(IViewModelFactory viewModelFactory, DialogManager dialogManager,
|
||||
SettingsService settingsService, UpdateService updateService, DataService dataService,
|
||||
ExportService exportService)
|
||||
{
|
||||
_viewModelFactory = viewModelFactory;
|
||||
_dialogManager = dialogManager;
|
||||
_settingsService = settingsService;
|
||||
_updateService = updateService;
|
||||
_dataService = dataService;
|
||||
_exportService = exportService;
|
||||
|
||||
// Set title
|
||||
var version = Assembly.GetExecutingAssembly().GetName().Version;
|
||||
DisplayName = $"DiscordChatExporter v{version}";
|
||||
}
|
||||
|
||||
protected override async void OnViewLoaded()
|
||||
{
|
||||
// Load settings
|
||||
_settingsService.Load();
|
||||
|
||||
// Get last token
|
||||
if (_settingsService.LastToken != null)
|
||||
{
|
||||
IsBotToken = _settingsService.LastToken.Type == AuthTokenType.Bot;
|
||||
TokenValue = _settingsService.LastToken.Value;
|
||||
}
|
||||
|
||||
// Check and prepare update
|
||||
try
|
||||
{
|
||||
var updateVersion = await _updateService.CheckPrepareUpdateAsync();
|
||||
if (updateVersion != null)
|
||||
{
|
||||
Notifications.Enqueue(
|
||||
$"Update to DiscordChatExporter v{updateVersion} will be installed when you exit",
|
||||
"INSTALL NOW", () =>
|
||||
{
|
||||
_updateService.FinalizeUpdate(true);
|
||||
RequestClose();
|
||||
});
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
Notifications.Enqueue("Failed to perform application auto-update");
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnClose()
|
||||
{
|
||||
// Save settings
|
||||
_settingsService.Save();
|
||||
|
||||
// Finalize updates if necessary
|
||||
_updateService.FinalizeUpdate(false);
|
||||
}
|
||||
|
||||
public async void ShowSettings()
|
||||
{
|
||||
// Create dialog
|
||||
var dialog = _viewModelFactory.CreateSettingsViewModel();
|
||||
|
||||
// Show dialog
|
||||
await _dialogManager.ShowDialogAsync(dialog);
|
||||
}
|
||||
|
||||
public void ShowAbout()
|
||||
{
|
||||
Process.Start("https://github.com/Tyrrrz/DiscordChatExporter");
|
||||
}
|
||||
|
||||
public bool CanPopulateGuildsAndChannels => IsEnabled && TokenValue.IsNotBlank();
|
||||
|
||||
public async void PopulateGuildsAndChannels()
|
||||
{
|
||||
IsEnabled = false;
|
||||
Progress = -1;
|
||||
|
||||
// Sanitize token
|
||||
TokenValue = TokenValue.Trim('"');
|
||||
|
||||
// Create token
|
||||
var token = new AuthToken(
|
||||
IsBotToken ? AuthTokenType.Bot : AuthTokenType.User,
|
||||
TokenValue);
|
||||
|
||||
// Save token
|
||||
_settingsService.LastToken = token;
|
||||
|
||||
// Clear existing
|
||||
_guildChannelsMap.Clear();
|
||||
|
||||
try
|
||||
{
|
||||
// Get DM channels
|
||||
{
|
||||
var channels = await _dataService.GetDirectMessageChannelsAsync(token);
|
||||
var guild = Guild.DirectMessages;
|
||||
_guildChannelsMap[guild] = channels.OrderBy(c => c.Name).ToArray();
|
||||
}
|
||||
|
||||
// Get guild channels
|
||||
{
|
||||
var guilds = await _dataService.GetUserGuildsAsync(token);
|
||||
foreach (var guild in guilds)
|
||||
{
|
||||
var channels = await _dataService.GetGuildChannelsAsync(token, guild.Id);
|
||||
_guildChannelsMap[guild] = channels.Where(c => c.Type == ChannelType.GuildTextChat)
|
||||
.OrderBy(c => c.Name)
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
Notifications.Enqueue("Unauthorized – make sure the token is valid");
|
||||
}
|
||||
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden)
|
||||
{
|
||||
Notifications.Enqueue("Forbidden – account may be locked by 2FA");
|
||||
}
|
||||
|
||||
AvailableGuilds = _guildChannelsMap.Keys.ToArray();
|
||||
SelectedGuild = AvailableGuilds.FirstOrDefault();
|
||||
|
||||
Progress = 0;
|
||||
IsEnabled = true;
|
||||
}
|
||||
|
||||
public bool CanExportChannel => IsEnabled;
|
||||
|
||||
public async void ExportChannel(Channel channel)
|
||||
{
|
||||
IsEnabled = false;
|
||||
Progress = -1;
|
||||
|
||||
// Get last used token
|
||||
var token = _settingsService.LastToken;
|
||||
|
||||
// Create dialog
|
||||
var dialog = _viewModelFactory.CreateExportSetupViewModel();
|
||||
dialog.Guild = SelectedGuild;
|
||||
dialog.Channel = channel;
|
||||
|
||||
// Show dialog
|
||||
if (await _dialogManager.ShowDialogAsync(dialog) == true)
|
||||
{
|
||||
// Export
|
||||
try
|
||||
{
|
||||
// Create progress handler
|
||||
var progressHandler = new Progress<double>(p => Progress = p);
|
||||
|
||||
// Get chat log
|
||||
var chatLog = await _dataService.GetChatLogAsync(token, dialog.Guild, dialog.Channel,
|
||||
dialog.From, dialog.To, progressHandler);
|
||||
|
||||
// Export
|
||||
_exportService.ExportChatLog(chatLog, dialog.FilePath, dialog.SelectedFormat,
|
||||
dialog.PartitionLimit);
|
||||
|
||||
// Notify completion
|
||||
Notifications.Enqueue("Export complete");
|
||||
}
|
||||
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden)
|
||||
{
|
||||
Notifications.Enqueue("You don't have access to this channel");
|
||||
}
|
||||
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
|
||||
{
|
||||
Notifications.Enqueue("This channel doesn't exist");
|
||||
}
|
||||
}
|
||||
|
||||
Progress = 0;
|
||||
IsEnabled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user