Skip to content

Commit 09e1ab1

Browse files
authored
Merge pull request #31 from GrandDynamo/settings-page
Implements the settings page and closes #31.
2 parents 5e12df9 + afeb6fc commit 09e1ab1

File tree

10 files changed

+350
-47
lines changed

10 files changed

+350
-47
lines changed

OneDrive-Cloud-Player/App.xaml.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Threading.Tasks;
1010
using Windows.ApplicationModel;
1111
using Windows.ApplicationModel.Activation;
12+
using Windows.Storage;
1213
using Windows.ApplicationModel.Core;
1314
using Windows.UI.Core;
1415
using Windows.UI.Xaml;
@@ -28,6 +29,18 @@ sealed partial class App : Application
2829
public IPublicClientApplication PublicClientApplication { get; private set; }
2930
public string[] Scopes { get; private set; }
3031
public CacheHelper CacheHelper { get; private set; }
32+
public ApplicationDataContainer UserSettings { get; private set; }
33+
34+
/// <summary>
35+
/// The current version of the application data structure.
36+
/// </summary>
37+
/// <remarks>
38+
/// If the structure of the application data changes (which is currently determined by the
39+
/// requiredSettings dictionary in <see cref="App.UpdateAppDataFormat(SetVersionRequest)"/>),
40+
/// this number should be incremented at the very same time (meaning compile-time). If not,
41+
/// the application data could enter an inconsistent state.
42+
/// </remarks>
43+
private const int appDataVersion = 1;
3144

3245
private List<CachedDriveItem> mediaItemList;
3346
/// <summary>
@@ -51,11 +64,60 @@ public App()
5164
{
5265
Core.Initialize();
5366
this.InitializeComponent();
67+
this.LoadUserSettings();
5468
this.CreateScopedPublicClientApplicationInstance();
5569
this.Suspending += Application_Suspending;
5670
this.CacheHelper = new CacheHelper();
5771
}
5872

73+
/// <summary>
74+
/// Load the user settings from disk and check if they contain all the required entries.
75+
/// </summary>
76+
private void LoadUserSettings()
77+
{
78+
// Load the saved settings from disk and check the version.
79+
UserSettings = ApplicationData.Current.LocalSettings;
80+
if (ApplicationData.Current.Version < appDataVersion)
81+
{
82+
ApplicationData.Current.SetVersionAsync(appDataVersion, UpdateAppDataFormat).AsTask().Wait();
83+
}
84+
}
85+
86+
/// <summary>
87+
/// Update the application data format. Afterwards, the version will be incremented.
88+
/// </summary>
89+
/// <param name="request">Different upgrade paths are enabled by checking the current
90+
/// and desired properties of this parameter.</param>
91+
private void UpdateAppDataFormat(SetVersionRequest request)
92+
{
93+
// Create a dictionary with the required settings and their default values.
94+
Dictionary<string, object> requiredSettings = new Dictionary<string, object>
95+
{
96+
{ "MediaVolume", 100 },
97+
{ "ShowDefaultSubtitles", true }
98+
};
99+
100+
// Verify disk settings against default settings.
101+
foreach (string key in requiredSettings.Keys)
102+
{
103+
if (!UserSettings.Values.ContainsKey(key))
104+
{
105+
// Required setting not found on disk, so add it.
106+
UserSettings.Values[key] = requiredSettings.GetValueOrDefault(key);
107+
}
108+
}
109+
110+
// Check disk settings for unneeded entries.
111+
foreach (string diskEntry in UserSettings.Values.Keys)
112+
{
113+
if (!requiredSettings.ContainsKey(diskEntry))
114+
{
115+
// Disk settings contain more info than needed.
116+
UserSettings.Values.Remove(diskEntry);
117+
}
118+
}
119+
}
120+
59121
/// <summary>
60122
/// Check whether or not the user credentials are cached via MSAL
61123
/// </summary>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
<Compile Include="Services\Utilities\JsonHandler.cs" />
143143
<Compile Include="ViewModels\LoginPageViewModel.cs" />
144144
<Compile Include="ViewModels\MainPageViewModel.cs" />
145+
<Compile Include="ViewModels\SettingsPageViewModel.cs" />
145146
<Compile Include="ViewModels\VideoPlayerPageViewModel.cs" />
146147
<Compile Include="ViewModels\ViewModelLocator.cs" />
147148
<Compile Include="Models\ParamNavigationPage.cs" />
@@ -151,6 +152,9 @@
151152
<Compile Include="Views\MainPage.xaml.cs">
152153
<DependentUpon>MainPage.xaml</DependentUpon>
153154
</Compile>
155+
<Compile Include="Views\SettingsPage.xaml.cs">
156+
<DependentUpon>SettingsPage.xaml</DependentUpon>
157+
</Compile>
154158
<Compile Include="Views\VideoPlayerPage.xaml.cs">
155159
<DependentUpon>VideoPlayerPage.xaml</DependentUpon>
156160
</Compile>
@@ -243,6 +247,10 @@
243247
<SubType>Designer</SubType>
244248
<Generator>MSBuild:Compile</Generator>
245249
</Page>
250+
<Page Include="Views\SettingsPage.xaml">
251+
<Generator>MSBuild:Compile</Generator>
252+
<SubType>Designer</SubType>
253+
</Page>
246254
<Page Include="Views\VideoPlayerPage.xaml">
247255
<SubType>Designer</SubType>
248256
<Generator>MSBuild:Compile</Generator>

