|
| 1 | +# Cupcake Notes and Documentation |
| 2 | + |
| 3 | +> This document is intended for developers. If you just want to build Cupcake, please see the top-level `README.md` and `Makefile`. |
| 4 | +
|
| 5 | +## Project Structure |
| 6 | + |
| 7 | +Cupcake adheres to a standard project structure; currently, everything resides inside the `lib` directory. This setup may change in the future. |
| 8 | + |
| 9 | +## `lib/coins` |
| 10 | + |
| 11 | +This directory contains the abstract `coin` definition. To add a new currency to Cupcake, simply implement the abstract `Coin` class found here and update `lib/coins/list.dart` with the new class. The app will automatically pick it up, and a new entry will appear on the coin selection screen during creation or restoration. |
| 12 | + |
| 13 | +> **NOTE:** Currently, only one coin is supported. To ensure a smooth user experience, Cupcake will not prompt the user to select an option if there is only one available. |
| 14 | +
|
| 15 | +## `lib/dev` |
| 16 | + |
| 17 | +Cupcake's state management is implemented in a very simple way using MVVM. Since the app is designed to work entirely offline, its state doesn't change often. To keep the code simple, I opted to create an in-house state management solution instead of using an existing one-size-fits-all package. While those packages are great, I believe they would be overkill for an app with a minimal UI and limited processing. |
| 18 | + |
| 19 | +This directory contains three code generation utilities that, while entirely optional, make my life much easier and the code much simpler. For example, instead of converting between `ObservableList` and `List` or wrapping widgets to notify about state changes, we simply trigger a rebuild. |
| 20 | + |
| 21 | +### `@GenerateRebuild()` |
| 22 | + |
| 23 | +Place this annotation around a class to ensure that all other generation annotations work correctly. |
| 24 | + |
| 25 | +### `@RebuildOnChange()` |
| 26 | + |
| 27 | +This annotation wraps a variable in a getter and setter that triggers a rebuild when the variable changes. |
| 28 | + |
| 29 | +```dart |
| 30 | +@RebuildOnChange() |
| 31 | +Barcode? $barcode; |
| 32 | +``` |
| 33 | + |
| 34 | +You can then use `barcode` (or `viewModel.barcode`), and updating this value will rebuild the UI. |
| 35 | + |
| 36 | +### `@ThrowOnUI(message: "message...", L: "translation_key")` |
| 37 | + |
| 38 | +If you have a function that may throw an error—and that error should be presented to the user in a dismissible manner—use this annotation. It indicates that something went wrong, allowing the user to retry or correct the issue without restarting the entire action. |
| 39 | + |
| 40 | +```dart |
| 41 | +@ThrowOnUI(message: "Error handling URQR scan") |
| 42 | +Future<void> $handleUR() async { |
| 43 | + if (formInvalid) { |
| 44 | + throw Exception("The form is invalid"); |
| 45 | + } |
| 46 | + await wallet.handleUR(c!, ur); |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +You can use either `message:` or `L:`. The `message:` text will be displayed in plain text, whereas `L:` will use a translation key. The error message will appear as a dismissible alert. |
| 51 | + |
| 52 | +If you do not use `@ThrowOnUI()`, you must wrap the function in a try-catch block. If an error is thrown without being caught, the app will display a panic handler screen that prevents further use. |
| 53 | + |
| 54 | +### `@ExposeRebuildableAccessors()` |
| 55 | + |
| 56 | +This annotation is somewhat special and is designed for the settings page. It exposes all setters and getters from a class instance, making it possible to access them as elements of the ViewModel. |
| 57 | + |
| 58 | +```dart |
| 59 | +@ExposeRebuildableAccessors(extraCode: r'$config.save()') |
| 60 | +CupcakeConfig get $config => CupcakeConfig.instance; |
| 61 | +``` |
| 62 | + |
| 63 | +This approach lets you access settings, for example, using `viewModel.configBiometricEnabled`. It may be overkill, but I like it because it essentially removes almost all logic from the settings view model. |
| 64 | + |
| 65 | +## `lib/gen` |
| 66 | + |
| 67 | +The `lib/gen` directory uses `flutter_gen` to access assets. This method is preferable to using string-based paths in UI code, as it prevents accidental typos that might not be caught by the linter or at compile time. |
0 commit comments