Skip to content

Commit

Permalink
Merge pull request #68 from fossmium/replace-mvvmlight
Browse files Browse the repository at this point in the history
Removed MVVM Light and replaced it partially with MVVM Toolkit.
  • Loading branch information
TimGels authored May 25, 2022
2 parents e5e5f46 + 8c56bd4 commit c719283
Show file tree
Hide file tree
Showing 17 changed files with 257 additions and 221 deletions.
5 changes: 0 additions & 5 deletions OneDrive-Cloud-Player/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
x:Class="OneDrive_Cloud_Player.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:OneDrive_Cloud_Player"
xmlns:vm="using:OneDrive_Cloud_Player.ViewModels"
RequestedTheme="Dark"
>
<Application.Resources>
<vm:ViewModelLocator xmlns:vm="using:OneDrive_Cloud_Player.ViewModels" x:Key="Locator" />
</Application.Resources>
</Application>
20 changes: 17 additions & 3 deletions OneDrive-Cloud-Player/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LibVLCSharp.Shared;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Identity.Client;
using OneDrive_Cloud_Player.Models.GraphData;
using OneDrive_Cloud_Player.Services;
Expand Down Expand Up @@ -31,6 +32,8 @@ sealed partial class App : Application
public CacheHelper CacheHelper { get; private set; }
public ApplicationDataContainer UserSettings { get; private set; }

public IServiceProvider Container { get; }

