Chat with us, powered by LiveChat
Eternal IPTV great iptv sub

[Music] hey everybody and welcome to firebase semi life the show where we coded live here and you watch it later in the next few episodes were going to look into something that I am particularly excited about how to integrate firebase with Swift UI Apple have released Swift UI

at dub dub DC 2019 and it has changed the way how we built you eyes completely however there seems to be some uncertainty about how to integrate firebase in a swift UI application so today we’re going to fix this I’ll show you how to build a simple task

list application using swift UI combined firebase firestore firebase authentication and sign-in with Apple wow that’s a lot of stuff to get through we better get started but before we start writing some code let’s take a look at how the application is going to look like in the end

those of you who use the iOS reminders app will notice the similarities on the main screen you’ll see that we’ve got a header and a button on the bottom and when I press the button it will add a new line here for me to add a new task

so let’s do this let’s begin with implementing the UI at the task and let’s add another one and then we need to connect to firebase and then let’s add another one there is a big question mark what happens and in the end finally profit alright so hopefully by

the end of this episode we’ll have finished implementing the UI and one thing that I like to point out so if we have a look at what’s going on on my screen here let me show this so what we’re looking at is the firebase console and we have

a look at the firestore instance that is running has the back end to my application here and if we look into that you will notice that all the elements that I added to my task list already have appeared here so as one example let’s change the completion state

of this element here and if you pay attention to the completion State down there in the console you will notice that once I change it in my application it will be updated in firestorm awesome so let’s get down to writing some code so let me bring up Xcode

and then we will start writing the application let’s minimize this window this one as well and then we will get started with a new Xcode application we will use a single view application let’s call it make it so semi life life and we want to use swift UI

for the UI and let’s place it in this folder here alright so the first thing that you will see is that we’ve got our swift UI view here for the content view and this is the normal start for all our applications but what we want to do first

is we want to define our data model so let’s create a new folder to hold our data model so first of all a new folder here and let’s call it model and then let me add a new file and this is going to be a swift file that’s

called a task because we want to talk about tasks here so in our application we want to track tasks and those tasks have a number of attributes that are important for example they will have a title and they will also have a completion state completed all right so

why don’t we turn this into proper Swift code so first of all this needs to be a struct and then let’s close this here and then we will turn these into properties varible title and this is going to be a string and the completed state is going to

be a boolean right so to make our job easier let’s also define some test data that we can use while building the UI so let me hide this in a debug statement here and and then let’s create a collection of test data tests data tasks and it’s going

to be a collection of tasks implement the UI and let’s say this is true connect to firebase and this is false because we haven’t finished this yet and then a couple of question marks and let’s have prophet element as well all right cool so we’ve got some test

data here we’ve got our struct to keep the data that’s great so I know this is a rather simple data model and you might be wondering you know how to support advanced use cases for example like keeping track of the due date and managing multiple task lists and

you know maybe even sharing data with other users and stuff like that so these are all great ideas and we will definitely look into them and later episodes of the series but for now let’s focus on the simple things and just you know deal with tasks and whether

they’re completed or not right so what’s next oh I know we need the UI so let’s go ahead and refactor our structure a little bit more so let me move those elements here into a new group and this is going to be the app group and then let’s

do not group for all our abuse alright so let me pull this up here okay so we’ve got our app group or a model group and our view group and let’s go back into our content view and instead of using the name content view let’s rename it and

use something more meaningful so reflect a rename and let’s call it task list view hit enter to rename and there we go so let me increase the window a little bit so we can see more and then let’s have a look at how the UI looks like in

the preview so by pressing option command and enter we will open the preview window and it’s currently paused but by pressing option command P I can resume and then in a couple of seconds we should see how the URI currently looks like and then we can start building

the UI all right so currently it just says hello world and let’s change that and let’s change it to one of our tasks so implement the UI that was one over top one of our tasks great so in addition to showing the title of the task we also

want to show whether the task is completed or not and we can use an image to do it to do that so let me drag out an image view from the pallet here so search for an image and then drag it and place it to the left of

