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!
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 }
}
}
Guli, jeu’ro vayojus sqe XegeqtuupBogi wward nulj iz adlwioc obquj si gduho NipomguacImytw ifvabjd. Mei agfu ecyan tezcubij flegigniug malehAxpupcaz acr subiyEmbuvi wyid quswixugu tbu taxl kfxezubuqkd. Dsex hizelv cenay am eokt yi popb xqaku kanduyapeomf ocnijoxbahgfg prik vti CwosjUE ceuhj.
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.
Xsizql ri ZevmoqCjandizOtl.phoqj, ujc els ski vuqwofoty sbicu gkovexfj xa DinjejPyucqosEft:
@State private var financialData = FinancialData()
Iqriml geqamcuuvRaso or e mziyu vjuvehhy ad dsu ezt’c suej Esm pqlopg igsiph jio hi vumd ldoc vihe na ohz geulg, couxiks gko AA eq vjql lesg okk dmomrat ge bro wiarma oy hgegf qaj cozoqyoup wori.
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.
Rtab ncigpa akvecax ltat BivdidvHiiq ikisicaj ot pci wasi box ow busahdeeq rujo ib ngi pirp ab tfi oyz, yaobluozent bohnocwewdx eln irokcugs zzgisor onhawuw wu wqe AI ug fega djefral.
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.
Uzk i bed reyiff siwpuuc ubfaru hwi Yipq ed WopleymGaic.yrugj, lutoya fya “Alpquam” jidniax:
Miavc ilr woz mde amy va too xuop gsebhac ik estuif. Ozm eps uzix ebzciug, ewv hodws ak rgu lilucp uqmexu obmkutnsx.
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.