OneDrive-Cloud-Player/ViewModels/MainPageViewModel.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ public class MainPageViewModel : ViewModelBase
2727
public ICommand LogoutCommand { get; set; }
2828
public ICommand ToParentFolderCommand { get; set; }
2929
public ICommand GetProfileInfoCommand { get; set; }
30+
public ICommand ToSettingsPageCommand { get; set; }
3031

31-
private readonly GraphHelper graph;
32+
private readonly GraphHelper graphHelper = GraphHelper.Instance();
3233
private readonly INavigationService _navigationService;
3334

3435
// The list of the different drives
@@ -140,15 +141,13 @@ public MainPageViewModel(INavigationService navigationService)
140141
{
141142
_navigationService = navigationService;
142143

143-
DriveList = null;
144-
this.graph = GraphHelper.Instance();
145-
//DisplayMessageCommand = new RelayCommand(DisplayMessage, CanExecuteCommand);
146144
GetDrivesCommand = new RelayCommand(GetDrives, CanExecuteCommand);
147145
GetChildrenFomItemCommand = new RelayCommand(GetChildrenFomItem, CanExecuteCommand);
148146
GetChildrenFomDriveCommand = new RelayCommand(GetChildrenFomDrive, CanExecuteCommand);
149147
ReloadCommand = new RelayCommand(ReloadCache, CanExecuteCommand);
150148
LogoutCommand = new RelayCommand(Logout, CanExecuteCommand);
151149
ToParentFolderCommand = new RelayCommand(ToParentFolder, CanExecuteCommand);
150+
ToSettingsPageCommand = new RelayCommand(ToSettingsPage, CanExecuteCommand);
152151

153152
// OnLoad runs the login and gets the shared drives
154153
GetUserInformation();
@@ -162,11 +161,11 @@ public MainPageViewModel(INavigationService navigationService)
162161
/// </summary>
163162
public async void GetUserInformation()
164163
{
165-
CurrentUsername = "Hi, " + (await graph.GetOneDriveUserInformationAsync()).GivenName;
164+
CurrentUsername = "Hi, " + (await graphHelper.GetOneDriveUserInformationAsync()).GivenName;
166165
try
167166
{
168167
var bitmapImage = new BitmapImage();
169-
IRandomAccessStream imageRandomAccessStream = (await graph.GetOneDriveOwnerPhotoAsync()).AsRandomAccessStream();
168+
IRandomAccessStream imageRandomAccessStream = (await graphHelper.GetOneDriveOwnerPhotoAsync()).AsRandomAccessStream();
170169
await bitmapImage.SetSourceAsync(imageRandomAccessStream);
171170

172171
ProfileImage = bitmapImage;
@@ -338,5 +337,10 @@ private void ToParentFolder()
338337
ParentItem = App.Current.CacheHelper.GetParentItemByParentItemId(SelectedDriveFolder, ParentItem.ParentItemId);
339338
}
340339
}
340+
341+
private void ToSettingsPage()
342+
{
343+
_navigationService.NavigateTo("SettingsPage");
344+
}
341345
}
342346
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using GalaSoft.MvvmLight;
2+
using GalaSoft.MvvmLight.Command;
3+
using GalaSoft.MvvmLight.Views;
4+
using System;
5+
using System.ComponentModel;
6+
using System.Windows.Input;
7+
using Windows.ApplicationModel;
8+
using Windows.Storage;
9+
using Windows.UI;
10+
using Windows.UI.Xaml.Controls;
11+
using Windows.UI.Xaml.Media;
12+
13+
namespace OneDrive_Cloud_Player.ViewModels
14+
{
15+
public class SettingsPageViewModel : ViewModelBase, INotifyPropertyChanged
16+
{
17+
public ICommand ToMainPageCommand { get; set; }
18+
public ICommand DisplayWhatsNewDialogCommand { get; set; }
19+
public string AppVersion { get; }
20+
public string PackageDisplayName { get; }
21+
22+
private readonly ApplicationDataContainer settings = App.Current.UserSettings;
23+
24+
private bool showDefaultSubtitles;
25+
26+
public bool ShowDefaultSubtitles
27+
{
28+
get
29+
{ return showDefaultSubtitles; }
30+
set
31+
{
32+
settings.Values["ShowDefaultSubtitles"] = value;
33+
showDefaultSubtitles = value;
34+
RaisePropertyChanged("ShowDefaultSubtitles");
35+
}
36+
}
37+
38+
39+
private readonly INavigationService _navigationService;
40+
41+
42+
public SettingsPageViewModel(INavigationService navigationService)
43+
{
44+
//Initialize settings
45+
ShowDefaultSubtitles = (bool)settings.Values["ShowDefaultSubtitles"];
46+
47+
Package package = Package.Current;
48+
PackageVersion version = package.Id.Version;
49+
50+
AppVersion = string.Format("{0}.{1}.{2}.{3}", version.Major, version.Minor, version.Build, version.Revision);
51+
PackageDisplayName = package.DisplayName;
52+
_navigationService = navigationService;
53+
ToMainPageCommand = new RelayCommand(ToMainPage, CanExecuteCommand);
54+
DisplayWhatsNewDialogCommand = new RelayCommand(DisplayWhatsNewDialog, CanExecuteCommand);
55+
}
56+
57+
private bool CanExecuteCommand()
58+
{
59+
return true;
60+
}
61+
62+
public void ToMainPage()
63+
{
64+
_navigationService.NavigateTo("MainPage");
65+
}
66+
67+
/// <summary>
68+
/// Displays a content dialog showing the new features added in the current version.
69+
/// </summary>
70+
private async void DisplayWhatsNewDialog()
71+
{
72+
ContentDialog whatsNewDialog = new ContentDialog
73+
{
74+
Title = $"Whats new in {PackageDisplayName}",
75+
PrimaryButtonText = "Ok",
76+
DefaultButton = ContentDialogButton.Primary,
77+
Background = new SolidColorBrush(Color.FromArgb(255, 30, 41, 49)),
78+
};
79+
whatsNewDialog.Content += "* Added a settings page";
80+
81+
await whatsNewDialog.ShowAsync();
82+
}
83+
}
84+
}

