341 lines
18 KiB
Text
341 lines
18 KiB
Text
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
export module Nostro.App:App;
|
||
|
|
|
||
|
|
import std;
|
||
|
|
import Nostro;
|
||
|
|
import Nostro.Ui;
|
||
|
|
import :Banks;
|
||
|
|
import :Format;
|
||
|
|
import :Forms;
|
||
|
|
import :Errors;
|
||
|
|
|
||
|
|
// Nostro's window without the window: what it knows of the user's banks, where the user is in it, what is being
|
||
|
|
// filled in and what is under way, and the screens built from that. It asks nostrod through the asynchronous Client
|
||
|
|
// and never waits for it: a call's answer changes the state and asks for a new frame. Nothing here draws or reads a
|
||
|
|
// device (that is Nostro.Ui.Shell's), so a test builds a frame, reads what it says and taps it by name.
|
||
|
|
//
|
||
|
|
// The rules that keep a bank's client honest are here. A press acts on what it went down on, and every action names
|
||
|
|
// what it is about by value: an IBAN or the bank's id, never a place in a list. One change at a time: while one is
|
||
|
|
// under way its sheet covers the screen, so nothing else can be asked for and nothing is sent twice. A payment from
|
||
|
|
// the form is sent by the form's own button, with no question of the app's before it: what confirms it is the system's
|
||
|
|
// password dialog, which nostrod words itself. What the app does ask about first (a request to send, a request or a
|
||
|
|
// draft to accept, a card to report) it shows built from the same values that are then sent. And an answer that leaves
|
||
|
|
// open whether money moved says so, and offers no button to try again.
|
||
|
|
//
|
||
|
|
// Which bank an account is with makes no difference to any of it. A bank's own is in two places: what :Banks says of
|
||
|
|
// it, and the forms a connection to it is made with, which are the last pages of adding a connection in the settings
|
||
|
|
// (Nostro.App-Bunq.cpp has bunq's). Everything else says "your bank", and asks :Banks what a connection can do.
|
||
|
|
|
||
|
|
export namespace Nostro::App {
|
||
|
|
// What the app needs of the program around it.
|
||
|
|
struct Host {
|
||
|
|
// A client for nostrod, attached to the program's event loop. Throws when there is no system bus.
|
||
|
|
std::function<Client()> connect;
|
||
|
|
// Asks the desktop to open an address in the user's browser; `then` hears whether it took the request.
|
||
|
|
std::function<void(std::string_view uri, std::function<void(bool taken)> then)> openUri;
|
||
|
|
// The time, for "today" and for what is in the past. The system's clock when left empty.
|
||
|
|
std::function<std::chrono::system_clock::time_point()> now;
|
||
|
|
// A new state for an OAuth request, which ties the browser's answer to it. Random when left empty.
|
||
|
|
std::function<std::string()> oauthState;
|
||
|
|
std::string version;
|
||
|
|
};
|
||
|
|
|
||
|
|
// How far the asking for something is.
|
||
|
|
enum class LoadState : std::uint8_t {
|
||
|
|
Idle, // not asked for yet
|
||
|
|
Loading,
|
||
|
|
Loaded,
|
||
|
|
Failed
|
||
|
|
};
|
||
|
|
|
||
|
|
// Something nostrod is asked for.
|
||
|
|
template<typename T>
|
||
|
|
struct Loadable {
|
||
|
|
LoadState state = LoadState::Idle;
|
||
|
|
T value{};
|
||
|
|
Guidance problem; // why it failed
|
||
|
|
bool refreshing = false; // loaded, and asked for again: the old value shows meanwhile
|
||
|
|
};
|
||
|
|
|
||
|
|
// A bank connection with its accounts: Connectors() and Accounts() joined.
|
||
|
|
struct Connection {
|
||
|
|
std::string name;
|
||
|
|
std::string bank; // as nostrod calls it; BankName() says it as the bank does
|
||
|
|
std::string environment; // "production" or "sandbox"
|
||
|
|
std::string auth; // the way it was made, as nostrod calls that; WayName() says it in words
|
||
|
|
bool working = false;
|
||
|
|
std::vector<Account> accounts;
|
||
|
|
// It pays between the user's own accounts and nobody else, by what :Banks knows of the way it was made.
|
||
|
|
bool ownAccountsOnly = false;
|
||
|
|
};
|
||
|
|
|
||
|
|
// One account, found by its key.
|
||
|
|
struct AccountRef {
|
||
|
|
const Connection* connection = nullptr;
|
||
|
|
const Account* account = nullptr;
|
||
|
|
std::string key; // names it in the app: its IBAN, or its name where the bank gave none
|
||
|
|
};
|
||
|
|
|
||
|
|
class App {
|
||
|
|
public:
|
||
|
|
App(Ui::Context& context, Host host);
|
||
|
|
App(const App&) = delete;
|
||
|
|
App& operator=(const App&) = delete;
|
||
|
|
|
||
|
|
// Asks nostrod for the connections and accounts; the first frame shows them loading.
|
||
|
|
void Start();
|
||
|
|
// Builds the screen the user is on.
|
||
|
|
void Build(Ui::Frame& frame);
|
||
|
|
|
||
|
|
// Whether nostrod is still being waited for: for whoever runs the loop until the app has settled.
|
||
|
|
bool Busy() const noexcept;
|
||
|
|
// Whether a change is under way. The window may close then, and the process must not: the answer says whether
|
||
|
|
// money moved. onSettled is called when it has come.
|
||
|
|
bool Changing() const noexcept;
|
||
|
|
std::function<void()> onSettled;
|
||
|
|
// An answer came, to the method of that name, and whether it was an error: for a log or a debugger. Called once
|
||
|
|
// the app has taken the answer in, so what it shows is what the answer led to.
|
||
|
|
std::function<void(std::string_view member, bool failed)> onAnswer;
|
||
|
|
|
||
|
|
// Goes to a place by name: "accounts", "cards", "settings", "connections", "checkout", "about", "account:<IBAN>",
|
||
|
|
// or one of the steps of adding a connection: "add" for the banks, "add:<bank>" for a bank's ways and
|
||
|
|
// "add:<bank>:<way>" for a way's form, as in "add:bunq:key". False for a name that is none of them, and while
|
||
|
|
// a change is under way, which the screen waits out.
|
||
|
|
bool Navigate(std::string_view route);
|
||
|
|
// Where the user is, as a name of the same kind, with what is open over it after a '+': "account:NL…+filter".
|
||
|
|
std::string Screen() const;
|
||
|
|
// The connections and accounts as they are known.
|
||
|
|
const Loadable<std::vector<Connection>>& Overview() const noexcept;
|
||
|
|
// The state of the OAuth request that is being made, for a test that plays the browser.
|
||
|
|
std::string_view OAuthState() const noexcept;
|
||
|
|
|
||
|
|
private:
|
||
|
|
// -- where the user is ---------------------------------------------------------------------------------------
|
||
|
|
enum class Place : std::uint8_t {
|
||
|
|
Accounts,
|
||
|
|
Cards,
|
||
|
|
Settings
|
||
|
|
};
|
||
|
|
enum class Page : std::uint8_t {
|
||
|
|
Account,
|
||
|
|
Pay,
|
||
|
|
Request,
|
||
|
|
Connections,
|
||
|
|
Banks, // adding a connection: the banks to choose from
|
||
|
|
Ways, // the ways to connect to the bank that was chosen
|
||
|
|
Connect, // the form of the way that was chosen
|
||
|
|
Checkout,
|
||
|
|
About
|
||
|
|
};
|
||
|
|
struct Route {
|
||
|
|
Page page = Page::Account;
|
||
|
|
std::string account; // the key of the account it is about
|
||
|
|
const Bank* bank = nullptr; // of Ways and Connect
|
||
|
|
const Way* way = nullptr; // of Connect
|
||
|
|
};
|
||
|
|
enum class Tab : std::uint8_t {
|
||
|
|
Activity,
|
||
|
|
Requests,
|
||
|
|
Incoming,
|
||
|
|
Scheduled,
|
||
|
|
Drafts
|
||
|
|
};
|
||
|
|
|
||
|
|
// -- what is known -------------------------------------------------------------------------------------------
|
||
|
|
// One account's lists, each asked for when its tab is first shown.
|
||
|
|
struct AccountData {
|
||
|
|
Tab tab = Tab::Activity;
|
||
|
|
FilterForm filter;
|
||
|
|
TransactionQuery query; // the filter as it was last asked for
|
||
|
|
Loadable<std::vector<Transaction>> activity;
|
||
|
|
bool more = false; // the list is as long as was asked for: there may be more
|
||
|
|
Loadable<std::vector<BunqPaymentRequest>> requests;
|
||
|
|
Loadable<std::vector<BunqIncomingRequest>> incoming;
|
||
|
|
Loadable<std::vector<BunqScheduledPayment>> scheduled;
|
||
|
|
Loadable<std::vector<BunqDraftPayment>> drafts;
|
||
|
|
};
|
||
|
|
|
||
|
|
// One kind of call at a time, and at most one waiting behind it: the newest. nostrod's worker makes its calls
|
||
|
|
// to the bank one after another, and an answer dropped here does not stop it, so a queue of searches the user
|
||
|
|
// has already typed past would only use up the bank's patience.
|
||
|
|
struct Lane {
|
||
|
|
Client::Pending pending;
|
||
|
|
bool busy = false;
|
||
|
|
std::function<void()> next;
|
||
|
|
};
|
||
|
|
|
||
|
|
// -- what is open over the screen ----------------------------------------------------------------------------
|
||
|
|
struct Line {
|
||
|
|
std::string label;
|
||
|
|
std::string value;
|
||
|
|
// What a press on the line puts on the clipboard, where that is not the value as it is shown: an IBAN
|
||
|
|
// without the spaces it is read with.
|
||
|
|
std::string copied;
|
||
|
|
};
|
||
|
|
struct Choice {
|
||
|
|
std::string key;
|
||
|
|
std::string label;
|
||
|
|
Ui::ButtonKind kind = Ui::ButtonKind::Tonal;
|
||
|
|
bool enabled = true;
|
||
|
|
std::string reason; // why it cannot be chosen
|
||
|
|
std::function<void()> onTap;
|
||
|
|
};
|
||
|
|
// What is asked or told over the screen: a thing to look at with what can be done about it, a question before
|
||
|
|
// a change, the wait for its answer, and how it went.
|
||
|
|
struct Dialog {
|
||
|
|
enum class Kind : std::uint8_t {
|
||
|
|
Detail, // a row's own sheet
|
||
|
|
Ask, // before a change: what will be done, to confirm
|
||
|
|
Working, // the change is under way
|
||
|
|
Done, // it worked
|
||
|
|
Problem // it did not, or may not have
|
||
|
|
};
|
||
|
|
Kind kind = Kind::Detail;
|
||
|
|
std::string name; // for Screen(): "transaction", "confirm", "working"
|
||
|
|
std::string title;
|
||
|
|
std::vector<std::string> text;
|
||
|
|
std::vector<Line> lines;
|
||
|
|
std::vector<Choice> choices;
|
||
|
|
std::string link; // an address to copy or open
|
||
|
|
std::function<void()> onClose; // after it is sent away
|
||
|
|
};
|
||
|
|
enum class Picking : std::uint8_t {
|
||
|
|
None,
|
||
|
|
PayFrom,
|
||
|
|
RequestInto
|
||
|
|
};
|
||
|
|
|
||
|
|
// -- nostrod -------------------------------------------------------------------------------------------------
|
||
|
|
Client& Nostrod();
|
||
|
|
void Reconnect();
|
||
|
|
void Note(const std::vector<std::string>& notes);
|
||
|
|
void Answered(std::string_view member, bool failed);
|
||
|
|
// A reading: `start` makes the call, given what to do with its answer, in the lane of that name.
|
||
|
|
template<typename T>
|
||
|
|
void Fetch(std::string lane, std::string member, std::function<Client::Pending(Client&, Client::Then<T>)> start, std::function<void(Answer<T>)> then);
|
||
|
|
// A change: shows the wait, makes the call, and shows how it went. `done` says what to tell the user; what it
|
||
|
|
// throws is told as the change having failed. `account` is the account it is about, if any.
|
||
|
|
template<typename T>
|
||
|
|
void Change(Operation operation, std::string member, std::string title, std::string account, std::function<Client::Pending(Client&, Client::Then<T>)> start, std::function<void(T&)> done);
|
||
|
|
void Failed(const Guidance& guidance, Operation operation, const std::string& account);
|
||
|
|
void Wait(Operation operation, std::string title);
|
||
|
|
|
||
|
|
void Refresh(bool again = false);
|
||
|
|
void LoadAccounts(std::vector<ConnectorSummary> summaries, bool again);
|
||
|
|
void OverviewFailed(const Guidance& guidance);
|
||
|
|
void ListFailed(const Guidance& guidance);
|
||
|
|
void LoadActivity(const std::string& key, bool stored = false);
|
||
|
|
template<typename T>
|
||
|
|
void LoadList(const std::string& key, std::string_view member, Loadable<std::vector<T>> AccountData::* list, Client::Pending (Client::* call)(std::string_view, Client::Then<std::vector<T>>));
|
||
|
|
void LoadTab(const std::string& key, Tab tab, bool again = false);
|
||
|
|
void LoadCards(bool again = false);
|
||
|
|
void LoadCheckout();
|
||
|
|
void Changed(const std::string& key);
|
||
|
|
|
||
|
|
// -- going places --------------------------------------------------------------------------------------------
|
||
|
|
void Go(Place to);
|
||
|
|
void Push(Page page, std::string account = {}, const Bank* bank = nullptr, const Way* way = nullptr);
|
||
|
|
void Back();
|
||
|
|
void Show(Dialog shown);
|
||
|
|
void Close();
|
||
|
|
void Tell(std::string_view notice);
|
||
|
|
std::chrono::system_clock::time_point Today() const;
|
||
|
|
std::optional<AccountRef> Find(std::string_view key) const;
|
||
|
|
std::vector<AccountRef> Accounts() const;
|
||
|
|
AccountData& Data(const std::string& key);
|
||
|
|
bool Own(std::string_view iban) const;
|
||
|
|
|
||
|
|
// -- what the user asks for ----------------------------------------------------------------------------------
|
||
|
|
void OpenPay(std::string account);
|
||
|
|
void OpenRequest(std::string account);
|
||
|
|
void SubmitPayment();
|
||
|
|
void SendPayment(const std::string& key, Currency currency, const PaymentOrder& order);
|
||
|
|
void ReviewRequest();
|
||
|
|
void SendRequest(const std::string& key, Currency currency, const RequestOrder& order);
|
||
|
|
void ShowTransaction(const std::string& key, const Transaction& transaction);
|
||
|
|
void ShowRequest(const std::string& key, const BunqPaymentRequest& request);
|
||
|
|
void ShowIncoming(const std::string& key, const BunqIncomingRequest& request);
|
||
|
|
void ShowScheduled(const std::string& key, const BunqScheduledPayment& scheduled);
|
||
|
|
void ShowDraft(const std::string& key, const BunqDraftPayment& draft);
|
||
|
|
void ShowCard(const BunqCard& card);
|
||
|
|
void ShowConnection(const Connection& connection);
|
||
|
|
// A change that needs a yes and no more: asks `question`, makes the change, and tells `told`.
|
||
|
|
void Simple(Operation operation, std::string member, std::string question, std::string detail, std::string confirm, std::string told, std::string account, std::function<Client::Pending(Client&, Client::Then<std::monostate>)> start);
|
||
|
|
void SetCard(std::uint64_t card, std::string lastFour, Operation operation, std::string status);
|
||
|
|
void RemoveConnection(const Connection& connection);
|
||
|
|
// What every way's form ends in: nostrod is asked to add the connection, as `start` asks it, and the user is
|
||
|
|
// told how that went. For the form's own button: which bank and which way it is are the page's that is open.
|
||
|
|
void Connect(std::string member, std::function<Client::Pending(Client&, Client::Then<AddedConnector>)> start);
|
||
|
|
void Connected(const AddedConnector& added, const Way& way);
|
||
|
|
void ForgetConnecting();
|
||
|
|
// bunq's two ways (Nostro.App-Bunq.cpp).
|
||
|
|
void ConnectBunqKey();
|
||
|
|
void BeginBunqOAuth();
|
||
|
|
void ConnectBunqOAuth();
|
||
|
|
void ChooseCheckout(const std::string& key, const std::string& name);
|
||
|
|
void Search(const std::string& key, bool now);
|
||
|
|
|
||
|
|
// -- the screens ---------------------------------------------------------------------------------------------
|
||
|
|
// A form's column inside a page: the page's rows run from edge to edge, a field keeps clear of the edges.
|
||
|
|
static Ui::Box Padded(const Ui::Frame& frame, Ui::Box box);
|
||
|
|
Ui::Box Scaffold(Ui::Frame& frame);
|
||
|
|
std::function<void()> Refresher();
|
||
|
|
std::string Title() const;
|
||
|
|
void Content(Ui::Frame& frame, Ui::Box area);
|
||
|
|
void Notes(Ui::Frame& frame, Ui::Column& column);
|
||
|
|
void Trouble(Ui::Frame& frame, Ui::Column& column, const Guidance& guidance, std::string_view key, std::function<void()> retry);
|
||
|
|
void Waiting(Ui::Frame& frame, Ui::Column& column, std::uint32_t rows);
|
||
|
|
void Empty(Ui::Frame& frame, Ui::Column& column, std::string_view text);
|
||
|
|
void Section(Ui::Frame& frame, Ui::Column& column, std::string_view title);
|
||
|
|
void HomePage(Ui::Frame& frame, Ui::Column& column);
|
||
|
|
void Welcome(Ui::Frame& frame, Ui::Column& column);
|
||
|
|
void AccountPage(Ui::Frame& frame, Ui::Column& column, const AccountRef& account, Ui::Box viewport);
|
||
|
|
void ActivityTab(Ui::Frame& frame, Ui::Column& column, const AccountRef& account, AccountData& data, Ui::Box viewport);
|
||
|
|
template<typename T, typename Make>
|
||
|
|
void ListTab(Ui::Frame& frame, Ui::Column& column, const std::string& key, Tab tab, Loadable<std::vector<T>>& list, std::string_view none, Ui::Box viewport, Make&& row);
|
||
|
|
void PayPage(Ui::Frame& frame, Ui::Column& column);
|
||
|
|
void RequestPage(Ui::Frame& frame, Ui::Column& column);
|
||
|
|
void AccountField(Ui::Frame& frame, Ui::Column& column, std::string_view key, std::string_view label, const std::string& account, Picking picking);
|
||
|
|
void CardsPage(Ui::Frame& frame, Ui::Column& column, Ui::Box viewport);
|
||
|
|
void SettingsPage(Ui::Frame& frame, Ui::Column& column);
|
||
|
|
void ConnectionsPage(Ui::Frame& frame, Ui::Column& column);
|
||
|
|
void BanksPage(Ui::Frame& frame, Ui::Column& column);
|
||
|
|
void WaysPage(Ui::Frame& frame, Ui::Column& column, const Bank& bank);
|
||
|
|
void BunqKeyPage(Ui::Frame& frame, Ui::Column& column);
|
||
|
|
void BunqOAuthPage(Ui::Frame& frame, Ui::Column& column);
|
||
|
|
void CheckoutPage(Ui::Frame& frame, Ui::Column& column);
|
||
|
|
void AboutPage(Ui::Frame& frame, Ui::Column& column);
|
||
|
|
void FilterSheet(Ui::Frame& frame, const AccountRef& account);
|
||
|
|
void PickSheet(Ui::Frame& frame);
|
||
|
|
void DialogSheet(Ui::Frame& frame);
|
||
|
|
|
||
|
|
Ui::Context& context;
|
||
|
|
Host host;
|
||
|
|
std::optional<Client> client;
|
||
|
|
std::map<std::string, Lane, std::less<>> lanes;
|
||
|
|
Client::Pending change; // the one change under way
|
||
|
|
bool changing = false;
|
||
|
|
std::vector<std::string> notes;
|
||
|
|
|
||
|
|
Loadable<std::vector<Connection>> overview;
|
||
|
|
std::map<std::string, AccountData, std::less<>> accounts;
|
||
|
|
Loadable<std::vector<ConnectorCards>> cards;
|
||
|
|
Loadable<std::optional<Account>> checkout;
|
||
|
|
|
||
|
|
Place place = Place::Accounts;
|
||
|
|
std::vector<Route> stack;
|
||
|
|
std::optional<Dialog> dialog;
|
||
|
|
std::string filtering; // the account whose filter sheet is open
|
||
|
|
Picking picking = Picking::None;
|
||
|
|
|
||
|
|
PaymentForm payment;
|
||
|
|
std::string payFrom;
|
||
|
|
RequestForm request;
|
||
|
|
std::string requestInto;
|
||
|
|
BunqKeyForm bunqKey;
|
||
|
|
BunqOAuthForm bunqOAuth;
|
||
|
|
std::string bunqOAuthError; // what was wrong with the address the browser was sent to
|
||
|
|
};
|
||
|
|
}
|