mirror of
https://github.com/Ottermandias/Glamourer.git
synced 2026-02-18 13:37:44 +01:00
Add favorite option to items.
This commit is contained in:
parent
00883943cd
commit
ea4fb49c0f
11 changed files with 211 additions and 31 deletions
96
Glamourer/Unlocks/FavoriteManager.cs
Normal file
96
Glamourer/Unlocks/FavoriteManager.cs
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Dalamud.Interface.Internal.Notifications;
|
||||
using Glamourer.Services;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using OtterGui.Classes;
|
||||
using Penumbra.GameData.Structs;
|
||||
|
||||
namespace Glamourer.Unlocks;
|
||||
|
||||
public class FavoriteManager : ISavable
|
||||
{
|
||||
private readonly SaveService _saveService;
|
||||
private readonly HashSet<ItemId> _favorites = new();
|
||||
|
||||
public FavoriteManager(SaveService saveService)
|
||||
{
|
||||
_saveService = saveService;
|
||||
Load();
|
||||
}
|
||||
|
||||
private void Load()
|
||||
{
|
||||
var file = _saveService.FileNames.FavoriteFile;
|
||||
if (!File.Exists(file))
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
var text = File.ReadAllText(file);
|
||||
var array = JsonConvert.DeserializeObject<uint[]>(text) ?? Array.Empty<uint>();
|
||||
_favorites.UnionWith(array.Select(i => (ItemId)i));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Glamourer.Messager.NotificationMessage(ex, "Could not read Favorite file.", NotificationType.Error);
|
||||
}
|
||||
}
|
||||
|
||||
public string ToFilename(FilenameService fileNames)
|
||||
=> fileNames.FavoriteFile;
|
||||
|
||||
private void Save()
|
||||
=> _saveService.DelaySave(this);
|
||||
|
||||
public void Save(StreamWriter writer)
|
||||
{
|
||||
using var j = new JsonTextWriter(writer)
|
||||
{
|
||||
Formatting = Formatting.Indented,
|
||||
};
|
||||
j.WriteStartArray();
|
||||
foreach (var item in _favorites)
|
||||
j.WriteValue(item.Id);
|
||||
j.WriteEndArray();
|
||||
}
|
||||
|
||||
public bool TryAdd(EquipItem item)
|
||||
=> TryAdd(item.ItemId);
|
||||
|
||||
public bool TryAdd(ItemId item)
|
||||
{
|
||||
if (item.Id == 0 || !_favorites.Add(item))
|
||||
return false;
|
||||
|
||||
Save();
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Remove(EquipItem item)
|
||||
=> Remove(item.ItemId);
|
||||
|
||||
public bool Remove(ItemId item)
|
||||
{
|
||||
if (!_favorites.Remove(item))
|
||||
return false;
|
||||
|
||||
Save();
|
||||
return true;
|
||||
}
|
||||
|
||||
public IEnumerator<ItemId> GetEnumerator()
|
||||
=> _favorites.GetEnumerator();
|
||||
|
||||
public int Count
|
||||
=> _favorites.Count;
|
||||
|
||||
public bool Contains(EquipItem item)
|
||||
=> _favorites.Contains(item.ItemId);
|
||||
|
||||
public bool Contains(ItemId item)
|
||||
=> _favorites.Contains(item);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue