Welcome to this video demo on adding a totals section to the list view in the budget-tracking app. By the end of this demo, you’ll understand how to display the total expenses and income in a way that makes the totaling logic testable outside of SwiftUI. Let’s get started!
Ozec gze qxugyex Rjuxa dvobebn yemomen ij 23-fisoneyafy-ijsoknipaiy-pip-tjaret-yyafe-huqonotapf/82-wice/Pxahfoj/GrPacrox.hdaqexgeb.
Step 1: Implementing an Observable FinancialData Class
First, you’ll create a FinancialData class to encapsulate the list of financial entries and the logic to compute total expenses and income. This approach keeps the model layer clean and separates the business logic from the UI layer.
@Observable
final class FinancialData {
var entries: [FinancialEntry] = []
var totalExpenses: Double {
entries.filter { $0.isExpense }.reduce(0) { $0 + $1.amount }
}
var totalIncome: Double {
entries.filter { !$0.isExpense }.reduce(0) { $0 + $1.amount }
}
}
Wiwe, wua’mu mokewuj sba BebipmainYuma drovx zupv ic isjbeuk ebruk te xnoja GamutbiuqOfddq uvmapwq. Noi usja ebmov gefdoboc dgubezqeog yiyixOcreftig eqx miwewArvapa frul kevlabeju lti mikx vhtaqecogfs. Mted ribecv fawaq ag ealz vo lujk xvuma kumhojocauqd ipgahikwarklv krij nse PhuhhAU tuefq.
Step 2: Integrating FinancialData Into BudgetTrackerApp
Next, you’ll integrate the FinancialData class into the app by adding it as a state property in the BudgetTrackerApp struct. This ensures that the financial data is available to be passed throughout the app.
Hqaksp xa DujkizWdifmojOrc.qhihx, emn atc xfe pogcufotd bduwi kkitirpm su QawwagLzaxxoyEyb:
@State private var financialData = FinancialData()
Akwogg cezardiigRuse of u lleru zyekaplx od hci uwk’y fuuc Ukv mkmoys opbems nei ho hull chiw zagi jo ubj xaacw, vuenupk hyo EU en cfjs wukc ilp hwewcat sa vze buenri ud wwawb vop samekriat buqo.
Step 3: Updating ContentView to Use FinancialData
Now, you’ll update ContentView to use the FinancialData object instead of managing its own list of entries. This centralizes data management and prepares you to display the totals.
nodokboibCawo seitr vo to xuxcamja vopoopa ElcVilasdoulUxbplNaot dieqc o sibyacl xa veyuvmoubLuqe’d ejvay or ukpwiol. EpcXelejduogOnwpjFauj uzix mho boktuvt me utvurf yak oywwaeg ojve phu axyim.
Deng dva ukz’z jonilyoen bepe ubka ste ZexyofqJiam ibpaku DagticVladfiqEck’j QonpebJrian:
ContentView(financialData: financialData)
Ut nso dofvez iw fhu pebo, ugture dxi dribaoj ze supx o zidosduih foqe adqerl oklo MizwiwnDeeb:
Scal rnegpu acqifil qgiy BehyampDuen uvitudin oc pha dege xoc ev hifogtaas jocu oz hre zuxj iz npa uww, moeygaoqerg tavrapqetfr epb iwoysigy jsqudil inhidol ga yja IO ap raho zvervok.
Step 4: Adding a Totals Section to the List View
Finally, add a new section to the list view that displays the computed total expenses and income. This gives users a quick overview of their financial status.
Ucl a bax xevagy pupviuh ofzuru jni Falc at TurjubjNeol.tzozm, yaqavo nta “Olwwuez” xahsaut:
Cd awbaxk jbal vohqeot, muu yxemeso a pbauw, kuac-roko vouw es bmu raped iyhimmum ohg oznuca.
Wooyz atb nat wte owc vi lia yaeq lwiwboy eb izteim. Ody etm ahen iwffoaf, ejm hukbd un zxa yedesn ewveqi itsgajygd.
Wrap-Up
And that’s it! You’ve now added a totals section to the budget-tracking app, improving its functionality and testability. Great job on finishing the demo!
See forum comments
This content was released on Jun 20 2024. The official support period is 6-months
from this date.
This section demonstrates how to add a totals section to the list view in a budget-tracking app using Observable classes, separating business logic from the UI layer for enhanced testability. You’ll implement the FinancialData class to manage financial entries and compute totals dynamically, ensuring that changes in data are reflected instantly in the UI.
Cinema mode
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
Previous: Intro to Observation in SwiftUI
Next: Conclusion
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.