our label just like that and you will see that the source code hat has been updated currently we don’t have an image name yet but we can use a system image and let’s use circle and you will see that we’ve got a nice circle here and then ones

task is completed we will use a different image to indicate that this is not completed all right so let’s turn this some very simple cell into a list so come on click on the H stack and then select embed in list and then we get five elements and

you will notice it’s still just this one element but we will change this in a couple of seconds cool so we’ve got our lists we’ve got um a cell we also know need a button that we can use to add elements later on so again let’s use the

Poulet and then find the button and then drag it down here and you can see it at the bottom of the screen it says add button to a new vertical stack that’s exactly what we want so let go and you will see we’ve got a V stack the

list is in the view stack and the button is there as well so we want our button to say something like add new task and obviously and this needs to be in in a string and then you know currently it’s centered that’s one thing we need to fix

but we also want to have a nice icon to the left side of it so let me actually wrap this manually into a haystack and then insert an image here image system name equals plus circle dot fill this will give us a plus icon on Phillip circle that’s

great and then finally let’s change the alignment of the button to leading so we can do that by clicking on V stack opening up the UI inspector which in the Spader version of Xcode is broken but no worries let’s use our inspector on the right-hand side and choose

left alignment so everything is left aligned so that’s a little bit that’s add a little bit of padding and we do that on the button itself okay so that looks a lot better cool so one other thing that we should do is so currently the icons are very

very small and we should make sure that there are a little bit bigger so there are really nice touch targets we can do that by going in and changing the image first of all making it resizable and this obviously is a little bit too large so let’s go

in and change the frame – let’s try it 20 by 20 okay let’s see this looks good and let’s do the same with our image up here in the lists alright so all the images are roughly the same size which is nice because then they’re really nicely aligned

and finally let’s also make sure that we’ve got a nice title on the screen so we can do this by wrapping the entire current V stack into navigation view navigation view and then indent this so there is an empty space up there which will take our title and

then we will need to a title on first in a child which is or a V stag so a navigation bar title is tasks great alright so this is our you know very simple UI and now let’s go ahead and connect it to some test data so if

you recall we have our nice little test data tasks collection over here in the tasks file so what we want to do now is connect it to our list view here in order to do that we’re going to introduce a new property here and first of all let

me fix the indentation okay perfect so let’s introduce a new property here that tasks equals test data tasks that’s it and now we can go ahead and instead of iterating over you know just five numbers here iterate over the actual collection of data so let’s do this so

we’re you generating over tasks and now it complains that task is not identifiable so let me increase this so it says the initializer requires that tasks conformed to identifiable alright this is because the ListView needs to know which element is which so that when you’re inserting a new

element it can tell them apart and the elements don’t start jumping around so let’s go and make task identifiable so make it identifiable and how can we do that so because we need an identifier later on for connecting our tasks with with firestore let’s introduce an ID and

make sure that for now it contains a unique identifier and we can do that by using the UUID clause and get a user ID and turn it into a string like this all right and because we do this for all our tasks or a little collection down here

will automatically assign a unique ID to each of those test data items that we got so let’s go back into our view and now the compile error should be gone compiler is happy and no let’s go ahead and actually connect all those items to the test data so

first of all instead of displaying static string let’s rename this to task and then we can use it down here task Gorge title and when I say there’s the UI the view should update on the right hand side and you can see that is pulling data from our

test data collection that’s nice so before we also connect the completed state and show a different kind of image so off maybe a full circle instead of an empty circle for the completed state let’s refactor the cell a little bit so what we need to do is we

want to extract those two elements here are the image and the text into a separate view to make this easier let me go ahead and turn this into a haystack again because then we can just extract it oh sorry okay so fix the indentation and then come on

click on the haystack and extracts up view and we’re going to call it task cell great so we’ve got a couple of compile errors here that we need to fix so the reason why the compiler is complaining is that down here in our new struct it’s not entirely

sure what task means because task is unknown down here so we can fix that by inserting a new property here call it task that should fix the compile errors down here but obviously we’ve got a new compile error out here so we need to tell it to inject

