Skip to content

Commit c719283

Browse files
authored
Merge pull request #68 from fossmium/replace-mvvmlight
Removed MVVM Light and replaced it partially with MVVM Toolkit.
2 parents e5e5f46 + 8c56bd4 commit c719283

17 files changed

+257
-221
lines changed

OneDrive-Cloud-Player/App.xaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
x:Class="OneDrive_Cloud_Player.App"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5-
xmlns:local="using:OneDrive_Cloud_Player"
6-
xmlns:vm="using:OneDrive_Cloud_Player.ViewModels"
75
RequestedTheme="Dark"
86
>
9-
<Application.Resources>
10-
<vm:ViewModelLocator xmlns:vm="using:OneDrive_Cloud_Player.ViewModels" x:Key="Locator" />
11-
</Application.Resources>
127
</Application>

OneDrive-Cloud-Player/App.xaml.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using LibVLCSharp.Shared;
2+
using Microsoft.Extensions.DependencyInjection;
23
using Microsoft.Identity.Client;
34
using OneDrive_Cloud_Player.Models.GraphData;
45
using OneDrive_Cloud_Player.Services;
@@ -31,6 +32,8 @@ sealed partial class App : Application
3132
public CacheHelper CacheHelper { get; private set; }
3233
public ApplicationDataContainer UserSettings { get; private set; }
3334

