📄 src/Activities/NoteResultIntent.cs
using System.Globalization;
using Android.Content;
using NotesApp.Models;

namespace NotesApp.Activities;

// The extras contract shared by NoteListActivity <-> NoteViewActivity: launching an existing
// note passes its current cached fields in, and closing (whether or not anything was edited)
// passes the freshest known fields back out, so the list can patch its cache without a
// network round trip either way.
internal static class NoteResultIntent
{
    public static void Put(Intent intent, NoteCacheEntry entry)
    {
        intent.PutExtra(NoteViewActivity.ExtraNoteId, entry.Id);
        intent.PutExtra(NoteViewActivity.ExtraNoteName, entry.Name);
        intent.PutExtra(NoteViewActivity.ExtraNotePreview, entry.Preview);
        intent.PutExtra(NoteViewActivity.ExtraNoteETag, entry.ETag);
        intent.PutExtra(
            NoteViewActivity.ExtraNoteLastModifiedUtc,
            entry.LastModifiedUtc?.ToString("O") ?? ""
        );
        intent.PutExtra(NoteViewActivity.ExtraNoteSize, entry.Size ?? -1L);
    }

    public static NoteCacheEntry? TryGet(Intent? intent)
    {
        var id = intent?.GetStringExtra(NoteViewActivity.ExtraNoteId);
        var name = intent?.GetStringExtra(NoteViewActivity.ExtraNoteName);
        if (id is null || name is null)
            return null;

        var preview = intent?.GetStringExtra(NoteViewActivity.ExtraNotePreview) ?? "";
        var eTag = intent?.GetStringExtra(NoteViewActivity.ExtraNoteETag) ?? "";
        var lastModifiedRaw = intent?.GetStringExtra(NoteViewActivity.ExtraNoteLastModifiedUtc);
        var lastModified = string.IsNullOrEmpty(lastModifiedRaw)
            ? (DateTimeOffset?)null
            : DateTimeOffset.Parse(lastModifiedRaw, null, DateTimeStyles.RoundtripKind);
        var sizeRaw = intent?.GetLongExtra(NoteViewActivity.ExtraNoteSize, -1) ?? -1;
        var size = sizeRaw < 0 ? (long?)null : sizeRaw;

        return new NoteCacheEntry(id, name, eTag, lastModified, preview, size);
    }
}