Skip to content
This repository was archived by the owner on Apr 30, 2024. It is now read-only.

Commit

Permalink
Added guild as context to guild settings sub page
Browse files Browse the repository at this point in the history
  • Loading branch information
Avid29 committed May 19, 2022
1 parent f84b9cc commit d690d75
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Quarrel © 2022

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.DependencyInjection;
using System;

namespace Quarrel.Messages.Navigation.SubPages
Expand All @@ -14,12 +17,21 @@ public class NavigateToSubPageMessage
/// </summary>
public NavigateToSubPageMessage(Type targetViewModelType)
{
TargetViewModelType = targetViewModelType;
// TODO: Investigate alternative to using Ioc
ViewModel = (ObservableObject)Ioc.Default.GetRequiredService(targetViewModelType);
}

/// <summary>
/// Initializes a new instance of the <see cref="NavigateToSubPageMessage"/> class.
/// </summary>
public NavigateToSubPageMessage(ObservableObject viewModel)
{
ViewModel = viewModel;
}

/// <summary>
/// Gets the type of ViewModel for the target SubPage.
/// </summary>
public Type TargetViewModelType { get; }
public ObservableObject ViewModel { get; }
}
}
13 changes: 11 additions & 2 deletions src/Quarrel.ViewModels/ViewModels/Panels/GuildsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ public partial class GuildsViewModel : ObservableRecipient
/// <summary>
/// Initializes a new instance of the <see cref="GuildsViewModel"/> class.
/// </summary>
public GuildsViewModel(IAnalyticsService analyticsService, IMessenger messenger, ILocalizationService localizationService, IDiscordService discordService, IDispatcherService dispatcherService)
public GuildsViewModel(
IAnalyticsService analyticsService,
IMessenger messenger,
ILocalizationService localizationService,
IDiscordService discordService,
IDispatcherService dispatcherService)
{
_analyticsService = analyticsService;
_messenger = messenger;
Expand Down Expand Up @@ -114,7 +119,11 @@ public void LoadGuilds()

private void OpenGuildSettings()
{
_messenger.Send(new NavigateToSubPageMessage(typeof(GuildSettingsPageViewModel)));
if (SelectedGuild is BindableGuild guild)
{
var viewModel = new GuildSettingsPageViewModel(_localizationService, guild);
_messenger.Send(new NavigateToSubPageMessage(viewModel));
}
}

private void ForwardNavigate(ulong guildId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Quarrel © 2022

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.DependencyInjection;
using Microsoft.Toolkit.Mvvm.Input;
using Microsoft.Toolkit.Mvvm.Messaging;
using Quarrel.Messages.Navigation.SubPages;
Expand Down Expand Up @@ -33,7 +31,7 @@ public SubPageHostViewModel(IAnalyticsService analyticsService, IMessenger messe
_messenger = messenger;
_navStack = new Stack<object>();

_messenger.Register<NavigateToSubPageMessage>(this, (s, e) => NavigateToSubPage(e.TargetViewModelType));
_messenger.Register<NavigateToSubPageMessage>(this, (s, e) => NavigateToSubPage(e.ViewModel));
_messenger.Register<GoBackSubPageMessage>(this, (s, e) => HandleGoBackSubPage());
}

Expand All @@ -43,7 +41,7 @@ public SubPageHostViewModel(IAnalyticsService analyticsService, IMessenger messe
public object? ContentViewModel
{
get => _contentViewModel;
set
private set
{
if (SetProperty(ref _contentViewModel, value))
{
Expand All @@ -64,17 +62,15 @@ public object? ContentViewModel
public bool IsStacked => _navStack.Count > 0;

[ICommand]
private void NavigateToSubPage(Type viewModelType)
private void NavigateToSubPage(object viewModel)
{
// TODO: Investigate alternative to using Ioc
object viewModel = Ioc.Default.GetRequiredService(viewModelType);
if (ContentViewModel is not null)
{
_navStack.Push(ContentViewModel);
}

_analyticsService.Log(LoggedEvent.SubPageOpened,
("Type", viewModelType.Name));
("Type", viewModel.GetType().Name));

ContentViewModel = viewModel;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Quarrel © 2022

using Microsoft.Toolkit.Mvvm.ComponentModel;
using Quarrel.Bindables.Guilds;
using Quarrel.Services.Localization;
using Quarrel.ViewModels.SubPages.Settings;
using Quarrel.ViewModels.SubPages.Settings.Abstract;
using System.Collections.ObjectModel;

Expand All @@ -22,7 +22,7 @@ public class GuildSettingsPageViewModel : ObservableObject
/// <summary>
/// Initializes a new instance of the <see cref="GuildSettingsPageViewModel"/>.
/// </summary>
public GuildSettingsPageViewModel(ILocalizationService localizationService)
public GuildSettingsPageViewModel(ILocalizationService localizationService, BindableGuild guild)
{
Pages = new ObservableCollection<ISettingsMenuItem>();

Expand Down

0 comments on commit d690d75

Please sign in to comment.