Adding and Deleting Articles to our Room Database - MVVM News App

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to a new video in this video we will set up the functionality to save and delete articles in our database so if you remember in our article Dow object we have that function to insert an article into our database and we have that function to get all articles of the database so we can show that in our safe news fragments and we also have that function to delete an article of course so first of all to start with this we want to go into our news repository and add all of these functions in here first of all that is a suspend function absurd and we want to pass the article that we want to insert into our database and that just takes our DB reference here gets the article Dow and cause absurd with the past article then we have a normal function so another suspend function because that returns our life data to get our safe news and that is equal to our DB not get article Dow get all articles and finally a suspend function to delete an article elite article we passed the article that we want to delete as a parameter and we set it to DB get articles au dot delete article and pass the article so that is very easy to do here next we want to go to the next layer so our news of your model and add these functions here so our fragments can actually call these first of all we have that function to save an article which takes that article that should be safe of course and since that is a span function we also need to start a crew routine for that also nothing new we need to use view models called launched for that and in here we want to call our news repository and absurd and then simply pod has the article then we have that function to get the safe news and that will just take our news repository and get the safe news because that gets safe news function in the news repository is not a suspend function we don't need to start a coup routine for that instead we will just observe on this function from our fragments so we are directly notified about changes in our debt database and finally the function to delete an article that also takes an article as a parameter and for that we have to start a crew routine again it's a view model scope that launched and call news repository dot delete article and pass our article here so for now that was very simple we just took our functions from the article Dao and implemented them in our news repository and news of your model but now we have to actually think about when do we want to call these separate functions so when do we want to save an article well we have our recycler view with the news in it and whenever we click on a single item then that item opens up in a web view and that article fragment with the web view also has a floating action button that adds this article to our database so that is whether it is easy to do because we just need to add an onclicklistener and our get safe news function is also very easy because we only use this to observe on changes in our database but for our delete article function we also need to implement that swipe to delete functionality so there is nothing that candy it can be done by default but it's also not too difficult so let's start to implement the functionality to save an article let's jump into our article fragment for that and here below that web view of Klawock we just want to call our floating action button our fab and set an onclicklistener to that where we just call our view model dot safe article and we save our current article so that is the article that was passed to this fragment as an argument so we get that from our arguments dot article and then we could also make a snack bar here to notify the user that the article was saved so pass the view as a first parameter and just write articles saved successfully and use snack bar dot length short and call it show afterwards the next step is to actually observe on our changes in the database in our safe news fragment that is also very easy to do just go below the newse adapter on item click listener block and here we call a view model dot get safe news and call dot observe on that here we need to pass our view lifecycle owner because we're inside of a fragment and has an observer so whenever or data in the database changes then this observer gets called and passes us the new list of articles that we can actually name articles here and what do we want to do with that list we just want to update our recycler view so we call our new adapter dot differ submit list and as our articles list and then our illicit affair will automatically calculate the differences of the new list and the old list and update our respective accordingly and to actually be able to swipe and delete articles we need to add what is called an item touch helper to our our recycler view adapter and that is also not too difficult let's do that above here and create a callback for that first so well item charge helper callback we have to create an anonymous class for that so object colon item judge helper dot simple callback and in the constructor of that we first need to specify the directions in which we want to drag our recycler view itself and then the directions we want to be able to swipe the items so for the directors so for the directions we want to be able to drag our sector view is of course up and down because that's how we scroll it so we pass item charge helper Dodge up or item charge however dot down and as a second parameter we pass the directions we want to be able to swipe our items so that will be item touch helper dot left or item touch elder dot right and then we can open curly brackets here and press ctrl I to invert instead we need for that press enter here so first of all we have the on move function which we will just leave empty we just want to return through here we don't need that function because that is called when we move an item but we don't even have any functionality we want to adapt to that when we move that item we just want to detect when the user actually swiped that item away so the on swipe function is actually the function that we want to implement here first of all we want to get the position of the item that we actually deleted here so that we actually swipe to the left or to the right so that's right well position and set that to view holder so that parameter is the view holder of the swiped item and call dot adapter position and then we can get the article the corresponding article that we want to delete in our database and we can get that from our news adapter that differ that current list at the index of that position and because now we know that article that we want to delete then we can simply call our view model dot delete article and pass that article but since we also want to have that undo functionality with the snack bar we also need to add that let's create a snack bar well snack bar but you don't even need a valve for that let's just call a snack bar dot make pass our view here and write successfully deleted article make that snack bar length long so we actually have a little bit of time to decide if we want to undo that and call apply afterwards and then we can add an action to the snack bar that is executed when we click on that undo button so we can call dot cell action the text of that action is undo and then we can open a new block of code there and in case you click on that undo button we just want you call our view model and save that article again that we just deleted and then simply all of that set action block we just want to call show so we show our snack bar and since that is only a call back now and not a real item touch helper we have to create that below here so we just create an item touch hover and pass our item touch up or call back here call that apply on that and attach it to a recycler view and that will be our RV safe news and then we can actually try it out and see if everything is working so let's jump into our emulator you can see our new so loading currently our safe news fragments is empty but if we click on an article here and hit on that floating action button then you can see article saved successfully let's also add another article here click on that floating action button save that and also save a third one if you're now going to save news fragment then you can see here are our three articles that we save to our database and we are able to swipe those now swipe to the left you can see successfully deleted article if I click on undo let's do that again click on undo then it will insert that article at the right position and it will immediately show up in our recycler view because the function in our article Dao returns the live data that automatically notifies our asam or observers so or article fragment on sorry our safe news fragment if there is a change in our database so whenever we add or delete an article in our database then this function will automatically notify all fragments about that change so I hope everything that clear to you if so please leave a like and comment below and also if there's anything you didn't understand please let me know in the comments I can answer your questions and if there is anything I can improve on then please let me know that to leave me some feedback that is really helpful for me to improve on my content have a good day see you next video bye bye [Music]
Info
Channel: Philipp Lackner
Views: 7,254
Rating: undefined out of 5
Keywords: tutorial, android, development, learning, programming, programmer, kotlin, beginner, news, api, retrofit, coroutines, coroutine, networking, database, room, mvvm, clean architecture, architecture, software architecture
Id: hMpP6N9LGFA
Channel Id: undefined
Length: 11min 6sec (666 seconds)
Published: Mon May 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.