Android Development Like a Pro

I really bad code base and when you have a requirement change you’re gonna get oh I need to modify to in order to change a single line awesome that’s gonna be great yeah so unfortunately we have to think at the same time we are delivering good stuff we have to think about making a reliable scalable maintainable code and for that I want to be talking about some patterns some architectures and some well ways to test them so we can get that goal of really usable you really reusable call I’m table code I know we all have code bases that are really really oh so in the idea whoa i will say throw away everything else together but that’s impossible so all the things that i want to show you here and this talk you can reuse it you can use it in your code base just you know when you are doing a new feature or you are touching our feature just try to apply one of those patterns that i’m going to show you but before going into that topic let’s get into fragments I said I was not gonna be talking about fragments but I I like it’s just gonna be five minutes okay it’s gonna be really quick real quick you know fragments have really good like really like a it’s a component that Google bill in order that we can reuse French analogies in different screens um so if the capsule eights full features you know and then they are awesome and they’re couple you can use them with loaders and that’s in task and the only the context and is real great right it’s what I call rid of the same pattern so listen this came up I was in a burrito and I have to stain I love I love everything burrito I just wanted to beat the chief here i think this chip is one of the best in the history of the gifs so I love I love burritos I love burritos but i have a problem right and it’s a BN a problem i can not stand cilantro it is like soup like soap sorry soap which is different so it is like soap it is really bad i didn’t know this because in a spain where I am from we don’t need cilantro didn’t know it and then I went to my 23 army and after a year it in cilantro and didn’t know what was happening to my body it says well some some people from Europe has this issue with cilantro and it was like really okay so why I’m saying that the fragment is like a burrito well so when I go to order a breeder in the Pharaoh leader and they put cilantro on it I have a problem I I just can’t eat it so how do how we are gonna take out which you’ll answer well they can know they have to build a new burrito because everything it’s coupled together you know it’s always you know in that awesome turkey and stuff but it’s all couple so that’s the same problem that it happens with fragments it couples everything together and when there is a requirement change you just have to change the whole thing going on on the front one so you know it’s good for reusability but it’s not good for the coupling your your your business logic from everything else that’s one of the cons of the fragrance but it is another one yeah you probably love this uh so this is the official documentation of the state machine of the fragment but in reality is more like this actually is even worse because it depends on activity yeah it’s a complex escape machine you know why why you do this to me I seriously like I know you are super smart but I just want to build stuff you know I don’t really want to know 20 life estates I don’t I don’t have that many years to understand them so yeah so I mean you are totally fine using fragments just you will get used to the fragment manager and all this stuff and the life cycle and blah blah blah but you can yes as professional developers you can user you can do your own right because what is the goal of the family go the Fremen was reused that piece of code in different screens right so you can do the same I just you know having a single activity with a navigation manager or having managed that many activities with reduce of abuse because this thing about it what you really need in another education in order to show something on the screen the context that’s it everything goes around the context so what you can do is just abstract everything have an entry point where the context is and then you can inject sorry inflate layer to whenever you need them so this is the idea that I was talking about we have a long you know we need an activity because otherwise there’s no application oh you know it I mean Alice

