feature: favorite illusts

This commit is contained in:
2020-05-07 15:19:24 +08:00
parent 84aecdf39b
commit 8cf9ae288b
19 changed files with 446 additions and 94 deletions

View File

@@ -5,6 +5,7 @@ using System.Net;
using System.Net.Http;
using System.Text;
using Newtonsoft.Json;
using Pixiview.Illust;
using Xamarin.Essentials;
using Xamarin.Forms;
@@ -14,12 +15,17 @@ namespace Pixiview.Utils
{
public static readonly string PersonalFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
public static readonly string CacheFolder = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
private const string pagesFolder = "pages";
private const string imageFolder = "img-original";
private const string previewFolder = "img-master";
private const string thumbFolder = "img-thumb";
private const string userFolder = "user-profile";
private const string illustFile = "illust.json";
private const string favoriteFile = "favorites.json";
private static readonly object sync = new object();
public static bool NetworkAvailable
{
@@ -87,6 +93,64 @@ namespace Pixiview.Utils
}
}
private static T ReadObject<T>(string file)
{
string content = null;
if (File.Exists(file))
{
try
{
content = File.ReadAllText(file);
}
catch (Exception ex)
{
App.DebugError("read", $"failed to read file: {file}, error: {ex.Message}");
}
}
else
{
App.DebugError("read", $"file not found: {file}");
return default;
}
try
{
return JsonConvert.DeserializeObject<T>(content);
}
catch (Exception ex)
{
App.DebugError("read", $"failed to parse illust JSON object, error: {ex.Message}");
return default;
}
}
private static void WriteObject(string file, object obj)
{
var dir = Path.GetDirectoryName(file);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
string content;
try
{
content = JsonConvert.SerializeObject(obj, Formatting.None);
}
catch (Exception ex)
{
App.DebugError("write", $"failed to serialize object, error: {ex.Message}");
return;
}
try
{
File.WriteAllText(file, content, Encoding.UTF8);
}
catch (Exception ex)
{
App.DebugError("write", $"failed to write file: {file}, error: {ex.Message}");
}
}
public static IllustData LoadIllustData(bool force = false)
{
var file = Path.Combine(PersonalFolder, illustFile);
@@ -118,6 +182,21 @@ namespace Pixiview.Utils
return result;
}
public static IllustFavorite LoadFavoritesIllusts()
{
var file = Path.Combine(PersonalFolder, favoriteFile);
return ReadObject<IllustFavorite>(file);
}
public static void SaveFavoritesIllusts(IllustFavorite data)
{
var file = Path.Combine(PersonalFolder, favoriteFile);
lock (sync)
{
WriteObject(file, data);
}
}
public static ImageSource LoadIllustImage(string url)
{
return LoadImage(url, PersonalFolder, imageFolder);
@@ -306,6 +385,7 @@ namespace Pixiview.Utils
public const string Recommends = "recommends";
public const string ByUser = "byuser";
public const string Ranking = "ranking";
public const string Favorites = "favorites";
public const string Option = "option";
}
}