Skip to content

Commit

Permalink
Save preferences in seperate file
Browse files Browse the repository at this point in the history
Get them out of Properties.Settings.Default, that way a future cross patform version can still read the old preferences
  • Loading branch information
DSPaul committed May 1, 2024
1 parent 59ac05e commit a06d9b3
Show file tree
Hide file tree
Showing 40 changed files with 856 additions and 537 deletions.
10 changes: 6 additions & 4 deletions Tests/UnitTests/Serialization.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using COMPASS.Models;
using COMPASS.Services;
using COMPASS.Tools;
using Ionic.Zip;
using SharpCompress.Archives;
using SharpCompress.Archives.Zip;
using SharpCompress.Common;
using System.Text.Json;

namespace Tests.UnitTests
Expand Down Expand Up @@ -65,12 +67,12 @@ public async Task CheckSatchelInfoVersion()
MinTagsVersion = "1.0.0",
};

var zip = new ZipFile();
using var zip = ZipArchive.Create();
zip.AddEntry(Constants.SatchelInfoFileName, JsonSerializer.Serialize(info));
zip.AddEntry(Constants.TagsFileName, "pseudo data");

var path = Path.GetTempPath() + Guid.NewGuid().ToString() + Constants.SatchelExtension;
zip.Save(path);
zip.SaveTo(path, CompressionType.None);

//Because satchel does not contain a codexInfo file, should work
var collection = await IOService.OpenSatchel(path);
Expand All @@ -79,7 +81,7 @@ public async Task CheckSatchelInfoVersion()

//Now add a codex file
zip.AddEntry(Constants.CodicesFileName, "Not important");
zip.Save(path);
zip.SaveTo(path, CompressionType.None);

//Now that the codex file is added, should be null
collection = await IOService.OpenSatchel(path);
Expand Down
13 changes: 8 additions & 5 deletions src/Models/CodexCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ public class CodexCollection : ObservableObject
public CodexCollection(string collectionDirectory)
{
_directoryName = collectionDirectory;
_preferencesService = PreferencesService.GetInstance();
}

private PreferencesService _preferencesService;

public static string CollectionsPath => Path.Combine(SettingsViewModel.CompassDataPath, "Collections");
public string FullDataPath => Path.Combine(CollectionsPath, DirectoryName);
public string CodicesDataFilePath => Path.Combine(FullDataPath, Constants.CodicesFileName);
Expand Down Expand Up @@ -90,7 +93,7 @@ public int Load(bool MakeStartupCollection = true)
if (!loadedInfo) { result -= 4; }
if (MakeStartupCollection)
{
Properties.Settings.Default.StartupCollection = DirectoryName;
_preferencesService.Preferences.UIState.StartupCollection = DirectoryName;
Logger.Info($"Loaded {DirectoryName}");
}
return result;
Expand Down Expand Up @@ -252,7 +255,7 @@ public void Save()
bool savedTags = SaveTags();
bool savedCodices = SaveCodices();
bool savedInfo = SaveInfo();
Properties.Settings.Default.Save();
_preferencesService.SavePreferences();

if (savedCodices || savedTags || savedInfo)
{
Expand All @@ -269,7 +272,7 @@ public bool SaveTags()
}
try
{
using var writer = XmlWriter.Create(TagsDataFilePath, SettingsViewModel.XmlWriteSettings);
using var writer = XmlWriter.Create(TagsDataFilePath, XmlService.XmlWriteSettings);
XmlSerializer serializer = new(typeof(List<Tag>));
serializer.Serialize(writer, RootTags);
}
Expand All @@ -296,7 +299,7 @@ public bool SaveCodices()

try
{
using var writer = XmlWriter.Create(CodicesDataFilePath, SettingsViewModel.XmlWriteSettings);
using var writer = XmlWriter.Create(CodicesDataFilePath, XmlService.XmlWriteSettings);
XmlSerializer serializer = new(typeof(ObservableCollection<Codex>));
serializer.Serialize(writer, AllCodices);
}
Expand All @@ -318,7 +321,7 @@ public bool SaveInfo()
Info.PrepareSave();
try
{
using var writer = XmlWriter.Create(CollectionInfoFilePath, SettingsViewModel.XmlWriteSettings);
using var writer = XmlWriter.Create(CollectionInfoFilePath, XmlService.XmlWriteSettings);
XmlSerializer serializer = new(typeof(CollectionInfo));
serializer.Serialize(writer, Info);
}
Expand Down
54 changes: 33 additions & 21 deletions src/Models/CodexProperty.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
using CommunityToolkit.Mvvm.ComponentModel;
using COMPASS.Models.XmlDtos;
using COMPASS.Tools;
using COMPASS.ViewModels.Sources;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Xml.Serialization;

namespace COMPASS.Models
{
public class CodexProperty : ObservableObject
{
//Empty ctor for serialization
public CodexProperty() { }

public CodexProperty(string propName, Func<Codex, bool> isEmpty, Action<Codex, Codex> setProp, List<MetaDataSource> defaultSources, string? label = null)
{
Name = propName;
Expand All @@ -23,25 +20,35 @@ public CodexProperty(string propName, Func<Codex, bool> isEmpty, Action<Codex, C
DefaultSourcePriority = defaultSources;
}

public CodexProperty(CodexPropertyDto dto, CodexProperty defaultProp)
{
Name = dto.Name;
SourcePriority = dto.SourcePriority;
OverwriteMode = dto.OverwriteMode;

Label = defaultProp.Label;
IsEmpty = defaultProp.IsEmpty;
SetProp = defaultProp.SetProp;
DefaultSourcePriority = defaultProp.DefaultSourcePriority;

UpdateSources();
}

public string Name { get; init; } = "";

[XmlIgnore]
public string Label { get; init; } = "";

private Func<Codex, bool>? _isEmpty;
[XmlIgnore]
public Func<Codex, bool> IsEmpty
{
get => _isEmpty ??= Codex.Properties.First(prop => prop.Name == Name).IsEmpty;
private init => _isEmpty = value;
}

private Func<Codex, object?>? _getProp;
[XmlIgnore]
public Func<Codex, object?> GetProp => _getProp ??= codex => codex.GetPropertyValue(Name);

private Action<Codex, Codex>? _setProp;
[XmlIgnore]
public Action<Codex, Codex> SetProp
{
get => _setProp ??= Codex.Properties.First(prop => prop.Name == Name).SetProp;
Expand All @@ -60,8 +67,6 @@ private List<MetaDataSource> DefaultSourcePriority
/// <summary>
/// Ordered List of sources that can set this prop, named for data binding
/// </summary>
[XmlIgnore]

public ObservableCollection<NamedMetaDataSource> SourcePriorityNamed
{
get => _sourcePriorityNamed ??= new(SourcePriority.Select(source => new NamedMetaDataSource(source)));
Expand All @@ -74,18 +79,16 @@ public ObservableCollection<NamedMetaDataSource> SourcePriorityNamed
public List<MetaDataSource> SourcePriority { get; set; } = new();

private MetaDataOverwriteMode _overwriteMode = MetaDataOverwriteMode.IfEmpty;
public MetaDataOverwriteMode? OverwriteMode
public MetaDataOverwriteMode OverwriteMode
{
get => _overwriteMode;
set
{
if (value is not null)
{
SetProperty(ref _overwriteMode, (MetaDataOverwriteMode)value);
}
}
set => SetProperty(ref _overwriteMode, value);
}

#endregion

#region Mapping

public void UpdateSources()
{
// If a new possible source was not found in the save, add it
Expand All @@ -99,9 +102,18 @@ public void UpdateSources()
SourcePriority.RemoveAll(source => !DefaultSourcePriority.Contains(source));
}

public void PrepareForSave() =>
//Use order from NameMetaDataSources (which was reordered by user)
SourcePriority = SourcePriorityNamed.Select(namedSource => namedSource.Source).ToList();
public CodexPropertyDto ToDto()
{
CodexPropertyDto dto = new()
{
Name = Name,
OverwriteMode = OverwriteMode,
//Use order from NameMetaDataSources (which was reordered by user)
SourcePriority = SourcePriorityNamed.Select(namedSource => namedSource.Source).ToList(),
};

return dto;
}

#endregion
}
Expand Down
49 changes: 0 additions & 49 deletions src/Models/GlobalPreferences.cs

This file was deleted.

84 changes: 84 additions & 0 deletions src/Models/Preferences/CardLayoutPreferences.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using CommunityToolkit.Mvvm.ComponentModel;

namespace COMPASS.Models.Preferences
{
public class CardLayoutPreferences : ObservableObject
{
private bool _showTitle = true;
public bool ShowTitle
{
get => _showTitle;
set => SetProperty(ref _showTitle, value);
}

public bool ShowAuthor
{
get => Properties.Settings.Default.CardShowAuthor;
set
{
Properties.Settings.Default.CardShowAuthor = value;
OnPropertyChanged();
}
}

public bool ShowPublisher
{
get => Properties.Settings.Default.CardShowPublisher;
set
{
Properties.Settings.Default.CardShowPublisher = value;
OnPropertyChanged();
}
}

public bool ShowReleaseDate
{
get => Properties.Settings.Default.CardShowRelease;
set
{
Properties.Settings.Default.CardShowRelease = value;
OnPropertyChanged();
}
}

public bool ShowVersion
{
get => Properties.Settings.Default.CardShowVersion;
set
{
Properties.Settings.Default.CardShowVersion = value;
OnPropertyChanged();
}
}

public bool ShowRating
{
get => Properties.Settings.Default.CardShowRating;
set
{
Properties.Settings.Default.CardShowRating = value;
OnPropertyChanged();
}
}

public bool ShowTags
{
get => Properties.Settings.Default.CardShowTags;
set
{
Properties.Settings.Default.CardShowTags = value;
OnPropertyChanged();
}
}

public bool ShowFileIcons
{
get => Properties.Settings.Default.CardShowFileIcons;
set
{
Properties.Settings.Default.CardShowFileIcons = value;
OnPropertyChanged();
}
}
}
}
30 changes: 30 additions & 0 deletions src/Models/Preferences/HomeLayoutPreferences.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using CommunityToolkit.Mvvm.ComponentModel;

namespace COMPASS.Models.Preferences
{
public class HomeLayoutPreferences : ObservableObject
{
public double TileWidth
{
get => Properties.Settings.Default.HomeCoverSize;
set
{
Properties.Settings.Default.HomeCoverSize = value;
OnPropertyChanged();
OnPropertyChanged(nameof(TileHeight));
}
}

public double TileHeight => (int)(TileWidth * 4 / 3);

public bool ShowTitle
{
get => Properties.Settings.Default.HomeShowTitle;
set
{
Properties.Settings.Default.HomeShowTitle = value;
OnPropertyChanged();
}
}
}
}
Loading

0 comments on commit a06d9b3

Please sign in to comment.