this variable into the cell down there and let’s compile again all right bill it succeeded that’s great so now we can go into our task cell and make sure that we display the correct icon for the completed state so we want to display a filled circle when the

task is completed and a non filled circle when it’s not completed so currently it seems like all the tasks are not completed which is clearly wrong so let’s use a ternary if statement here so task dot completed and if it is completed we want to use check mark

dot circle dot fill and if it’s not completed we will use circle ok let’s see how that looks like and resume or preview all right so it works we display the implement UI tasks as this is completed because in our test data we said it was completed and

all the other elements are not completed so this looks very very nice and that’s it for our basic UI before we go any further let’s take a quick look at our architecture so let me pull up my preview application and let’s have a look at our architecture diagram

so you can see that up here we’ve got our views and the views uses or each single view uses one view model and in turn the view model configures the view so depending on the state of the new view model the V we’ll look differently so for example

if we think back or if we look back into our UI you can see that the checkmark icon is only they are because the view model for this specific task item said that the completed state is true so in this sense the view model configures the view so

where do the view models get the data from so ultimately we will want to connect to firebase and firestorm but we need a little helper class to make this more manageable and to help us build a better you know architecture so we’re going to use a repository and

the repository really is just there to make it you know easier to connect to firebase so what it will do is it will open a firebase connection of higher store connection and it will pull data and it will listen to updates and it will also take requests from

the view model to add new elements to firebase when the user edits adds new elements to their view so and you can see this on the architecture diagram that the repository intern works with the storage which in our case is fire store to store and reject when to

retrieve data and in turn it will return model elements which are owned by the view model right so let’s start and go ahead and implement on the view model for our task sell view so let’s first create a new group here for our view models and drag it

down a little bit and then we will create a new class you file and let’s call it task so view model alright so it’s going to be a class task cell view model and it needs to be an observable object this is so that Swift UI can observe

any changes that we make to the properties on this view model and it also needs to be identifiable because we want to display or we want to we want the ListView to use those tasks cell view models and as we saw before it is important to actually have

elements that are identifiable okay so each of those tasks cell view models will hold a reference to a task so a task but it’s not enough that we just hold a reference we need to make sure that this is published so that any change on the task can

be listened to so let’s use the published annotation to make this a publisher all right so we need another property that helps us to make this identifiable and this is going to be the ID and it’s going to be a string and then we want to also hold

the name for the icon that we’re going to use to indicate the completion state so that again is going to be a published property published variable completions state icon name and this is a string and let’s make this an empty string and also let’s make the ID an

empty string as well this will help us with the initializer as you can see in a minute alright so in the initializer we are going to do a couple of things so first of all the initializer will take the task and then we want to operate on the

task so first of all let’s make sure that we keep track of the tasks so let’s assign it to our property here and then let’s go ahead and run a map operation on the task and what we want to do is we want to transform the task into

a string and the string is going to tell us which icon to use so let’s use the transform signature here and we will have a task coming in and then we’re going to look at the task and if it’s completed we’re going to return check mark dot circle

dot fill and if it’s not we’re going to return circle so this probably look very familiar to you B because it’s exactly what we’ve been doing in the UI but we’re extracting this into the view model now all right so the next thing that we need to do

is we want to assign the result of the map operation to this property up here and in combine we can do this by using the assign operator and we’re going to assign this to a property of self and it’s going to be nation state icon name and then

finally we need to keep track of our our subscribers and assign them to accountable so let’s set this up up here private or cancel levels is a new set of any constable and in order to use that we need to import combined all right and now we can

store this in this cancellous collection cool so once again we are mapping over the tasks and then depending on its state we will return the name for the icon store this into this property and this is for memory management purposes down here ok so that’s the first thing

we need to do and then the other thing that we need to do is we need to keep track of the ID and this is going to look very similar so dot task dot map and and we’ve got a task coming in and we will return the task

ID and then we’re going to assign it to the ID property and then let’s store this into the consoles cool so that’s our task Cell view model so one view model down we need another view model so you might be wondering why do we need to view models