I mean we need an actress when I show something on the screen right so we have that activity and then we’re going to have that navigation manager and it could be as simple as having that URI and inches screen is going to be one path of that you are right so we are starting application will start screen number one division manager inflates that screen and at the same time I adds that path to the URI and then which is carried on the screen then the user interacts with that the screen and that starts the screen number to the launch activity is going to just get that even and it will just end up you get your manager append that and start on your screen it’s as easy as that ok so you can use fragrance but you can use all your own stuff that’s why we are professional developers and but we are done with reverence this is star about the real deal so let me explain it on a table or like a pro and for that that’s that’s me yeah let’s make an appt actually have an appt I have a code I have a repo and open source repo with all the things that I’m going to show you today so you are more than work with and in for kid so maybe ours and enjoy my code maybe yeah so this is the application you know you log in with twitter I work a tutor so you use favoring for ec login it gives you the API client and then I show the most retweeted links on your timeline and when you click one then it shows this web view which is really value I but it’s a demo okay so then you have the drover layout and then it shows the same dopey much is the most attunity images in your timeline that’s it that’s the application so let’s start with a I’m still going with the demo okay cool yeah so let’s start with one advice this is my advice try to the couple all implementation a try to a couple your app from implementation details and but that what I mean is if you hide the implementation by using a contract on interface what you get is that you can change the implementation details but the classes depending on that contract they are not going to be changed unless you are having a new feature but then that’s that’s normal right so in that application that i wish that i was showing you before i did i need much loaded right so the image so it could be an interface like this it loads a URL in 20 much field and I don’t really care how my team or I implement that I just care that I need that functionality so in this case you can use picasa or you can use glide or you can use fresco you can just do your own thing it doesn’t matter that’s not going to change anything using the image loaded construct so that’s my Fred back my first advice my second advice is let’s talk about the common UI because most of the applications have common UI my son let’s keep going so in this case I have as common UI to draw our layout and the toolbar right so we should be able to reuse that and then just inject the content in the part of the screen that we want for that we can use another contract screen container contract and it’s as easy as buying an activity and return of you cubby group that is going to be injected right and then get Rover layout because we want to show it and we want to use it in this case what you can notice you can have an effect I have actually half an effective activity which is an abstract TV that all my activities depend on extent on and there on the UM create what I’m going to do is exactly that is binding that activity so I’m said setting the content view for the common UI and then I’m gonna inflate that gay layout which is the abstract method that I want to implement in all the different activities so in that section of the screen when I’m gonna jet is the proper layout for each different screen right buying inflate and the abstract method so now let’s talk about patterns and architecture so all you’re going to start with MPP and then we are going to continue with clean architecture if you have questions you can keep it for the end it will have time we can do it offline let’s start with MPP that stands for model for you / center so you know how does this works well you have a view which receives the user interaction that notifies they use that sensor that the call to the percent are saying these interaction happen and

the presenter is going to call the model and in requesting the right data that the screen needs to render taba the model is going to be working on that and delivered identities to the center and the percentage will probably clear a lot of different type of entities that the screen can render in the screen so I’m going to deep on the code so we understand everything they key the key here is that each of those layers three layers they have the word responsibility the view only knows how to update and modify the UI the things that appear on the screen the presenter only knows what business logic model method calls when that event happens and the model only knows where to get the data process that data and send it back to the center so the present is kind of a proxy the model is the business logic and the view is just updating day one so in this case for this activity top images what would the npp look like well the view in this case I’m adding interfaces you don’t really meet them I’m just adding them because it’s you see to see the API so in this in this case I only really need to set a list of images those images is just a poacher that holds the data that I really want to show the screens which was the number of retweets the URL and something like that so in this case the bu it’s implemented by the activity remember that the responsibility of you it’s only to update the UI so it’s implementing this method and in the method what we are just doing is setting that content into an adapter and then that will show the images on the screen think about it if we change if we decided for example to not use the most tweeted links sorry the most tweeted pictures but we want to use the most favorite pictures sorry most liked pictures with change that then nothing is going to change because the beam is going to be the image is going to still be the same what is gonna change is the model and we see we’ll see that later yeah the logout you we don’t want to keep them logging forever they need they have the right to log out so we do the log out and then we are calling as you can see that is a percentage initialization and we can create because when the uncreate happened we have to render something the screen right so let’s talk about the presenter represented is really simple to in this case the application is simple it’s just going to be create and then set view we need to set view I was playing you why we need to set view especially to avoid memory leaks but we’ll talk about that later so as you can see the percent it has to be and then it has a model because he needs a reference both and call them both it’s the proxy is the money in the middle and then we’re going to have a list of views can you imagine why well the percentage is going to be the one keeping the state of the screen so when there is a computation change we can reuse that list of images we don’t need to call again the API or get the data again from database it’s just there we just loaded one so we don’t want to do the same work again and again and you know that’s exactly what I having create that if if that beer images is a Bray there just go back to the beam with a list otherwise model give me the motor to deliver cheese and head is the hit is the thing we need we need a weak reference otherwise we’re gonna provoke a leak because the bu is implemented by activity and I’m ending case that is destroyed we’re still the percentage is still holding that so that’s what we have we I implemented we call back you know that this is way better way to do this without the callback that I will explain you at the end of the talk but this is good enough this will stop there were like of course after some ethanol don’t do anything but that’s that’s better than leaking memory so let’s talk about the model the model is pleased it’s just one call in this case is get more tweeted images um what are you trying to do here well what I’m trying to hear is I need the data first right so that’s the client the custom API client that I have then those parameters