/// <summary>
/// The current version of the application data structure.
/// </summary>
Expand All @@ -50,7 +53,8 @@ sealed partial class App : Application
public List<CachedDriveItem> MediaItemList
{
get { return mediaItemList; }
set {
set
{
// Filter the list for playable media.
mediaItemList = App.Current.CacheHelper.FilterPlayableMedia(value);
}
Expand All @@ -65,11 +69,21 @@ public App()
Core.Initialize();
this.InitializeComponent();
this.LoadUserSettings();
this.Container = ConfigureDependencyInjection();
this.CreateScopedPublicClientApplicationInstance();
this.Suspending += Application_Suspending;
this.CacheHelper = new CacheHelper();
}

IServiceProvider ConfigureDependencyInjection()
{
var serviceCollection = new ServiceCollection();

// Add services for dependency injection here.

return serviceCollection.BuildServiceProvider();
}

/// <summary>
/// Load the user settings from disk and check if they contain all the required entries.
/// </summary>
Expand Down Expand Up @@ -163,12 +177,12 @@ protected override async void OnLaunched(LaunchActivatedEventArgs e)
// configuring the new page by passing required information as a navigation
// parameter
if (await IsLoggedIn())
{
{
await this.CacheHelper.Initialize(false);
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
else
{
{
rootFrame.Navigate(typeof(LoginPage), e.Arguments);
}
}
Expand Down
11 changes: 7 additions & 4 deletions OneDrive-Cloud-Player/OneDrive-Cloud-Player.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@
<Compile Include="Services\Helpers\CacheHelper.cs" />
<Compile Include="Services\Helpers\GraphHelper.cs" />
<Compile Include="Services\Helpers\GraphAuthHelper.cs" />
<Compile Include="Services\NavigationService.cs" />
<Compile Include="Services\Utilities\JsonHandler.cs" />
<Compile Include="ViewModels\LoginPageViewModel.cs" />
<Compile Include="ViewModels\MainPageViewModel.cs" />
<Compile Include="ViewModels\SettingsPageViewModel.cs" />
<Compile Include="ViewModels\VideoPlayerPageViewModel.cs" />
<Compile Include="ViewModels\ViewModelLocator.cs" />
<Compile Include="Models\ParamNavigationPage.cs" />
<Compile Include="Views\LoginPage.xaml.cs">
<DependentUpon>LoginPage.xaml</DependentUpon>
Expand Down Expand Up @@ -259,6 +259,9 @@
<PackageReference Include="LibVLCSharp">
<Version>3.6.6</Version>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection">
<Version>6.0.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.Graph">
<Version>3.19.0</Version>
</PackageReference>
Expand All @@ -268,12 +271,12 @@
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
<Version>6.2.12</Version>
</PackageReference>
<PackageReference Include="Microsoft.Toolkit.Mvvm">
<Version>7.1.2</Version>
</PackageReference>
<PackageReference Include="Microsoft.Xaml.Behaviors.Uwp.Managed">
<Version>2.0.1</Version>
</PackageReference>
<PackageReference Include="MvvmLight">
<Version>5.4.1.1</Version>
</PackageReference>
<PackageReference Include="System.Security.Cryptography.ProtectedData">
<Version>5.0.0</Version>
</PackageReference>
Expand Down
109 changes: 109 additions & 0 deletions OneDrive-Cloud-Player/Services/NavigationService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;

namespace OneDrive_Cloud_Player.Services
{
public static class NavigationService
{
public static event NavigatedEventHandler Navigated;

public static event NavigationFailedEventHandler NavigationFailed;

private static Frame _frame;
private static object _lastParamUsed;

public static Frame Frame
{
get
{
if (_frame == null)
{
_frame = Window.Current.Content as Frame;
RegisterFrameEvents();
}

return _frame;
}

set
{
UnregisterFrameEvents();
_frame = value;
RegisterFrameEvents();
}
}

public static bool CanGoBack => Frame.CanGoBack;

public static bool CanGoForward => Frame.CanGoForward;

public static bool GoBack()
{
if (CanGoBack)
{
Frame.GoBack();
return true;
}

return false;
}

public static void GoForward() => Frame.GoForward();

public static bool Navigate(Type pageType, object parameter = null, NavigationTransitionInfo infoOverride = null)
{
if (pageType == null || !pageType.IsSubclassOf(typeof(Page)))
{
throw new ArgumentException($"Invalid pageType '{pageType}', please provide a valid pageType.", nameof(pageType));
}

// Don't open the same page multiple times
if (Frame.Content?.GetType() != pageType || (parameter != null && !parameter.Equals(_lastParamUsed)))
{
var navigationResult = Frame.Navigate(pageType, parameter, infoOverride);
if (navigationResult)
{
_lastParamUsed = parameter;
}

return navigationResult;
}
else
{
return false;
}
}

public static bool Navigate<T>(object parameter = null, NavigationTransitionInfo infoOverride = null)
where T : Page
{
return Navigate(typeof(T), parameter, infoOverride);
}

private static void RegisterFrameEvents()
{
if (_frame != null)
{
_frame.Navigated += Frame_Navigated;
_frame.NavigationFailed += Frame_NavigationFailed;
}
}

private static void UnregisterFrameEvents()
{
if (_frame != null)
{
_frame.Navigated -= Frame_Navigated;
_frame.NavigationFailed -= Frame_NavigationFailed;
}
}

private static void Frame_NavigationFailed(object sender, NavigationFailedEventArgs e) => NavigationFailed?.Invoke(sender, e);

private static void Frame_Navigated(object sender, NavigationEventArgs e) => Navigated?.Invoke(sender, e);
}
}
19 changes: 9 additions & 10 deletions OneDrive-Cloud-Player/ViewModels/LoginPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Views;
using Microsoft.Identity.Client;
using Microsoft.Identity.Client;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input;
using OneDrive_Cloud_Player.Services;
using OneDrive_Cloud_Player.Services.Helpers;
using OneDrive_Cloud_Player.Views;
using System.Windows.Input;

namespace OneDrive_Cloud_Player.ViewModels
{
public class LoginPageViewModel : ViewModelBase
public class LoginPageViewModel : ObservableRecipient
{
private readonly INavigationService _navigationService;
public ICommand LoginCommand { get; }

private bool isLoginButtonEnabled = true;
Expand All @@ -18,13 +18,12 @@ public bool IsLoginButtonEnabled {
get { return isLoginButtonEnabled; }
set {
isLoginButtonEnabled = value;
RaisePropertyChanged("IsReloadButtonEnabled");
OnPropertyChanged();
}
}

public LoginPageViewModel(INavigationService navigationService)
public LoginPageViewModel()
{
_navigationService = navigationService;
LoginCommand = new RelayCommand(Login, CanExecuteLoginButton);
}

Expand All @@ -48,7 +47,7 @@ private async void Login()
// 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.
bool HasAlreadyLoggedIn = App.Current.CacheHelper.Cache.Count != 0;
await App.Current.CacheHelper.Initialize(HasAlreadyLoggedIn);
_navigationService.NavigateTo("MainPage");
NavigationService.Navigate<MainPage>();
IsLoginButtonEnabled = true;
}
}
Expand Down
Loading

0 comments on commit c719283

Please sign in to comment.