104 lines
3.4 KiB
C#
104 lines
3.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using Billing.Models;
|
|
using Billing.UI;
|
|
using Xamarin.Essentials;
|
|
|
|
namespace Billing.Store
|
|
{
|
|
public class StoreHelper
|
|
{
|
|
public static readonly string PersonalFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
|
|
public static readonly string CacheFolder = FileSystem.CacheDirectory;
|
|
|
|
private const string accountFile = "accounts.xml";
|
|
private const string billFile = "bills.xml";
|
|
private const string categoryFile = "categories.xml";
|
|
|
|
private static StoreHelper instance;
|
|
private static StoreHelper Instance => instance ??= new StoreHelper();
|
|
|
|
public static List<Account> GetAccounts() => Instance.GetAccountsInternal();
|
|
public static void WriteAccounts(IEnumerable<Account> accounts) => Instance.WriteAccountsInternal(accounts);
|
|
public static List<Bill> GetBills() => Instance.GetBillsInternal();
|
|
public static void WriteBills(IEnumerable<Bill> bills) => Instance.WriteBillsInternal(bills);
|
|
public static List<Category> GetCategories() => Instance.GetCategoriesInternal();
|
|
public static void WriteCategories(IEnumerable<Category> categories) => Instance.WriteCategoriesInternal(categories);
|
|
|
|
private StoreHelper() { }
|
|
|
|
private List<Account> GetAccountsInternal()
|
|
{
|
|
return GetList<Account>(Path.Combine(PersonalFolder, accountFile));
|
|
}
|
|
|
|
private void WriteAccountsInternal(IEnumerable<Account> accounts)
|
|
{
|
|
var filename = Path.Combine(PersonalFolder, accountFile);
|
|
WriteList(filename, accounts);
|
|
}
|
|
|
|
private List<Bill> GetBillsInternal()
|
|
{
|
|
return GetList<Bill>(Path.Combine(PersonalFolder, billFile));
|
|
}
|
|
|
|
private void WriteBillsInternal(IEnumerable<Bill> bills)
|
|
{
|
|
var filename = Path.Combine(PersonalFolder, billFile);
|
|
WriteList(filename, bills);
|
|
}
|
|
|
|
private List<Category> GetCategoriesInternal()
|
|
{
|
|
return GetList<Category>(Path.Combine(PersonalFolder, categoryFile));
|
|
}
|
|
|
|
private void WriteCategoriesInternal(IEnumerable<Category> categories)
|
|
{
|
|
var filename = Path.Combine(PersonalFolder, categoryFile);
|
|
WriteList(filename, categories);
|
|
}
|
|
|
|
#region Helper
|
|
|
|
private void WriteList<T>(string filename, IEnumerable<T> list) where T : IModel, new()
|
|
{
|
|
if (list == null)
|
|
{
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
using var stream = File.OpenWrite(filename);
|
|
list.ToStream(stream);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Helper.Error("file.write", $"failed to write file: {filename}, error: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private List<T> GetList<T>(string file) where T : IModel, new()
|
|
{
|
|
try
|
|
{
|
|
if (File.Exists(file))
|
|
{
|
|
using var stream = File.OpenRead(file);
|
|
var list = ModelExtensionHelper.FromStream<T>(stream);
|
|
return list;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Helper.Error("file.read", $"failed to read file: {file}, error: {ex.Message}");
|
|
}
|
|
return default;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|