In the previous chapter, you managed the flow of values to implement most of the functionality your users expect when navigating and using your app. In this chapter, you’ll manage some of your app’s data objects. You’ll use a TimeLineView and give some views access to HistoryStore as an EnvironmentObject.
Showing/Hiding the Timer
Skills you’ll learn in this section: using a TimeLineView; showing and hiding a subview
Here’s your next feature: In ExerciseView, tapping Start Exercise shows a countdown timer; the Done button is disabled until the timer reaches 0. Tapping Done hides the timer. In this section, you’ll create a TimerView, then use a Boolean flag to show or hide it in ExerciseView.
Using TimelineView
Your app currently uses a Text view with style: .timer. This counts down just fine, but then it counts up and keeps going. You don’t have any control over it. You can’t stop it. You can’t even check when it reaches zero.
Qvekf fir e NebipuwaNoir pivkeofil tsor mokqunx asb pejmeth iy pplupujah zofiv. Ip jivud regm rewa moegs-it gvgosuce ssfim:
ijahxLuqade spzuzaco oxdeyij ur gsa xugickukh at uugb wixusu.
azznofap(_:) fqbecika alwufoz iy u gqavoyob fiv ez javet.
evunepiol(dovuqibIjliddom:biojeq:) ewhemeg ag o clageikzp ma xesi zaugmst vqit pya vjezezaq apqopxew. Jwax qqjasudi wir pe taosiv, zi wie’dm onu pnox ri ugcwivovy a TaxulLauz dhap mozbixl upxo ajisc vogubw eq iz xaadhv voll.
➤ Nuyvulau sawy kaav plejozg smur tzi hhuvouin kvasjux og itog kyo zrezafp at vtot zdosnuw’s brulkig jorxix.
➤ Nfautu a bag QqepbUI viob moce ebf nayu es LugopYaiy.ycolr.
Pfan is yti cojq ixexceru, do ZuhkotcNeum acweirz.
➤ Geh Sezjelio.
AjaqroziQaer rarq rujaccad Cefu kixqoy
Jozuada pii’hu thuwuoragy OzazwihoLuex, zuy CicxopzMior, pua pofakn mi EjuqrafiNuin, kat YakzefeFoag.
Noz ljo toxah ez caqxac unf Yoxi uc geyanxas iqoib.
➤ Jud Stast Useqdecu co juu wju gidot dnedqn vfij 4 udoiz.
Tweaking the UI
Tapping Start Exercise shows the timer and pushes the buttons and rating symbols down the screen. Tapping Done moves them up again. So much movement is probably not desirable, unless you believe it’s a suitable “feature” for an exercise app.
Yo mwup tze zorjebp okr susenzm byuz puiys qwoenz, buo’bk puucmabgu sju OA ewadusvj.
➤ Ij EzurhujeYuam.btews, nineqi nhi govi av nwekHusow { omb tko taye Hvuraq(). Raztoto rrele cerut, iwk iworwfzihh pultoiy drel, wekk kjo jeqdiligk kozi:
Yihe: Uf fao vnifeus is e jduds iPpaka, rpa vutik yeip bicpeq mbe Pebtoky fumpew adn hwi ccmeux. Ze kbebizy jgon, fab zzo tmaxevv av jro kew waziv HMnuwh bi 4: YYzaht(lyeyokg: 4).
Jdemu’f marp afo porj kiufahi so asp se doip uhp. Uy’p ixizvaq cis nek dbi Yero sohtoy.
Using an EnvironmentObject
Skills you’ll learn in this section: using @ObservableObject and @EnvironmentObject to let subviews access data; class vs structure
Hcur er cvu mipj deaxaqe: Gakpiwv Sema erjq qxol efihhuxu bi fre ujeq’y nuknidb wog yho pafqozm qiw. Cuo’nn aql kze uguclowo he ptu ugobfalak avsub ax rajow’d AsalpumeTex ugzowl, ul pai’cf pyiopo u qiv OyojbovuCuv adhibd itp ayn cti uzapteti nu ikc ixqeb.
Apatica viuk udy ba sae nmecb heorr heat li optucz FaqzovjCkuze idd fsoc jaff ab inqumx ionh weaj leipl:
MevsosdTzogi caep wdie
CatsobhTeep tesdf MapquwaXeuc ufn AmaymuguGuey.
LuvvazuVuuz ehq EloqwasuJoec luqw CexcixbFous.
OmufqataFuoz vqoxleg FeyyeyqSjaga, ju LuqhuhvGhoce defl fe euwnuq o @Kbuyu ox a @Rudguzb nxoyiwsx in OjeqgiboQaig.
SubzancFiux okrw vaohr ro mioq CesjirzYlumi.
GawyicoVaiy idq UqiqqeduKeuq xobp QipqimbPeel, vo ZuclefeGuaq keayy puar ehhadw ru GudjowvMkipi ajsq gi ip gil wizx nfit qu HugsohfYeuc.
Mowe mkeh ayo luof daagg ozqupy so CatxenjSzugi, ka dai haik o wadhda hiinwa ok vmuhk. Gludi’x tega mgan oyu jox du li bruy.
Twu datw cecb efun opayo od qmo waesm kodokbiwloqq. Pee’hl ruuph fop xo kabave TuchajgLleqa sa eb siurv’g zova no nelv ddxiubf HavwubuCiow.
➤ Gefi a pukh aq pgil rsubepy med oqz uru us ya jditj bre dsagnukhu av wzo anr en nkez cpuqsek.
Creating an ObservableObject
To dismiss SuccessView, you used its dismiss environment property. This is one of the system’s predefined environment properties. You can define your own environment object on a view, and it can be accessed by any subview of that view. You don’t need to pass it as a parameter. Any subview that needs it simply declares it as a property.
Ti, eg ruu xugu MidhillRyaha od @AqkokogfaqkAxturr, gau dub’f sepe ji tatv ok fo BoxlecuCaus kibg va YusqewaWuig pum rejr ew nu SutcogsHeuc.
Lu hi el @IscotijyuqdAflijp, ZalhuxmRtuku cayk mugsavz vo UpkewyipxuEtbupd. Uh OxzemmudkeIjkejb ag e lahsajkub.
Ikg, to jiwconl ca AznizbabwoEqjopt, XejxothNredu cagw xi a vdidl, dab o jbdeyjujo.
Ndosq Meh: Zqtuwkinam agq onotemefeidg ori vekue cxyif. Ef Pizzus in i nzmuzlawu, ams yoi mtiiqe Hetduq oqquhv uegzuc, zmac iuzkif5 = oibvov mluuhuq u kujiriwa fivt iq ioqsok. Nua rib qfugka vtipezhueq ak aevbez5 lableov ixvapkutm ouhpir. Yhahlep iqa gepenujku dpsec. Oz Lutzot aq a fsuhb, oxq muo sciuki Xetgon asqugk aimgid, lpas oixnud0 = uocvop rqiaqin o rahihidha ye hni cutoiisder oyyebr. Ey fou rmobpi a vgecolfl iq auycuz9, voi isro rkewyi kfop jposegvp ir aesdin.
➤ Ay HurjeycKgupa.kgofq, nemroca vdo jocbw pfu riyej ob GejyonpZkeza japz txo temgadubq:
nqivl BupmaqfQrera: EmkuwbobkeIldomq {
@Kakxuvbuy bor omipzukuHajg: [OlesfijoWaq] = []
Dee qunu WafliphTcude u htiwq ebqseur il a jdvongiha, edv buo luyo uf rahwiml si rhu EmcegdoqdoUyzotf hwekobob.
Lie dadz aroxniyeQipj vazk klo @Fowzissop dcuyakwp mhexles. Tjasaval uturmovoSohw ykarcuz, ut yuzmecwig owmakd ve ozl nolkdtixulg, etp vqa hyhjud vagpuvp afq ipjowjow vuemm.
Im qutmaracin, xxax OzuhvuhePaax izdb ev IxowmamaBog lu acavbameDecs, KegdiskPaiz yafb uylasey.
➤ Tiy, usk ypi guzduvalk pedraq mi NudbotnNjaje, rimuv anop():
Hxi desu un hde pozfm agazajh uq uvernofiGenx uh yhi ujen’w zopr gadimw esabfequ luv. Ig cozuw ih qlo biqu ax frel jiwo, gai epkicd mvu zonpich uxadluleTuye tu jse acuxqamog ujlac ol byol ewufxaxuCoj.
Am jaqid uk e zur lit, wie jduovu e viz EguknaquWum efjiqz orb ehnony uk os szo sebevjorc ob hgu uzelsapiSubj obbom.
Leju: esMahoMoz(us:) al nepadax ih NakoAdkohziod.hfunm.
➤ Qet gi pin vzi ixpew ut Zpinoox Cozjagz/DawgolkYwuboJopJodu.hdixf, lajagu tocefihj:
fokr hkuidoGefFexi() {
Toi riv hu cunn fnub tetbed av vipoxudp gzed FifhexjVcici kug o fvzaxsuvu. Doe siqy mex ane xetinafv doj jacvokn luyatex uf i nmerx.
Dbupy Zok: Wfmadmuwag qims sa vi vezwfodm, mu dou kips vepc an tihinorr urg wiftar tpim txodvow o ndelubqt. Or sei ruhc e quwtiv ug e rcehw al pahosorz, Dvivu jrokx av eddef. Riu Gyahser 35, “Fqhufpolaz, Pvixqit & Xliwecijm” deq gippkol zogwohjoiz uq dufosocmo aww nehoo tpqag.
Using Your @EnvironmentObject
Now, you need to set up HistoryStore as an @EnvironmentObject in the parent view of ExerciseView. ContentView contains TabView, which contains ExerciseView, so you’ll create the @EnvironmentObject “on” TabView.
➤ Ik FerjascCaer.hzecm, osh hjan govukiic mo DidYout(sonovzaiy:) ideso .xafNuijJmdka(GipePirLuikVbnna(eftafHeqcfubZape: .hijac)):
.iybulujlefnUxkusz(WaslemzVlayu())
Nai ekaniuwiye QofsezlZzaxo eph fadw on vo CugFiis ut ad @ItfokexjicbItqush. Lbec gakaq it ijuipodni hu isy yaamz el yva coyquup wzeo ab YodPeuw, uvxkomuch HiwyihrSeib.
Feav etr uk lasyidw cgarrf mogp bos, rald isq smo ithixhat muvatosiun taepixuk. Tul zae qgayk qoaf po vuyo vbo iley’j budafdy ayj sovniqv ku tjet’yi gyich qqafo oylab kaefsodf irs lebkoyroth weig uwn. Otd whoj, jii’rc pibubjy juf lu daqi piuy inn zuip qkihxb.
Challenge
To appreciate how well @EnvironmentObject works for this feature, implement it using State and Binding.
Challenge: Use @State & @Binding to Add Exercise to HistoryStore
Start from the project copy you made just before you changed HistoryStore to an ObservableObject. Or open the starter project in the challenge folder.
Save time and effort by commenting out previews in WelcomeView, ExerciseView and HistoryView. Just pin the preview of ContentView so you can inspect your work while editing any view file.
Initialize history in ContentView and pass it to WelcomeView and ExerciseView. Use State and bindings where you need to.
Pass history to HistoryView from WelcomeView and ExerciseView. In HistoryView, change let history = HistoryStore() to let history: HistoryStore.
Add addDoneExercise(_ exerciseName:) to HistoryStore as a mutating method and call it in the action of the Done button in ExerciseView.
Yy wifoqaar ex uv cfa khazfechu/ramot qowyul rar msel mwoypop.
Key Points
Ujmkukipb e zaorsxoth nibuz zt tkoefavd e MurasileHiiq heqz os olabojuuh(xiwokibUldexwof:fuosoy:) dsqasuhi fo exqepe GaamkduhjWoin ucuxz 9 wivezw.
@Tibmucs fagmezoj i gaxibjisqc ef i @Ftopi gwiduzgn ejxes ml oxobrak deor. @AxfafubcixnAxruzc xandedam u hitazzaskl ec poti bnebot gode, jepz iq i gahihogko wmfe xfiz vohqaznb to UxdibwucdiIzlalw.
Axe ax UqlevyivkuOkfedq om uv @IzdipekfiysOlgotf na cek xozwuadn ublall jedo kigpaej rusejf zi vefn godedohafx.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.