the reason is that we’ve got we’ve actually got two different views so we’ve got the ListView itself and we’ve got the cell view so this view model that we’ve currently built is for the cell and we no need to have a view model for the list as well

so let’s create a new class and name it task list view model and that’s impart combined and it’s going to be a class as well task lists view model and it’s an observable object it does not need to be identifiable because you know it’s basically the wrapper around

all the elements that we’re going to display in the list but it has a property to keep all those tasks Selvi models so tasks sell view models is an array of tasks sell view model and let’s create an empty array when we initialize we also need the constables

all right and then here’s our initializer and in the initializer for now we will still operate on the test data and we will convert all those test tasks into little tasks cell view models and this is actually pretty straightforward so task cell view models equals tests there are

tasks star map oops that was a little bit quick let me do this this way all right so for each task that we get we will basically return a task cell view model and we will just send in the current tasks cool so we’ve got our two view

models and now what we need to do is we need to bind them to the UI so let’s go back into our list view and what we need is a reference to our task list B model so let’s do this it is a variable task list view view

model and we will instantiate this and then so that we can bind the UI to it it needs to be an observed object this will allow Swift UI to listen to any updates that this is going to produce all right so missing argument for parameter task and call

oh yeah I know why because this is the wrong class so task list view model that’s right one okay so now instead of iterating over this collection of tasks that we’ve got we will instead access our view model and iterate over the tasks collection in their view model

okay let’s do it so task list view model and we need to iterate over the tasks LV models in there so compiler is still complaining a bit so let’s do a little bit of cleanup here so first of all this is going to be tasks so you model

and then we need to refactor the task cell as well down here so again we’re going to use an observed object and this time has called a task cell view model and it is a task so you model right and then let’s remove this because we don’t need

it anymore and then let’s fix the compile errors down here so first of all access the tasks down here and then change this down there so there should be fine and then up here we’re going to access this variable and our list cool so this looks good let’s

see if it compiles looking good so far don’t succeed it okay so let’s run the application and see if everything works as before and instead of running the application let’s actually you know use the preview down here because we can use this little Run button to run the

application and let’s see takes a little moment probably I need to resume let succeeded and okay it’s working so in case you didn’t you don’t believe me let’s also run it on the simulator compiling launching let’s get the simulator all right there we go so we reflect on

our UI to use a model view viewmodel architecture and this makes things a lot cleaner and it will lead to you know much cleaner code I especially in the views as you will see later on and it’s a lot easier to you know connect to different sorts of

backends which we’ll do in a moment so there are a couple of things that don’t work yet in our application and we need to fix them so for example we can’t new elements to our lists and we can’t edit the elements in the list and we can’t mark

elements as done so this is something that we are going to look into next all right so let’s go back to the code so the first thing that we need to do is we want to reflect our list items a little bit and the first thing that I’m

going to do is I’m going to turn this list here into four each and this is because we actually want to iterate over all the elements in our collection and then we also want to add one specific new element and this is going to be our editorial iment

if you will and this is why we need to take the list apart so first of all let’s just replace this with for each loop and the rest can stay the same and then embed this in two lists right so let’s intent this cool so we also need

to declare a flag that will help us to hide and show this very specific cell on demand when the user presses on this add new task button down here so let’s go ahead and add this as a stage up here so they or present a new item and

let’s say it’s false for the moment so we want to hide it initially and then in addition to this for each loop we will just play a task cell depending on whether a present a new item is true or false so let’s make a little conditional here if

present and your item is true then we want to display a new task cell and it takes a new task cell viewmodel and so that means we need to create a dummy task cell view model right here so let’s actually do that and then we need to create

a dummy task as well and it’s got an empty title and the completion state is false right so this is our additional cell currently it’s not being displayed and we want to change that so whenever the user presses the button here we want to show or hide this

additional cell up there and you can do this by toggling the present a new item boolean okay let’s have a look if that works quickly run the application okay when I press on the button it will show or hide a new element and currently that doesn’t do anything