OneDrive-Cloud-Player/ViewModels/VideoPlayerPageViewModel.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
using System.Threading.Tasks;
1313
using System.Timers;
1414
using System.Windows.Input;
15-
using Windows.Storage;
1615
using Windows.System;
1716
using Windows.UI.Core;
1817
using Windows.UI.Xaml;
@@ -24,7 +23,6 @@ namespace OneDrive_Cloud_Player.ViewModels
2423
/// </summary>
2524
public class VideoPlayerPageViewModel : ViewModelBase, INotifyPropertyChanged, IDisposable, INavigable
2625
{
27-
private readonly ApplicationDataContainer localMediaVolumeLevelSetting;
2826
private readonly INavigationService _navigationService;
2927
private readonly GraphHelper graphHelper = GraphHelper.Instance();
3028
/// <summary>
@@ -210,15 +208,6 @@ public VideoPlayerPageViewModel(INavigationService navigationService)
210208
SeekForewardCommand = new RelayCommand<double>(SeekForeward);
211209
PlayPreviousVideoCommand = new RelayCommand(PlayPreviousVideo, CanExecuteCommand);
212210
PlayNextVideoCommand = new RelayCommand(PlayNextVideo, CanExecuteCommand);
213-
214-
this.localMediaVolumeLevelSetting = ApplicationData.Current.LocalSettings;
215-
216-
// Sets the MediaVolume setting to 100 when its not already set
217-
// before in the setting. (This is part of an audio workaround).
218-
if (localMediaVolumeLevelSetting.Values["MediaVolume"] is null)
219-
{
220-
localMediaVolumeLevelSetting.Values["MediaVolume"] = 100;
221-
}
222211
}
223212

