Nostro/interfaces/Nostro.App-Forms.cppm

175 lines
7.1 KiB
Text
Raw Normal View History

feat(app): add the Nostro app, a window for phones and desktops Linux phones have no banking apps, and a command line is no answer on a phone. The app does everything the nostro command does and, like it, only talks to nostrod: it holds no key and no token. - Nostro.Ui, a toolkit whose screens are functions that fill a frame, ported from imsd-messages ca5cd72d; Nostro.App, the app's state, forms and screens; Nostro.Ui.Shell, which puts them on a Wayland window with Crafter.Graphics; nostro-app; and Nostro.DebugCommands for the --debug-api build. - Client: every method also in an asynchronous form for an sd-event loop, with DaemonMissing and OutcomeUnknown. - nostrod's Flatpak is net.catcrafts.Nostrod now and the app takes net.catcrafts.Nostro; install.sh moves the state. The app's Flatpak, both Flatpaks cross-built for aarch64, and the licence texts they carry. - nostro-fake-system, a made-up bus and bank with a real nostrod on them, to look at the app against. The README's screenshots are taken on it. - The nostro command: an API key's environment is the key's own to say, so --sandbox is for bunq-oauth alone, and a virtual card is listed as VIRTUAL. The published contract changes in three places, before any release (design rule 10): - Transactions' query takes `stored`: answered from what nostrod has stored, without asking the bank. The app searches with it; before, every search was a call to bunq. - AddBunq's `environment` no longer chooses over what the key says of itself: a key of the other environment is refused. - SetCardStatus refuses to freeze or unfreeze a virtual card. bunq reports one as DEACTIVATED however well it pays, and leaves it as it is when asked, without an error. The app is GPL-3.0-only, the library stays LGPL-3.0-only, and no code moved from one to the other (design rule 13). Transaction's schema is unchanged. Verified: the 17 tests pass with every line, function and region of what they link run, and the five lint runs are clean. Every screen was looked at on the made-up system, at a phone's and a desktop's sizes in the light and the dark look, on Plasma under Wayland. What is said of bunq's cards comes from its sandbox, with throwaway users, and from one real virtual card. Known to have been used against production: connecting, and reading accounts, payments and cards. Not known to have been tried: a payment from the app against bunq, OAuth against bunq, and anything on a phone. Building the app takes --graphics=, a Crafter.Graphics checkout on its branch claude/nostro-app, until that branch is merged and pinned here. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 12:27:14 +01:00
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
export module Nostro.App:Forms;
import std;
import Nostro;
import Nostro.Ui;
// What the user fills in, and what it is read as. A form keeps its fields as they were typed and is checked as a
// whole: each field's error is a line for under that field, and a form without errors gives an order, the values
// nostrod is asked with. Nothing here talks to nostrod; nostrod checks everything again. The forms a payment, a
// request and a filter are filled in on are any bank's; what a bank asks for to be connected is its own, and so is
// the form for it, named for its bank.
export namespace Nostro::App {
// What a field's text was read as, or why it could not be.
template<typename T>
struct Read {
std::optional<T> value;
std::string error;
};
// An amount as a person writes it, "12,50", "12.50" or "12", in the currency's smallest unit; above zero.
Read<std::int64_t> ReadAmount(std::string_view text, Currency currency);
// Who is paid or asked: an IBAN, an e-mail address or a phone number starting with +.
Read<BunqCounterparty> ReadParty(std::string_view text);
// What kind of address that is, in words, for beside the field while it is typed; empty for none yet.
std::string PartyKind(std::string_view text);
// A day "YYYY-MM-DD" in local time, with a time "hh:mm" on it when one is given; the day's last moment when
// `end` and no time is given.
Read<std::chrono::system_clock::time_point> ReadDay(std::string_view day, std::string_view time = {}, bool end = false);
// A whole number above zero.
Read<std::uint32_t> ReadCount(std::string_view text);
// How often a scheduled payment runs, as the form offers it and as nostrod is told it.
struct ScheduleUnit {
std::string_view label;
std::string_view unit;
};
inline constexpr std::array<ScheduleUnit, 6> scheduleUnits = {{
{"Once", "ONCE"}, {"Hourly", "HOURLY"}, {"Daily", "DAILY"}, {"Weekly", "WEEKLY"}, {"Monthly", "MONTHLY"}, {"Yearly", "YEARLY"},
}};
enum class When : std::uint8_t {
Now,
Scheduled,
Draft // made now, paid once someone accepts it
};
// A payment as nostrod is asked for it.
struct PaymentOrder {
BunqCounterparty to;
std::int64_t amount = 0;
std::string description;
When when = When::Now;
BunqSchedule schedule; // for When::Scheduled
};
struct PaymentForm {
Ui::EditState to{Ui::FieldKind::Plain};
Ui::EditState name{Ui::FieldKind::Text};
Ui::EditState amount{Ui::FieldKind::Decimal};
Ui::EditState description{Ui::FieldKind::Text};
When when = When::Now;
Ui::EditState day{Ui::FieldKind::Plain};
Ui::EditState time{Ui::FieldKind::Plain};
std::size_t unit = 0; // of scheduleUnits
Ui::EditState every{Ui::FieldKind::Digits};
Ui::EditState until{Ui::FieldKind::Plain};
// Errors show once the user has tried to go on: an empty form is not a list of mistakes.
bool tried = false;
struct Errors {
std::string to;
std::string name;
std::string amount;
std::string day;
std::string time;
std::string every;
std::string until;
bool Any() const noexcept;
};
// Every field's error, and into `order` what the form asks for when there is none.
Errors Check(Currency currency, std::chrono::system_clock::time_point now, PaymentOrder& order) const;
void Clear();
};
// A request for money as nostrod is asked for it.
struct RequestOrder {
BunqCounterparty from;
std::int64_t amount = 0;
std::string description;
};
struct RequestForm {
Ui::EditState from{Ui::FieldKind::Plain};
Ui::EditState name{Ui::FieldKind::Text};
Ui::EditState amount{Ui::FieldKind::Decimal};
Ui::EditState description{Ui::FieldKind::Text};
bool tried = false;
struct Errors {
std::string from;
std::string name;
std::string amount;
bool Any() const noexcept;
};
Errors Check(Currency currency, RequestOrder& order) const;
void Clear();
};
// Which of an account's payments are listed.
struct FilterForm {
Ui::EditState search{Ui::FieldKind::Text};
Ui::EditState amount{Ui::FieldKind::Decimal};
bool received = false; // the amount came in; it went out otherwise
Ui::EditState from{Ui::FieldKind::Plain};
Ui::EditState to{Ui::FieldKind::Plain};
struct Errors {
std::string amount;
std::string from;
std::string to;
bool Any() const noexcept;
};
// The errors, and into `query` what the form asks for when there is none; its limit is left as it is.
Errors Check(Currency currency, TransactionQuery& query) const;
// What is asked for besides the search, each as a few words: "Paid €12.50", "From 3 Nov".
std::vector<std::string> Chips(Currency currency, std::chrono::system_clock::time_point now) const;
// Empties everything but the search.
void Reset();
};
// A connection to bunq made with an API key. The key says itself which of bunq's environments it is of, by how it
// starts, so there is none to choose: nostrod takes the key's own.
struct BunqKeyForm {
Ui::EditState key{Ui::FieldKind::Secret};
Ui::EditState ips{Ui::FieldKind::Plain};
bool advanced = false; // the allowed addresses are shown
bool tried = false;
BunqKeyForm();
// Why the form cannot be sent; empty when it can.
std::string Check() const;
void Clear();
};
// A connection to bunq made through OAuth: the client's registration first, then the address the browser ends
// up at.
struct BunqOAuthForm {
Ui::EditState clientId{Ui::FieldKind::Plain};
Ui::EditState redirectUri{Ui::FieldKind::Plain};
Ui::EditState secret{Ui::FieldKind::Secret};
bool sandbox = false;
Ui::EditState redirected{Ui::FieldKind::Plain};
// Ties the browser's answer to this request; made when the address to open is.
std::string state;
std::string address; // where the user approves, once the first step is filled in
bool tried = false;
struct Errors {
std::string clientId;
std::string redirectUri;
std::string secret;
bool Any() const noexcept;
};
Errors Check() const;
// Makes the address to open, with a new state. The form must be without errors.
void Begin(std::string newState);
// The code in the address the browser was sent to; throws what BunqConnector::OAuthCodeFromRedirect does.
std::string Code() const;
void Clear();
};
}