feat(mobile): budgets screen

Closed
BH
1mo ago
+0-0

Problem

The mobile app can't show budgets. To see how this month's spending compares with the plan, users have to open the web app, even though the read-only budgets API (GET /api/v1/budgets, GET /api/v1/budget_categories) already exists.

What changed

A new Budgets screen, opened from More, mirrors the web budgets page:

  • Prev/next month navigation. Next stops at the current month, the same as on web. The header uses the budget's server name, so families with a custom month start see their real range.
  • A summary card showing budgeted, actual and remaining spending, an overall progress bar, and actual vs. expected income.
  • One progress bar per category, with subcategories nested under their parent. Each row shows "spent of budget" and the amount left, or how much it is over. Rollover appears as "+X rolled over", and subcategories without their own limit show "Shared budget".
  • Near-limit (90% or more) and over-budget badges, and an All / Over budget (n) filter that appears when anything is over. The rules for which lines show follow the model helpers (budgeted?, over_budget?, near_limit?, visible_on_track?). Unbudgeted categories with no spending are hidden. Shared subcategories appear once they have spending.
  • Tapping a category opens its transactions for the budget period. For a parent category this includes its subcategories. The list is read-only and paged through GET /api/v1/transactions with category_ids[], start_date and end_date. TransactionsService.getTransactions gains these three optional params, which the API already supports. Existing callers are unchanged.
  • If a month has more than one visible budget (household, personal, or shared), chips let you switch between them.
  • Every amount goes through MoneyMasker, so privacy mode masks the whole screen. If the device is offline, a month already loaded in this session is still shown, with a note. Otherwise an offline state with a retry appears.
  • Empty and error states cover: no budget for the month, a budget with no amounts set yet, a session that has expired (401), and a failed load.
  • Strings are added to app_en.arb and app_sv.arb, and the generated localizations are regenerated.

Structure follows the existing layers: models/budget.dart, services/budgets_service.dart (injectable http.Client), providers/budgets_provider.dart (owned by the screen, so nothing survives a sign-out), screens/budgets_screen.dart and screens/budget_category_transactions_screen.dart, and widgets/budget_*. The widgets use SureCard, SureSegmentedControl, SureChip, SureButton and SureColors tokens.

API notes (no backend changes in this PR):

  • The index views of both endpoints leave out derived amounts (include_derived_amounts: false). To get actual and available figures, a month is built from the budgets list, the budget show request, and one budget_categories/:id show request per category. Those requests run 6 at a time. If one category request fails, that row still shows its budgeted amount. A 401 fails the whole month. An include_derived=true option on GET /api/v1/budget_categories?budget_id= would bring this down to a single request. I'm happy to send that as a follow-up.
  • Budgets only exist for months that were opened on web (Budget.find_or_bootstrap), and the API can't create them. Months that were never opened show "No budget for {month}" and point to the web app.
  • The budget payload doesn't say who owns a budget, so when several exist for the same month the chips read "Budget 1 / Budget 2".
  • The web's synthetic "Uncategorized" budget line isn't in the API, so it isn't shown.

How it was tested

  • flutter analyze --no-fatal-infos reports no new issues. The same 3 existing infos in intro_screen_web.dart appear on main.
  • flutter test: all 216 tests pass, 35 of them new:
    • test/models/budget_test.dart: parsing, over-budget, near-limit and visibility rules, rollover, shared subcategories, and grouping.
    • test/services/budgets_service_test.dart: query params, show and index parsing, 401 and error handling, and the new transaction filters (category_ids[], dates).
    • test/providers/budgets_provider_test.dart: assembling a month, the session cache, custom month start, empty months, switching budgets, 401, and offline with and without a cached month.
    • test/screens/budgets_screen_test.dart: totals, nested rows and badges, the over-budget filter, privacy masking, the empty state, month navigation, and tapping a parent to open its transactions with its subcategory ids.
  • New files pass dart format. Generated l10n files keep the repo's existing format.
  • Ran on Flutter 3.32.4, the version CI uses. I couldn't build an APK because there was no Android SDK in my environment.
  • The screenshots were rendered from a widget test using the Geist fonts and the same fixtures.

Summary by CodeRabbit

  • New Features
    • Added a Budgets screen in the More menu with monthly navigation, summaries, income, category spending, limits, shared budgets, rollovers, and over-budget filtering.
    • Added category transaction lists with privacy-aware amounts, refresh, retry, and pagination.
    • Added offline, cached-data, missing-budget, empty-state, and session-expiration messaging.
  • Localization
    • Added English and Swedish translations for budget screens, statuses, and loading states.
  • Improvements
    • Budget amounts respect privacy settings, and forward navigation is limited to supported periods.