ugly parameters the apia to the API I just have to to use it like that and then I have that process tweet the address a little bit down but it’s it’s pointing to process streets processed which which is doing is I don’t really need tweets what I only need beams which is like the model the entity the energy that I using the screen to render that recyclerview so that’s what it’s doing is processing of streets and changing them to the other class that the UW understands so the responsibility is clear is just get the data and parses it to the right entity objects think about it if we decided to use two more another API is not going to affect the presenter nor the view this is going to affect the model so again this layer allow us to change the apartments fast of course if they are new requirements that we have to add near fall that’s that’s okay well we want to be flexible is with all code that we want to make if we want to make behave different ok so now let’s talk about test that’s your favorite part of course so just one thing I wasn’t born with a skill to be to write tests but neither write code so it’s a muscle that you build and you want to build it because I like to sleep at night that doesn’t mean that my code is not going to break but if the test is passing I’m gonna be you know I would I have a peace of mind so why because what I’m going to test is the business logic so I know that the process tweets in that case for the model is going to be working correctly that the interaction between b % animal is going to be working of course he can break because for that you need interaction you know I was talking about unit test but you you need intonation test and then you itís which unites are really pain in the ass but they are really helpful so we need to test how we’re gonna test that is it so the video we are going to bug the presenter and we are just going to test that is modifying the day under the UI properly the presenter is going to mock the view on the model and we are going to test the interaction between press enter via model and then that when certaine even happens on the view it’s calling the right event on the right method on the on the model and for the model you just tested business logic you don’t test the connection to the API if the connection to the API failed well maybe the server is failing i don’t know but maybe maybe it’s the api client hopefully nobody broke that layer than they were layer so that’s it that’s how we test this is kind of simple actually try to do that with a fragment and and call me if you if you are able to do it okay so you have some press you know it has three layers that allow you to have three different responsibilities and a couple them so when you have to change one of the layers usually you don’t have to change the other one the twos and of course let’s think about it right so we’re using a recycler you know but what happened if I have to change it for a listview probably I don’t want to do it because of psych reviews way better but if you want to do it you just do it you just go to the B when you change it and nor the presenter nor the model needs and change so the same if you change the API for the today’s of you change the twitter api for an audit API you know the effects on the model and the presenter it’s affected when there is requirement changes when I’m creating we don’t want to show the least we want to let them choose for example a topic that they want to see the most tweeted pictures right but that’s like a huge nearly feature so that’s okay the discounts off of course nothing comes for free that is what I prayed to wear up the layers of course and then at the end model and vu and presenter they are super tied to one use case so you don’t really can reduce them a lot you will need extra layers of separation and the coupling but we’ll talk that about that on the clinical picture but for now let’s talk about oriented changes or computation changes how you do that well as I was giving you the hint before the presenter is going to hold the state of the screen or the data that the script needs to render again so before that we just have to remember how that tivity life cycle works where are we are going to do is the oncreate on the oncreate and i have the code for all of this so you can check it out later on they own create we’re gonna go to a press Enter holder and we’re going to say do you have a percentage for this activity if you have then give it to me otherwise then I will just start to give you know I will distance and your percentage into it again and then what we know is that when the design oriented and change it goes to the unsafe in solid-state method at that point we are going to save the presenter in the percentage holder but of course we want to check if on the on destroy the activities finishing if it’s finishing we want to remove the from the percentage older because we

