Skip to content

Commit

Permalink
Launcher Preferences are now saved and loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
TrinityDevelopers committed Apr 10, 2020
1 parent 0c8fa39 commit a3c22ff
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 17 deletions.
2 changes: 2 additions & 0 deletions ZenovaLauncher/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public void AppStart(object sender, StartupEventArgs e)
SetupEnvironment();
VersionManager.instance = new VersionManager(Path.Combine(VersionsDirectory, "versions.json"));
ProfileManager.instance = new ProfileManager(ProfilesDirectory);
Preferences.LoadPreferences(DataDirectory);
Dispatcher.Invoke(async () =>
{
await VersionManager.instance.LoadMinecraftVersions();
Expand All @@ -38,6 +39,7 @@ public void AppStart(object sender, StartupEventArgs e)
public void AppExit(object sender, ExitEventArgs e)
{
ProfileManager.instance.SaveProfiles();
Preferences.SavePreferences();
}

private void SetupEnvironment()
Expand Down
20 changes: 20 additions & 0 deletions ZenovaLauncher/NotifyPropertyChangedBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZenovaLauncher
{
public class NotifyPropertyChangedBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
8 changes: 4 additions & 4 deletions ZenovaLauncher/Pages/ProfilesPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<ComboBox
x:Name="SortProfileBox"
Style="{DynamicResource StandardComboBoxStyle}"
SelectedIndex="0"
SelectedIndex="{Binding Path=ProfileSortingId}"
MinWidth="120"
HorizontalContentAlignment="Left"
Background="Transparent"
Expand All @@ -58,9 +58,9 @@
<ui:SimpleStackPanel Margin="15,0,0,0">
<TextBlock Margin="0,-8,0,0" FontSize="15" FontWeight="SemiBold" Typography.Capitals="AllSmallCaps" Text="Versions" />
<ui:SimpleStackPanel Orientation="Horizontal" Spacing="13">
<CheckBox x:Name="ReleasesBox" Style="{DynamicResource SmallCheckBoxStyle}" Content="Releases" Click="CheckBoxClick" IsChecked="True" />
<CheckBox x:Name="BetasBox" Style="{DynamicResource SmallCheckBoxStyle}" Content="Beta Builds" Click="CheckBoxClick" />
<CheckBox x:Name="HistoricalBox" Style="{DynamicResource SmallCheckBoxStyle}" Content="Historical" Click="CheckBoxClick" />
<CheckBox x:Name="ReleasesBox" Style="{DynamicResource SmallCheckBoxStyle}" Content="Releases" Click="CheckBoxClick" IsChecked="{Binding Path=EnableReleases}" />
<CheckBox x:Name="BetasBox" Style="{DynamicResource SmallCheckBoxStyle}" Content="Beta Builds" Click="CheckBoxClick" IsChecked="{Binding Path=EnableBetas}" />
<CheckBox x:Name="HistoricalBox" Style="{DynamicResource SmallCheckBoxStyle}" Content="Historical" Click="CheckBoxClick" IsChecked="{Binding Path=EnableHistorical}" />
</ui:SimpleStackPanel>
</ui:SimpleStackPanel>
</ui:SimpleStackPanel>
Expand Down
22 changes: 13 additions & 9 deletions ZenovaLauncher/Pages/ProfilesPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ public partial class ProfilesPage : Page
public ProfilesPage() {
InitializeComponent();
ProfileListBox.ItemsSource = ProfileManager.instance;
SortProfileBox.DataContext = Preferences.instance;
ReleasesBox.DataContext = Preferences.instance;
BetasBox.DataContext = Preferences.instance;
HistoricalBox.DataContext = Preferences.instance;

SortProfileList(true);
SortProfileList(Preferences.instance.ProfileSorting);
FilterProfileList();
}

Expand Down Expand Up @@ -56,7 +60,7 @@ private void DuplicateProfileClick(object sender, RoutedEventArgs e)
index++;
newProfile.Name += " (" + index + ")";
ProfileManager.instance.Add(newProfile);
SortProfileList(SortProfileBox.SelectedIndex == 0);
SortProfileList(Preferences.instance.ProfileSorting);
}

private void DeleteProfileClick(object sender, RoutedEventArgs e)
Expand All @@ -72,7 +76,7 @@ private void ProfileSelected(object sender, MouseButtonEventArgs e)

private void SortChanged(object sender, SelectionChangedEventArgs e)
{
SortProfileList((sender as ComboBox).SelectedIndex == 0);
SortProfileList(Preferences.instance.ProfileSorting);
}

private void CheckBoxClick(object sender, RoutedEventArgs e)
Expand All @@ -83,20 +87,20 @@ private void CheckBoxClick(object sender, RoutedEventArgs e)
protected void FilterProfileList()
{
List<Predicate<object>> predicates = new List<Predicate<object>>();
if (ReleasesBox.IsChecked == true)
if (Preferences.instance.EnableReleases)
predicates.Add(Profile.releaseFilter);
if (BetasBox.IsChecked == true)
if (Preferences.instance.EnableBetas)
predicates.Add(Profile.betaFilter);
if (HistoricalBox.IsChecked == true)
if (Preferences.instance.EnableHistorical)
predicates.Add(Profile.historicalFilter);
if(ProfileListBox != null)
ProfileListBox.Items.Filter = o => predicates.Any(predicate => predicate(o));
}

protected void SortProfileList(bool sortType)
protected void SortProfileList(Profile.ProfileSortType sortType)
{
string sortTypeString = sortType ? "LastPlayed" : "Name";
ListSortDirection sortDirection = sortType ? ListSortDirection.Descending : ListSortDirection.Ascending;
string sortTypeString = sortType == Profile.ProfileSortType.ByLastPlayed ? "LastPlayed" : "Name";
ListSortDirection sortDirection = sortType == Profile.ProfileSortType.ByLastPlayed ? ListSortDirection.Descending : ListSortDirection.Ascending;
if (ProfileListBox != null)
{
ProfileListBox.Items.SortDescriptions.Clear();
Expand Down
61 changes: 61 additions & 0 deletions ZenovaLauncher/Preferences.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZenovaLauncher
{
public class Preferences : NotifyPropertyChangedBase
{
public static Preferences instance;

private static string _preferencesFile = "preferences.json";
private static JsonSerializerSettings camelCaseSerialization;

public bool EnableReleases { get; set; }
public bool EnableBetas { get; set; }
public bool EnableHistorical { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public Profile.ProfileSortType ProfileSorting { get; set; }
[JsonIgnore]
public int ProfileSortingId
{
get { return (int)ProfileSorting; }
set { ProfileSorting = (Profile.ProfileSortType)value; }
}


public static void LoadPreferences(string dataDir)
{
_preferencesFile = Path.Combine(dataDir, _preferencesFile);
camelCaseSerialization = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
if (File.Exists(_preferencesFile))
{
instance = JsonConvert.DeserializeObject<Preferences>(File.ReadAllText(_preferencesFile), camelCaseSerialization);
}
else
{
instance = new Preferences();
instance.SetDefaultPreferences();
}
}

public static void SavePreferences()
{
File.WriteAllText(_preferencesFile, JsonConvert.SerializeObject(instance, Formatting.Indented, camelCaseSerialization));
}

public void SetDefaultPreferences()
{
EnableReleases = true;
EnableBetas = false;
EnableHistorical = false;
ProfileSorting = Profile.ProfileSortType.ByLastPlayed;
}
}
}
8 changes: 8 additions & 0 deletions ZenovaLauncher/Profiles/Profile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace ZenovaLauncher
{
Expand Down Expand Up @@ -61,5 +63,11 @@ public enum ProfileType
LatestRelease,
LatestBeta
}

public enum ProfileSortType
{
ByLastPlayed,
ByName
}
}
}
2 changes: 1 addition & 1 deletion ZenovaLauncher/Profiles/ProfileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void SaveProfiles()
DirectoryInfo di = new DirectoryInfo(_profilesDir);
foreach (FileInfo file in di.EnumerateFiles()) file.Delete();
foreach (DirectoryInfo dir in di.EnumerateDirectories()) dir.Delete(true);
File.WriteAllText(Path.Combine(_profilesDir, _profilesFile), JsonConvert.SerializeObject(this));
File.WriteAllText(Path.Combine(_profilesDir, _profilesFile), JsonConvert.SerializeObject(this, Formatting.Indented));
}
}
}
11 changes: 8 additions & 3 deletions ZenovaLauncher/ZenovaLauncher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<Compile Include="Dialogs\AddProfileDialog.xaml.cs">
<DependentUpon>AddProfileDialog.xaml</DependentUpon>
</Compile>
<Compile Include="NotifyPropertyChangedBase.cs" />
<Compile Include="Pages\ModsPage.xaml.cs">
<DependentUpon>ModsPage.xaml</DependentUpon>
</Compile>
Expand All @@ -81,6 +82,7 @@
<Compile Include="Pages\ProfilesPage.xaml.cs">
<DependentUpon>ProfilesPage.xaml</DependentUpon>
</Compile>
<Compile Include="Preferences.cs" />
<Compile Include="Profiles\MinecraftVersion.cs" />
<Compile Include="Profiles\Profile.cs" />
<Compile Include="Pages\SettingsPage.xaml.cs">
Expand Down Expand Up @@ -239,9 +241,12 @@
<PostBuildEvent>$(ProjectDir)Inno\ISCC.exe $(ProjectDir)SetupZenovaLauncher.iss</PostBuildEvent>
</PropertyGroup>
<Target Name="SpicNSpan" AfterTargets="Clean">
<RemoveDir Directories="$(TargetDir)" /> <!-- bin -->
<RemoveDir Directories="$(SolutionDir).vs" /> <!-- .vs -->
<RemoveDir Directories="$(ProjectDir)$(BaseIntermediateOutputPath)" /> <!-- obj -->
<RemoveDir Directories="$(TargetDir)" />
<!-- bin -->
<RemoveDir Directories="$(SolutionDir).vs" />
<!-- .vs -->
<RemoveDir Directories="$(ProjectDir)$(BaseIntermediateOutputPath)" />
<!-- obj -->
<RemoveDir Directories="$(ProjectDir)Output" />
</Target>
</Project>

0 comments on commit a3c22ff

Please sign in to comment.