flower-story/FlowerApp/Data/Constants.cs
2023-07-27 22:07:24 +08:00

76 lines
2.1 KiB
C#

using Microsoft.Extensions.Logging;
using SQLite;
namespace Blahblah.FlowerApp.Data;
internal sealed class Constants
{
public const string CategoryOther = "other";
public const string EventUnknown = "unknown";
public const string ApiVersionName = "api_version";
public const string LastTokenName = "last_token";
public const string BaseUrl = "https://app.blahblaho.com";
public const SQLiteOpenFlags SQLiteFlags =
SQLiteOpenFlags.ReadWrite |
SQLiteOpenFlags.Create |
SQLiteOpenFlags.SharedCache;
const string dbFilename = "flowerstory.db3";
public static string DatabasePath => Path.Combine(FileSystem.AppDataDirectory, dbFilename);
static string? apiVersion;
public static string? ApiVersion => apiVersion;
static string? authorization;
public static string? Authorization => authorization;
public static void SetAuthorization(string auth)
{
authorization = auth;
}
public static async Task<Definitions?> Initialize(ILogger logger, string? version, CancellationToken cancellation = default)
{
try
{
var v = await Extensions.FetchAsync<string>("api/version", cancellation);
apiVersion = v;
if (v != version)
{
var definition = await Extensions.FetchAsync<Definitions>($"api/consts?{v}", cancellation);
return definition;
}
}
catch (Exception ex)
{
logger.LogError("error occurs on fetching version and definitions, {error}", ex);
}
return null;
}
}
internal record Definitions
{
public required string ApiVersion { get; init; }
public required Dictionary<int, NamedItem> Categories { get; init; }
public required Dictionary<int, EventItem> Events { get; init; }
}
internal record NamedItem(string Name, string? Description)
{
public string Name { get; init; } = Name;
public string? Description { get; init; } = Description;
}
internal record EventItem(string Name, string? Description, bool Unique) : NamedItem(Name, Description)
{
public bool Unique { get; init; } = Unique;
}