35+
public IServiceProvider Container { get; }
36+
3437
/// <summary>
3538
/// The current version of the application data structure.
3639
/// </summary>
@@ -50,7 +53,8 @@ sealed partial class App : Application
5053
public List<CachedDriveItem> MediaItemList
5154
{
5255
get { return mediaItemList; }
53-
set {
56+
set
57+
{
5458
// Filter the list for playable media.
5559
mediaItemList = App.Current.CacheHelper.FilterPlayableMedia(value);
5660
}
@@ -65,11 +69,21 @@ public App()
6569
Core.Initialize();
6670
this.InitializeComponent();
6771
this.LoadUserSettings();
72+
this.Container = ConfigureDependencyInjection();
6873
this.CreateScopedPublicClientApplicationInstance();
6974
this.Suspending += Application_Suspending;
7075
this.CacheHelper = new CacheHelper();
7176
}
7277

78+
IServiceProvider ConfigureDependencyInjection()
79+
{
80+
var serviceCollection = new ServiceCollection();
81+
82+
// Add services for dependency injection here.
83+
84+
return serviceCollection.BuildServiceProvider();
85+
}
86+
7387
/// <summary>
7488
/// Load the user settings from disk and check if they contain all the required entries.
7589
/// </summary>
@@ -163,12 +177,12 @@ protected override async void OnLaunched(LaunchActivatedEventArgs e)
163177
// configuring the new page by passing required information as a navigation
164178
// parameter
165179
if (await IsLoggedIn())
166-
{
180+
{
167181
await this.CacheHelper.Initialize(false);
168182
rootFrame.Navigate(typeof(MainPage), e.Arguments);
169183
}
170184
else
171-
{
185+
{
172186
rootFrame.Navigate(typeof(LoginPage), e.Arguments);
173187
}
174188
}

OneDrive-Cloud-Player/OneDrive-Cloud-Player.csproj

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,12 @@
138138
<Compile Include="Services\Helpers\CacheHelper.cs" />
139139
<Compile Include="Services\Helpers\GraphHelper.cs" />
140140
<Compile Include="Services\Helpers\GraphAuthHelper.cs" />
141+
<Compile Include="Services\NavigationService.cs" />
141142
<Compile Include="Services\Utilities\JsonHandler.cs" />
142143
<Compile Include="ViewModels\LoginPageViewModel.cs" />
143144
<Compile Include="ViewModels\MainPageViewModel.cs" />
144145
<Compile Include="ViewModels\SettingsPageViewModel.cs" />
145146
<Compile Include="ViewModels\VideoPlayerPageViewModel.cs" />
146-
<Compile Include="ViewModels\ViewModelLocator.cs" />
147147
<Compile Include="Models\ParamNavigationPage.cs" />
148148
<Compile Include="Views\LoginPage.xaml.cs">
149149
<DependentUpon>LoginPage.xaml</DependentUpon>
@@ -259,6 +259,9 @@
259259
<PackageReference Include="LibVLCSharp">
260260
<Version>3.6.6</Version>
261261
</PackageReference>
262+
<PackageReference Include="Microsoft.Extensions.DependencyInjection">
263+
<Version>6.0.0</Version>
264+
</PackageReference>
262265
<PackageReference Include="Microsoft.Graph">
263266
<Version>3.19.0</Version>
264267
</PackageReference>
@@ -268,12 +271,12 @@
268271
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
269272
<Version>6.2.12</Version>
270273
</PackageReference>
274+
<PackageReference Include="Microsoft.Toolkit.Mvvm">
275+
<Version>7.1.2</Version>
276+
</PackageReference>
271277
<PackageReference Include="Microsoft.Xaml.Behaviors.Uwp.Managed">
272278
<Version>2.0.1</Version>
273279
</PackageReference>
274-
<PackageReference Include="MvvmLight">
275-
<Version>5.4.1.1</Version>
276-
</PackageReference>
277280
<PackageReference Include="System.Security.Cryptography.ProtectedData">
278281
<Version>5.0.0</Version>
279282
</PackageReference>
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System;
2+
3+
using Windows.UI.Xaml;
4+
using Windows.UI.Xaml.Controls;
5+
using Windows.UI.Xaml.Media.Animation;
6+
using Windows.UI.Xaml.Navigation;
7+
8+
namespace OneDrive_Cloud_Player.Services
9+
{
10+
public static class NavigationService
11+
{
12+
public static event NavigatedEventHandler Navigated;
13+
14+
public static event NavigationFailedEventHandler NavigationFailed;
15+
16+
private static Frame _frame;
17+
private static object _lastParamUsed;
18+
19+
public static Frame Frame
20+
{
21+
get
22+
{
23+
if (_frame == null)
24+
{
25+
_frame = Window.Current.Content as Frame;
26+
RegisterFrameEvents();
27+
}
28+
29+
return _frame;
30+
}
31+
32+
set
33+
{
34+
UnregisterFrameEvents();
35+
_frame = value;
36+
RegisterFrameEvents();
37+
}
38+
}
39+
40+
public static bool CanGoBack => Frame.CanGoBack;
41+
42+
public static bool CanGoForward => Frame.CanGoForward;
43+
44+
public static bool GoBack()
45+
{
46+
if (CanGoBack)
47+
{
48+
Frame.GoBack();
49+
return true;
50+
}
51+
52+
return false;
53+
}
54+
55+
public static void GoForward() => Frame.GoForward();
56+
57+
public static bool Navigate(Type pageType, object parameter = null, NavigationTransitionInfo infoOverride = null)
58+
{
59+
if (pageType == null || !pageType.IsSubclassOf(typeof(Page)))
60+
{
61+
throw new ArgumentException($"Invalid pageType '{pageType}', please provide a valid pageType.", nameof(pageType));
62+
}
63+
64+
// Don't open the same page multiple times
65+
if (Frame.Content?.GetType() != pageType || (parameter != null && !parameter.Equals(_lastParamUsed)))
66+
{
67+
var navigationResult = Frame.Navigate(pageType, parameter, infoOverride);
68+
if (navigationResult)
69+
{
70+
_lastParamUsed = parameter;
71+
}
72+
73+
return navigationResult;
74+
}
75+
else
76+
{
77+
return false;
78+
}
79+
}
80+
81+
public static bool Navigate<T>(object parameter = null, NavigationTransitionInfo infoOverride = null)
82+
where T : Page
83+
{
84+
return Navigate(typeof(T), parameter, infoOverride);
85+
}
86+
87+
private static void RegisterFrameEvents()
88+
{
89+
if (_frame != null)
90+
{
91+
_frame.Navigated += Frame_Navigated;
92+
_frame.NavigationFailed += Frame_NavigationFailed;
93+
}
94+
}
95+
96+
private static void UnregisterFrameEvents()
97+
{
98+
if (_frame != null)
99+
{
100+
_frame.Navigated -= Frame_Navigated;
101+
_frame.NavigationFailed -= Frame_NavigationFailed;
102+
}
103+
}
104+
105+
private static void Frame_NavigationFailed(object sender, NavigationFailedEventArgs e) => NavigationFailed?.Invoke(sender, e);
106+
107+
private static void Frame_Navigated(object sender, NavigationEventArgs e) => Navigated?.Invoke(sender, e);
108+
}
109+
}

OneDrive-Cloud-Player/ViewModels/LoginPageViewModel.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
using GalaSoft.MvvmLight;
2-
using GalaSoft.MvvmLight.Command;
3-
using GalaSoft.MvvmLight.Views;
4-
using Microsoft.Identity.Client;
1+
using Microsoft.Identity.Client;
2+
using Microsoft.Toolkit.Mvvm.ComponentModel;
3+
using Microsoft.Toolkit.Mvvm.Input;
4+
using OneDrive_Cloud_Player.Services;
55
using OneDrive_Cloud_Player.Services.Helpers;
6+
using OneDrive_Cloud_Player.Views;
67
using System.Windows.Input;
78

89
namespace OneDrive_Cloud_Player.ViewModels
910
{
10-
public class LoginPageViewModel : ViewModelBase
11+
public class LoginPageViewModel : ObservableRecipient
1112
{
12-
private readonly INavigationService _navigationService;
1313
public ICommand LoginCommand { get; }
1414

1515
private bool isLoginButtonEnabled = true;
@@ -18,13 +18,12 @@ public bool IsLoginButtonEnabled {
1818
get { return isLoginButtonEnabled; }
1919
set {
2020
isLoginButtonEnabled = value;
21-
RaisePropertyChanged("IsReloadButtonEnabled");
21+
OnPropertyChanged();
2222
}
2323
}
2424

25-
public LoginPageViewModel(INavigationService navigationService)
25+
public LoginPageViewModel()
2626
{
27-
_navigationService = navigationService;
2827
LoginCommand = new RelayCommand(Login, CanExecuteLoginButton);
2928
}
3029

@@ -48,7 +47,7 @@ private async void Login()
4847
// This is used to decide whether or not to read cache from disk. It prevents reading from old disk cache, since the cache is only written to disk upon application suspension.
4948
bool HasAlreadyLoggedIn = App.Current.CacheHelper.Cache.Count != 0;
5049
await App.Current.CacheHelper.Initialize(HasAlreadyLoggedIn);
51-
_navigationService.NavigateTo("MainPage");
50+
NavigationService.Navigate<MainPage>();
5251
IsLoginButtonEnabled = true;
5352
}
5453
}

0 commit comments

Comments
 (0)