Repository Setup (MVVM Jetpack Compose)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to work on the repository for our mvvm jetpack compose application and we have a single layer that's the network layer so we only have one data source you know what instead of talking about it let me actually just quickly remind you what our architecture diagram sort of looks like so here we have the diagram that i drew a few videos ago we have our retrofit service which is our data source we call this our data source in a in a real application normally you'd have multiple data sources so you could imagine like there'd be an arrow coming off here an error coming off here to like different data sources but in our app we just have a single data source which is this retrofit service now the data coming from the retrofit service that's being serialized from the network is modeled by our recipe dto previously this was called recipe entity but in the previous video i kind of changed the way that i'm going to be doing the naming because dto is a common naming convention for data coming from the network basically data yeah data data coming from the network that's being serialized down to your business model so recipe data transfer object is what that stands for then you have the repository which we're going to build in this video looks like i changed the y there repository there we go and inside there that's going to have a reference to the retrofit service so it's going to be a constructor argument in the retrofit or the repository that will then get the recipe dtl go through this mapper down here and then it's going to spit out a recipe object so that's kind of how it's going to work repository will take the retrofit service as a constructor argument the data goes you know into the repository and then the ui is going to look at that repository it's going to be the view model actually the view model is going to take the repository as a constructor argument but we're going to be looking at that stuff later so go into the main package directory right click create a new package and call this one repository now i know i've said it many times before but i'll say it one more time usually i like to use use cases because then you can pass multiple data sources into it and really write out the specific use case that we're going to use but we have a single data source which is retrofit so really it's just too simple to use use cases i'm going to be using just a single repository yes i'm going to be doing another course after this one that's continuing on to it there's going to be a caching layer there's going to be use cases there's going to be a caching strategy we're going to be doing some unit tests that one's going to have use cases because we have you know multiple things happening given a certain event so that course will come after this one so if you want to know kind of the more advanced implementation make sure to watch out for that course join my mailing list go to codingfish.com sign up create a free account it's free like i said join the mailing list and then you'll get an email when that course is published so we're still going to do this kind of the best practice way just because to go through uh go through the correct processes it's just a good habit to have i guess so i'm going to build an interface first so i'm going to do recipe repository create that interface and now write out the functions that this repository will be able to do so these will be suspend functions because we're using kotlin co routines it's going to have two functions one called search the search will take the token the the authorization header for the the network token or the network request second parameter will be the page because we are using pagination for these requests and the third parameter will be the query so that's the the thing that you send the server saying hey i'm looking for you know recipes with chicken or recipes with whatever it's going to return a list of recipes now this is important this return type some of you might be thinking i would return a recipe dto here but no you don't want to do that remember at this point of the architecture the repository this is kind of like the ui and backwards the ui only cares about recipe recipe objects the ui should never see dtos it should never see entities it doesn't know those things exist as far as as far as it's concerned you always want to return your your domain model to your ui never an entity never a dto so now the second function will be the get request also it will take the token and the second parameter will be the specific id that unique id for that recipe and of course return recipe not recipe dto not recipe entity only the business model so that's our repository or our recipe repository that kind of shows what this repository is capable of and now we want to build up an implementation for that recipe repository so right click on repository again go to new kotlin file and call this recipe repository now this is another these are two different naming conventions i'm going to use you can do recipe repository implementation or recipe repository underscore implementation these are two common naming conventions you can choose whichever one you like i'm going to go with the underscore not for any particular reason typically i actually don't go with the underscore but i really want to emphasize that the this is an implementation of the interface since this is a beginner course i really want this to kind of stand out so let me just create that class i'll close this to give us some more room and of course we want to extend the recipe repository recipe repository interface that we just created now this is going to give me some warnings because i just added the implementation so i need to right click on there or sorry go alt enter on there select those two functions and now these are the two functions that we need for this repository now we need to add some constructor arguments so i'll open this up and do private value recipe service and do recipe service now we need our mapper so private value we'll call it mapper now here you could do recipe dto mapper or you could do the generic which is just the mapper or what did we call it i think it was domain mapper but then you got to pass the type so that would be the recipe dto and the recipe you could do that also because when we have hilt and we build out this this dependency you know it's not going to matter i'm going to use the actual implementation so that means using the recipe dto mapper like i said i don't think it really matters conventionally which one you do i'm just going to go with the recipe dto mapper so the actual implementation i'm going to give you some more room to give you a better view and let's work on these functions now these are going to be very straightforward since we've already done kind of all the hard work building the mappers and the dto and everything like that so i just want to do return mapper dot from entity list this actually i thought we'd change the name of that did i forget to change that in the last video should not be from entity list if we go into our recipe dto mapper the name of this should be oh i forgot to change the list names so we need to change the names of these so this one should be two domain so if i change this to refactor this should be two domain list two to two domain list and then the second one here should be two uh dto list or i guess from domain list from domain list i forgot to change those in the previous video my bad if you're confused you just look at the return type so this one's returning a list of domain models so we're going to uh two domain lists oops that should be two domain lists and this one is returning the dto list so we're going from a domain list so now i've fixed that we can go back to our repository and we should have our correct function name here and if you're confused about like which function to call you can actually write it out the long way which is what i recommend if you're confused just do you know value let's just do call it result do value result equals recipe service dot search you know pass the token pass the page pass the query parameter and then you can look at that return type what is that return type well that's going to be the recipe search response so of course i need to call dot recipes on there so what is this return type well this is a list of recipe dtos so now what i what i want to return is a list of recipes so i would just do return mapper dot i want to return to domain list because that's the type of list that i'm returning and then just pass that result so if you're confused just write it out but i can write this in one line obviously so if i cut that and paste it in there that's a one-liner for this request now the get request will be quite similar we're just doing a different request so return mapper dot should be two domain models so map two domain model and then just do recipe service dot get do the token and do the id that's it for our repository a pretty simple use case again we have one data source we don't have a bunch of stuff happening in our app it's pretty straightforward and if you want to be notified when the next course comes out with the database caching you know building up building out a caching strategy how to use use cases uh we're gonna be doing unit tests on those use cases so if you want to know more about that kind of stuff which is a more more realistic app that we're gonna be building make sure to go to codingwithmitch.com register an account it's completely free you'll get an email when those that kind of stuff when i start producing that content and uh and yeah so if you're interested in that make sure to do that and check that out do not forget to leave some engagement i noticed that a lot of you are leaving really great engagement but not a lot of you are hitting that like button so go down there there is a like button on every video it's probably a little gray thumbs up turn into a blue thumbs up i'd really appreciate that i noticed that a lot of people aren't hitting that like button it it costs you nothing it's free just like this video in this course it's free so go down there hit the like button and give give me something give me something out of this thanks for watching and i will see you in the next video
Info
Channel: CodingWithMitch
Views: 20,790
Rating: undefined out of 5
Keywords: mvvm repository, mvvm android kotlin, mvvm android kotlin repository, mvvm repository pattern android kotlin, repository pattern android kotlin, android repository pattern, android repository pattern retrofit
Id: SQ0czZdQPRA
Channel Id: undefined
Length: 9min 40sec (580 seconds)
Published: Mon Dec 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.