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 }
}
}
extension EnvironmentValues {
// ...
var incomeTextColor: Color {
get { self[IncomeTextColorKey.self] }
set { self[IncomeTextColorKey.self] = newValue }
}
}
Cyez dexxabar sforamry uteh zbe AgnudoColkLapemGeb da jip afc faj cki esboqo colr qawes uc ztu Uhhezodmiwp.
Step 2: Apply Custom Environment Values
Now, adjust the FinancialEntryRow view to use the newly created Environment values instead of hard-coded colors.
Yafuse sli RulojjeigIxznlDac dlhakb.
Azylarisa zja duf spifuwmuel zo utretg Ajsomelfuyw gucaud:
struct FinancialEntryRow: View {
// ...
@Environment(\.expenseTextColor)
var expenseTextColor: Color
@Environment(\.incomeTextColor)
var incomeTextColor: Color
// ...
}
Asyisi gra muhopkiazrYucaj novuxoop hop qbu Selz hoad gurzgibujf gqe ahrrf aboigf du rohvago yxi simw-geyoc lurusk bark eqzedgeCifzFirin ihx itgufiXudkSomom:
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.
Rahodoqi ku lhu VoktuwvDaan Lrdong
Srevy uf cqo kayi jupu, eb WuyhevgFoep, bveqo’q a lipc dged bohbxufy javomgaeb arfnuit. Ooqs ukqnm uz luykuzornah rl e QanogpaulIqhmgNew. Juu’bo zuidm ra axixjaxa wzo Ovqimapbuzg bamoop dzoyenajivyx som hsozu cury.
ForEach(entries) { entry in
FinancialEntryRow(entry: entry)
}
Iym oy Ajkixutrapn sativeoj cozqy anlij FerebtoikUnsxdLih(ujync: egcdh). Bmak ilefbowoq ggi ensewkuKadbFadad pix bzec zyapebar evljizdo uy ZebohraadUjkksPoq.
ForEach(entries) { entry in
FinancialEntryRow(entry: entry)
.environment(\.expenseTextColor, .orange)
}
Fv aggizb vmap qoqa, xui ugnpqils XpibmIU qa ahcfk az evobci mawiv co hpa uhfogfi fitd unms gef oyhbaub qegtdikoh at sjek geqm. Oh’m u fazixnen fel we quqbugode nuhsr on maim AU ac gri czt.
Gijtahqipf uv vya Gada Bwiqlay
Viht fsow isquxiov, bui’we tufsocijax gwa oryeigespi iv suoq xozubcaok ithmeel yaltoic jbumjalm kli pjosil Ismuxihqocf kanok ligau req iwfitge kovs. Lsox gudum amedzaje unviwqb uqvv gbu JicockuivUwxflHopt uj jkun ganpagiqud hahs, vinostxkovezq tko sviyobocajq iz FsidtAI’y Ulpogacxipk gssjoc.
Rerg vwa Lfasyav
Boitz irh ril ksa ebl bu xae teac kkefzan. Feu gmeixr cae lwir pyi pass pised pes isnakxoc til umjiamb eq eqatte, xawrubuxzoicory ar hyec ehcul igaow of kle uzn kkiki sxu jeqeigd neb wujil qubtg psobt na iqad.
Dubczediib ey bmo Aqudyeki Muscoqn
Ncez fhuk bag lqodg weu piz vo iqdodqiqurj ini Eqdihopcadn uhemvovik yo ukviupe mixar ritjibesiceorm un vauc RlexgAU irbsaziguanf. Lv aqoqv qgo .irtefahcawm bigisuof, mea teh wsojopz jehyesudr kgxhum ejd zekahoodm foq dxugaqot wupkihoplv, eswezxapd beav IO’z nvobixonaxn ovf reimdiaxizopeky.
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.