mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2025-05-31 06:48:23 -04:00
More refactoring
This commit is contained in:
parent
9d0d7cd5dd
commit
d03be8b1dd
43 changed files with 617 additions and 655 deletions
|
@ -1,17 +0,0 @@
|
|||
using DiscordChatExporter.Domain.Discord.Models;
|
||||
using Stylet;
|
||||
|
||||
namespace DiscordChatExporter.Gui.ViewModels.Components
|
||||
{
|
||||
public partial class ChannelViewModel : PropertyChangedBase
|
||||
{
|
||||
public Channel? Model { get; set; }
|
||||
|
||||
public string? Category { get; set; }
|
||||
}
|
||||
|
||||
public partial class ChannelViewModel
|
||||
{
|
||||
public static implicit operator Channel?(ChannelViewModel? viewModel) => viewModel?.Model;
|
||||
}
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
using System.Collections.Generic;
|
||||
using DiscordChatExporter.Domain.Discord.Models;
|
||||
using Stylet;
|
||||
|
||||
namespace DiscordChatExporter.Gui.ViewModels.Components
|
||||
{
|
||||
public partial class GuildViewModel : PropertyChangedBase
|
||||
{
|
||||
public Guild? Model { get; set; }
|
||||
|
||||
public IReadOnlyList<ChannelViewModel>? Channels { get; set; }
|
||||
}
|
||||
|
||||
public partial class GuildViewModel
|
||||
{
|
||||
public static implicit operator Guild?(GuildViewModel? viewModel) => viewModel?.Model;
|
||||
}
|
||||
}
|
|
@ -1,9 +1,9 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using DiscordChatExporter.Domain.Discord.Models;
|
||||
using DiscordChatExporter.Domain.Exporting;
|
||||
using DiscordChatExporter.Gui.Services;
|
||||
using DiscordChatExporter.Gui.ViewModels.Components;
|
||||
using DiscordChatExporter.Gui.ViewModels.Framework;
|
||||
|
||||
namespace DiscordChatExporter.Gui.ViewModels.Dialogs
|
||||
|
@ -13,9 +13,9 @@ namespace DiscordChatExporter.Gui.ViewModels.Dialogs
|
|||
private readonly DialogManager _dialogManager;
|
||||
private readonly SettingsService _settingsService;
|
||||
|
||||
public GuildViewModel? Guild { get; set; }
|
||||
public Guild? Guild { get; set; }
|
||||
|
||||
public IReadOnlyList<ChannelViewModel>? Channels { get; set; }
|
||||
public IReadOnlyList<Channel>? Channels { get; set; }
|
||||
|
||||
public bool IsSingleChannel => Channels == null || Channels.Count == 1;
|
||||
|
||||
|
@ -61,7 +61,7 @@ namespace DiscordChatExporter.Gui.ViewModels.Dialogs
|
|||
var channel = Channels.Single();
|
||||
|
||||
// Generate default file name
|
||||
var defaultFileName = ChannelExporter.GetDefaultExportFileName(Guild!, channel!, SelectedFormat, After, Before);
|
||||
var defaultFileName = ChannelExporter.GetDefaultExportFileName(Guild!, channel, SelectedFormat, After, Before);
|
||||
|
||||
// Generate filter
|
||||
var ext = SelectedFormat.GetFileExtension();
|
||||
|
|
|
@ -1,33 +1,13 @@
|
|||
using System.Collections.Generic;
|
||||
using DiscordChatExporter.Domain.Discord.Models;
|
||||
using DiscordChatExporter.Gui.ViewModels.Components;
|
||||
using DiscordChatExporter.Gui.ViewModels.Dialogs;
|
||||
|
||||
namespace DiscordChatExporter.Gui.ViewModels.Framework
|
||||
{
|
||||
public static class Extensions
|
||||
{
|
||||
public static ChannelViewModel CreateChannelViewModel(this IViewModelFactory factory, Channel model, string? category = null)
|
||||
{
|
||||
var viewModel = factory.CreateChannelViewModel();
|
||||
viewModel.Model = model;
|
||||
viewModel.Category = category;
|
||||
|
||||
return viewModel;
|
||||
}
|
||||
|
||||
public static GuildViewModel CreateGuildViewModel(this IViewModelFactory factory, Guild model,
|
||||
IReadOnlyList<ChannelViewModel> channels)
|
||||
{
|
||||
var viewModel = factory.CreateGuildViewModel();
|
||||
viewModel.Model = model;
|
||||
viewModel.Channels = channels;
|
||||
|
||||
return viewModel;
|
||||
}
|
||||
|
||||
public static ExportSetupViewModel CreateExportSetupViewModel(this IViewModelFactory factory,
|
||||
GuildViewModel guild, IReadOnlyList<ChannelViewModel> channels)
|
||||
Guild guild, IReadOnlyList<Channel> channels)
|
||||
{
|
||||
var viewModel = factory.CreateExportSetupViewModel();
|
||||
viewModel.Guild = guild;
|
||||
|
|
|
@ -1,15 +1,10 @@
|
|||
using DiscordChatExporter.Gui.ViewModels.Components;
|
||||
using DiscordChatExporter.Gui.ViewModels.Dialogs;
|
||||
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
|
||||
{
|
||||
ChannelViewModel CreateChannelViewModel();
|
||||
|
||||
GuildViewModel CreateGuildViewModel();
|
||||
|
||||
ExportSetupViewModel CreateExportSetupViewModel();
|
||||
|
||||
SettingsViewModel CreateSettingsViewModel();
|
||||
|
|
|
@ -9,7 +9,6 @@ using DiscordChatExporter.Domain.Exceptions;
|
|||
using DiscordChatExporter.Domain.Exporting;
|
||||
using DiscordChatExporter.Domain.Utilities;
|
||||
using DiscordChatExporter.Gui.Services;
|
||||
using DiscordChatExporter.Gui.ViewModels.Components;
|
||||
using DiscordChatExporter.Gui.ViewModels.Framework;
|
||||
using Gress;
|
||||
using MaterialDesignThemes.Wpf;
|
||||
|
@ -37,11 +36,17 @@ namespace DiscordChatExporter.Gui.ViewModels
|
|||
|
||||
public string? TokenValue { get; set; }
|
||||
|
||||
public IReadOnlyList<GuildViewModel>? AvailableGuilds { get; private set; }
|
||||
private IReadOnlyDictionary<Guild, IReadOnlyList<Channel>>? GuildChannelMap { get; set; }
|
||||
|
||||
public GuildViewModel? SelectedGuild { get; set; }
|
||||
public IReadOnlyList<Guild>? AvailableGuilds => GuildChannelMap?.Keys.ToArray();
|
||||
|
||||
public IReadOnlyList<ChannelViewModel>? SelectedChannels { get; set; }
|
||||
public Guild? SelectedGuild { get; set; }
|
||||
|
||||
public IReadOnlyList<Channel>? AvailableChannels => SelectedGuild != null
|
||||
? GuildChannelMap?[SelectedGuild]
|
||||
: null;
|
||||
|
||||
public IReadOnlyList<Channel>? SelectedChannels { get; set; }
|
||||
|
||||
public RootViewModel(
|
||||
IViewModelFactory viewModelFactory,
|
||||
|
@ -142,71 +147,18 @@ namespace DiscordChatExporter.Gui.ViewModels
|
|||
|
||||
var discord = new DiscordClient(token);
|
||||
|
||||
var availableGuilds = new List<GuildViewModel>();
|
||||
|
||||
// Direct messages
|
||||
{
|
||||
var guild = Guild.DirectMessages;
|
||||
var channels = await discord.GetDirectMessageChannelsAsync();
|
||||
|
||||
// Create channel view models
|
||||
var channelViewModels = new List<ChannelViewModel>();
|
||||
foreach (var channel in channels)
|
||||
{
|
||||
// Get fake category
|
||||
var category = channel.Type == ChannelType.DirectTextChat ? "Private" : "Group";
|
||||
|
||||
// Create channel view model
|
||||
var channelViewModel = _viewModelFactory.CreateChannelViewModel(channel, category);
|
||||
|
||||
// Add to list
|
||||
channelViewModels.Add(channelViewModel);
|
||||
}
|
||||
|
||||
// Create guild view model
|
||||
var guildViewModel = _viewModelFactory.CreateGuildViewModel(guild,
|
||||
channelViewModels.OrderBy(c => c.Category)
|
||||
.ThenBy(c => c.Model!.Name)
|
||||
.ToArray());
|
||||
|
||||
// Add to list
|
||||
availableGuilds.Add(guildViewModel);
|
||||
}
|
||||
|
||||
// Guilds
|
||||
var guilds = await discord.GetUserGuildsAsync();
|
||||
foreach (var guild in guilds)
|
||||
var guildChannelMap = new Dictionary<Guild, IReadOnlyList<Channel>>();
|
||||
await foreach (var guild in discord.GetUserGuildsAsync())
|
||||
{
|
||||
var channels = await discord.GetGuildChannelsAsync(guild.Id);
|
||||
var categoryChannels = channels.Where(c => c.Type == ChannelType.GuildCategory).ToArray();
|
||||
var exportableChannels = channels.Where(c => c.IsTextChannel).ToArray();
|
||||
|
||||
// Create channel view models
|
||||
var channelViewModels = new List<ChannelViewModel>();
|
||||
foreach (var channel in exportableChannels)
|
||||
{
|
||||
// Get category
|
||||
var category = categoryChannels.FirstOrDefault(c => c.Id == channel.ParentId)?.Name;
|
||||
|
||||
// Create channel view model
|
||||
var channelViewModel = _viewModelFactory.CreateChannelViewModel(channel, category);
|
||||
|
||||
// Add to list
|
||||
channelViewModels.Add(channelViewModel);
|
||||
}
|
||||
|
||||
// Create guild view model
|
||||
var guildViewModel = _viewModelFactory.CreateGuildViewModel(guild,
|
||||
channelViewModels.OrderBy(c => c.Category)
|
||||
.ThenBy(c => c.Model!.Name)
|
||||
.ToArray());
|
||||
|
||||
// Add to list
|
||||
availableGuilds.Add(guildViewModel);
|
||||
guildChannelMap[guild] = channels
|
||||
.OrderBy(c => c.Category)
|
||||
.ThenBy(c => c.Name)
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
AvailableGuilds = availableGuilds;
|
||||
SelectedGuild = AvailableGuilds.FirstOrDefault();
|
||||
GuildChannelMap = guildChannelMap;
|
||||
SelectedGuild = guildChannelMap.Keys.FirstOrDefault();
|
||||
}
|
||||
catch (DiscordChatExporterException ex) when (!ex.IsCritical)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue