📄 src/Storage/NoteCacheStore.cs
using System.Text.Json;
using NotesApp.Models;

namespace NotesApp.Storage;

public static class NoteCacheStore
{
    private const string FileName = "notes_cache.json";

    private static string FilePath =>
        Path.Combine(Application.Context.FilesDir!.AbsolutePath, FileName);

    public static List<NoteCacheEntry> Load()
    {
        try
        {
            if (!File.Exists(FilePath))
                return [];

            using var stream = File.OpenRead(FilePath);
            return JsonSerializer.Deserialize(
                    stream,
                    NoteCacheJsonContext.Default.ListNoteCacheEntry
                ) ?? [];
        }
        catch
        {
            // Corrupt or unreadable cache; treat as empty and let the next refresh rebuild it.
            return [];
        }
    }

    public static void Save(List<NoteCacheEntry> entries)
    {
        using var stream = File.Create(FilePath);
        JsonSerializer.Serialize(stream, entries, NoteCacheJsonContext.Default.ListNoteCacheEntry);
    }
}