224213
private bool CanExecuteCommand()
@@ -256,14 +245,20 @@ private async void MediaPlayer_Playing(object sender, EventArgs e)
256245
Debug.WriteLine(DateTime.Now.ToString("hh:mm:ss.fff") + ": Media is playing");
257246
await App.Current.UIDispatcher.RunAsync(CoreDispatcherPriority.High, () =>
258247
{
259-
MediaVolumeLevel = (int)this.localMediaVolumeLevelSetting.Values["MediaVolume"];
260-
Debug.WriteLine(DateTime.Now.ToString("hh:mm:ss.fff") + ": Set volume in container: " + this.localMediaVolumeLevelSetting.Values["MediaVolume"]);
248+
MediaVolumeLevel = (int)App.Current.UserSettings.Values["MediaVolume"];
249+
Debug.WriteLine(DateTime.Now.ToString("hh:mm:ss.fff") + ": Set volume in container: " + App.Current.UserSettings.Values["MediaVolume"]);
261250
Debug.WriteLine(DateTime.Now.ToString("hh:mm:ss.fff") + ": Set volume in our property: " + MediaVolumeLevel);
262251
Debug.WriteLine(DateTime.Now.ToString("hh:mm:ss.fff") + ": Actual volume: " + MediaPlayer.Volume);
263252
//Sets the max value of the seekbar.
264253
VideoLength = MediaPlayer.Length;
265254

266255
PlayPauseButtonFontIcon = "\xE769";
256+
257+
//Enable or disable default subtitle based on user setting.
258+
if (!(bool) App.Current.UserSettings.Values["ShowDefaultSubtitles"])
259+
{
260+
MediaPlayer.SetSpu(-1);
261+
}
267262
});
268263
}
269264

@@ -313,7 +308,9 @@ await App.Current.UIDispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
313308
/// <returns></returns>
314309
private async Task<string> RetrieveDownloadURLMedia(MediaWrapper mediaWrapper)
315310
{
316-
var driveItem = await graphHelper.GetItemInformationAsync(mediaWrapper.DriveId, mediaWrapper.CachedDriveItem.ItemId);
311+
var driveItem = await GraphHelper.Instance().GetItemInformationAsync(
312+
mediaWrapper.DriveId,
313+
mediaWrapper.CachedDriveItem.ItemId);
317314

318315
//Retrieve a temporary download URL from the drive item.
319316
return (string)driveItem.AdditionalData["@microsoft.graph.downloadUrl"];
@@ -368,7 +365,7 @@ private void SetMediaVolume(int volumeLevel)
368365
Debug.WriteLine("Error: Could not set the volume.");
369366
return; // Return when the MediaPlayer is null so it does not cause exception.
370367
}
371-
this.localMediaVolumeLevelSetting.Values["MediaVolume"] = volumeLevel; // Set the new volume in the MediaVolume setting.
368+
App.Current.UserSettings.Values["MediaVolume"] = volumeLevel; // Set the new volume in the MediaVolume setting.
372369
MediaPlayer.Volume = volumeLevel;
373370
UpdateVolumeButtonFontIcon(volumeLevel);
374371
}

OneDrive-Cloud-Player/ViewModels/ViewModelLocator.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
using GalaSoft.MvvmLight;
33
using GalaSoft.MvvmLight.Ioc;
44
using GalaSoft.MvvmLight.Views;
5-
using OneDrive_Cloud_Player.Models.GraphData;
65
using OneDrive_Cloud_Player.Views;
7-
using Windows.UI.Xaml;
86

97
namespace OneDrive_Cloud_Player.ViewModels
108
{
@@ -18,6 +16,7 @@ public class ViewModelLocator
1816
public const string MainPageKey = "MainPage";
1917
public const string VideoPlayerPageKey = "VideoPlayerPage";
2018
public const string LoginPageKey = "LoginPage";
19+
public const string SettingsPageKey = "SettingsPage";
2120

2221
/// <summary>
2322
/// Initializes a new instance of the ViewModelLocator class.
@@ -31,6 +30,7 @@ public ViewModelLocator()
3130
nav.Configure(MainPageKey, typeof(MainPage));
3231
nav.Configure(VideoPlayerPageKey, typeof(VideoPlayerPage));
3332
nav.Configure(LoginPageKey, typeof(LoginPage));
33+
nav.Configure(SettingsPageKey, typeof(SettingsPage));
3434

3535
if (ViewModelBase.IsInDesignModeStatic)
3636
{
@@ -47,6 +47,7 @@ public ViewModelLocator()
4747
SimpleIoc.Default.Register<VideoPlayerPageViewModel>();
4848
SimpleIoc.Default.Register<LoginPageViewModel>();
4949
SimpleIoc.Default.Register<MainPageViewModel>();
50+
SimpleIoc.Default.Register<SettingsPageViewModel>();
5051

5152
}
5253

@@ -100,6 +101,19 @@ public MainPageViewModel MainPageInstance
100101
return ServiceLocator.Current.GetInstance<MainPageViewModel>();
101102
}
102103
}
104+
// <summary>
105+
// Gets the SettingsPage view model.
106+
// </summary>
107+
// <value>
108+
// The SettingsPage view model.
109+
// </value>
110+
public SettingsPageViewModel SettingsPageInstance
111+
{
112+
get
113+
{
114+
return ServiceLocator.Current.GetInstance<SettingsPageViewModel>();
115+
}
116+
}
103117

104118
// <summary>
105119
// The cleanup.

0 commit comments

Comments
 (0)