Welcome back and this video, we are going to add the added feature and also the swipe to edit so you
can see once you swipe over the background changes and there is this little icon in the background,
and once I slide over, you can see the edit screen comes up where I can then go ahead and edit entries
and I can go ahead and update this entry.
For example, I call this one Home three and then you can see it creates new entries.
So this is still a bug that we will need to fix later on.
But for now, you can see we have this added functionality in here.
And in order to add this feature, what we will do is, first of all, start with creating a new package
and I'm going to call this one utils.
So I want to put my utilities in there and I'm going to paste in this web to add a callback.
So despite the added callback, uses an import here, which we don't have.
So it should be changed to you.
Tutorials.
Thought happy places in my case, so whatever the director is that you have, and we're using this story
by Marcin Kittenish and he created this beautiful little.
Example here on how to create such a callback function so you can see abstract class, what to delete
callback, and that's exactly what we're going to use.
And you can generally see there is a little bit of code that you're going to need, and it's really
not that much.
Then he creates this feature that you can see here, which creates this delete sort of swipe to delete
functionality, which we also will implement later on.
But I thought it makes sense to start with editing.
And you can see he created this very brief or short version of it because it can be pretty cumbersome
to add this feature.
There are many different ways, of course, you can use a third party, library and so forth.
But I think what he did is pretty nice and it's a great example of how to get things done in very little
code.
And we're just going to use that.
So if you want to know in detail about what's going on here, you can, of course, read this article.
But other than that, let's just quickly look over it.
So he has created this item toucher a simple callback.
And he adds this item to help our direction so you can just say to which direction you want it to react.
So in this case, when we swipe towards the right, then swipe to edit, callback will be used and we
can assign an icon.
And I created this icon called Icey added White 24 DPI.
And it's just this icon here.
You can see on the left hand side, in order to create an icon, you can just go ahead and create a
new vector asset and then select a clip.
Art and I selected added one, so.
This one here.
All right, and then it creates this thing, this icon, and I called it a little differently, I called
it AC added White 2014.
All right.
And then basically what he did is he created some sort of with an hitier for the icons.
That's one thing, the background that he wants to use to color that you want to use here.
And I'm using this color here, which is this green color that we have in the background.
Then he applies the paint and here he limits the SWIP to added feature to a certain amount.
So he says if the view holder is 10, then he says that to zero.
So you can deactivate this functionality for specific items.
Then he uses on move so you could move the whole thing around altogether.
So this recital give you Holder so I could move it all around that something that you can do with this
move, by the way, then on Child Draw, that's where it becomes interesting.
So what he does here is he just says, OK, set the background to the background color and move things
around a little bit.
So draw the green added background, then calculate the position of the edit icon so that it always
is at the right spot.
So if you want to know even more about this class, then of course check out his website.
There you'll find a lot more information about that.
All right.
So the next thing we need to do is we need to bind this feature.
Does Swype to added feature to our recital of you?
So how do we do that?
Well, we go to the set up places.
Or heavy places, recycler view.
And in there, we create a new added swipe handler, so level headed swipe handler, which is going
to be a new object of the swipe to add it callback.
Now, this requires the context.
So we need to parse this.
And here I'm going to override that on swipe functionality.
So I want to override the functionality that will be run once we swipe over one of our view holders,
which means one of our list elements.
All right.
So what do I want to do in there?
Well, I want to notify my doctor about changes that I want to make.
All right.
So when I want to do something about an adapter, it makes sense to implement that functionality in
the adapter.
So let's go over to our adapter here.
Happy place.
End up to and in here.
I'm going to create a new function.
So let me create a new function, just.
Underneath here, and I'm going to call this function notify headed item.
And a little activity that will be passed here, so I'm going to call this activity, which will be
of type activity.
And then I need to position.
And.
You need to type it correctly, so then I need the position which will be of type integer.
And finally, the request code, which will also be of.
Type integer.
So let's import activity real quick.
So basically, I want to notify our doctor that from this particular list element, I would like to
make changes.
So let's create an intent which will use the context.
And it will send us to had a happy place activity.
Like that, now I need to import the intent here and now I can go ahead and put some extra information
into that intent.
So intent put extra and I want to pass on our extra police detail.
So main activity thought extra the details and then the list put this position, so to speak.
So here, list, position.
Now, by the way, list is, of course, the list of our adapter here, the one that is passed.
When we get this adapter, all right, so then.
I cannot start the activity for assault, so activity that start activity for result, and I need this
activity so that I know from which activity I should start the activity for result.
So, you see, I cannot stop this activity from the adapter itself, but the adapter is in our main
activity.
So it will start this activity for result from the main activity because the adapter is positioned in
the main activity.
OK, so then I can go ahead and pass in the intent and the request code.
That's why I need to request court when notifying added item.
So why do I need to request court in general?
Well, because I want to know whether I want to add it to the entry or what.
I want to do something else with it again.
And then finally, I may not forget to call notify item changed.
And that one for the position that we are looking at, why is that?
Well, if you make any changes to an adapter or to your recycler, if you want to make sure that your
adapter is notified about those changes, and that's exactly what we do here.
Otherwise, you will not see any changes until you restart your application or until, yeah, pretty
much you're close to the application and started again.
All right, I know we can go ahead and go back to our.
Main activity, so here and this added swipe handler where we said on Swiped, so when we swipe, what
we want to do is we want to pretty much call this notify added item.
So I'm creating a new adapter, which will be my RV Happy Places list adapter.
As a happy place, this adapter, OK, and then we can go ahead and call this adapter DOT notify added
item function that we just created.
So here we pass and our main activity as the activity and the view holder dot adapter position as the
position.
And then has to request code and underscore place activity request code.
So it's going to send us to the hard place activity screen and which can add it to activity.
So now, of course, the activity that will get this information, which is our ad happy place activity,
needs to know about what it should change.
So either it gets populated with nothing.
So either it's a completely new entry that it should make or it should get the data from another entry
and should update it.
So these are the two options that we have.
Right.
So let's go ahead and create a variable which will know about it.
So private var and happy places or place details.
So I'm going to call it and it will be a happy place model nullable object which I'm going to set now.
So now of course if the intent has some extra information.
So when we get to the screen via an Internet which has some extra details.
Then we can, of course, use that, but otherwise it will just create a new entry.
OK, so we need to go to our.
Create here and create a new entry, which I'm going to call if intent has extra.
And here we need to call our extra place details.
Or use this main activity, extrapolate details, which is this string here.
So if the extra information is extra details, then assign happy places with the details from it.
So intent get.
Possible extra brain activity that.
Extra details.
As a happy place model.
And of course, at this point, there is only one thing left, and that is to populate all of the details.
All right, so we have this update in view where we currently.
Only set on listeners to all of the different elements here, that state to TV ad image and so forth,
but what we also need to do is we need to make sure that if our happy place is details, so unhappy,
happy place details is not now, then we know that we are editing because if it is not, then we know
that we are just creating a new entry.
But if it's not now, then we know that we actually edit something.
So let's set the title of the action bar accordingly.
So here that title is going to be added Happy Place.
And then we need to set different variables accordingly, so I'm just going to do that here real quick.
So I'm setting the.
Added tax title to the title of The Happy Place, details that we were past the same with the description,
the date, location, latitude and longitude.
So basically what we did is we.
Now sent the information from whatever element we're currently at.
So whatever model object we're currently at, we set that over here, we send it into this screen and
then you can see it's automatically populated, all of that.
So the title and so forth, the image and then the text here at the bottom is now not create or save,
but it's update.
OK, so that's what we're basically doing here now.
In order to do the image, the update, we actually need to add those three lines here as well.
So here we are getting the information as a you I.
So we get it from our happy place details the image, which is a string, we pass it to a urai and then
we assign it to the image of you place image, which is this image for you here.
And finally, of course, the text is said to update.
So now, of course, there's only one thing left to do, and that is to use this added swipe handler
because we prepared everything pretty well.
But of course, we need to assign it here.
So added item touch helper.
So I'm going to call it and it will be our item touch helper object where we use our added swipe handler
and now we can go ahead and use this Otterton Touch helper to attach that to the recycle of you so attached
to recycle of you.
And now we need to pass and the recycler you where we want to attach it to and that's our are very happy
places.
A list.
All right.
At this point we can test our application.
And see if it's actually going to work.
All right, there we are, let's drag it out.
And as you can see, it works so we can swipe to decide it's really good.
And now let's see if it actually sends us to the added activity you can see here added happy place.
The text was changed to update.
Now, let's.
Enter to here and actually two, three, so that it's really a different one and it's update and this
can see we have this new entry here.
So, of course, we don't want to create a new entry each time we're updating, but we actually just
want to store the changes to the actual object that we started with.
Right.
And I'd say we finalized that and the next video.
But overall, just a quick repetition of what we've done, so first of all, we needed to find a good
option on how to create such a swipe handler.
And luckily, there are a bunch of good resources on this.
And, for example, this article by this developer here, as we've seen, and this is the SWAT to edit
callback, we can basically use it directly.
We just need to make sure that we change the item, touch, help or direction here.
And I'm just using.
Right.
And then, of course, we can define which editable or drawable we want to have here.
In our case, it's to edit one and then we can just use it.
So then we can go ahead and implement that or add that to our main activity.
So we need to prepare that SWIP handler.
And have this on swiped functionality in there where we just locate the adapter, which we are using,
should notify the added item, which is dysfunction that we call here, or dysfunction that we create
here, which basically just calls an intent and notifies our adapter about changes.
And here we start the activity for result with this intent.
So we basically just start the activity and we expect something to come back, which you don't want
to, of course, change for that particular position.
So our list element needs to know when any changes were made.
And then, of course, we may not forget to.
Use this item, Todd Schepper, and then attach that to our recycler view.
Right.
And finally, of course, when we get to the other activity, which is our ad happy place activity,
we, of course, need to make sure that we sent the right information over because, well, we did that
with this extra.
But here we need to receive it.
Right.
So at this point, we need to receive the information that we get.
Any additional information?
If so, then fill up our happy place.
Details favorable, which we set up here, which is of our happy place model type.
And then we can use that information that was passed to us to fill the whole view with the relevant
information so that the added title to the scripture and and so forth.
And then that's pretty much it.
All right.
So I'd say let's finalize this functionality and fix the box that we still have in the next video.