<?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 Amanjeet Singh on Medium]]></title>
        <description><![CDATA[Stories by Amanjeet Singh on Medium]]></description>
        <link>https://medium.com/@droid_singh?source=rss-4637405dcd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*deS5jBF90lPw1e7Yg85NaQ.png</url>
            <title>Stories by Amanjeet Singh on Medium</title>
            <link>https://medium.com/@droid_singh?source=rss-4637405dcd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 04:38:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@droid_singh/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[Maestro — Re-building the iOS driver]]></title>
            <link>https://medium.com/@droid_singh/maestro-re-building-the-ios-driver-603998b012e8?source=rss-4637405dcd------2</link>
            <guid isPermaLink="false">https://medium.com/p/603998b012e8</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[automation-testing]]></category>
            <dc:creator><![CDATA[Amanjeet Singh]]></dc:creator>
            <pubDate>Tue, 27 Dec 2022 14:16:02 GMT</pubDate>
            <atom:updated>2025-01-09T19:41:29.768Z</atom:updated>
            <content:encoded><![CDATA[<h3>Maestro — Re-Building the iOS Driver</h3><p><em>Looking for the easiest way to test iOS, Android, and Web with a single platform? Check out </em><a href="https://www.robintest.com/"><strong><em>Robin — The new standard in Mobile and Web end-to-end testing</em></strong></a><em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*IZvblyLlbrCzJFvPISiqiA.png" /></figure><p>If you were using Maestro for iOS in the past, chances are — you have seen the issues with elements not being easily discoverable or outright missing. No more!</p><p>Today we are excited to announce a completely new version of the iOS driver for Maestro, rebuilt from the ground up. Starting from Maestro 1.18 the new driver now provides a complete understanding of the UI layout.</p><h3>TL;DR</h3><p>All you have to do is <a href="https://maestro.mobile.dev/getting-started/installing-maestro#upgrading-the-cli">upgrade</a> to the latest version of Maestro. Didn’t use Maestro before? Just install it with this one-liner:</p><pre>curl -Ls &quot;https://get.maestro.mobile.dev&quot; | bash</pre><p>Curious to learn about our journey and how we actually arrived at the new implementation? Then keep reading as we shed light on how Maestro works internally.</p><h3>What makes Maestro different</h3><p>Before we dive into the implementation details, let’s clarify one thing — what are we actually accomplishing with Maestro that is not solved by existing solutions like Appium?</p><p>While there are several advantages that we hope you already had a chance to <a href="https://maestro.mobile.dev/">experience yourself</a> (take ease of setup for one), at the core, the superpower of Maestro lies in the fact that, <strong>unlike Appium, Maestro fully controls view matching and assertion logic</strong>.</p><p>Where Appium delegates call to underlying XCUITest and Android’s Espresso frameworks, Maestro believes that it can do a better job than both by proactively pulling view hierarchy from the device and executing its own view-matching logic. Among other things, this allows for more sophisticated view selection conditions such as relative matching.</p><p>Take this snippet, for example, that selects a view underneath a Title text field:</p><pre>- tapOn:<br>    below: “Title”</pre><p>So, how do we build it?</p><h3>Unifying the interface</h3><p>The first problem to solve — how do we define an OS-agnostic API that would allow us to keep Maestro unaware of differences between iOS and Android?</p><p>Meet Driver— a rather simple interface that defines what you can do with a device. It boils down to two things: getting a view hierarchy and tapping on something.</p><pre>// Maestro framework itself is written in Kotlin<br>interface Driver {<br>    // Returns a root of the view hierarchy.<br>    // We can use this information to understand what is currently<br>    // shown on the screen.<br>    fun contentDescriptor(): TreeNode<br>    // Taps on a given point on the screen<br>    fun tap(point: Point)<br>}</pre><p>Where TreeNode is an OS-agnostic representation of a view (such as <em>UIView/SwiftUI View</em> on iOS or <em>View</em> on Android).</p><pre>data class TreeNode(<br><br>    // Contains view attributes that are brought to a common format<br>    //<br>    // For example attributes[“text”] contains the text shown on<br>    // the screen and attributes[“bounds”] contains visual position<br>    // of the element.<br>    val attributes: Map&lt;String, String&gt;,<br><br>    // Children of this view node, if any<br>    val children: List&lt;TreeNode&gt;<br>)</pre><h3><em>Our initial implementation with IDB</em></h3><p>Now that the interface is defined, how do we implement it?</p><p>For our initial implementation, we decided to leverage <a href="https://fbidb.io/">Facebook’s IDB</a> tool — a great multipurpose CLI tool for working iOS Simulators. For example, one can obtain the view hierarchy from the device using a single command:</p><pre>$ idb ui describe-all</pre><p>As a result, we get a neat list of all* accessibility nodes visible on the screen. Here is the output when running the command on a Settings app:</p><pre>[<br>  {<br>    &quot;AXFrame&quot;: &quot;{{16, 338}, {358, 44}}&quot;,<br>    &quot;AXUniqueId&quot;: null,<br>    &quot;frame&quot;: {<br>      &quot;y&quot;: 338,<br>      &quot;x&quot;: 16,<br>      &quot;width&quot;: 358,<br>      &quot;height&quot;: 44<br>    },<br>    &quot;role_description&quot;: &quot;button&quot;,<br>    &quot;AXLabel&quot;: &quot;General&quot;,<br>    &quot;content_required&quot;: false,<br>    &quot;type&quot;: &quot;Button&quot;,<br>    &quot;title&quot;: null,<br>    &quot;help&quot;: null,<br>    &quot;custom_actions&quot;: [],<br>    &quot;AXValue&quot;: null,<br>    &quot;enabled&quot;: true,<br>    &quot;role&quot;: &quot;AXButton&quot;,<br>    &quot;subrole&quot;: null<br>  },<br>  // etc.<br>]</pre><p>From that, we can get any element the user might be looking for, get its bounds, and finally tap on it using:</p><pre>$ idb ui tap X Y</pre><p>Great! Seems like all the components are there. We can then relatively easily write mapping code in Kotlin that would take the data from IDB, find the element, and then call IDB back to tap on an element.</p><p>Or so we thought.</p><h3>Problems with IDB</h3><p>Though the approach worked for many common cases, it introduced a few significant problems that became blockers for any serious attempt to implement a test in Maestro on iOS.</p><h4>Problem 1 — Incomplete list of UI elements</h4><p>Earlier we said that idb ui describe-allreturns all visible elements. That was not quite true. Most notably, elements of UITabBarare somehow completely ignored by IDB.</p><p>Take this example:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F782126677%3Fh%3D29611704eb%26app_id%3D122963&amp;dntp=1&amp;display_name=Vimeo&amp;url=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F782126677%3Fh%3D29611704eb&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F1570127636-28bc9e7ee1f999106bf2c201271212c73954050cb9c24ab27e1045bf9ee37bb0-d_1280&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=vimeo" width="1920" height="1080" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9421cd1e9e2c96800a547170036b6d1/href">https://medium.com/media/c9421cd1e9e2c96800a547170036b6d1/href</a></iframe><p>Saved tab is clearly visible on the screen. Yet, it is missing from the idb ui describe-alloutput and, as a result, is not showing up in Maestro Studio (that we can easily verify by running maestro studio):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xRpzzmQz1Vy2B0mQ.png" /><figcaption>Maestro Studio</figcaption></figure><h4>Problem 2 — Mystical UNKNOWN gRPC errors</h4><p>From time to time, IDB crashes with cryptic gRPC-related errors.</p><pre>io.grpc.StatusRuntimeException: UNAVAILABLE: Network closed for unknown reason at<br>    maestro.drivers.IOSDriver.contentDescriptor(IOSDriver.kt:154) at<br>    maestro.Maestro$findElementWithTimeout$1.invoke(Maestro.kt:287) at<br>    maestro.Maestro$findElementWithTimeout$1.invoke(Maestro.kt:286)</pre><p>The frequency depends on the exact app (some apps crash <em>every</em> time). Apparently, the cause is somewhere within IDB internals (<a href="https://github.com/facebook/idb/issues/802">GitHub issue</a>) and is seemingly related to the fact that <strong>IDB is using private Apple APIs to obtain the accessibility information</strong> from the Simulator.</p><h4>Problem 3 — iOS 16 Support</h4><p>At the time iOS 16 was published, existing installations of IDB were not able to interact with it properly, resulting in a wide range of problems starting from gRPC errors mentioned earlier and ending with accessibility information being completely empty.</p><p>Later on, we figured out that updating the IDB version alongside re-installing Xcode solved the problem, but that still set a precedent — IDB can be fragile to iOS version updates.</p><h3>Searching for a solution</h3><p>Because of the issues mentioned above, the experience of writing tests for iOS at times, was quite frustrating, unlike our Android counterpart. <strong>We had to do something about it.</strong></p><p>If we can’t use IDB for fetching view hierarchy, how else can we do it? What tools allow us to get that information? The first thing that came to mind was— the <strong>XCUITest framework</strong>.</p><p>As we considered this option, we immediately asked ourselves — aren’t we trying to move away from the platform-specific frameworks? Isn’t Maestro supposed to do the view-matching logic?</p><p>The answer — use an XCUITest as our API layer to get access to the hierarchy. If we can somehow establish communication between Maestro and iOS device, we can transmit this information and still let Maestro handle all the important decisions:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rQBxrF_dDM_swazE.png" /><figcaption>New iOS Driver</figcaption></figure><h4>Writing XCUITest driver</h4><p>To start off, we had to figure out how we could talk to XCUITest from Maestro, which is not only written in Kotlin but also runs outside of the iOS Simulator itself. <strong>The solution — start an HTTP server</strong>.</p><p>We created an XCUITest that appears to be a normal test at first. The difference is that it never completes and instead launches an HTTP server (we are using <a href="https://github.com/swhitty/FlyingFox">FlyingFox</a>) that can reply back with the information we need from the application, such as view hierarchy.</p><pre>func testHttpServer() async throws {<br>    let server = HTTPServer(port: 9080)<br>    let subTreeRoute = HTTPRoute(Route.subTree.rawValue)<br>    await server.appendRoute(subTreeRoute) { request in<br>        let handler = SubTreeRouteHandler()<br>        return try await handler.handle(request: request)<br>    }<br><br>    try await server.start()<br>}</pre><p>SubTreeRouteHandler can then obtain an instance of XCUIApplication using bundle id to get <a href="https://developer.apple.com/documentation/xctest/xcuielementsnapshot">XCUIElementSnapshot</a> which is a snapshot of attributes of XCUIElement and its descendants.</p><pre>// appId of the running app<br>let xcuiApplication = XCUIApplication(bundleIdentifier: appId)<br><br>let viewHierarchyDictionary = try xcuiApplication<br>  .snapshot()<br>  .dictionaryRepresentation<br><br>let hierarchyJsonData = try JSONSerialization.data(<br>    withJSONObject: viewHierarchyDictionary,<br>    options: .prettyPrinted<br>)<br>return HTTPResponse(statusCode: .ok, body: hierarchyJsonData) </pre><p>This new driver also determines the current running app in order to get the bundle id of the running app. The <a href="https://github.com/mobile-dev-inc/maestro/blob/main/maestro-driver-ios/maestro-driver-iosUITests/Routes/Handlers/RunningAppRouteHandler.swift">RunningAppRouteHandler</a> in the HTTP server is responsible to return the first foreground app out of the installed apps.</p><p>Maestro iOSDriver runs this XCUITest to create the HTTP server before Maestro requests any view hierarchy.</p><p>With the help of the following endpoints, Maestro is now able to extract the view hierarchy of the running app:</p><ul><li>/runningApp : A POST request with request body as installed apps. Returning the foreground running app.</li><li>/subTree?appId=com.example.runningApp : A GET request to get the view hierarchy of the running app passed as a query parameter.</li></ul><p>Let’s verify whether this worked by running maestro studio:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vofG8izqWQUuxtIE.png" /></figure><p>Eureka! The driver can clearly see the Saved tab now.</p><p>And if we run our earlier test again, we see that it now passes too!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F782126690%3Fh%3D25616b4437%26app_id%3D122963&amp;dntp=1&amp;display_name=Vimeo&amp;url=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F782126690%3Fh%3D25616b4437&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F1570127828-690482bf562c356e2d2c5b24a44a91ce100ea323ced3ed6c708d51e57bac7d3a-d_1280&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=vimeo" width="1920" height="1080" frameborder="0" scrolling="no"><a href="https://medium.com/media/e97bb174ad75ec13eca70b426dbb04d7/href">https://medium.com/media/e97bb174ad75ec13eca70b426dbb04d7/href</a></iframe><h3>Conclusion</h3><p>XCUITest output showed to be superior to IDB in terms of the richness of the view hierarchy, making this our <strong>official driver for iOS</strong> starting from Maestro 1.18.0.</p><p>We invite you to give it a go by installing Maestro via this one-liner command:</p><pre>curl -Ls &quot;https://get.maestro.mobile.dev&quot; | bash</pre><p>Did it work for you? Did it not? In either case, we would love to hear back. You can always reach us by joining our <a href="https://docsend.com/view/3r2sf8fvvcjxvbtk">Slack community</a> or tagging <a href="https://twitter.com/mobile__dev">@mobile__dev</a> on Twitter.</p><h3>Credit</h3><p>I wanted to take the opportunity to call out the amazing work done by <a href="https://www.linkedin.com/in/artemsidorenko/">Artem Sidorenko</a> and <a href="https://www.linkedin.com/in/berikv/">Berik Visschers</a> who contributed to Rebuilding the iOS Driver throughout.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=603998b012e8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dagger Undercover agent: By example]]></title>
            <link>https://proandroiddev.com/dagger-undercover-agent-by-example-4741f1aba7c2?source=rss-4637405dcd------2</link>
            <guid isPermaLink="false">https://medium.com/p/4741f1aba7c2</guid>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[dependencies]]></category>
            <category><![CDATA[dagger-2]]></category>
            <dc:creator><![CDATA[Amanjeet Singh]]></dc:creator>
            <pubDate>Tue, 19 Mar 2019 10:32:46 GMT</pubDate>
            <atom:updated>2019-03-19T10:38:10.928Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rOKKFKzsUjsaIUlOQTtBjg.jpeg" /><figcaption>Dagger Undercover agent</figcaption></figure><p>Software Engineers need not take care of the underlying complexity of the systems because of the abstractions over them to make the APIs more understandable. But there comes a day, when you need to understand that it is equally important to know the internals of any abstraction to solve any errors which may rise using any abstraction. That’s why I say:</p><blockquote>“With great abstractions comes great responsibility. “</blockquote><p>One of such wonderful abstractions is <strong>Dagger</strong>. As we all know it provides a wonderful API to have power over dependencies in our project, and get rid of spaghetti code that we may have. 🍝</p><p>Now, as an undercover agent I decided to keep track of what <strong>Dagger</strong> is doing under the hood and consolidate all the key points in a blog post. I will try to mention all the pre-requisites and explain them in points.</p><p>I made a sample project which I will use to explain all steps that dagger is taking. You can find the link to it below:</p><p><a href="https://github.com/amanjeetsingh150/kotlin-android-examples/tree/master/MVVMSample">amanjeetsingh150/kotlin-android-examples</a></p><p>Let’s first dive into the structure of the project which I used:</p><h3>Structure of DI in Project:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*btgr16E9s0P5Dz27CKbT0w.png" /><figcaption>DI structure in project</figcaption></figure><p>So, we have following modules:</p><ol><li><strong>App Module</strong>: Simply providing app Context, DataManager and Schedulersfor RxJava thread switching.</li><li><strong>Activity Module</strong>: Providing ViewModel for the activities.</li><li><strong>NetModule</strong>: Providing Retrofit and API interfaces for API calls.</li></ol><p>Other than Modules we have an <strong>AppComponent</strong> with following code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a237d6c4be5f94c4e8683b30199da168/href">https://medium.com/media/a237d6c4be5f94c4e8683b30199da168/href</a></iframe><p>We have <strong>ActivityComponent </strong>as<strong>:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ea55292d152bb517dd7b78a80549ecd/href">https://medium.com/media/7ea55292d152bb517dd7b78a80549ecd/href</a></iframe><p>This is the dagger graph generated for my app:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7AjgzPFldW-DjjHF2Z-GXg.png" /><figcaption>Dependency graph for project</figcaption></figure><p>You can generate the graph for your apps from this useful tool here:</p><p><a href="https://github.com/dvdciri/daggraph">dvdciri/daggraph</a></p><h3>Spying the Stage 1: Providing your dependencies.</h3><p>Here is an overview of what <strong>Dagger</strong> will do as soon as you will build your project:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BjSh3VbKVDUvbsISFzXUgA.png" /><figcaption>Modules as Dependency provider.</figcaption></figure><p>This is what happens to the Modules that we make for providing different dependencies. It creates different factories for all the dependencies we mentioned. The factory code is generated at compile time build through the annotations like @Provides that we use in Module classes.</p><blockquote>Dagger uses <a href="https://github.com/square/javapoet">JavaPoet</a> for generating the .java files you use.</blockquote><blockquote><strong>Note</strong>: <em>Factory</em> is a creational design pattern used to create objects. These factories are used to setup providers of your dependencies in project. ( )</blockquote><p>Now as an example, let us see what <strong>Dagger</strong> generates for our <strong>ActivityModule</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4d3368b5c585533dcce1c0968c1d49db/href">https://medium.com/media/4d3368b5c585533dcce1c0968c1d49db/href</a></iframe><p>Here are the key things done by this Factory:</p><ol><li>Factory for the dependency is created. In this case for MainViewModel as <strong>ActivityModule_ProvideMainViewModelFactory. </strong>If you have any other dependency provided by ActivityModule, a separate Factory will be generated as ActivityModule_Provides........Factory</li><li>This factory will have the instance of <strong>ActivityModule </strong>we have created. Thus, the overriden get method will fetch you dependency of MainViewModel that you created in your module by executing providesMainViewModel method of the <strong>ActivityModule.</strong></li><li>The static create method simply would create the instance of the factory.</li><li>Also, it is important to note here that dagger would take care of providing any other dependency that the current factory would have. Like in this case, we have our MainViewModel dependent on DataManager . Dagger knows this through the constructor injection. You can see this from following that we wrote:</li></ol><pre><strong>class </strong>MainViewModel @Inject <strong>constructor</strong>(<strong>private var dataManager</strong>:<br>    DataManager, <strong>private var schedulerProvider</strong>: SchedulerProvider) {</pre><pre>/* Other code **/</pre><pre>}</pre><h3>Spying the Stage 2: Getting into the injection magic</h3><p>In our example, <strong>ActivityModule</strong> provides the <strong>MainViewModel</strong>. Thus, this class must have a factory as <strong>ActivityModule_ProvideMainViewModelFactory.java.</strong></p><p>Now for doing the magic you all must be familiar with the following piece of code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df749b0ccc2738c6188dc223c50f1b91/href">https://medium.com/media/df749b0ccc2738c6188dc223c50f1b91/href</a></iframe><p>You all must be observing this line in all your Activities after which you’ll put @Inject to inject any dependency. Let’s see what it is doing.</p><p>This DaggerActivityComponent is a class generated by Dagger which uses <strong>Builder pattern</strong> to build itself. Through this <strong>Builder Pattern</strong>, it may take all kind of dependencies which this component may have.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/902c28296abb442a40b7d6e3ba093414/href">https://medium.com/media/902c28296abb442a40b7d6e3ba093414/href</a></iframe><p>In our example, it takes ActivityModule and AppComponent . It also applies a precondition to have a null check on the component passed. Calling build will create the instance of the DaggerActivityComponent.</p><p>Here is the point where all the required factories and MemberInjectors are created:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/35e53b4d160b8760a7db97bb0c8ccb92/href">https://medium.com/media/35e53b4d160b8760a7db97bb0c8ccb92/href</a></iframe><h3>activityComponent.inject(this)</h3><p>Calling this line would tell MemberInjector to try to inject all the @Inject member dependencies for current class.</p><blockquote>MemberInjector injects dependencies into the fields and methods on instances of type T.</blockquote><p>For example, MainActivity in our project injects MainViewModel then calling inject on DaggerActivityComponent would get the dependencies from factories. Let’s see the code generating under the hood:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b3da7fcd9ed1b710ee8d1dc9e73ba5f1/href">https://medium.com/media/b3da7fcd9ed1b710ee8d1dc9e73ba5f1/href</a></iframe><p>Here it is important to note that DaggerActivityComponent implements ActivityComponent in which the overriden inject would command MemberInjector to inject dependencies.</p><p>Here is the flow chart of what is happening in this scenario:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IRkLw3V3VNZW_Rr5NcL_Wg.png" /><figcaption>Member Injection from MemberInjectors</figcaption></figure><p>This diagram shows that as soon as you will put @Inject on any member, the dagger would command factory to get the dependency and assign it back to the references in MainActivity .</p><h3>get( )-ing the dependency</h3><p>It is important to see that what factories are doing to get the dependency. The get() would command Module to provide the needed dependency from instance of module in it. It can be easily seen from the following generated code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d20d8f304d1c9cbbe80c900a0007b05d/href">https://medium.com/media/d20d8f304d1c9cbbe80c900a0007b05d/href</a></iframe><p>It is important to note here that <strong>Scoping </strong>your<strong> </strong>dependencies plays an important role in providing dependencies. Otherwise, all your dependency may get created and allocated again and again.</p><p>For this let’s see what happens when you put @Singleton on any provider in your module. We have seen in above case that what happens when we simply provide @Provides on any provider. But as soon as we provide a singleton scope this is what happens when you’ll get dependencies:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2fbcc1cef6aaf1994cd50d8a4522f3db/href">https://medium.com/media/2fbcc1cef6aaf1994cd50d8a4522f3db/href</a></iframe><p>This <strong>DoubleCheck </strong>would ensure that the objects are not created again and again.</p><p>DoubleCheck would only initialize the dependencies the first time when you get() them and on later calls it would memoize that value as a singleton is expected to do and return the already initialized value.</p><p>So key points to remember here are:</p><ol><li><strong>Scope</strong> intelligently. As you definitely not want unnecessary allocations all over your app.</li><li>Put @Singleton on the dependencies for which you want its lifetime to be cached and not create again and again.</li><li>Scoping can help you to reuse the instances. So, dagger is really helpful if you want to avoid heavy initializations all over your app.</li></ol><p>Hope you have all liked the duty of this detective to undercover what Dagger Magic is doing.</p><p>Stay tuned for more interesting articles. Show some ❤ by hitting clap. Do 👏 if you liked the read and let me know your feedbacks.</p><p>Also thanks to <a href="https://medium.com/u/8e1b5fb365d6">Gurpreet Singh</a> and <a href="https://medium.com/u/f7ae9f0b7f97">Ashish Krishnan</a> for reviewing it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4741f1aba7c2" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/dagger-undercover-agent-by-example-4741f1aba7c2">Dagger Undercover agent: By example</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[UberCarAnimation: A try to make animation of cars on map — Part II]]></title>
            <link>https://medium.com/android-news/ubercaranimation-a-try-to-make-animation-of-cars-on-map-part-ii-50e142efcdc7?source=rss-4637405dcd------2</link>
            <guid isPermaLink="false">https://medium.com/p/50e142efcdc7</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[uber]]></category>
            <category><![CDATA[android-animations]]></category>
            <category><![CDATA[animation]]></category>
            <dc:creator><![CDATA[Amanjeet Singh]]></dc:creator>
            <pubDate>Sun, 23 Sep 2018 14:33:56 GMT</pubDate>
            <atom:updated>2018-11-29T10:12:36.895Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://android.jlelse.eu/ubercaranimation-a-try-to-make-animation-of-cars-on-map-part-i-a080cad3ad81">In the previous part</a> I covered that how we can dispatch an animation to move a car on map if we have the steps between the source and destination. In this part, I am going to explain a real life problem and the solution to it.</p><p>The Problem:</p><blockquote>How the driver app publishes the location updates and the animation occurs simultaneously on the user’s app?</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*Lb7bmttewRpRxpgKObfERg.gif" /><figcaption>How this animation happens?</figcaption></figure><p>For this I used MQTT protocol, which helped me to push the location updates on the android client and then dispatch the animations simultaneously. Let’s see step by step that how I achieved this:</p><h3>Setting up the Client Side:</h3><ol><li>Establish a MQTT broker by logging into one of the MQTT providers. I used <strong>CloudMQTT</strong> service. You can log in from the following link: <a href="https://customer.cloudmqtt.com/login">https://customer.cloudmqtt.com/login</a></li><li>Generate the necessary credentials. You will have following things generated: <strong>username</strong>, <strong>password </strong>and the <strong>serverURI. </strong>Also remember to choose a free plan so that you are not charged. On <strong>CloudMQTT</strong> the free plan is named as <strong>Cute Cat</strong>. Your dashboard will be displayed as following:</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*DEKWYb4SVfsmDH8ghf9tcg.jpeg" /><figcaption>Credentials on the Dashboard</figcaption></figure><p>3. Integrate the <strong>MQTT Paho Client</strong> for android by including following in your app module build.gradle:</p><pre>implementation &#39;org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0&#39;<br>implementation &#39;org.eclipse.paho:org.eclipse.paho.android.service:1.1.1&#39;</pre><p>4. Create a class <strong>MQTTHelper. </strong>Now add the following data variables in this class: <strong>subscriptionTopic, username, password </strong>and<strong> ServerURI. </strong>Here username, password and ServerURI are the credentials which are generated by the CloudMQTT server.</p><p><strong>Subscription topic</strong> is the topic or an event on which your clients are interested in. The clients subscribed to the topic will be notified when something will change on the server. Here the topic will be a type of a regular expression. For example, we have selected it as <strong>location/+.</strong></p><p>5. Add a <strong>connect( )</strong> function in the constructor of the <strong>MQTTHelper</strong> class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d02362bb164bd03ef272ad0275e1bc59/href">https://medium.com/media/d02362bb164bd03ef272ad0275e1bc59/href</a></iframe><p>Here we just create <strong>MQTTConnectOptions</strong> object and on the successful connection we try to subscribe to the topic we have mentioned above in the class file.</p><p>6. Add the following in the <strong>subscribeToTopic( )</strong> function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af6c5ef44c394b8e2fd642646f1fce9e/href">https://medium.com/media/af6c5ef44c394b8e2fd642646f1fce9e/href</a></iframe><p>Here we call subscribe function by passing the respective subscription Topic and the callback which returns the result of subscription as <strong>onSuccess</strong> or <strong>onFailure</strong>.</p><p>7. Now in the <strong>DriverModeActivity </strong>in which your car will be animating, we add a function <strong>startMQTT</strong>( ) in <strong>onCreate</strong>( ) and add the following code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/34ee8e268a70ac8285738ed6f08ae7c5/href">https://medium.com/media/34ee8e268a70ac8285738ed6f08ae7c5/href</a></iframe><p>Here we will be receiving the location updates and converting the <strong>MQTTMessage</strong> object in String and then to LatLng type by simple string formatting. For processing these messages we use a RxRelay to consume the LatLng objects.</p><p>RxRelay is used because it is a subject without termination event that is without the ability of onError or onComplete.</p><p>8. We will be subscribing to the <strong>RxRelay</strong> in <strong>onResume</strong> of activity before the events are consume by the relay. Now as soon as we receive 2 consecutive location updates we want to fire a translation animation of the car. Thus we want a type of operator which emits 2 LatLng pairs in a group together. This can be done by the <strong>buffer</strong> operator by keeping the buffer as 2. This can also be explained by the following marble diagram:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wq9wdXcCOaH7NehrSIbpLA.png" /><figcaption>Buffer operator marble diagram</figcaption></figure><p>The buffer operator will wait as soon as we have 2 LatLng emissions and as soon as we get them it emits them in a list. Like for example, in above marble diagram the buffer is kept as 3 and the emissions occur in a list of 3.</p><p>9. In the consumer of this RxRelay now we will dispatch the animation. This is shown in the following code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd754fa16a65c0876f9023721c2eb07d/href">https://medium.com/media/dd754fa16a65c0876f9023721c2eb07d/href</a></iframe><p>10. In the <strong>animateCarOnMap </strong>function we will be animating the car between the 2 LatLng in the list with the ValueAnimator with the similar concept that I explained in the previous part <a href="https://medium.com/@droid_singh/ubercaranimation-a-try-to-make-animation-of-cars-on-map-part-i-a080cad3ad81">here</a>. Here is the code snippet:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/52eee637de819654c4c55e2a9b837125/href">https://medium.com/media/52eee637de819654c4c55e2a9b837125/href</a></iframe><p>Here:</p><ul><li>We create a LatLng bound around the 2 LatLng we receive and animate camera of map to that bound.</li><li>We set the marker or the car at the first position in the list</li><li>In the update listener of the ValueAnimator we used the linear interpolation to translate the car between the 2 points in the similar way as we have done in the previous part.</li><li>And finally we start( ) the animation..</li></ul><p>After these steps we have finally done with the setup at the client side or the user side.</p><h3>Setting up the Driver Side:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*KL8Olf7GuuD4dx57GKEmTA.gif" /><figcaption>Let’s setup our driver side</figcaption></figure><p>For the driver side I have written a python script which is going to publish location to MQTT broker in interval of 1 second. This interval can be changed according to the use case.</p><ul><li>We will be going to take command line argument as the destination where we want our car to reach.</li><li>We hit Google Maps directions API by providing the source, destination and driving mode and get the “<strong>points”</strong> key from the “<strong>overview_polyline” </strong>JSON object from the response<strong> </strong>similarly as we got in Part 1.</li><li>Now its time to publish the location updates in 1 second interval:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae195af4f17a19d21ffb99ceba29e15a/href">https://medium.com/media/ae195af4f17a19d21ffb99ceba29e15a/href</a></iframe><h3>Lets see the results:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*H6E51Bo8UZfLSKBi-aVX0Q.gif" /><figcaption>Final Output</figcaption></figure><p>Here as soon as we start getting the Latitude and Longitude pairs on terminal animation of car starts simultaneously on our android client.</p><p>Hope you have enjoyed this series.</p><p>Do give any suggestions if you have. Also show some ❤ by clapping on the article if you enjoyed it.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F3e491f%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2F3e491f%2F&amp;image=http%3A%2F%2Fapi.screenshotlayer.com%2Fapi%2Fcapture%3Faccess_key%3Dfe59908dad3baab69ffab249a2224b03%26viewport%3D1024x612%26width%3D1000%26url%3Dhttps%253A%252F%252Fupscri.be%252F3e491f%253Fscreenshot&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/fedb9f91e907af2468a937c944ed32ab/href">https://medium.com/media/fedb9f91e907af2468a937c944ed32ab/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=50e142efcdc7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-news/ubercaranimation-a-try-to-make-animation-of-cars-on-map-part-ii-50e142efcdc7">UberCarAnimation: A try to make animation of cars on map — Part II</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[UberCarAnimation: A try to make animation of cars on map — Part I]]></title>
            <link>https://medium.com/android-news/ubercaranimation-a-try-to-make-animation-of-cars-on-map-part-i-a080cad3ad81?source=rss-4637405dcd------2</link>
            <guid isPermaLink="false">https://medium.com/p/a080cad3ad81</guid>
            <category><![CDATA[uber]]></category>
            <category><![CDATA[android-animations]]></category>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Amanjeet Singh]]></dc:creator>
            <pubDate>Sat, 22 Sep 2018 21:30:17 GMT</pubDate>
            <atom:updated>2018-09-27T18:30:28.469Z</atom:updated>
            <content:encoded><![CDATA[<p>Every time I notice some feature in an android application, I always try to figure out that how those great developers must have developed it. One of those features was animation of the cars on the maps that we see on Uber, Lyft or any other similar applications. In this article I will try to demonstrate that how I ended up making such an animation in my own way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*RKolE5SYUifd9vjrK3O7bw.gif" /><figcaption>After seeing some amazing features</figcaption></figure><h3>Final outcome</h3><p>Introducing, <strong>UberCarAnimation</strong> which you can find on Github as:</p><p><a href="https://github.com/amanjeetsingh150/UberCarAnimation">amanjeetsingh150/UberCarAnimation</a></p><p>Some of the features which can be found in the project are:</p><ol><li>Demonstration of how car animates on map</li><li>An event bus implementation with help of RxJava to give callbacks for starting of the Journey, end of the journey and current location of the car.</li><li>Real world demonstration of how drivers app publish location updates on client side and animations occurs simultaneously on client’s side.</li></ol><p>Let’s straight away dive into the features one by one:</p><h4>1. How animation occurs on the map:</h4><p><strong>Things</strong> <strong>needed</strong>: Google Maps API setup and key, Google Directions API key.</p><p>In this feature I kept my source fixed on some origin LatLng1:</p><ol><li>You will be using Google directions API to get the steps between the entered source and destination.</li><li>You can use Retrofit+RxJava for API calls and get the <strong>overview_polyline</strong> JSONObject and obtain the String <strong>points</strong> from the API results. The <strong>overview_polyline </strong>JSONObject and points in the response will be something like:</li></ol><pre>&quot;overview_polyline&quot; : {<br>  &quot;points&quot; : &quot;kz~mDkb|vMjA|DsAp@o@VYD[EcCCmD?yIHkDHY`@MXDvAT~MFrE&quot;<br>}</pre><p>3. These are then further decoded to list of LatLngs which are the actual steps to be followed for reaching the destination. You can see the function to decode Polyline String to the list of LatLng in my project with the name as <strong>decodePoly</strong>.</p><p>4. Now its time to animate the car from the obtained List&lt;LatLng&gt;. First we will be posting a Handler on UI Thread which will help us to loop over the pair of LatLng and dispatch animation for each pair of LatLng.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af28d0f4b7acab9860d14d3fb9d87a1b/href">https://medium.com/media/af28d0f4b7acab9860d14d3fb9d87a1b/href</a></iframe><p><strong>Here we perform three tasks</strong>: post a Handler to loop over the pair of LatLng, get the pair of LatLng from list of LatLng as startPosition and endPosition and at last we setup the ValueAnimator with its update listener.</p><p>5. Now we have two adjacent or a pair of LatLng and the problem statement breaks down to how we will translate the car between that two points. Here car is nothing but a marker on the map.</p><p>If we will be able to find this solution then this translation is going to occur between each pair of LatLng we have in our set from polyLine list. This is achieved by something called as Linear Interpolation we have studied in our higher studies. The theory is as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*_vzpOAkfWiygZ0zOogjlBQ.png" /><figcaption>Linear Interpolation</figcaption></figure><p>Imagine that the two red points are the pair of LatLng. According to linear interpolation we will take a parameter ‘v’ which is going to range between 0 and 1. This ‘v’ can help us to define the points between x0 and x1. The parameter is 0 for x0 and 1 for the x1. We use getAnimatedFraction() function as this ‘v’ parameter as it is ranged between 0 and 1 in this case and we can have the new points equation in our update listener callback as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/578d8ce8235adba940af4f14eb1f0828/href">https://medium.com/media/578d8ce8235adba940af4f14eb1f0828/href</a></iframe><p>Here the <strong>newPos </strong>is the new calculated point to which we will move our car on.</p><p>6. <strong>Beware the turn has come</strong>!!. You guys must have thought that what will be happening at the turns. For this purpose we have something called as <strong>Bearing</strong>. Bearing is the angle at which a person should rotate while standing on a point P1 to reach P2. We will calculate bearing for each consecutive LatLng.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*g3G2TaXa-VFrXaLmF60qtQ.gif" /><figcaption>Turning of the car-via Giphy</figcaption></figure><p>The movement of car will be executed from:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e9bb16388c829718ec3dd907d5c2d1a/href">https://medium.com/media/8e9bb16388c829718ec3dd907d5c2d1a/href</a></iframe><p><strong>getBearing</strong> is the function with argument as <strong>startPosition</strong> and the <strong>newPosition</strong>. It will give the angle at which marker should be rotated from <strong>startPosition</strong> to go to <strong>newPos</strong>.</p><p>Overall Picture of above algorithm:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/445/1*woa0nQ-689LcHbNYx-Y39Q.png" /><figcaption>Explained diagram — via <a href="https://dribbble.com/tiltman">https://dribbble.com/tiltman</a> (Pankaj Kumar)</figcaption></figure><p>Here think this machine as a Handler posted on UI and which picks up consecutive LatLng pair and the animator which is ValueAnimator calculates the points between them (via Linear Interpolation) and moves the marker in its update Listener.</p><h4>2. Event Bus to provide Journey related Events</h4><p>For providing callbacks of Journey I implemented a Event Bus through RxJava.</p><ol><li>There are three type of events we are interested in: start of journey, end of Journey and the current location of car.</li><li>Create a Singleton class <strong>JourneyEventBus</strong>.</li><li>Declare a PublishSubject which we will use to consume the events. And now as the subject act as observer as well, we will be able to subscribe it and get the events.</li><li>The consuming of events in <strong>JourneyEventBus </strong>class is given in the code below:</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d70020b778ef93bc13c7f1033537b29e/href">https://medium.com/media/d70020b778ef93bc13c7f1033537b29e/href</a></iframe><p>5. <strong>Informing</strong> <strong>the</strong> <strong>RxBus</strong>: We will be informing our event bus about events when appropriate events occur. This is depicted in following snippet:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/449fa5d608b87a968e0bdce755dd499a/href">https://medium.com/media/449fa5d608b87a968e0bdce755dd499a/href</a></iframe><p>6. In <strong>onResume( ) </strong>that is before any events are consumed by our subject we subscribe to the EventBus we made as follows and perform the actions we need:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a969c7a3787fdbc1477edf132151aea/href">https://medium.com/media/6a969c7a3787fdbc1477edf132151aea/href</a></iframe><p>Finally we have established the EventBus. Now lets see the overall outcome:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/281/1*ICnln5X09RWdwqkbxm6KaQ.gif" /><figcaption>UberCarAnimation</figcaption></figure><p>Similarly you can get current location update in Log console or wherever you like according to the use case.</p><p>In the next part I will show you how I demonstrated the real world scenario of how driver app publishes location and animation occurs simultaneously in our maps. You can find the link to the next part here</p><p><a href="https://medium.com/@droid_singh/ubercaranimation-a-try-to-make-animation-of-cars-on-map-part-ii-50e142efcdc7">UberCarAnimation: A try to make Animation of cars on the map-II</a></p><p>Till then, Show some ❤ if you liked this article by hitting clap and starring the repository. Also the project is open for any type of issues and PR. Feel free to open any issues or PR.</p><p>Happy Coding ❤</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a080cad3ad81" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-news/ubercaranimation-a-try-to-make-animation-of-cars-on-map-part-i-a080cad3ad81">UberCarAnimation: A try to make animation of cars on map — Part I</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[What is GraphQL and using it on Android.]]></title>
            <link>https://medium.com/mindorks/what-is-graphql-and-using-it-on-android-ab8e493abdd7?source=rss-4637405dcd------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab8e493abdd7</guid>
            <category><![CDATA[apollo-client]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[graphql]]></category>
            <dc:creator><![CDATA[Amanjeet Singh]]></dc:creator>
            <pubDate>Thu, 01 Feb 2018 21:58:35 GMT</pubDate>
            <atom:updated>2022-11-25T13:18:13.460Z</atom:updated>
            <content:encoded><![CDATA[<p>We all have worked by consuming REST APIs from day one of our development but as data queries are getting more complex with data growing larger it is turning to be expensive with respect to time and computations. There was requirement of framework or a layer which could make the appropriate optimization in querying and consuming APIs. Then comes GraphQL:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*NSDbbsYoby2IHALp2HaeGA.gif" /></figure><h3>What is GraphQL?</h3><p>GraphQL is a query language for the APIs for getting your data. It is an alternative for the REST APIs. It is not specific for a single platform and <strong>works for all type of clients including Android, iOS or web</strong>. It stands between your server and client and helps you to query your data in a more optimized way. Lets see why:</p><p><strong>Main Advantages:</strong></p><ul><li><strong>Client driven, You only get what you need: </strong>This means that clients define what type of response they are interested to get in return which provides more control powers to the client on server. For example if the query is:</li></ul><pre><strong>Query:</strong></pre><pre>query {<br>  repository(owner:&quot;jakewharton&quot;, name:&quot;butterknife&quot;) {<br>    name<br>    description<br>    forkCount<br>    url<br>  }<br>}</pre><pre><strong>Response:</strong></pre><pre>{<br>  &quot;data&quot;: {<br>    &quot;repository&quot;: {<br>      &quot;name&quot;: &quot;butterknife&quot;,<br>      &quot;description&quot;: &quot;Bind Android views and callbacks to fields and methods.&quot;,<br>      &quot;forkCount&quot;: 3989,<br>      &quot;url&quot;: &quot;<a href="https://github.com/JakeWharton/butterknife">https://github.com/JakeWharton/butterknife</a>&quot;<br>    }<br>  }<br>}</pre><p>In this example the client query requests the name, description, fork count and URL of the respective repository and thus in response we only get only those fields which were requested which will reduce our parse time comparatively from the case in which we will get much detailed JSON response.</p><ul><li><strong>Avoid doing multiple calls : </strong>In case of REST APIs we have to maintain multiple endpoints. For instance, one end point for getting the “id” of user from <strong>/users</strong> and details of user from <strong>/users/&lt;id&gt; </strong>which will cost us two calls for the details. Now from the graphQL implementation this will reduce to single query because of the concept of <a href="http://graphql.org/learn/queries/#arguments"><strong>Arguments</strong></a> in GraphQL</li></ul><pre><strong>Resultant single Query:</strong></pre><pre>query{<br>User(id:&quot;abcj34&quot;){ //Argument<br>   name        <br>   subject           <br> }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*PAOo-E_A3GYAoIGkg0W9mw.png" /><figcaption>A much clearer picture</figcaption></figure><h4><strong>Important tools and APIs to know before using:</strong></h4><p>So, you must be thinking that this is OK but I need some already developed graphQL APIs for practising the working on a client side (if you are a client side developer) and other important tools. So, here is an important list you can bookmark:</p><blockquote><strong>PS:</strong> <strong>If you already have a REST API and want to shift to graphQL API you can use </strong><a href="https://github.com/graphql/express-graphql"><strong>express-graphql</strong></a><strong> which will make a graphQL wrapper around your<br>REST API or a SOAP and then you can use it as a graphQL API.</strong></blockquote><ul><li><strong>Yelp GraphQL API</strong>: <a href="https://www.yelp.com/developers/graphql/guides/intro">https://www.yelp.com/developers/graphql/guides/intro</a></li><li><strong>Github GraphQL API: </strong><a href="https://developer.github.com/v4/">https://developer.github.com/v4/</a></li><li><strong>Star Wars GraphQL API: </strong><a href="https://github.com/graphql/swapi-graphql">https://github.com/graphql/swapi-graphql</a></li></ul><p>Important tools:</p><ul><li><strong>GraphiQL: </strong>This is a type of plugin in your browser which is used to test your queries on the APIs. You can customize your requests by adding URLs and adding on different header types. Link: <a href="https://github.com/graphql/graphiql">https://github.com/graphql/graphiql</a></li><li><strong>Altair GraphQL Client: </strong>It is a type of postman for graphQL APIs which is also available as Mozilla Firefox add-on to test the queries. Link: <a href="https://github.com/imolorhe/altair">https://github.com/imolorhe/altair</a></li></ul><h3>Starting Up GraphQL on ANDROID</h3><p>Now for testing GraphQL on Android I have made two projects one is a simple project with just posting up queries on the URL using Retrofit with appropriate headers and other is using GraphQL using <a href="https://github.com/apollographql/apollo-android">apollo-android</a> which is a compliant client that generates Java models according to your query which makes it easier to get your results from the response of the API. Let’s start how you will configure the apollo-android and how to use it to make following demo :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/365/1*6RdaWmybUqKykm3ZP4VQXw.gif" /><figcaption>Resultant app</figcaption></figure><ol><li><strong>Configuring apollo-android and setting up</strong></li></ol><p>In your <strong>project</strong> build.gradle add following:</p><pre>//In your <strong>project</strong> build.gradle<br>dependencies {<br>    classpath <strong>&#39;com.android.tools.build:gradle:3.0.1&#39;<br>    </strong>classpath <strong>&quot;org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.21&quot;<br>    </strong>classpath <strong>&#39;com.apollographql.apollo:gradle-plugin:0.3.2&#39; //Add this<br><br>    </strong><em>// NOTE: Do not place your application dependencies here; they belong<br>    // in the individual module build.gradle files<br></em>}</pre><p>Now go to <strong>app</strong> build.gradle add the following on top:</p><pre>apply <strong>plugin</strong>: <strong>&#39;com.android.application&#39;<br><br></strong>apply <strong>plugin</strong>: <strong>&#39;kotlin-android&#39;<br><br></strong>apply <strong>plugin</strong>: <strong>&#39;kotlin-android-extensions&#39;<br><br></strong>apply <strong>plugin</strong>: <strong>&#39;com.apollographql.android&#39; // Add this<br>.....<br>.....</strong></pre><p>Now in dependencies add following:</p><pre>implementation <strong>&#39;com.apollographql.apollo:apollo-runtime:0.3.2&#39;<br></strong>implementation <strong>&quot;com.apollographql.apollo:apollo-android-support:0.3.2&quot;</strong></pre><p>After this sync your project.</p><p>2. <strong>Adding files for CodeGen of models</strong></p><p>For code generation of models two files are required to be added in project first with a .graphql extension and second a schema.json file which is also an input for the code generation of the models.</p><ul><li>For schema.json you should have <a href="https://github.com/apollographql/apollo-codegen">apollo-codegen</a> which is used to send an introspection query to server and get schema.json. For getting apollo-codegen execute following:</li></ul><pre><strong>1. npm-install apollo-codegen</strong> //For installing the apollo-codegen</pre><pre><strong>2. For sending the introspection query and getting the schema.json execute following:</strong></pre><pre><strong>apollo-codegen download-schema </strong><a href="https://api.github.com/graphql"><strong>https://api.github.com/graphql</strong></a><strong> --output schema.json --header &quot;Authorization: Bearer &lt;Your TOKEN here&gt;&quot;</strong></pre><ul><li>Now make a file with .graphql queries and add following queries:</li></ul><pre>query FindQuery($owner:String!,$name:String!){<br>  repository(owner:$owner, name:$name) {<br>    name<br>    description<br>    forkCount<br>    url<br>  }<br>}</pre><p>Make a directory with name “graphql”under the /main of your project in the same level of java directory and paste both the files. Now rebuild your project and when the build is successful you will see auto generated models in build/generated like shown in following screenshot:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/977/1*2cNUvd6vb6GP0yJpynsjvQ.png" /><figcaption>Auto generated model FindQuery.java</figcaption></figure><p>3. <strong>Setting up Apollo client</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba24d15c122ab22ecb0765ad7d7e0796/href">https://medium.com/media/ba24d15c122ab22ecb0765ad7d7e0796/href</a></iframe><p>Apollo-android uses okhttp client for requesting the queries and getting response. For this, setup the client by adding appropriate headers by adding okhttp interceptor. Add the respective okHttpClient to ApolloClient.builder( ) as shown.</p><p>4.<strong> Querying from the client</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/459acd4ca160796aed3e0871d1ebed9d/href">https://medium.com/media/459acd4ca160796aed3e0871d1ebed9d/href</a></iframe><p>From the client object build the query as shown above and pass the appropriate arguments. Enqueue the result as shown above and change UI correspondingly.</p><p><strong>5. Setup UI accordingly as shown above in screenshot</strong></p><p>Your application will be ready in above steps.</p><p>For more closer look you can find the project on:</p><p><a href="https://github.com/amanjeetsingh150/GraphQL-Android">amanjeetsingh150/GraphQL-Android</a></p><p>Show some ❤ by hitting clap. The project is open for any kind of PR or issue just open and you are in. Would love to here feedbacks.</p><p>Happy Coding ❤</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab8e493abdd7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mindorks/what-is-graphql-and-using-it-on-android-ab8e493abdd7">What is GraphQL and using it on Android.</a> was originally published in <a href="https://medium.com/mindorks">MindOrks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Splash Screen Using Physics Animation]]></title>
            <link>https://medium.com/android-news/splash-screen-using-physics-animation-4bd98eedfe04?source=rss-4637405dcd------2</link>
            <guid isPermaLink="false">https://medium.com/p/4bd98eedfe04</guid>
            <category><![CDATA[physics]]></category>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Amanjeet Singh]]></dc:creator>
            <pubDate>Thu, 04 Jan 2018 21:17:36 GMT</pubDate>
            <atom:updated>2018-01-09T16:50:24.664Z</atom:updated>
            <content:encoded><![CDATA[<p>As a developer I always try to decode the logics behind any particular feature of live Android applications and redesign them according to my logic. You can check out my previous try to demonstrate the animation of cars in Uber android app attached below.</p><p><a href="https://github.com/amanjeetsingh150/UberCarAnimation">amanjeetsingh150/UberCarAnimation</a></p><p>Yesterday I came across this splash screen of <strong>ShowBox </strong>App which is nice app for streaming movies and TV shows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/225/1*3OEk11gb0sX9UR9zBeH0Yg.gif" /><figcaption>Show Box Splash Screen</figcaption></figure><p>After seeing this I decided to re-engineer this screen according to my logic. I broke this animation in following parts:</p><ol><li>A rotation which is driven by a force about (0,0) of the screen.</li><li>Translate that screen downwards</li></ol><p>In this article I will cover how I achieved this animation and made the following demo:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/225/1*FiM0Q9sqp4WfvFWoz2eeFw.gif" /><figcaption>Show Box Splash Demonstration</figcaption></figure><p>Thanks to Google for introducing the <a href="https://developer.android.com/guide/topics/graphics/physics-based-animation.html">Physics Based Animation</a> in Google I/O 2017, which helped me to cover the first part that is rotation powered by a force.</p><h3>What is Physics Based Animation and how are they different from rest?</h3><p>Till now developers have animation APIs like <a href="https://developer.android.com/reference/android/animation/ValueAnimator.html">Value Animators</a>, <a href="https://developer.android.com/reference/android/animation/ObjectAnimator.html">Object Animators</a> where we apply different interpolators which control the rate of change of animation for these animations. They all lack in one thing that is they all are somewhere static which every time ends up in a complex code when we have to deal with complex animations which should act like real world objects. And we all are like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*s17onJvCQzjOgcIEXjVNng.gif" /><figcaption>Ohhh my code!!</figcaption></figure><h4>Now comes the PHYSICS ANIMATION:</h4><p>Google made these animations to give more realistic experiences for their users. These animations are included in support library so that it is backward compatible till API 16. These type of animations are powered and assisted by a <strong>force </strong>which controls the progress of animations. You will define the characteristics of these forces and it guards all the animation value on its own.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*KJzKxFqe4bZVAwyzU45tTg.gif" /><figcaption>Apply physics like a boss</figcaption></figure><p>In order to get start with these animation, add the physics-based support library to your build.gradle file:</p><pre>compile <strong>&quot;com.android.support:support-dynamic-animation:25.4.0&quot;</strong></pre><p>Now you do not need to define any duration or any other values. Your defined Force will take care of animations itself.</p><p>This Physics based animation API provided two types of Animations:</p><ol><li><strong>Spring Animation</strong>: These animations are driven by a spring force where all the velocities of your animation will be dependent on the spring force. You will define all the properties of the spring force including damping ratio and stiffness. APIs have provided different type of constants to set them easily.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/458/1*HKwhSutUAJhkV1TVcTI7rA.gif" /><figcaption>Spring animation</figcaption></figure><p>2. <strong>Fling Animations: </strong>Animation of view property is driven by frictional force. Fling animation has an initial momentum and gradually slows down. It obeys the laws of friction that is the frictional force is proportional to the object’s velocity and thus gradually slowing down the object.</p><p>In this article I will cover the Spring Animation from which I actually achieved the splash screen . Enough talk lets go to code.</p><h4>1. Creating a spring Force and applying it to the SpringAnimation</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d3f2296ca29444c57198fbd097e2a8d/href">https://medium.com/media/0d3f2296ca29444c57198fbd097e2a8d/href</a></iframe><ul><li>In this part of code we are initializing a Spring Force with constructor value which defines its equilibrium position in float.</li><li>We define a pivot for the layout of the activity which is relative in my case. It will be (0,0) because we want rotation of layout about origin</li><li>Start Defining the SpringAnimation which will takes following parameters:</li></ul><ol><li>View on which you want to apply the spring animation.</li><li>Second is DynamicAnimation which will tell you which property of view we have to animate which is <strong>Rotation</strong> in our case.</li></ol><p>Now, thanks to Kotlin for giving us <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/apply.html">apply</a> keyword which provide us a block and help us to define the properties of Spring Force in it. In this I have used Damping ratio as <strong><em>DAMPING_RATIO_HIGH_BOUNCY </em></strong><em>and </em>stiffness as <strong><em>STIFFNESS_VERY_LOW. </em>DAMPING_RATIO_HIGH_BOUNCY </strong>will ensure higher oscillations and stiffness when equal to <strong><em>STIFFNESS_VERY_LOW </em></strong>would cause object to slowly come back in its original position.</p><ul><li>Now at last we will tell our spring animation to adapts the spring properties from the spring force we made and set a start value for the animation</li></ul><p>2. <strong>Setting up DynamicAnimation.OnAnimationEndListener</strong></p><p>Now we have described that the layout should rotate about (0,0) with given stiffness and damping ratio and now we need to translate our layout downwards after the spring animation ends. For this purpose our API has provided us with following two Listeners:</p><ul><li><strong><em>OnAnimationUpdateListener</em></strong>: This listener overrides method<strong> </strong>onAnimationUpdate<strong> </strong>which is used to receive the changes in the property of the view on the progress of animation.</li><li><strong><em>OnAnimationEndListener: </em></strong>This listener overrides method onAnimationEnd which is used to notify the end of the animation it is registered to.</li></ul><p>In our application as we want to start the translation after the spring animation ends so we register a <strong>OnAnimationEndListener , </strong>which will override onAnimationEnd. Then after the animation ends we will translate our layout in next step.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f2d1e2ea4e0e7f330a66144eb6029d77/href">https://medium.com/media/f2d1e2ea4e0e7f330a66144eb6029d77/href</a></iframe><p>3. <strong><em>Translation layout after end is notified and opening the second activity</em></strong></p><p>Now in OnAnimationEnd we will use <a href="https://developer.android.com/reference/android/view/ViewPropertyAnimator.html">ViewPropertyAnimator</a> which is one more class in android available for performing animations related to property of the views with just few lines of code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ebd2f523014d29c394063fec124eadcd/href">https://medium.com/media/ebd2f523014d29c394063fec124eadcd/href</a></iframe><p>Lets go step by step:</p><ul><li>First we get our default display metrics to get our screen width height and width so that we can translate it.</li><li>With the relative layout instance we call <strong>animate( ) </strong>which will return our object’s appropriate ViewPropertyAnimator reference so that we can start our property animations.</li><li>After calling animate, we called <strong>setStartDelay( ) </strong>which sets a delay before starting animation, then we set a translation on both X and Y axis according to the respective width and height we got in the display metrics.</li><li>After setting these animation we also added a listener to this ViewPropertyAnimator in which when we receive the OnAnimationEnd callback we will start the second activity.</li></ul><p><strong><em>PS: Don’t forget to add overridePendingTransition(0, 0) after your startActivity( ) call which tells our Window Manager to disable its default activity transition for this second Activity invocation.</em></strong></p><ul><li>Then at last at this ViewPropertyAnimator we will set our Interpolator and call start()</li></ul><p>4. <strong>At the end get your Spring Animation object you declared and then call start( ).</strong></p><p>Your Splash is ready to setup now compiling all above the code will give you the resultant animation as shown above. For more you can also have a look to the Github repository of this project:</p><p><a href="https://github.com/amanjeetsingh150/SpringBasedSplash">amanjeetsingh150/SpringBasedSplash</a></p><p>Do star the project and give clap to this medium article if you like the project. This project is also open for any kind of improvements or creating issues just open a PR and you are in.</p><pre>              &quot;May the mass*acceleration be with you.&quot;</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F3e491f%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2F3e491f%2F&amp;image=http%3A%2F%2Fapi.screenshotlayer.com%2Fapi%2Fcapture%3Faccess_key%3Dfe59908dad3baab69ffab249a2224b03%26viewport%3D1024x612%26width%3D1000%26url%3Dhttps%253A%252F%252Fupscri.be%252F3e491f%253Fscreenshot&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/fedb9f91e907af2468a937c944ed32ab/href">https://medium.com/media/fedb9f91e907af2468a937c944ed32ab/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4bd98eedfe04" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-news/splash-screen-using-physics-animation-4bd98eedfe04">Splash Screen Using Physics Animation</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>
    </channel>
</rss>