📄 src/Activities/SettingsActivity.cs
using Android.Content;
using AndroidX.AppCompat.App;
using NotesApp.Auth;
using NotesApp.Storage;
using NotesApp.Views;
using Toolbar = AndroidX.AppCompat.Widget.Toolbar;

namespace NotesApp.Activities;

[Activity(Label = "@string/settings_title", Exported = false)]
public class SettingsActivity : AppCompatActivity
{
    private const int ChooseFolderRequestCode = 200;

    private TextView _accountStatus = null!;
    private Button _signInButton = null!;
    private TextView _folderStatus = null!;

    protected override void OnCreate(Bundle? savedInstanceState)
    {
        AndroidX.Activity.EdgeToEdge.Enable(this);
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_settings);

        EdgeToEdgeHelper.Apply(FindViewById(Resource.Id.settings_root)!);

        SetSupportActionBar(FindViewById<Toolbar>(Resource.Id.settings_toolbar));
        // Only show Up when reached via the note list's gear icon - there's nothing to
        // go "up" to yet during first-run setup, before a folder is configured.
        SupportActionBar?.SetDisplayHomeAsUpEnabled(AppSettings.HasRootFolder);

        _accountStatus = FindViewById<TextView>(Resource.Id.account_status)!;
        _signInButton = FindViewById<Button>(Resource.Id.sign_in_button)!;
        _folderStatus = FindViewById<TextView>(Resource.Id.folder_status)!;

        _signInButton.Click += async (_, _) => await SignInAsync();
        FindViewById<Button>(Resource.Id.choose_folder_button)!.Click += (_, _) =>
            OpenFolderPicker();

        RefreshFolderStatus();
        _ = RefreshAccountStatusAsync();
    }

    public override bool OnSupportNavigateUp()
    {
        Finish();
        return true;
    }

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent? data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        AuthService.HandleActivityResult(requestCode, resultCode, data);

        if (requestCode == ChooseFolderRequestCode && resultCode == Result.Ok && data != null)
        {
            AppSettings.RootFolderId = data.GetStringExtra(FolderPickerActivity.ExtraFolderId);
            AppSettings.RootFolderPath = data.GetStringExtra(FolderPickerActivity.ExtraFolderPath);
            RefreshFolderStatus();
        }
    }

    private async Task SignInAsync()
    {
        _signInButton.Enabled = false;
        try
        {
            await AuthService.GetAccessTokenAsync(this);
            await RefreshAccountStatusAsync();
        }
        catch (Exception ex)
        {
            var message = string.Format(GetString(Resource.String.sign_in_failed), ex.Message);
            Toast.MakeText(this, message, ToastLength.Long)?.Show();
        }
        finally
        {
            _signInButton.Enabled = true;
        }
    }

    private async Task RefreshAccountStatusAsync()
    {
        var signedIn = await AuthService.IsSignedInAsync();
        _accountStatus.Text = GetString(
            signedIn ? Resource.String.signed_in : Resource.String.not_signed_in
        );
    }

    private void OpenFolderPicker() =>
        StartActivityForResult(
            new Intent(this, typeof(FolderPickerActivity)),
            ChooseFolderRequestCode
        );

    private void RefreshFolderStatus() =>
        _folderStatus.Text = AppSettings.RootFolderPath is { } path
            ? string.Format(GetString(Resource.String.folder_selected_format), path)
            : GetString(Resource.String.no_folder_selected);
}