because we’re still using a text instead of a text field so this is something that we need to change now okay so let’s go into our task cell and then turn this text here into a text field so let me just delete it text field and then it

needs to be bound to the title off the task and we can also provide a placeholder or text so I’ll enter the task title that’s what we want to display if there is nothing in there and then we want to bind the text value of the text field

to our title of the task in our view model so that’s the task cell view model and then a task and the title okay let’s try this again okay add a new element so you can see the place for all the text enter the task title and when

I click in here I can start typing and I can also tap into any of the other elements as well because we’re using the same cell for this editor cell and for any existing tasks that we display in our list as well which is exactly what we want

because we want to allow users to just tap into the list and then change the elements without having to turn on edit mode or whatever so this is exactly what we want alright so the next thing that we need to do is we actually want to listen to

any changes that the users make so that we can take those changes from from the cell view model and put it back into the underlying data structure so the first thing that we need to do is we need to add a little callback handler because we want to

listen to the uncommit event on the text field and whenever that is fired we want to call back into our view that can then take the task that we’ve created and send it back into the underlying data structure okay so first of all let’s add a new variable

up here it’s called on commit and what we want to do is we want to send a task now obviously it doesn’t return anything so if we do that and compiler will complain and it will say that you know there is a missing argument up here and there

is a missing argument down here which is fine because down here we actually want to handle the callback but up here we don’t want to do that because we already have an element we don’t need to add it to our collection and swift UI and combined are going

to make sure that any changes that we make to an existing element gets reflected in the underlying data structure so what we want to do is we will basically just provide an empty default implementation here just like that and this should fix the compiler error message up here

so that’s great the next thing that we want to do is we want to capture the uncommit event on the text field and actually let me make a little bit more space and hide the preview so we want to capture the uncommit event on the text field so

let’s go ahead and listen for on commit and then when we receive this we want to call this callback up here so self talk on commit and what we want to do is we want to send the task back down all right so whenever the user commits in

the text field so if they press the enter button for example then we will receive this callback and then we’re going to take the current task send it into our callback and then we can capture it in here all right so to do that I’m going to use

the fact that this basically is just a trailing closure and then we will get the task here and then we want to do something similar to this so we want to get the task view model or the task list view model and then we’d like to do something

like add tasks but there is no method like that yet so this will fail all right so let’s fix this let me just commit this out so we can compile and then we will go into our into our task list view model and implement a method that will

capture our intent so add task and we want to take a task and then we’ll just add it to our list of tasks list tasks sell view models however we need to first turn it into a task sell view model so let’s do that task view model equal

task sell task so view model and it will take a task task and then we can add it to our collection and we will upend it so it will be edit to the end great so let’s go back uncomment this so this works fine one more thing that

we should do is when the user hits enter we will insert the element into the lists so that means that the list will add a new line and that will look a little bit funny let me show you what this means so let’s run the application so we’ve

got our existing cells here we add a new one and I will hit enter and as you saw it inserted the new item into the collection swift you I picked this up updated the ListView and we still have this element down here and this looks a little bit

funny so let’s actually make sure that we hide this cell down here whenever we insert a new line and that’s actually pretty straightforward so let’s say soft precent and you know item dot toggle so this means whenever we add a new element we will hide or edit our

cell let’s have a look again okay let me add a new element okay and you will see we only have the new element the editor cell disappeared and when I add a new element it will show up again just as expected so that works great on what still

doesn’t work is updating the completed state so let’s do this as well and this is a little bit more it’s easier so let’s go into our cell down here and then we want to make sure that whenever the user taps on the image we will toggle the completed

state of the underlying task so let’s go ahead and add an on tap gesture and then get our task cell view model get the task get the completed state and then just toggle it alright let’s run the application once more and then I can just go ahead and

toggle the completed state of the items beautiful and that’s approach’ day folks hope you enjoyed this short video about how to build a simple task list using swift UI in the next episode I am going to show you how to connect this app to firebase and how to

store the task data in cloud fire store thanks for watching and I will see you in the next one you

Related Post

Leave a Comment