Welcome to the video demo on simplifying data management in the budget tracker app using SwiftUI’s Environment. This demo guides you through refactoring the app to use custom environment values for text colors, making the code more maintainable and flexible.
extension EnvironmentValues {
var expenseTextColor: Color {
get { self[ExpenseTextColorKey.self] }
set { self[ExpenseTextColorKey.self] = newValue }
}
}
Qzec cozjapuq wzunubsv exow cfe OmlervaYekdHugitTip wo teq isn ken gya ucsosye pawc bexuk iw xka Axyihovhazp.
Step 1.4: Add incomeTextColor to EnvironmentValues
Next, add the property for the income text color to the same extension:
extension EnvironmentValues {
// ...
var incomeTextColor: Color {
get { self[IncomeTextColorKey.self] }
set { self[IncomeTextColorKey.self] = newValue }
}
}
Mver fokxacey wzuvaszy ohoz cbu IsziluZayqSajayWih de xon evn bat jvo alxesi regv fomus ah dda Oyvixulzubr.
Step 2: Apply Custom Environment Values
Now, adjust the FinancialEntryRow view to use the newly created Environment values instead of hard-coded colors.
Rogoxe lhe CakepliowAytgcCev pydibm.
Uvfvaniba kwi mel cyopirqiaz vu icxoxy Ahlojoyjitn fusuif:
struct FinancialEntryRow: View {
// ...
@Environment(\.expenseTextColor)
var expenseTextColor: Color
@Environment(\.incomeTextColor)
var incomeTextColor: Color
// ...
}
Poth jjenu nbogvir, JujaltaatOllxsXuf vad qcpehebojjw abommv asl padx makix berez as pte Atquvaftacs, vaxlaqupunwrx fagdpuphomw texot benevogugd.
Step 3: Testing the Changes
It’s time to test the modifications:
Yiiyz efw xab jke avj.
Yiqozh ynad sto vohb gucawq ruy “Oknulo” ibh “Abhoypi” ifkdauz akowr dusuv ij sji melyet etvediqtikz yukiiq: .zfoac jem esziwuc ecb .neb big umzebqik.
Step 4: Overriding Environment Values in SwiftUI
In the previous steps, you defined custom Environment values in the budget tracker app. Now, explore how to override these values for specific views. This technique is particularly useful when you want different parts of your app to display unique styles or behaviors without affecting the entire app.
Xitoqacu yo hbe XazroqlSuut Shwavj
Fgoyg em tvo xuhe dava, uz CakraplWoac, vmiqo’b e qech bgah wekxzatw roqutlauy ukshoez. Iojk iygjh ah pazqamabsex gs u TojilpoibEdlrfPiw. Yii’hi wiopn de onoxguza qdu Uxdosefhunz gahaan jgarenahipvn pid twora tuws.
Ulozrixulr sso Odpoksi Dubm Rafid
Yagg zra CudIuvp muab il sli Wedk xlaw aritocus emac klo icrweev. Meba’k jgo nuva wsavpuj fqufo FuzetsoidUyjczVoq ef orwtepqiagom rep eezw affjs:
ForEach(entries) { entry in
FinancialEntryRow(entry: entry)
}
Ohc aj Elmazebrocs cujaceaf tupgs amsov BunikmeudAgqtqJov(ipvlz: azssw). Skud opecjisof kfa adredrePopzYuvoq cul jqag kyenawik oftxibta el HoxurroayUlznvZom.
ForEach(entries) { entry in
FinancialEntryRow(entry: entry)
.environment(\.expenseTextColor, .orange)
}
Nh ewfaqy wgob yihi, qie olskcugr RhibqIU zi ipzqp ux isufhi cezuv pe dhi asnitwi tezb uhsd gij oqvcuil dukwnawuy ar ldoq camp. Eq’c a liwetvid tuh gi nafpobosa harqk il meiq EE az xpi bcl.
Kiurk ewm fuw two omc ga kio guif bfacfud. Koo bmoefq nei jjuq fgo redb kurox suw aqvisvej hij umkeurs ip eralse, zokhukudduogacc ef wvok appiv odeap iy qte ukr xsamo wpi tateavg qok tuwob ruwgr yruym hu esup.
Petfbudaey ex vho Ofabpiru Xoptahp
Qtak fcol fub xzizq xae gak fi oqdobduxiyc uqa Onjowudwikm ucaxwixef xo alkaehu zezun kaxbomiqadiolr oz souy BxevjEO eswjuvoyoexf. Pc oxafc gca .uqguyizmuyz kezazion, pao nop byepitx fuqdevoyz sfbzub evl juqehuefg qeh ndewanoq tiknepackh, awperbaxb faom OO’m zfiyuweruvl ovc zuojloonorahelg.
Additional Note on Historical Context
As you continue exploring SwiftUI and perhaps look at other resources or older projects, you might come across older SwiftUI data-flow tools used before iOS 17 such as @EnvironmentObject. The methods you’ve learned today take advantage of the latest versions of SwiftUI.
Wrapping Up the Video Demo
Congratulations! You’ve successfully refactored the budget tracker app to use SwiftUI’s Environment for managing text colors. By defining custom Environment keys and injecting Environment values, you’ve streamlined how shared data is accessed and modified across multiple views. This approach not only simplifies data management but also enhances the flexibility and maintainability of your SwiftUI apps.
Transition to Lesson Conclusion
Now that you have a solid foundation in implementing data-passing techniques in SwiftUI apps, it’s time to conclude this lesson by summarizing the key points.
See forum comments
This content was released on Jun 20 2024. The official support period is 6-months
from this date.
This lesson guides you through creating custom views and passing data from parent to child views. Additionally, it introduces the concept of Environment in SwiftUI, teaching you how to set and access shared data across an app. By the end of the lesson, you’ll have essential skills in building interconnected user interfaces in SwiftUI applications.
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: Introducing the SwiftUI Environment
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.