know that that activity is not coming back anytime soon so you can do this implementation in two ways one that I don’t recommend I mean yeah of course you can do whatever I mean you can implement all the business logic of your application in the application class if you want it met it make it a static it’s gonna be fun but so just don’t use the application for everything okay so don’t do that it’s a easy way to sleep I don’t do it you can you can do this you can do a presenter holder which is a single tone the spoiler alert yeah then you single don’t but in this case we are only keeping those presenter holders for a short amount of time and they are not changing any be hey you know that the presenter hold it is not changing at all so it’s just data for me and that’s how you implement it is really easy and it works you can even you know you can even do it better in the future and you can persist that on the database so when the users exits from the application but it starts again you can even show the state before they exited the application they’re going to be like whoa what happened here right because usually they don’t expect that we are keeping that estate cool so that’s how it works that’s how the presenter holder works and you can wire them up you can put put it on on the beer on activity on the oncreate you get the percent of that activity then you save it on their own safety and state and if it’s finishing your mobile you don’t want to keep that there forever cool let’s talk about cleaner architecture I hope you are enjoying this MVP but now let’s get a you know fragments is like a burrito clean architecture is like a bento it’s you know all really good food all really nicely packed they don’t touch each other you know they are work together really well for your taste right so we’re gonna do the bent of the architecture the clear architecture so you can read this long long article about it from one kebab i mean i think is very interesting that he never got into how implemented so it’s more like a theory i think it’s fine you can relate a story interesting that was so after reading many people how is implemented at all Android I think this is what I like the most we’re we’re actually using some kind of version in Twitter now and it works pretty well we will have the domain layer which I am sad about it because I really like the domain later but let’s get to the insides of the clinical picture so the presentation layer could be the be the vp part of the MVP yeah and with a twist of course you see the BU entity that I will explain ER and then the domain layer is going to be the use cases and then the data layer is going to be the repository it’s like just give me the data give me the data that I need for do my business logic and then send it back to the presentation mode so it’s actually like a kind of an MVP but with two more layers right yeah I pass many layers it’s like like like I don’t get the name now isaga right nice idea right you can put more lately and it’s better go to me okay so presentation layer so it’s like VP with the trees and the twist is the view entity we are not going to persist the presenter anymore the percentage could be bigger in complexes in complex up advice if you have a complex you I you can divide the UI different presenters and different in a smaller MPP so you don’t get be percentage but you don’t want to you don’t want to pursue percenter too much because it could be big but the view entity could be just the state of the screen meaning is that edit text with some extreme if it is just started on the view entity you know is that enable or disable then study the subunit as a boolean and then you match those views with data state so it’s like a POJO that’s it and in orientation changes you persist that view entity the same idea about this time on with the project really small project now let’s talk about interactors I like interactors the handle one use case and it’s one single try to maximize and for maximizer you severely try to put just one use case for example in that application we have this we have to get top articles right but get the particles could be even more small at the end what we are doing is getting some data sort it in a way that I want to render it so what we are certain it is sorting by retreats so that one I can actually create us a unique interactor use case and then reducing with the other screen that I have pictures okay so that’s that’s the cool of the Interactive’s

