<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Patryk Poborca on Medium]]></title>
        <description><![CDATA[Stories by Patryk Poborca on Medium]]></description>
        <link>https://medium.com/@patrykpoborca?source=rss-ef5bf72f8620------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*UF4nDQJCw-HJwNvMkd1LnA.jpeg</url>
            <title>Stories by Patryk Poborca on Medium</title>
            <link>https://medium.com/@patrykpoborca?source=rss-ef5bf72f8620------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 22:58:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@patrykpoborca/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Helping Multidexing and Compilation Speed]]></title>
            <link>https://medium.com/@patrykpoborca/multidexing-and-compilation-times-418e98a7d286?source=rss-ef5bf72f8620------2</link>
            <guid isPermaLink="false">https://medium.com/p/418e98a7d286</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Patryk Poborca]]></dc:creator>
            <pubDate>Tue, 08 Mar 2016 06:30:05 GMT</pubDate>
            <atom:updated>2016-03-08T16:00:57.983Z</atom:updated>
            <content:encoded><![CDATA[<p>So a common story in modern android development is hitting the dreaded Multidex limit. Luckily enough for us starting sdk 21 there is a very easy to follow <a href="http://developer.android.com/tools/building/multidex.html">native solution to the problem</a>. However, not a lot of applications are lucky enough to set their min sdk to 21 just yet. So during development compilation speeds are atrocious, usually between 45–90 seconds! So what are our options to this situation?</p><ol><li>Product flavors. This is a google stamped solution and is usually an easy quick fix to your issues. And it’s actually discussed in the link above…</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/350b5b7c1f8c9db5daaa1d59bf5e2123/href">https://medium.com/media/350b5b7c1f8c9db5daaa1d59bf5e2123/href</a></iframe><p>2. Product Dimensions. This is a slightly less known approach since it’s not used too frequently. This is useful if say you have several flavors for Premium, Free, Brands, etc. Instead of having to create a Dev Flavor manually for each variant you can use dimensions to generate your permutations for you!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c3bc89b747848fdd0f2ccef42ffaa29/href">https://medium.com/media/8c3bc89b747848fdd0f2ccef42ffaa29/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/418/1*9BqW911bsNWSh0-Ca9ZjJw.png" /></figure><p>As you can see, the build variants are a result of a permutation between the UAT/Developer <strong>(version)</strong> flavor and the BrandOne/Two/Three <strong>(brand) </strong>which saves you the effort of having to maintain the permutations manually.</p><p>3. BuildConfig function calls.. this one is a bit more obscure. Basically we just check to see if we’re building release or debug, if debug then set the minsdk higher</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0fcb722913d092e75f22aad36fb6c29e/href">https://medium.com/media/0fcb722913d092e75f22aad36fb6c29e/href</a></iframe><p>Alright, that’s all I got. I hope that one of these options helps you out in your time of trouble (65536 breaking point)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=418e98a7d286" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A small Rx+ Kotlin tip]]></title>
            <link>https://medium.com/@patrykpoborca/a-small-rx-kotlin-tip-c9abb925021d?source=rss-ef5bf72f8620------2</link>
            <guid isPermaLink="false">https://medium.com/p/c9abb925021d</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[rxjava]]></category>
            <dc:creator><![CDATA[Patryk Poborca]]></dc:creator>
            <pubDate>Thu, 04 Feb 2016 20:56:15 GMT</pubDate>
            <atom:updated>2016-02-04T20:56:34.821Z</atom:updated>
            <content:encoded><![CDATA[<p>A situation I have seen come around is where you might have multiple ways to manipulate a stream based on the state in your application. The normal way is to simply store the Observable in a variable and then do a switch (when) statement or an if statement.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38035f37610a19e5426a8fbb0280e3e5/href">https://medium.com/media/38035f37610a19e5426a8fbb0280e3e5/href</a></iframe><p>Not very fun, however this is where the <strong>let</strong> operator comes in… (Keep in mind this is a very arbitrary example)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/561bc32827c085044e219d362867fc56/href">https://medium.com/media/561bc32827c085044e219d362867fc56/href</a></iframe><p>With the first approach, you are saving the stream in a variable in a sense distracting the reader of your code. Additionally as you add more if statements or more divergences in your state you will end up with switch/if statements plaguing your code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05594b8ec01686cea0aec6a66d34f723/href">https://medium.com/media/05594b8ec01686cea0aec6a66d34f723/href</a></iframe><p>As you can see, we have a stream with many different paths based exterior states, however you never really exit the flow and reads like an instruction list. So next time you have state based on user progress in your app or other various things, don’t forget about the let statement!</p><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c9abb925021d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Junit testing and Android dependencies]]></title>
            <link>https://medium.com/android-news/junit-testing-and-android-dependencies-e65e66afce61?source=rss-ef5bf72f8620------2</link>
            <guid isPermaLink="false">https://medium.com/p/e65e66afce61</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Patryk Poborca]]></dc:creator>
            <pubDate>Thu, 15 Oct 2015 14:13:29 GMT</pubDate>
            <atom:updated>2015-10-15T14:25:05.399Z</atom:updated>
            <content:encoded><![CDATA[<p>Today’s article is going to be a quick blurb in regards to a common incorrect statement that</p><blockquote>Oh wow, you have to be careful with your android dependencies, otherwise you won’t have POJO Viewmodels or Presenters</blockquote><p><a href="https://github.com/patrykpoborca/JunitMockitoExample">Repository for this article</a></p><p>POJO is an acronym for “Plain old Java Object” quite literally it means no non-java framework dependencies, so no android classes such as LatLng or SparseArray. Often we want to be able to have our VM or Presenter push strings that might be located in XML to the view. However we’re concerned that the dependency will stop us from being able to run our tests. Fortunately this is not the case, we can mock Resources in our VM and still have Junit tests.</p><p>A while back the Android package has been stubbed out for the purpose of Junit tests and classes are no longer final. This means that you can write mocks in order to utilize the class in you business logic. I have written a repository where a presenter after having a FAB clicked will push a message to the screen. In this past I would’ve thought that was the end of my Junit tests, but now that’s true!</p><p>With Mockito:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2249efdf74a032d01456b8d33ab4016e/href">https://medium.com/media/2249efdf74a032d01456b8d33ab4016e/href</a></iframe><p>Without Mockito</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/db77b7af7c6f83a406ee8bb91fc0c8d7/href">https://medium.com/media/db77b7af7c6f83a406ee8bb91fc0c8d7/href</a></iframe><p>Where this is our mocked Resources class</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e3bd746e1f73a400446f931c568ebef4/href">https://medium.com/media/e3bd746e1f73a400446f931c568ebef4/href</a></iframe><p>As you can see, in both cases I’m accomplishing the same thing, providing my Presenter with a mocked Resource class, and giving it a fake string. This string is obviously arbitrary and we’re done! That’s all it takes to mock an android dependency. You can throw in an override for getDrawable,or getDimensions.</p><h3>How to run a JUnit test</h3><p>Really quickly I will outline the steps since many people seem to skim over it. (It’s super easy!)</p><ol><li>Create a folder called “test” with a java directory:</li></ol><pre>app/src/test</pre><p>2. Switch your build variant to UnitTests</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*Fp8L15TGb_g214Yc-lkIAw.png" /></figure><p>3. Right click on your App directory and select “Run ‘All Tests’”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/764/1*tsU3rbWX07aHFAehOulcNw.png" /></figure><p>That’s all! Now each time you run your app simply switch your Run configuration from “app” to “Run all tests” and run your project, your tests will execute</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/654/1*joZJvSyw5urrtP8FwT4uHg.png" /></figure><h4>Done.</h4><p>Thanks for reading, if you enjoyed this or my other articles don’t forget to subscribe/favorite me here, or if you aren’t that into medium I have a <a href="https://twitter.com/patrykpoborca">Twitter as well</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e65e66afce61" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-news/junit-testing-and-android-dependencies-e65e66afce61">Junit testing and Android dependencies</a> was originally published in <a href="https://medium.com/android-news">AndroidPub</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dagger 2 and Base Classes — Generics and Presenter injection]]></title>
            <link>https://medium.com/@patrykpoborca/dagger-2-and-base-classes-generics-and-presenter-injection-7d82053080c?source=rss-ef5bf72f8620------2</link>
            <guid isPermaLink="false">https://medium.com/p/7d82053080c</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Patryk Poborca]]></dc:creator>
            <pubDate>Thu, 10 Sep 2015 16:44:09 GMT</pubDate>
            <atom:updated>2015-09-10T16:44:09.726Z</atom:updated>
            <content:encoded><![CDATA[<p>So this is for all of you that have already messed around with Dagger 2 and/or ready my previous articles (<a href="https://medium.com/@patrykpoborca/making-a-best-practice-app-4-dagger-2-267ec5f6c89a">Dagger 2 </a>&amp; <a href="https://medium.com/@patrykpoborca/making-a-best-practice-app-5-clean-architecture-testing-84a1672dd000">Testing</a>)</p><p>A problem that presents itself when you write a base class which has a Generic is that Dagger 2 doesn’t know how to go about creating an injection class for it. Case in point:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/22f0ac7cd5824ce68b40d2a2d24a6e12/href">https://medium.com/media/22f0ac7cd5824ce68b40d2a2d24a6e12/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e1ba579182193f6466a1c1191a3b061/href">https://medium.com/media/0e1ba579182193f6466a1c1191a3b061/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec8c39adcd8024f171542091f9d2dfea/href">https://medium.com/media/ec8c39adcd8024f171542091f9d2dfea/href</a></iframe><p>So given all this, based on what we know, all should be okay, but then you hit Rebuild and you get this dreaded error:</p><pre>Error:(7, 17) error: io.mydemo.ui.widgets.test.MyClass has type parameters, cannot members inject the raw type. via:<br>io.mydemo.ui.widgets.test.MyComponent.inject(io.mydemo.ui.widgets.test.MyClass myClass)<br>[component injection method for type: io.mydemo.ui.widgets.test.MyClass]</pre><p>What this error is telling us is that your injector does not play nicely with generics. So in other words the moment we change our injector to this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ec6ef747276c0e0ec1e8e679f0109dd/href">https://medium.com/media/7ec6ef747276c0e0ec1e8e679f0109dd/href</a></iframe><p>Our problem is gone! But… here’s the thing, this doesn’t help us in the grand scheme of things. Now we have a tightly coupled Inject method to a specific version of MyClass, when in reality I just want to be able to inject MyClass regardless of what its Generic is so my Subclasses get what they need.</p><p>So something not everyone knows, is that any injectable fields in the Super class will be injected when the subclass is injected. (Provided that you have the right dependencies set for the injector)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50d107a4b4c3bde9cd55aff1422f91c6/href">https://medium.com/media/50d107a4b4c3bde9cd55aff1422f91c6/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/204c5fad1d773c57e071a3d88f663565/href">https://medium.com/media/204c5fad1d773c57e071a3d88f663565/href</a></iframe><p>The above will work. However if you look back at the MyClass.java implementation, you realize that we have a public field meaning that any subclass will have access to the fields. Leading to bad practice, you always want to have getter’s in situations like this. Simply because you don’t want the consumer to concern themselves with the workings of its super class. So what are your options?</p><p>First one comes in the form of a helper object. Case in point, this is a snippet in an upcoming demo application:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e953595ca8d18eede2f7e0a449d05ad/href">https://medium.com/media/0e953595ca8d18eede2f7e0a449d05ad/href</a></iframe><p>And here’s the UtilityWrapper Class</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/73574128ef124c3b01b04524e6a1698e/href">https://medium.com/media/73574128ef124c3b01b04524e6a1698e/href</a></iframe><p>And there we go! Now we have our Base Presenter class be able to be “injected” in the background.</p><h2>Alternative</h2><p>As we discussed, public fields are bad! You don’t want your Subclasses to be exposed to them. So how do you fix this issue? Well say hello to method injection! It’s a topic I skimmed over in the previous articles. Basically methods with this inject annotation will be called if Dagger has their dependencies. Of course we’re trading exposed fields for exposed setters, but inmy opinion it’s a lesser evil.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ea14e4fbf7d96fd2e331ff7727e4b10/href">https://medium.com/media/4ea14e4fbf7d96fd2e331ff7727e4b10/href</a></iframe><p>So with that, anytime you inject a SubClass it will now have the setPresenter method called which will allow your getPresenter to return the provided Presenter. Done! There are ways you can enforce people not trying to set your Presenter/Other objects in sub classes is to add flags to your method. Simply a boolean that once it gets injected renders the setter useless. Or simply throw run time exceptions! (JK you probably shouldn’t do that)</p><p>So what do I recommend? Well it depends on your situation, if you might not have your Subclass getting injected, then you should probably consider using the wrapper since the base can inject the wrapper itself. However if the subclass is ALWAYS going to inject itself, then perhaps the method injection is the route to go.</p><p>As always if you enjoyed this article or any of my other ones please subscribe/recommend. Or follow me on <a href="https://twitter.com/patrykpoborca">Twitter here</a>. Additionally feel free to leave comments here, Twitter or in the reddit post.</p><h4>Fin</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7d82053080c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making a Best Practice App #5 — Clean Architecture & Testing]]></title>
            <link>https://medium.com/@patrykpoborca/making-a-best-practice-app-5-clean-architecture-testing-84a1672dd000?source=rss-ef5bf72f8620------2</link>
            <guid isPermaLink="false">https://medium.com/p/84a1672dd000</guid>
            <category><![CDATA[rxjava]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[dagger-2]]></category>
            <dc:creator><![CDATA[Patryk Poborca]]></dc:creator>
            <pubDate>Thu, 27 Aug 2015 21:59:35 GMT</pubDate>
            <atom:updated>2016-08-12T07:42:13.748Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://medium.com/@patrykpoborca/making-a-best-practice-app-4-dagger-2-267ec5f6c89a">See Dagger 2 here — If you are familiar with design patterns you may wish to skip down in the article to the testing segment.</a></p><p>Even though it’s possible to make tests for even the most poorly architected application, it’s important to understand that having business logic embedded in your Activity/Fragment/Custom View you will have to take additional steps to make sure you can even test the app. Case in point:</p><p>Say we want to test the functionality of fetching a tweet, in this scenario we would have to use <a href="https://developer.android.com/training/testing/ui-testing/espresso-testing.html">Espresso</a> to mock the user action of clicking the button, if the response occurs (is displayed in the view), etc. Not only is this dependent on Android, it’s also depends on Espresso, means convoluted tests, and longer load/deployment. Meaning that your build server running your tests each time your developers push will take longer and be more likely to fail tests in bizzarre ways.</p><p>This is where application architecture comes in. The purpose of application architectures is to modularize your business logic, data layer, and view layer so that you can test your application and not worry about each module’s implementation. In some ways it’s separate black boxes interacting with one another and reacting/responding. The base architectures that mesh well with Android are:</p><h4>MVP — Model View Presenter</h4><p>Simple explanation, the <strong>Model</strong> are classes which represent the data of your application, such as User Profiles, Messages, Reservations, trips, and any other objects useful in your application. In essence anything that can be represented in JSON, they may also contain helper methods. <strong>View </strong>is the Fragment/Activity/Custom view layer of Android, meaning it represent what gets rendered to the screen. <strong>Presenter </strong>is the object which dictates how the View is rendered and how it behaves. It also reacts to changes to the Data layer (Network/Db’s, Models) and pushes changes to the View.</p><blockquote>The View has a direct reference to the Presenter and the Presenter has a reference of an interface which the View implements. The presenter may also implement an interface if you want total abstraction. The View should never have business logic inside of it, it should push input to the Presenter and wait to be told what to do by the presenter.</blockquote><p>There are two variations of this, Passive view and Supervising Controller-Presenter. The passive view requires for the Presenter to be larger than some are comfortable, it’s a bit of a god object telling the view precisely what to render while still containing business logic.</p><p>Meanwhile the supervising controller will allow the View to react to Models being changed, and relying on direct instructions from the Presenter when it comes to complex logic. Thus allowing the View to be slightly more aware and the Presenter to be less bloated with code.</p><h4>MVVM — Model View View Model.</h4><p><strong>Model</strong> is the same. However the <strong>View</strong> is in some ways even smarter than the Supervising variant of MVP, it has a reference to the View Model class. <strong>View Model </strong>is like the Presenter however it has no reference to the View. We NEED some sort of data-binding (See RxJava) in order to have the View react to changes to the View model/ Models since the View model has no reference to the view. This is a rapid development approach, however it is prone to mistakes where a user might embed business logic into the View making unit testing not as complete as it otherwise might be.</p><h4>Interactors (MVP-I, MVVM-I)</h4><p>This is not a stand alone architecture such as MVVM and MVP as much as best practice. It is important to understand that the View Model and Presenter can still become tightly coupled with 3rd party libraries such as networking/data caching/disk writing libraries and service APIs. This means that you can write wrappers around them so that if you swap the library you will not have to refactor the whole app, just the implementation of your wrapper. So what are Interactors? Interactors are bundles of several 3rd party libraries <strong>or</strong> wrappers around those 3rd party libraries. Each Interactor represents any DB/Network/etc calls the View Model/Presenter will need to make.</p><p>This is convenient for when you begin testing because even though you can mock your Networking library, or any other library for your whole test suite, sometimes you want your View Model/Presenter to have unique mocks of some call, you can simply create a MockInteractor which inherits from your VM/Presenter’s Interactor.</p><p>I think it’s helpful to look at the code of my Dagger 2/Clean architecture repository as there you can easily begin to tell the key differences between each implementation.</p><p><a href="https://github.com/patrykpoborca/CleanArchitecture">Repository</a></p><p>The above repo demonstrates no-architecture, MVVM and two variations of MVP <strong>(Passive view MVP, and Supervising Controller with an Interactor — MVPCI, stands for Model, View, Presenter Controller, Interactor)</strong></p><h2>Testing</h2><p>This is what everything has been leading up to. We saw how Dagger 2 helped us decouple our dependencies from our app, but the advantages offered are not so amazing that you’re excited to jump on board the hype train. Assuming we architected our application to have our business logic separated away from our Activity/Fragment/Custom Views we run into the issue where during our tests we might rely on an Android library (meaning no POJO tests for us) or simply the network calls take long to come back and may be difficult to encapsulate their logic in a simple Assert.</p><p>This is where mock classes come into play, and it’s simple to do. Back in the previous article we had OKHttp, Retrofit, LocalDataCache, and TweeterAPI classes. These all had delays on their trxjava observables of 2 seconds. Meaning that their “lag” was simulated, but as in the real world when testing you would be hit by a 2*N (where N is number of API calls) lag on your tests. Since most test suites number in the dozens if not hundreds of tests this is simply unnacceptable. Every single second matters, additionally the <strong>LocalDataCache </strong>has a dependency on the Android object <strong>Context.</strong> This was done on purpose to show how mocking can allow us to decouple ourselves.</p><p>One last thing, before we can go about mocking the classes I listed earlier, in the previous article I had all of my RxJava observables observe on the</p><pre>AndroidSchedulers.<em>mainThread</em>()</pre><p>Because I wanted to demonstrate POJO JUnit tests I needed to remove the Android dependency. Thus I created the mainScheduler variable in my classes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e0134a009c6f0b0ae61d25b56ad5108/href">https://medium.com/media/7e0134a009c6f0b0ae61d25b56ad5108/href</a></iframe><p>This required for me to change my NetworkModule since it was the provider of this class…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/091b01cbcf2da83b8835cb75b1dc3f66/href">https://medium.com/media/091b01cbcf2da83b8835cb75b1dc3f66/href</a></iframe><p>Now if you remember in the Dagger 2 article, parameters must be provided by dependencies. As such I created a ThreadingModule.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/37b28dface8d3cb54d28bf92cae62617/href">https://medium.com/media/37b28dface8d3cb54d28bf92cae62617/href</a></iframe><p>Alright, with that background we can move on to the mocking of the classes I created to drive the application.</p><p>MockOkHTTP — Switched the raw response from some some generated UID to a static text “Mocked raw response:”</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e68209beb38d468032b606108e8d9e5/href">https://medium.com/media/7e68209beb38d468032b606108e8d9e5/href</a></iframe><p>MockRetrofit — Removed the delay on both the <strong>completeRequest </strong>and <strong>fetchSomePage</strong> methods</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc8f4abad0172895f5c1ffb9d25118f1/href">https://medium.com/media/dc8f4abad0172895f5c1ffb9d25118f1/href</a></iframe><p>MockLocalDataCache — Removed delay on <strong>fetchRecentTweets</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e7b487b90d7297ea09b231d51ed641b/href">https://medium.com/media/4e7b487b90d7297ea09b231d51ed641b/href</a></iframe><p>MockTweeterApi — Removed delay on <strong>login/logout</strong> calls</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b47c0454ae6eee4fed121a6f8e7018ca/href">https://medium.com/media/b47c0454ae6eee4fed121a6f8e7018ca/href</a></iframe><p>Because the delay in our application was artificial it was simple to remove it, however in some cases you might just was want to replace the returned values with static ones such as I did with my mock OKHTTP. Alright, so we have removed delays and dependencies on Android from our “apis”. We will begin by setting up a folder structure as such:</p><pre>/app/src/test/java</pre><p>This is where our POJO tests will go. Next in order to have the proper IDE support and to run our tests correctly, on the bottom side of the left pane of Android studio you should see “Build Variants” click on this, and switch from <strong>AndroidInstrumentationTests</strong> to <strong>UnitTests</strong>. This allows us to compile and run the tests in the newly created directory.</p><p>I personally create a folder structure as such</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/1*t0FwlmD6-oK3Zdy9EhXYAg.png" /></figure><p>As you can see we will be working with Dagger again, we put our mocked classes (which I listed above) inside of <strong>mockimpl, </strong>tests go in their own folder. And finally we have this helper folder. In order to make my test classes less bloated I created a quick little helper. This class does all of the Dagger building allowing us to keep lines of code required for Dagger to a minimum. Additionally it has a little helper method called “waitFor” which allows us to wait for work that may be done Async due to RxJava. This is important for my androidTests implementations.</p><blockquote>To run these tests, simply right click the “java/tests” folder and click run.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ralb9SmesQOZ91CPg-zPgA.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e5eb3a91482fd66a3a43623ae567af7/href">https://medium.com/media/9e5eb3a91482fd66a3a43623ae567af7/href</a></iframe><p>In the above code you can see though that I’m providing mock modules to my builders. This is because this allows me to inject my mock classes into my Presenters/ViewModels. Please go to my repository if you want to take a look at their implementation. I do not want to clutter the article with code more than I have to. Alright onto the tests. First I’m going to look at the no architecture implementation to show you why it’s bad [<a href="https://github.com/patrykpoborca/CleanArchitecture/blob/feature/nested-unit-testing/app/src/main/java/io/patrykpoborca/cleanarchitecture/ui/PlainTweeterActivity.java">source code here</a>]. For one, you cannot add any tests for it into the src/test directory because you cannot decouple its Android dependencies. This means we need to run it on an Android device and put it into the src/androidTest directory. Next, we will have to utilize Espresso to mock user interaction with our Activity and check the View to see if the API’s did thier job and the View was changed accordingly. You can see the test down below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/899e7e3857fd00121e6d3d95ecc774d1/href">https://medium.com/media/899e7e3857fd00121e6d3d95ecc774d1/href</a></iframe><p>So a little bit about this and all other tests. Each method labeled with a <strong>@Test</strong> annotation is considered to be a “test” I could clump them all together into one large method. However it’s best to split the tests into logical groupings. Such as a test for testing Tweets, Login, etc.</p><p>AndroidJunit runner will call each test method found, and see if all the Asserts it contains are passed. Additionally it will inform you of any exceptions as well. In cases such as ours, we want to set up the class to be ready to be tested, that is where the <strong>@Before </strong>annotation comes in. This method will be called BEFORE any test methods. There’s a matching <strong>@After</strong> annotation which you can use to tear down any objects that you don’t want persisting between your Tests.</p><p>Finally, there are “Rules” in tests, these are written in order to have reusable logic between tests. So instead of having to utilize the Before/After annotation each time for a rule, these <strong>@Rule</strong> annotated Rules will hook into the Junit’s Before/After calls. This is simply a reusable code for tests.</p><p>So back to the test, as you can see in my @Before method I use my TestHelper to inject my Activity object, then I check to see if the View behaves as expected, checking to see if Views get populated, and with what, if my LoginContainer gets shown/revealed upon login and logout.</p><p>My MockRetrofit/MockTweeterApi get directly injected into the Activity and that’s that. So what are the problems with this? I have to mock user interaction, inspect views to see if they changed, and worst of all I’m working with Espresso to test business logic. It’s ugly and prone to errors. Average test runtime= ~3 seconds on Genymotion on my computer for JUST this the listed tests. Accounting for my other AndroidTests for MVVM, MVPCI, and MVP my test take between ~20 seconds to compile, deploy, and run.</p><p>Let’s take a look at the MVVM test to see if there’s a light at the end of the tunnel of testing. For this we can actually utilize the src/test directory. This is using POJO, meaning no Android dependencies.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f125029b72fac801db791cab6ff95b8f/href">https://medium.com/media/f125029b72fac801db791cab6ff95b8f/href</a></iframe><p>In this case, we have our MVVMTest class plays the role of the TweeterActivity and binds to the ViewModel’s observables. We simply check what the ViewModel sends our way and use Assert to check to see if it’s what we expect. We check to see if fetching a tweet works, then see if it comes up in a recent tweet list, see if logging in / out works. It’s all very straight forward and not convoluted. In my mind MVVM tests are the easiest to work with, and which is why I think of MVVM as the rapid development architecture. That’s it, it’s quick and concise.</p><p>As you can see, the decoupling of View and Business logic makes testing more direct and easy to read. It is important to see how testing differs between MVVM and MVP. Since in MVVM we simply subscribe to the observables provided to us by the view model, we don’t have to worry about the View logic too much. However in MVP we don’t utilize data binding between the view nad presenter we must worry about the callbacks featured in the View’s interface.</p><p>In MVP, we must create a MockView which the view of MVP implements. This is important because in MVP variants the view is told what to do via method calls which it inherits from the interface. So first we create a mock view (Alternatively we could’ve had our Test class implement the view’s interface)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5011413033f3faf3a5116d44109d29e0/href">https://medium.com/media/5011413033f3faf3a5116d44109d29e0/href</a></iframe><p>As you can see, the mock is mostly just a wrapper filled with methods which simply have a flag (Boolean or Object) which you can utilize to to check if the method was called or determine if a certain result occurred besides the method being called. In the test below that I check what I expect, against what the mock view receives.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56b99a35623a6e3f515ccda34236f925/href">https://medium.com/media/56b99a35623a6e3f515ccda34236f925/href</a></iframe><p>One more thing, the purpose of the <strong>TestHelper.waitFor </strong>method is to allow the async calls from RxJava to occur. Since we removed the Delay it should be near instant, the waitFor checks every 5 ms, it doesn’t even make it make it to 10 ms (Waits for up to 50 ms by default). It seems to happen only in the emulated android phone because it’s slower then its POJO Junit test counter parts. I kept the waitFor in the POJO tests though because if it appears to be a race-condition and I want my tests to be consistent.</p><h3>So what’s the purpose of these ‘Tests’?</h3><p>Well it’s pretty simple if you think about it for a moment. Tests are useful because you want to:</p><ol><li>Think through your application and make sure the business logic is indeed testable and readable. Meaning that you didn’t write obscure code and you also put the code inside the proper module (The presenter, View Model as opposed to in the View)</li><li>You want to make sure the application behave how you, THE PROGRAMMER expects it to. Case in point, while I was working on this demo application it slipped my attention that the fetch-recent tweets operation was incorrect and was <strong>simply returning the oldest tweets. Not the newest.</strong></li><li>For later down the road if there’s a application update you push, or a change to the underlying libraries you use (say you switch from Retrofit to Volley or something of that sort) you want a test which tells you if the app is still working like it was before the updates. Simply put the tests will start breaking telling you that thing aren’t working how you expect them to.</li></ol><p>Alright, thank you for reading through my series, I hope it’s informative and helpful. If you have any questions or comments (ahem.. even hate mail) feel free to <a href="https://twitter.com/patrykpoborca">tweet me</a> also I’d appreciate if you follow me too. Or simply make a comment here!</p><p>Looking for an Android Contractor? Look no further, contact me at: info@koziodigital.com</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=84a1672dd000" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making a Best Practice App #4 — Dagger 2]]></title>
            <link>https://medium.com/@patrykpoborca/making-a-best-practice-app-4-dagger-2-267ec5f6c89a?source=rss-ef5bf72f8620------2</link>
            <guid isPermaLink="false">https://medium.com/p/267ec5f6c89a</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[rxjava]]></category>
            <category><![CDATA[dagger-2]]></category>
            <dc:creator><![CDATA[Patryk Poborca]]></dc:creator>
            <pubDate>Thu, 27 Aug 2015 21:56:05 GMT</pubDate>
            <atom:updated>2016-08-12T07:43:04.395Z</atom:updated>
            <content:encoded><![CDATA[<p>Edit* <a href="https://www.youtube.com/watch?v=JNbz_rgdQ10">Video relating to this article</a></p><p>So this article is going to be about understanding <strong>dependency injection</strong>, <strong>testing</strong> and the various sorts of <strong>architectures</strong> available to you. For some of you, this may be the first time you hear about these topics, for others you know the indecisiveness that comes with trying to pick which DI library to go with and what design pattern to pair it with.</p><p>Let’s start with the topic most of you came here for first, Dagger 2. Dagger 2 is a dependency injection library (DI) which was created by Google, based upon Dagger 1 which was created by Square. Dagger 2 is slightly more performant according to some blog posts but it’s negligible. The biggest reasons to advocate Dagger 2 are:</p><ol><li>Dagger 1 issues with Proguard</li><li>Dagger 1 generates unreadable code making debugging a pain</li><li>Dagger 2 has better scoping to my understanding</li></ol><p>So let’s get started and talk about what DI is, and how to use Dagger 2. To first understand DI, you must understand the principle of <a href="http://stackoverflow.com/questions/3058/what-is-inversion-of-control">inversion of control</a>. In essence it’s the idea that your class should not create new instances of classes that it depends on. This does not means you can’t instantiate Strings, but rather more complex classes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b91cac6f1bd98608b0ef182707a9bf8/href">https://medium.com/media/4b91cac6f1bd98608b0ef182707a9bf8/href</a></iframe><p>As you can see above, that by having myClass rely on MyDependency we have created the beginnings of a <a href="http://mathworld.wolfram.com/AcyclicDigraph.html">Directed Acyclic Graph (DAG)</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fczMu_NVjwMcDge0vsRQwQ.png" /><figcaption><strong>MyClass</strong> now depends on <strong>MyDependency</strong></figcaption></figure><p>We have started the path to a potentially large and unforgiving dependency graph. Say we have dozens of classes which have a variety of dependencies, what happens when one of these has its implementation changed? A concrete example being the example below for a slightly larger dependency.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BFFDm1IAGVlH5EAoU2eOcw.png" /></figure><h4>Bad Implementation</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa2854c576b01baa56a3b994d965d5a9/href">https://medium.com/media/fa2854c576b01baa56a3b994d965d5a9/href</a></iframe><p>But why is the above a bad implementation? This is because the moment the implementation details of <strong>MyClass</strong> or <strong>MyDependency</strong> change we will have errors with how we’re instantiating <strong>MyClass. </strong>Now this isn’t an issue in a super small scope, but say we have several class which depend on <strong>MyClass</strong>. That means that the moment implementation changes, we will have to refactor code in N different places.</p><h4>Good Implementation</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b826db048784b801a8ca2a96609a14b/href">https://medium.com/media/0b826db048784b801a8ca2a96609a14b/href</a></iframe><p>Alright, that’s great, we basically did the same thing as we did with <strong>myClass</strong>, now what? Well ideally it would be nice if there was a factory object which could provide our dependencies for us..</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b7f4104438b43da49d8d74c4eb91f3c/href">https://medium.com/media/2b7f4104438b43da49d8d74c4eb91f3c/href</a></iframe><p>Well that’s great, but I just wrote a large amount of boiler plate considering the simplicity of the classes which will only grow more complex as we get more and more dependencies! Just thinking about manually managing this in a growing project scares me!</p><p>To clarify, we would no longer have to depend on things being provided at the constructor level, we could rely on our factory…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f2a24489f8d5509e8cf37d0a2220f432/href">https://medium.com/media/f2a24489f8d5509e8cf37d0a2220f432/href</a></iframe><h3>Hello Dependency Injection</h3><p>This is where dependency injection comes in, instead of having to write this factory method, and then access factory provider methods to grab instances of objects I depend on, I can simply annotate injectable fields and Dagger 2 will provide me an instance of this object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d7cb264491f4a6897a29d8bf667aa93/href">https://medium.com/media/6d7cb264491f4a6897a29d8bf667aa93/href</a></iframe><p>And just like that, our dependency injectors knows to have its <strong>Factory</strong> provide instances of those objects. Dagger will examine these fields/methods/constructors and then instantiate/call them based on whether it has the ability to “provide” their parameters. This is where you come in.</p><p>In order to get Dagger 2 running in our project we need to do two generalized steps:</p><ol><li>Create a DAG</li><li>Annotate injectable methods</li></ol><p>You just learned how to annotate methods, so it just comes down to making a DAG. Dag as linked above is just a representation for dependencies, so that this way we happen upon an injectable field, we know how to provide anything our annotated classes need, no matter how deep. Dagger 2 uses <strong>Components</strong> and <strong>Modules</strong> to illustrate a DAG. I’m going to try to help you visualize what their purpose is, hopefully my paint illustrations help</p><h4>Modules:</h4><p>Modules are the providers of the factory that I listed above, modules are <strong>logical/arbitrary groupings</strong> of provider methods which Dagger will utilize in order to provide instances/parameters to injectable fields/constructor/methods. In fact a module has a annotation for its methods called <strong>@Provides</strong> this is what you will use in order to annotate your provider methods to help identify them for Dagger 2. The only parameter the <strong>@Module</strong> annotation takes is the <strong>includes</strong> list, which is a list of modules that our annotated module extends its grouping to.</p><h3><strong>tl;dr Modules are grouped nodes of DAG where the nodes are the @Provides methods.</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ijuGipBarHU9DVH1MZh2aQ.png" /></figure><p>Implementation</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc35630fbb657695e9a83b14b2f7fb41/href">https://medium.com/media/fc35630fbb657695e9a83b14b2f7fb41/href</a></iframe><p>As you can see it’s the class <strong>MyModule</strong> which for our case is the grouping of nodes with the word “My” in their name. As you can see for the purpose of this demonstration this module is very arbitrary. So now we have the nodes of our DAG.</p><h4>Components:</h4><p>Components are also nodes, but their <strong>dependencies</strong> define the arrows of the DAG. Despite being nodes, they represent a <strong>FILTERED</strong> version of the module. So if the grouping of nodes is the module, the component is the gate/road which permits providers to be used outside of their module and components which list them as their module. The implementation of a component is an interface which has methods which will be implemented by a generated class in Dagger called <strong>DaggerMyComponent</strong>. Any interface method will expose its return parameter’s provider. Additionally, any interface method with a parameter will be essentially an <strong>injection</strong> method, a method to which you pass an object with injectable fields.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1e1a0ccd2f25021db58c6166573a704b/href">https://medium.com/media/1e1a0ccd2f25021db58c6166573a704b/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ngDjQjYaI1GQpkmxxGfnXw.png" /></figure><h2>Let’s Elaborate</h2><p>The above snippets are available in the <strong>demo</strong> package in my original <a href="https://github.com/patrykpoborca/MVPDagger2">MVPDagger2 demo.</a> However we’re going to use my <a href="https://github.com/patrykpoborca/CleanArchitecture">Clean Architecture</a> repository for the remaining examples of this article, the next article of this series will go over the architecture/testing aspect of this repository. [I highly recommend taking a peek at the Clean Architecture repo if you’re not familiar with the subject matter]</p><p>Okay, so you somewhat understand the concept of <strong>Components, Modules, and DAG’s.</strong> But how does this help you, how can you utilize it? We’re going to dissect Tweeter API and its related components. Let’s set the stage. If we were writing a Tweeter API, what would it need? Well we need some sort of API to hit endpoints, that’s where <strong>OKHttp</strong> comes in. So let’s create a mock OKHttp class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/33954a2bc56bfc10abe57e2d515fb8ad/href">https://medium.com/media/33954a2bc56bfc10abe57e2d515fb8ad/href</a></iframe><p>Next, how are we going to deal with getting this data, parsing it and returning it to our calling Activity/Fragment/View? Well that’s where our <strong>Retrofit </strong>class<strong> </strong>comes in.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9846449e82cd4b0d1661e25c9cb79bfe/href">https://medium.com/media/9846449e82cd4b0d1661e25c9cb79bfe/href</a></iframe><p>Finally we can create a simple Tweeter API, right? Well, what about caching fetched tweets so they can be pulled up without hitting the the endpoint. That’s where we have our <strong>LocalDataCache.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42a4e919afc3b2a2654e23de180a02be/href">https://medium.com/media/42a4e919afc3b2a2654e23de180a02be/href</a></iframe><p>And there we go! We have created all the classes that our Tweeter API will rely on, so let’s create our Tweeter API, all of the classes it depends on are completely finished.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3bb29dd1c2f990013cfac7de40b3b4d4/href">https://medium.com/media/3bb29dd1c2f990013cfac7de40b3b4d4/href</a></iframe><p>So right now our mock classes simulate getting an arbitrary response from server, doing work on it via retrofit, and then formatting it via our Tweeter api. Or if we want, we can grab recently fetched tweets from our <strong>LocalDataCache (LDC).</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*_TxVGmbwOgVuvfvPGBC3bA.png" /><figcaption>Right now the application component just exposes the module’s <strong>Application </strong>provider, meaning that any dependency on this component or injectable object will have access to it.</figcaption></figure><p>So where do we start implementing Dagger? Well, since our <strong>LDC (LocalDataCache) </strong>requires a context so let’s create an <strong>AppModule &amp; AppComponent</strong> to expose <strong>Context</strong>.</p><h4>Module:</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c83b38d64de6c51d7dae83966931daa/href">https://medium.com/media/3c83b38d64de6c51d7dae83966931daa/href</a></iframe><h4>Component:</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/25a35d9ac9ada3ef8ca2f932bea70df1/href">https://medium.com/media/25a35d9ac9ada3ef8ca2f932bea70df1/href</a></iframe><p>With our <strong>Application</strong> being available to be used by any dagger components which depend on <strong>ApplicationComponent</strong> we can proceed to create two new modules.</p><p><strong>LocalModule:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed32fa6465889b8e02ce1003e838f1b9/href">https://medium.com/media/ed32fa6465889b8e02ce1003e838f1b9/href</a></iframe><p><strong>NetworkModule:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ace239058a34af3bb88323d28389d27d/href">https://medium.com/media/ace239058a34af3bb88323d28389d27d/href</a></iframe><p>Notice how <strong>providesRetrofit </strong>and <strong>providesDataCache</strong> both have parameters? Dagger’s annotation processor will figure out if it can provide those arguments. Due to Application being provided from ApplicationModule and OKHttp coming from the same module we can can create <strong>LocalDataCache</strong> and <strong>Retrofit</strong>. If Dagger finds out it can’t provide an object as a parameter you will get an error during compilation.</p><p>So, now if you wanted to have these completely decoupled, you could create two different Components <strong>NetworkComponent</strong> and an <strong>LocalComponent </strong>to expose their respective provided objects. However, for the purpose of this app, I want any component which needs access to the <strong>OkHTTP, Retrofit, and LocalDataCache</strong> objects to only depend on ONE component.</p><p><strong>BaseComponent:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f4ad46fc27eda57a1d1575086c6914f7/href">https://medium.com/media/f4ad46fc27eda57a1d1575086c6914f7/href</a></iframe><p>As you can see we have this component lists two modules, and lists that it depends on <strong>BaseComponent</strong>, so it will expose nodes from two different groupings (Modules). This is how our DAG looks like now.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gfayICGOPMkK50tqTAQkUw.png" /><figcaption>The application component exposes the Application, thus when the BaseComponent DEPENDS on it, it shall receive an instance of the Application.</figcaption></figure><p>So with our modules providing <strong>Context</strong>, <strong>Local Data Cache</strong>, <strong>OKHTTP, Retrofit </strong>we have successfully exposed every Tweeter API dependency. If you remember its constructor takes a Retrofit and LocalDataCache instance. Retrofit takes OKHTTP, and LDC takes Context. All of these are exposed in our DAG thus allowing us to annotate Tweeter API’s constructor with</p><pre>@Inject</pre><p>Our BaseComponent will have a class generated which implements it called <strong>DaggerBaseComponent</strong>.</p><pre>public final class DaggerBaseComponent implements BaseComponent</pre><p>This class will have a builder style implementation, which we will have to provide Components it depends on, along with any linked module which has a non-default constructor. Since <strong>BaseComponent’s </strong>“Scope” (More on that later in the article) is meant for the entirety of the project, we create it inside the Application’s <strong>onCreate.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e78af3fb5c0a5621e444ed5dde078d6/href">https://medium.com/media/4e78af3fb5c0a5621e444ed5dde078d6/href</a></iframe><p>I save the generated components as static variables which are exposed via getter’s, this is because you do not want to rebuild the components thus recreating their provided objects. We only do this because the BaseComponent provides objects which should persist the lifetime of the application. Because Dagger is aware of Modules’ default constructors which have 0 parameters I have the choice not to provide a module as can be seen in the code below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/49571797a2f8e4ea611207a2b0a48976/href">https://medium.com/media/49571797a2f8e4ea611207a2b0a48976/href</a></iframe><p>So, we have instantiated all of our “factories” which provide us instances of each exposed class. However, we’re missing a component which can <strong>Inject</strong> our objects into a class that needs them. Case in point, the only way for us to get a <strong>Retrofit</strong> right now is by doing this in the app.</p><pre>CleanArchitectureApplication.getBaseComponent<strong>()</strong><br>        .getRetrofit();</pre><p>However this sort of defeats the purpose of simply relying on our DI framework on injecting anything we need into our class. So in other words any @Inject annotations in a class will simply not be injected. This is because we need to create a method which takes an object with @Inject non-private annotated fields.</p><p><strong>ActivityInjectorComponent:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/02b750204bee69520a6ffbb038273ced/href">https://medium.com/media/02b750204bee69520a6ffbb038273ced/href</a></iframe><p>Thanks to do this, we can now build a <strong>ActivityInjectorComponent</strong> to inject our MainActivity, so we could do this now:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a05a632bff7330b3f0015101e56986f3/href">https://medium.com/media/a05a632bff7330b3f0015101e56986f3/href</a></iframe><p>If you’re interested as to how Dagger injects the tweeterApi variable, it’s pretty simple. By not making the variable private, generated code has the ability to simple set the field variables, which is why we pass the instance of MainActivity to the inject method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/734793eb2a0f40436605aae66327cd4f/href">https://medium.com/media/734793eb2a0f40436605aae66327cd4f/href</a></iframe><h2>Done?</h2><p>Well, for the purposes of our demonstration we have a finished TweeterAPI injector. As you can see, Dagger 2 simplifies the whole factory provider pattern. So in our current implementation, each time we attempt to inject our object we will simply ask the provider for another instance.</p><p>This is where Scopes come in. There’s one that’s provided with Dagger 2 by default. It’s called <strong>@Singleton</strong>. However it’s a bit deceptively named, it implies that other scopes are not Singletons when that’s not the case! Scopes are user-managed, and you can easily create new Scopes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95615ec9a56202676ef0ef79c1f57551/href">https://medium.com/media/95615ec9a56202676ef0ef79c1f57551/href</a></iframe><p>The naming scheme is arbitrary as its lifetime is determined by my implementation. All I have to do is annotate my provider methods:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/027a8709afec9d633dd326a02da66bc9/href">https://medium.com/media/027a8709afec9d633dd326a02da66bc9/href</a></iframe><p>And a interface-level annotation for their components</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc1e7c2e7a03c3fc694d8b28ddbd0e02/href">https://medium.com/media/cc1e7c2e7a03c3fc694d8b28ddbd0e02/href</a></iframe><p>If you dig into the generated files, you can see that in fact that a Scoped provider provides a “singleton”</p><p>UnScoped:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/43a7ed0979d302aa7362e70232f50008/href">https://medium.com/media/43a7ed0979d302aa7362e70232f50008/href</a></iframe><p>Scoped:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e5fa8407f61495cc0136809e74d0398/href">https://medium.com/media/5e5fa8407f61495cc0136809e74d0398/href</a></iframe><h3>I could have used @MyRandomScope</h3><p>Scopes are simply a way to logically group your component/modules together in terms of how long they should stay in existence before you re-instantiate them. There’s really only one rule to Scopes, each component-module relation ship can hold one scope, so if the component is scoped <strong>@MyRandomScope</strong> then the module provider methods need to annotated as well, and they cannot be <strong>@AnotherScope.</strong> Additionally, if you have a scoped component/module, any dependent components NEED to be scoped, and they <strong>cannot</strong> have the same scope.</p><h3>What if I have multiple objects of the same type in my DAG?</h3><p>This is where <strong>@Named </strong>comes in. You simply annotate your provider method, and now it has its own “type” for the purposes of the DAG. Example listed below,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5bb6ea2baf76aaa2f06b3830fd3c295/href">https://medium.com/media/e5bb6ea2baf76aaa2f06b3830fd3c295/href</a></iframe><p>In the above example, even though we have MyModule with to provider methods returning the same type <strong>MyClass</strong> we utilize the Named annotation to distinguish the two.</p><p>Finally there are a few other tools you can use such as</p><pre>@Inject Provider<strong>&lt;MyClass&gt;</strong> myClass;<br>@Inject Lazy<strong>&lt;MyClass&gt;</strong> myClass2;</pre><p>Where a Provider will instantiate a new myClass from the factory each time you do <strong>myClass.get()</strong> will instantiate a new version of the MyClass. Meanwhile the Lazy will delay instantiate until the first <strong>myClass2.get() </strong>this means it’s a lazy loader. That said I have yet to utilize either of these.</p><h2>Continued Reading</h2><p><a href="https://medium.com/p/84a1672dd000/">If you are interested in how Dagger 2 can be used in conjunction with Testing, please check out Part 2</a></p><h3>Links</h3><p><a href="https://github.com/patrykpoborca/CleanArchitecture">Clean Architecture</a></p><p><a href="https://github.com/patrykpoborca/MVPDagger2">MVP basic demo (multiple branches unmaintained)</a></p><p>Thank you for reading my series, I hope it’s informative and helpful. If you have any questions or comments (ahem.. even hate mail) feel free to <a href="https://twitter.com/patrykpoborca">tweet me</a> also I’d appreciate if you follow me too. Or simply make a comment here!</p><p>Looking for an Android Contractor? Look no further, contact me at: info@koziodigital.com</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=267ec5f6c89a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Trying to make a best practice app #3 — Bleeding edge]]></title>
            <link>https://medium.com/android-news/trying-to-make-a-best-practice-app-3-bleeding-edge-6450c298b76f?source=rss-ef5bf72f8620------2</link>
            <guid isPermaLink="false">https://medium.com/p/6450c298b76f</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[startup]]></category>
            <dc:creator><![CDATA[Patryk Poborca]]></dc:creator>
            <pubDate>Thu, 23 Jul 2015 02:20:38 GMT</pubDate>
            <atom:updated>2015-07-23T04:33:32.442Z</atom:updated>
            <content:encoded><![CDATA[<p>So this is just a quick update about design decisions and being stubborn. So in my first post I stated that I was going to implement MVP as my architecture and use the android DataBinding Library to “Find views by ID”</p><p>Sadly, this has quickly resulted in several weird compilation errors. Both databinding and Dagger library’s generate at runtime, leading to some awkward scenarios from time to time. Case in point, I setup this nice little base viewmodel Activity/Fragment class, however after brief success the databinding library started to dissapear!</p><pre>import android.databinding.DataBindingUtil;<br>import android.databinding.ViewDataBinding;<br>import android.support.annotation.LayoutRes;<br>import android.support.v7.app.AppCompatActivity;<br><br><em>/**<br> * Created by Patryk on 7/13/2015.<br> */<br></em>public abstract class BaseViewModelActivity&lt;T extends BaseViewModel, G extends ViewDataBinding&gt; extends AppCompatActivity {<br><br><br>    private G mBinding;<br><br>    @Override<br>    public void onResume() {<br>        super.onResume();<br>        getViewModel().onAttachToView();<br>    }<br><br>    @Override<br>    public void onPause() {<br>        super.onPause();<br>        getViewModel().onDetachFromView();<br>    }<br><br>    protected void setBindingView(@LayoutRes int layout){<br>        mBinding = DataBindingUtil.<em>setContentView</em>(this, layout);<br>    }<br><br>    protected G getBinding(){<br>        return mBinding;<br>    }<br>    <br>    public abstract T getViewModel();<br>}</pre><p>As you can see, I added a little transparent databinding getter, so that I could easily lookup views by doing</p><pre>getBinding().someViewWithID.doSomething()</pre><p>This brings us to my quick post’s topic. Don’t be stubborn. I realized that MVP wasn’t what I wanted to do when I realized that even though it would increase how verbose my project would also slow down my development cycle. So I decided to stick to the tried and true, MVVM, allowing me to explore RxJava better than I otherwise would. With this databinding mess and its unreliability at compile, I have decided that I will fallback on Butterknife.</p><p>That’s all folks!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6450c298b76f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-news/trying-to-make-a-best-practice-app-3-bleeding-edge-6450c298b76f">Trying to make a best practice app #3 — Bleeding edge</a> was originally published in <a href="https://medium.com/android-news">AndroidPub</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Trying to make a best practice app #2]]></title>
            <link>https://medium.com/@patrykpoborca/trying-to-make-a-best-practice-app-2-c2f6d1f2ea82?source=rss-ef5bf72f8620------2</link>
            <guid isPermaLink="false">https://medium.com/p/c2f6d1f2ea82</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[startup]]></category>
            <dc:creator><![CDATA[Patryk Poborca]]></dc:creator>
            <pubDate>Sun, 19 Jul 2015 00:40:21 GMT</pubDate>
            <atom:updated>2015-07-19T00:40:21.469Z</atom:updated>
            <content:encoded><![CDATA[<p>So I’ve set up my libraries, updated Android studio to run with the new Databinding library. Now what? Well, after working on app with well over 100 custom views/layouts, I have come to the conclusion that styles are important!!!</p><p>With styles you go from having potentially 10–15 lines of code for a TextView to only having 1. Styles to consider:</p><ol><li>Style for each text size you plan to implement. This can be helpful if you want to change certain things like spacing later on.</li><li>Section Header/Divider (with text)</li><li>Divider (no text)</li><li>Paragraph text</li><li>Container style (default padding/margins)</li></ol><p>Create a custom EditText / TextView for the ability to switch font-types in the app. Case in point I plan to implement Roboto text for now, so I will wrap my text-views in it.</p><p>Make sure your color resources are set and ready to go, this seems like a no brainer but every once in a while you see an app with colors hardcoded within XML. This is a reminder to anyone who’s reading my articles, wrap the classes you use! Anything that is going to be implemented throughout the app, you neever know when you want to change certain foundational implementation. So wrap your Fragments, TextViews, Activties, etc.</p><p>Finally, the most overlooked thing for last. When you’re creating your string resources. Don’t just throw all of them into one Strings.xml. Rather specify them into a thing such as “introduction_strings.xml”, name them with a contextual name attribute so that translation/localisation is NOT a headache later on if by some miracle your app becomes wildly popular.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c2f6d1f2ea82" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Trying to make my first Best Practice app #1]]></title>
            <link>https://medium.com/android-news/trying-to-make-my-first-best-practice-app-1-3aa6e987b150?source=rss-ef5bf72f8620------2</link>
            <guid isPermaLink="false">https://medium.com/p/3aa6e987b150</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[startup]]></category>
            <dc:creator><![CDATA[Patryk Poborca]]></dc:creator>
            <pubDate>Sat, 18 Jul 2015 03:20:12 GMT</pubDate>
            <atom:updated>2015-07-18T03:20:12.527Z</atom:updated>
            <content:encoded><![CDATA[<p>So I’ve wanted to create my first popular app for a bit now. I only started learning Android in Jan 2015… As such my experience isn’t that extensive. In fact my first app was <a href="https://github.com/patrykpoborca/flung">https://github.com/patrykpoborca/flung</a> an app where I created a flappy-bird / angry bird combination. It was alright, but it was mostly me learning a bit here and there about the platform, and re-acquainting myself with Java. It wasn’t pretty but it got me my job.</p><p>So here I am, 7 months later, 6 months of professional experience later itching to make something that’s well made to call my own. So, how do I start?</p><h3>Architecture</h3><p>Since this is an Android application I need to decide between leaning towards an MVP or an MVVM implementation. For this project I have decided to try my hand with MVP. I have never worked with it but after working with MVVM for months I think it’ll be a nice change of pace. The big advantage of it in the Android world will be having it be pure JAVA, so that non-android testing will be possible. Speaking of which…</p><h3>Testing</h3><p>Test driven development is a keyword in any good developer’s vocabulary. I mean to do my best follow this style. I will try to write tests as I go along for my presenters. It may be rough to begin with as my experience with testing is limited but I’ve heard good things.</p><h3>Back end</h3><p>There are several options. The top contenders seem to be: Heroku, AWS, and Firebase. I decided to go with Firebase due to its relatively easy-to-start nature. Additionally in this app’s infancy I don’t want to invest too much time into services. I plan to write a wrapper around the firebase code so that if i decide to switch to a REST api it own’t be too much of a hassle.</p><h3>Libraries</h3><ol><li>RxJava, I’ve been using a small implementation of reactive programming called <a href="https://github.com/dinosaurwithakatana/Reactor">Reactor</a> As such I’m familiar with the design pattern but I’m eager to learn something more popular.</li><li>Retro lambda, haven’t you heard? It’s nice to use them :)</li><li>I will use the “binding” library google has in its canary builds. All that is required is for me to wrap my layouts with a &lt;layout&gt; tag to easily grab the views in the java code. However if this turns out to be a hassle I will use Butterknife.</li><li>Firebase, as mentioned earlier</li><li>Appcompat Recyclerview. I’ve fallen in love with these, so much simpler than listviews.</li><li>Material design support library</li><li>LoganSquare. This is apparently the fastest json serializer/deserializer out there. Part of it being thanks to it generating files based on your annotations.</li><li>Dagger 2, inversion of control, dependency injection. Ever hear of those? If not you should do some reading. Inversion of control is a simple concept in itself, but the idea of dependency injection is a bit hard to grasp. Even now I feel as though my understanding on its implementation is rather light. Thus the need for it to be in my app. Best way to learn is to do!</li><li>Picasso or Volley for async image loads</li></ol><h2>Ready? Go!</h2><p>Seeing as I’m ready to start, my next steps will be to write the base classes for my app. I plan to write a BaseFragment, a BaseActivity, BaseModel, BasePresenter, NetworkWrapper, etc!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3aa6e987b150" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-news/trying-to-make-my-first-best-practice-app-1-3aa6e987b150">Trying to make my first Best Practice app #1</a> was originally published in <a href="https://medium.com/android-news">AndroidPub</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Data Binding in Android]]></title>
            <link>https://medium.com/@patrykpoborca/data-binding-in-android-32ac282aca95?source=rss-ef5bf72f8620------2</link>
            <guid isPermaLink="false">https://medium.com/p/32ac282aca95</guid>
            <category><![CDATA[android-binding-google]]></category>
            <dc:creator><![CDATA[Patryk Poborca]]></dc:creator>
            <pubDate>Sat, 30 May 2015 21:41:36 GMT</pubDate>
            <atom:updated>2015-05-30T21:42:14.167Z</atom:updated>
            <content:encoded><![CDATA[<p>So this Google I/O was rather exciting, we had Google Photo’s revealed to have unlimited upload counts (for limited sized photos). Google On-Tap and of course Data binders. For those of us familiar with Reactive programatic (React and RxJava) the concept of having observables has been around for a while.</p><p>There are plenty resources available online, this article is to inform you about what these bindings do and what they mean.</p><p>Any Android Developer knows that any time you write any app you end up writing a lot of boiler plate.</p><ol><li>Create layout files with views with ID’s</li><li>Find those views by ID with java code</li><li>add data</li><li>(BONUS) Add in some binding using React, etc to have the view change to data being displayed</li><li>Repeat for each view</li></ol><p>Now the new way of doing things</p><ol><li>Setup your Layout by wrapping it in &lt;layout&gt; tags, add a &lt;data&gt; inside of the &lt;layout&gt;</li><li>Set your java variables to the generated binding method</li><li>Bind an observable to an xml attribute such as android:text</li><li>Repeat 3 for all the fields of your model</li></ol><p>Now as to how the actual coding works you can see it here:</p><ol><li>Go to the data binding guide <a href="https://developer.android.com/tools/data-binding/guide.html">here</a></li><li>After you’ve setup your Android Studio 1.3, you must setup your project build.graddle files</li><li>Start coding</li></ol><p>If you want to have an example of how you can write a binding project here is a link to my<a href="https://github.com/patrykpoborca/BindingTester"> github repo</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*801_M4f1yWKg-yQg_aj5kA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=32ac282aca95" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>