like a mathematical function you get an input and you get an apple you apply something on that input and you return and you get an output with the expected and the cool thing is that if you’re you know I really like to make them super small so that’s why I call the method execute I’m forcing myself to do one single thing and the documentation will be the test of course but the name of the class of the interface so what I’m doing is captive articles that’s it so this is the implementation this is not actual really interesting but you know you can see I’m using the Twitter repository that I’m gonna splain you right after this repository okay so repository we say we have the model which is the business logic and then is retrieving the data from somewhere actually though those were two different responsibilities what is retrieving data from somewhere and another is having the business logic which means process those tweets into the images that we want to show so that’s the repository the repository knows where to get the data from from different that the sources of you you can have you can have a database you can have a API you can have a cash right and then the interactive is what I call that to get that input that has to modify and send it back to the presentation layer cool so this is the twitter post story has get timeline and get to it right because that’s what we want we want to get the timeline and then we’re going to persist those streets and get the most retweeted images and our end links so it’s going to be as simple as that that’s that’s really cool and think about it now super simple it’s just an API but what about if tomorrow I want to have a cash in memory so I don’t have to keep doing that I don’t have to keep making requests all the time and what about if I want a database where the user can actually do some kind of action like put this retweet as my favorite right so that will be a nice that I want to store in my database so that will be all it changes on the repository that’s only the responsorial repository ah so that’s it that’s the clean architecture right easy presentation is a couple from the main interactor is just a function that gets an input and returns an output it could be a Java model if you don’t use anything that either it is providing to you could be even a Java module into your grade project that’s amazing right that means that you can actually test it easily and then you can reduce it to you can reduce it between different apps and then the data layer the repository the couple’s the rest of the app from any change in that sources and it just know where to get data from it could be a you too if you don’t use any other is the K component so that’s another Pro you know at that point what happened is what I’m saying like you get into this thing and say ok let’s add more more layers right let’s add like 20 more or whatever I don’t know that lets just a couple everything everything is an interface and and you know take you see don’t don’t do it because that’s what happened we like to come to make complex things so you remember fragments yeah sorry so let’s do / engineering the EDA it does so you have to be really careful and there is even more boilerplate than before because there is more later right so for that that is this free this is library that launch of you want to go that actually implement it in architecture and give you the base classes for your bills for your domain and for your repository and they use dagger so then you don’t have to do the boilerplate wiring the different layers it’s actually really good and it’s a company that is using that in order project it’s a consulting company so I recommend you to check it out it’s open source one more thing socal callbacks are really not powerful at all it’s just not in most classes you know strong reference to this blah blah blah they don’t give you any power the power comes when you put are examining you get actually if you were here for the belly you talk that was perfect example of what is the power of relics java and that’s what i’m doing actually that I’m changing all the code back sort of observables why well because I get the scheduling I get the back pressures I get the operators and for a simple application like that doesn’t make too much sense but imagine you have an application that in a screen you need like call to different API endpoints and you have to combine those data and at the same time you have to combine the data with the database so doing that with callbacks is going to be a covariance ancova and we tell each other so you know I really fluent api that you can read and you understand what is going on and then it allows you to schedule in one thread is another super

easy and it gives you observables that notion of a stream of data so listen I Rho is great the cops of the concept of reactive is amazing I don’t know why we forget it for so many years so check it out you probably all know about this and the last but not least i want to quote myself which is terrible but this is where you know I want you to know what I think about our position we are not going to keep we’re not we’re doing social I’m doing social apps okay but people has their phone 24 I was 24 hours in their hand even when they sleep they put in the nightstand so test your code be sure that your code is solid be sure that is delightful because otherwise you are affecting those people life so just think about it I want to give thanks to some people to help me and you can get the code in that build a link and if you have any questions I’m any value we can talk offline now and here we have time for questions maybe maybe a maybe I don’t know this just right see if you have questions yeah go ahead and give us first sorry ah you know again Google gives you a lot of really good components you are the one that should make the call if you think the pros are more beneficial to you than the cons and go for it right I so personally I don’t like to add logic into the XML because that remains to GSP or something like that and I don’t want to remember that but but you do for you you’re free to do to you see I mean it’s really I mean whatever Google does is awesome they are the best engineer or really talented people so if you want to use it in it’s good you have a question too sir um so he depends right if you want to use one activity then you need a class the handle side navigation for yet that is stacked for you and that was there could be you know what I was showing you the URI and append path and that part is actually the name the unique name of that feel and then you know what is going on right oh right that’s a question so there’s always have to be performing so that means if you try to not very inflated the book is gonna be better it depends on how expensive in memories though so you have to have always that balance type where they were talking at the beginning the keynote shed and remind like you think about what they do is we catch week as you stop we do things in a head of time so it’s faster but at the same time you ate the balance of we have a memory limiting the device right so so yeah depends but it yeah I will I will try not to worry inflatable you that I’m going to be using soon you’re hopeless right so yeah so what I was saying that the network in yeah sorry so the question was in case you want to do an Edward call in what layer of those architecture that I was explaining you will do it that will be the model or the repository Tom or the melee or the repository so that’s like the last of the layers that’s so only going to be there here and you want to do that of course in north in a worker thread you don’t want to do the main thread of course yeah yeah this so the repo actually you know the the repo there he has that example and it shows you how to make that Network they work call in one layer any more questions we can talk offline if you want ok thank you for coming