<?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 Alexander Zhdanov on Medium]]></title>
        <description><![CDATA[Stories by Alexander Zhdanov on Medium]]></description>
        <link>https://medium.com/@qwert2603?source=rss-d078c7360387------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*0ppv5yA_fNR4lMGocL3Yhw.jpeg</url>
            <title>Stories by Alexander Zhdanov on Medium</title>
            <link>https://medium.com/@qwert2603?source=rss-d078c7360387------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 21:39:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@qwert2603/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[Multibinding in Koin (almost like in Dagger 2)]]></title>
            <link>https://qwert2603.medium.com/multibinding-in-koin-almost-like-in-dagger-2-4d377c9606a6?source=rss-d078c7360387------2</link>
            <guid isPermaLink="false">https://medium.com/p/4d377c9606a6</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[koin]]></category>
            <dc:creator><![CDATA[Alexander Zhdanov]]></dc:creator>
            <pubDate>Sun, 08 Aug 2021 12:54:07 GMT</pubDate>
            <atom:updated>2021-08-08T12:54:07.246Z</atom:updated>
            <content:encoded><![CDATA[<p>There is some interesting and useful feature in Dagger 2 called <a href="https://dagger.dev/dev-guide/multibindings.html">Multibinding</a> that allows to bind several objects into a collection and implement a plugin architecture easily.</p><p>Unfortunately, <a href="https://github.com/InsertKoinIO/koin">Koin</a>, another popular DI framework, doesn’t have such feature out-of-box. But this doesn’t mean, that it’s impossible to add such feature. In this article, I’ll show how.</p><p><strong>TL;DR</strong>: gist with full code is in the end.</p><h3>How it can can be used</h3><p>Here is some showcase:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4849572685e6a79eca78edbd80a7f5f9/href">https://medium.com/media/4849572685e6a79eca78edbd80a7f5f9/href</a></iframe><p>Now let’s see the implementation details.</p><h3>How it can be implemented</h3><p>For simplicity, we will add values to a Map (if a Set is needed, it may be represented as the key-set of a Map with all values equal to Unit).</p><p>Using Map directly is not good, because injecting some other Map may be used for some other purpose. So another class Multibinding based on Map is defined:</p><pre>class Multibinding&lt;K, V&gt; : MutableMap&lt;K, V&gt; by <em>mutableMapOf</em>()</pre><p>Next, add function declareMultibinding that will declare a Multibinding instance in Koin’s module:</p><pre>inline fun &lt;reified K, reified V&gt; Module.declareMultibinding() {<br>    single(qualifier = <em>multibindingQualifier</em>&lt;K, V&gt;()) <strong>{<br>        </strong>Multibinding&lt;K, V&gt;()<br>    <strong>}<br></strong>}</pre><p>Multibinding will be identified by its key and value types. So different types of value may be used with the same key type. To distinguish different instances of Multibinding for different key and value types the qualifier is added. This qualifier is based on K and V type parameters:</p><pre>inline fun &lt;reified K, reified V&gt; multibindingQualifier(): Qualifier = <em>named</em>(&quot;${K::class.<em>getFullName</em>()}_${V::class.<em>getFullName</em>()}&quot;)</pre><p>Then comes the function for adding a keyed value to Multibinding. This function gets Multibinding from Koin and adds value to it. And, of course, some clean-up is added, so the given value will be removed from Multibinding when the given module will be unloaded from Koin.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a39bd1e101cc4ca88eb1b35215a79971/href">https://medium.com/media/a39bd1e101cc4ca88eb1b35215a79971/href</a></iframe><p>Finally, comes the function for receiving multibinding from Koin. The function returns Map because class Multibinding is the implementation detail that shouldn’t be exposed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7fd69eab19f9beb525c08e091e545291/href">https://medium.com/media/7fd69eab19f9beb525c08e091e545291/href</a></iframe><p>That’s all. So simple. Koin shows great power and possibilities here. Loading and unloading modules with multibinding values open up wide opportunities for a plugin-based architecture.</p><p>Full code can be found <a href="https://gist.github.com/qwert2603/4321d68d5408165794f3ede4ade6c18d">here</a>.</p><h3>There is more</h3><p>There are some more things that exist in Dagger but are not supported in Koin out-of-box. Read more on these issues:</p><ul><li><a href="https://github.com/InsertKoinIO/koin/issues/1028">“bind” method is not type-safe</a></li><li><a href="https://github.com/InsertKoinIO/koin/issues/1029">Simplified beans definition</a></li></ul><p>Based on the powerful Kotlin type system, Koin is the great DI instrument that is not inferior to Dagger. Especially, in the upcoming multiplatform world.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4d377c9606a6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Next step to avoid backing properties for  StateFlow]]></title>
            <link>https://itnext.io/next-step-to-avoid-backing-properties-for-stateflow-5107f7fd3dc4?source=rss-d078c7360387------2</link>
            <guid isPermaLink="false">https://medium.com/p/5107f7fd3dc4</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Alexander Zhdanov]]></dc:creator>
            <pubDate>Thu, 21 Jan 2021 13:54:29 GMT</pubDate>
            <atom:updated>2021-03-20T20:15:50.549Z</atom:updated>
            <content:encoded><![CDATA[<h3>Next step to avoid backing properties for StateFlow</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aRLXfWbNwlUq8FX0i-bmdw.jpeg" /><figcaption><a href="https://unsplash.com/photos/Zj8JwP3M3Do">https://unsplash.com/photos/Zj8JwP3M3Do</a></figcaption></figure><p>This article was inspired by <a href="https://medium.com/google-developer-experts/avoid-backing-properties-for-livedata-and-stateflow-706006c9867e">this original post</a> and <a href="https://itnext.io/re-avoid-backing-properties-for-livedata-and-stateflow-2160cab96b56">this reply</a>. Here I’m going to tell my observations about the mentioned problem.</p><p>We can reformulate the problem as “<em>make value getter visible in all places and value setter visible only inside </em><em>ViewModel</em>”. Let’s start with that statement and try to use Kotlin’s visibility structure to solve it.</p><h3><strong>Protect</strong> Flow’s mutability</h3><p>Making <em>value getter visible in all places</em> is easy — it will just be public (by default in Kotlin). In order to <em>make value setter visible only inside </em><em>ViewModel</em>, we need to make it private and place inside ViewModel. But there are many ViewModel. So we can create class BaseViewModel, place the value setter to that class and make it protected. Here is where interesting things start.</p><p>Value setter in MutableStateFlow is public and there is nothing we can do about it. That’s why we define ViewModelStateFlow that will wrap “real” StateFlow&lt;T&gt;:</p><pre>sealed class ViewModelStateFlow&lt;T&gt;(stateFlow: StateFlow&lt;T&gt;) : StateFlow&lt;T&gt; by stateFlow</pre><p>This class is public and is meant to be used in all places. But value setter for ViewModelStateFlow will be protected inside class BaseViewModel, so it can be used only from BaseViewModel‘s subclasses.</p><p>Now it only remains to provide the subclass for ViewModelStateFlow. This implementation will be private inside BaseViewModel.kt to avoid its leaking, too.</p><pre>private class ViewModelStateFlowImpl&lt;T&gt;(<br>        initial: T,<br>        val wrapped: MutableStateFlow&lt;T&gt; = <em>MutableStateFlow</em>(initial)<br>) : ViewModelStateFlow&lt;T&gt;(wrapped)</pre><p>Inside ViewModelStateFlowImpl we create MutableStateFlow that is used for delegation in ViewModelStateFlow.</p><p>Also, we need to provide a factory method inside class BaseViewModel for creating ViewModelStateFlow. We can not just use ViewModelStateFlowImpl‘s constructor, because else private type ViewModelStateFlowImpl will be exposed via public val someFlow in ViewModel.</p><pre>protected fun &lt;T&gt; createViewModelStateFlow(initial: T): ViewModelStateFlow&lt;T&gt; = ViewModelStateFlowImpl(initial)</pre><p>Next step is the value setter for ViewModelStateFlow. It is protected function inside class BaseViewModel that is aware of ViewModelStateFlowImpl‘s implementation details.</p><pre>protected fun &lt;T&gt; ViewModelStateFlow&lt;T&gt;.setValue(value: T): Unit = when (this) {<br>    is ViewModelStateFlowImpl -&gt; wrapped.value = value<br>}</pre><h3>Using ViewModelStateFlow</h3><p>With these definitions, we can easily create state flows in any ViewModel and access it’s value getter and setter there:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/876784f0a4f972b9dd11090c1c0a7d4c/href">https://medium.com/media/876784f0a4f972b9dd11090c1c0a7d4c/href</a></iframe><p>But in “view” class (SomeActivity, SomeFragment, etc) we can access value getter only:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a414e8f7d2ed7eccd3a74b85ba4c3467/href">https://medium.com/media/a414e8f7d2ed7eccd3a74b85ba4c3467/href</a></iframe><p>We achieved what we wanted. But there is always space for improvements. For example, we need to use setValue method instead of just assigning a new value to flow (as it is done in MutableStateFlow). So far, I have not been able to find a way to define the property setter for ViewModelStateFlow.</p><p>Btw, there is friend keyword in C++, which would be very helpful here. But that keyword in Kotlin would be out of place and bring more complexity to Kotlin’s visibility structure.</p><p>Full code to recap:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e55696e00fb8513e00b00cd0efe9fae6/href">https://medium.com/media/e55696e00fb8513e00b00cd0efe9fae6/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5107f7fd3dc4" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/next-step-to-avoid-backing-properties-for-stateflow-5107f7fd3dc4">Next step to avoid backing properties for  StateFlow</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to implement CircularRevealAnimation as Flutter library and publish it on pub.dev on the way]]></title>
            <link>https://medium.com/flutter-community/how-to-implement-circularrevealanimation-as-flutter-library-and-publish-it-on-pub-dev-on-the-way-34e8cd21a46?source=rss-d078c7360387------2</link>
            <guid isPermaLink="false">https://medium.com/p/34e8cd21a46</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[animation]]></category>
            <dc:creator><![CDATA[Alexander Zhdanov]]></dc:creator>
            <pubDate>Wed, 22 May 2019 10:01:00 GMT</pubDate>
            <atom:updated>2019-07-29T13:52:43.294Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to implement CircularRevealAnimation as Flutter widget and publish it on pub.dev on the way</h3><p>In Android, there is a very interesting Animation called CircularRevealAnimation. Flutter while have rich animation possibilities doesn’t provide this animation out of the box.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*FOXlcTIkBTZv47ERIPaEIw.gif" /></figure><p>In this article, I’ll describe how to implement this animation using Flutter SDK and publish the library on <a href="http://pub.dev/">pub.dev</a> for easy access and distribution.</p><h3>Animation implementation</h3><p>Everything is a widget in Flutter. And animations aren’t exceptions. That’s why we need to create, extending StatelessWidget.</p><p>Animations control (start, pause, stop, etc.) is controlled by AnimationController. To createAnimationController we need to extend StatefulWidget and add a special class SingleTickerProviderStateMixin to State.</p><p>Our class CircularRevealAnimation will not control animation by itself. It will receive Animation&lt;double&gt; as a required constructor parameter. That’s why there is no need to extend StatefulWidget. This is done to allow combine CircularRevealAnimation with other animations using the same AnimationController. For example, to combine circular reveal animation with opacity animation.</p><p>Another important constructor parameter CircularRevealAnimation is child that will be animated (appear or disappear). In general, a lot of widgets in Flutter has achild parameter. These widgets allow changing behaviour, drawing or position of the child widget. Or to add animation, like CircularRevealAnimation does.</p><p>In addition, to define animation we need such parameters as a reveal centre and a minimum and maximum radius of the reveal. These parameters are not required and may be null. In such a case, default values will be used. The default reveals centre is widget’s centre. The default minimum radius is zero. And the default maximum radius is the distance from reveal centre to further widget’s vertex.</p><p>The algorithm for calculating the maximum radius by default is as follows. First, calculate the distance horizontally and vertically from the centre to the vertex furthest from the centre of the reveal, and then calculate the diagonal using the Pythagorean theorem.</p><pre>static double calcMaxRadius(Size size, Offset center) {<br>  final w = max(center.dx, size.width - center.dx);<br>  final h = max(center.dy, size.height - center.dy);<br>  return sqrt(w * w + h * h);<br>}</pre><p>Now we need to implement clipping of child widget within the circle during the drawing. Class ClipPath will help us to do that. ClipPath allows clipping widget on an arbitrary template. Parameters of this widget are clipper (about it a little later) and child — the widget that must be clipped.</p><p>Parameter clipper of ClipPath defines the way of the clipping child widget. To define our own clipping template we create <a href="https://github.com/qwert2603/circular_reveal_animation/blob/master/lib/src/circular_reveal_clipper.dart">CircularRevealClipper</a> extending CustomClipper&lt;Path&gt; and override method Path getClip(Size size). This method returns Path that defines the cutting area. In our case, this area is a circle with a given centre. To calculate the circle’s radius we need to know the current animation’s value. This value is passed to CircularRevealClipper as a fraction parameter. The calculation of a circle’s radius is performed using linear interpolation between the minimum and maximum radii.</p><p>After that, we can implement CircularRevealAnimation. To create animation it is useful to use AnimatedBuilder. AnimatedBuilder takesAnimation&lt;double&gt; and builder that is used to build a child widget taking the current animation’s value into account. In builder we create ClipPath and pass current animation’s value (fraction) toCircularRevealClipper.</p><pre>class CircularRevealAnimation extends StatelessWidget {<br>  ...</pre><pre><a href="http://twitter.com/override">@override</a><br>  Widget build(BuildContext context) {<br>    return AnimatedBuilder(<br>      animation: animation,<br>      builder: (BuildContext context, Widget _) {<br>        return ClipPath(<br>          clipper: CircularRevealClipper(<br>            fraction: animation.value,<br>            center: center,<br>            minRadius: minRadius,<br>            maxRadius: maxRadius,<br>          ),<br>          child: this.child,<br>        );<br>      },<br>    );<br>  }<br>}</pre><p>This completes the creation of CircularRevealAnimation. To use it we need to create StatefulWidget, AnimationController and pass AnimationController to CircularRevealAnimation. <a href="https://gist.github.com/qwert2603/f8e77d2efc23cde488efb1a449637070">Gist</a> with example.</p><p><a href="https://github.com/qwert2603/circular_reveal_animation/tree/master/example">Demo-app</a> on GitHub.</p><h3>Creating a library</h3><p>To create Dart or Flutter library you need to add file pubspec.yaml to the same dir where lib folder with .dart files are located. This file contains the library’s description and info about authors and dependencies.</p><p>It is good practice to create a separated file to define public API. This file is located in the lib folder and contains the library’s name and list of files that need to be included in public API. All other .dart files are placed in src dir. This allows us to import the whole library using a single import expression and hide files that are not included in the public API. Content of this <a href="https://github.com/qwert2603/circular_reveal_animation/blob/master/lib/circular_reveal_animation.dart">file</a>:</p><pre>library circular_reveal_animation;</pre><pre>export &#39;package:circular_reveal_animation/src/circular_reveal_animation.dart&#39;;</pre><p>You can find more details about creating Dart libraries <a href="https://dart.dev/guides/libraries/create-library-packages">here</a>.</p><h3>Publication to <a href="http://pub.dev/">pub.dev</a></h3><p>It’s very easy to publish the Dart library to pub.dev. All you need to do is run flutter packages pub publish from the root dir of your library. The publication is carried out on behalf of the Google account. That’s why during publication you will need to open a given link in the browser and log in to Google. Later you can publish library updates only using the same account that was used to publish the first version.</p><blockquote>Before publication, it is recommended to check the correctness of the library using <em>flutter packages pub publish --dry-run</em>.</blockquote><p>After executing the flutter packages pub publish, the library will immediately be available at pub.dev. And, as written in the documentation, <em>“Publishing is forever” </em>— later you can only upload new versions. Old versions will also be available.</p><p>Although the publication of the library looks simple, it can also have pitfalls. For example, when publishing the first version of circular_reveal_animation, I was deprived of several points in the rating because the library description was too short (in pubspec.yaml).</p><p>You can find more details about publishing Dart libraries <a href="https://dart.dev/tools/pub/publishing">here</a>.</p><p>Finally, the library circular_reveal_animation on <a href="https://pub.dev/packages/circular_reveal_animation">pub.dev</a> and <a href="https://github.com/qwert2603/circular_reveal_animation">github.com</a>.</p><p><strong>Update</strong>: in version 1.0.6 added example for Flutter dialogs.</p><p><a href="https://twitter.com/FlutterComm">JavaScript is not available.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=34e8cd21a46" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-community/how-to-implement-circularrevealanimation-as-flutter-library-and-publish-it-on-pub-dev-on-the-way-34e8cd21a46">How to implement CircularRevealAnimation as Flutter library and publish it on pub.dev on the way</a> was originally published in <a href="https://medium.com/flutter-community">Flutter Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reduce code in RxJava’s combiners]]></title>
            <link>https://medium.com/android-news/reduce-code-in-rxjavas-combiners-a23bc810d8c1?source=rss-d078c7360387------2</link>
            <guid isPermaLink="false">https://medium.com/p/a23bc810d8c1</guid>
            <category><![CDATA[rxjava]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Alexander Zhdanov]]></dc:creator>
            <pubDate>Tue, 23 Oct 2018 15:00:06 GMT</pubDate>
            <atom:updated>2019-02-05T06:59:44.543Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*DERNKsR4v95_V6jKke2hzw.png" /><figcaption>My favourite library in Android development</figcaption></figure><p>RxJava is widely used in many Android applications and it’s common use case where data need to be combined from two or more data sources.</p><p>This may be done via variety of RxJava operators like zip() or zipWith()or combineLatest() or withLatestFrom() and so on.</p><p>The last parameter of these operators is combiner function that is used to combine data from several sources into one data-item.</p><p>Often we just need all data that come from all sources and we create Pair or Triple with code like this:</p><pre>obs1.withLatestFrom(<br>    obs2,<br>    obs3,<br>    <strong><em>Function3 </em>{ t1: Int, t2: Int, t3: Int -&gt; Triple(t1, t2, t3) }<br></strong>)</pre><p>Or sometimes we need to keep only second data-item of two items in situation like this:</p><pre>obs1.withLatestFrom(obs2, <strong><em>BiFunction </em>{ _: Int, t2: Int -&gt; t2 }</strong>)</pre><blockquote>This looks a bit verbose and redundant. Too many code for such a simple operation.</blockquote><p>But we can reduce it via combiners for widely used operations like create Pair or Triple:</p><pre><strong>fun </strong>&lt;T, U&gt; makePair() = <em>BiFunction </em><strong>{ </strong>t: T, u: U <strong>-&gt; </strong>Pair(t, u) <strong>}</strong></pre><pre><strong>fun </strong>&lt;T, U, V&gt; makeTriple() = <em>Function3 </em><strong>{ </strong>t: T, u: U, v: V <strong>-&gt; <br>    </strong>Triple(t, u, v) <br><strong>}</strong></pre><p>This function can be used easily:</p><pre>val o12 = obs1.withLatestFrom(obs2, <em>makePair</em>())<br>val o123 = obs1.withLatestFrom(obs2, obs3, <em>makeTriple</em>())</pre><p>Also functions that allows keep only first or only second of two data-items can be created:</p><pre><strong>fun </strong>&lt;T, U&gt; firstOfTwo() = <em>BiFunction </em><strong>{ </strong>t: T, _: U <strong>-&gt; </strong>t <strong>}<br>fun </strong>&lt;T, U&gt; secondOfTwo() = <em>BiFunction </em><strong>{ </strong>_: T, u: U <strong>-&gt; </strong>u <strong>}</strong></pre><p>With three elements the situations is similar:</p><pre><strong>fun </strong>&lt;T, U, V&gt; thirdOfThree() = <em>Function3 </em><strong>{ _</strong>: T, _: U, v: V <strong>-&gt; </strong>v <strong>}</strong></pre><pre><strong>fun </strong>&lt;T, U, V&gt; lastTwoOfThree() = <em>Function3 </em><strong>{ </strong>_: T, u: U, v: V <strong>-&gt;<br>   </strong>Pair(u, v)<br><strong>}</strong></pre><p>Usage is also simple:</p><pre>val o2 = obs1.withLatestFrom(obs2, <em>secondOfTwo</em>())<br>val o3 = obs1.withLatestFrom(obs2, obs3, <em>thirdOfThree</em>())</pre><p>That’s it.<br>Use it. Happy coding.</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=a23bc810d8c1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-news/reduce-code-in-rxjavas-combiners-a23bc810d8c1">Reduce code in RxJava’s combiners</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[Retrobase — like Retrofit, but for database]]></title>
            <link>https://medium.com/android-news/retrobase-like-retrofit-but-for-database-ec32abfd4793?source=rss-d078c7360387------2</link>
            <guid isPermaLink="false">https://medium.com/p/ec32abfd4793</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[retrofit]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Alexander Zhdanov]]></dc:creator>
            <pubDate>Thu, 17 Nov 2016 13:35:39 GMT</pubDate>
            <atom:updated>2019-05-06T10:13:17.828Z</atom:updated>
            <content:encoded><![CDATA[<p>Many Android developers use <a href="http://square.github.io/retrofit/">Retrofit</a> library, that allows turn HTTP API into java-interface. It’s very convenient, because it reduces the amount of code and this is one of the easiest to use. You just need to create interface and add some annotations.</p><p>Recently I was engaged in the development of applications for Android, that was necessary to make queries to the database through the JDBC-driver. Thereat I decided to do something like Retrofit, but for database. So Retrobase appeared, that I’m going to tell you about now.</p><p><a href="https://habrahabr.ru/post/311716/">Эта статья на русском. This article in Russian.</a></p><p>To turn interface and annotation into working code we need Annotation Processing, that gives us great opportunities in generating of similar code. Moreover, in conjunction with <a href="https://github.com/square/javapoet">JavaPoet</a> we can generate java-code easily and simply.</p><p>There are many <a href="http://blog.stablekernel.com/the-10-step-guide-to-annotation-processing-in-android-studio">articles</a> about Annotation Processing in the Internet, so it’s not a problem to understand and deal with it. Plus, all things You need to know to work with JavaPoet is in it’s <a href="https://github.com/square/javapoet/blob/master/README.md">README.md</a>.</p><p>The foundation of Retrobase is two annotaions — DBInterface andDBQuery together withDBAnnotationProcessorthat makes all code generation. WhileDBInterface marks interface with methods-queries to database, DBQuery is used to mark each method. Methods may have parameters, those will be used in SQL-query. For example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/725/1*OISOpCFqYxQ_x7jWBFEYMQ.png" /><figcaption>Interface with methods-queries to database</figcaption></figure><p>All interesting things happen in DBAnnotationProcessor, where implementation of interface is generated. Generated class have name *interface_name* + Impl.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/833/1*3226dvkBe-QGjVpbKDXkNg.png" /><figcaption>Creating implementing class that contains methods-wrappers. (Using JavaPoet)</figcaption></figure><p>Then the connection to database is created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/663/1*wS99c_Bm5yVWNeoni9iQCg.png" /><figcaption>Creating field Connection to database (Using JavaPoet)</figcaption></figure><p>Also DBAnnotationProcessor creates PreparedStatement for every query.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/815/1*pogQ_Wq8E6V-vLztOJ-pPQ.png" /><figcaption>Creating field PreparedStatement (Using JavaPoet)</figcaption></figure><p>… and realization of method for this query.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/747/1*WLKbfwygXfiqBjMBHB0WmQ.png" /><figcaption>Creating realization of method-query (Using JavaPoet)</figcaption></figure><p>In addition it takes into account return type of method. It can be void if SQL-query is INSERT, DELETE or UPDATE and it doesn’t return anything. Or it can be ResultSet if SQL-query is SELECT.</p><p>Farther DBAnnotationProcessor checks whether method can throw Exception. If it can, all exceptions will be throws from realization of method. Else they will be caught and printed to stderr.</p><p>All parameters of annotated method are added to implementing method and DBAnnotationProcessor will generate statement that transfers each parameter’s value to PreparedStatement.</p><p>Of course, quantity and types of method’s parameters must correlate with parameters of SQL-query that was transmitted by DBQuery annotation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fV7lpiE39ge2WiHGrzzYiw.png" /><figcaption>Generated implementation of method-query. Both method and SQL-query have 3 params of same types.</figcaption></figure><p>After generating of interface implementation, DBAnnotationProcessor writes it using instruments of Annotation Processing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/751/1*xaYJ7gtt0pjJJd8m4xpgVQ.png" /><figcaption>Writing implementing class</figcaption></figure><h3>Rx it!</h3><p>Surely, it’s convenient to get ResultSet, while defining only interface, but it can be improved by using popular <a href="https://github.com/ReactiveX/RxJava">RxJava</a>. Also using RxJava we can easily make async queries.</p><p>To make Retrobase reactive DBMakeRxAnnotationProcessor together with annotations DBInterfaceRx and DBMakeRx were created. You could see example of use of these annotation in example above. Class generated with DBMakeRxAnnotationProcessor will have name *interfave_name* + Rx and will have opened constructor that gets object of interface marked with DBInterfaceRx. Generated class will transfer queries to this object, returning result in reactive style.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/251/1*riEY3dkhIeRzr5PkHsn_-Q.png" /><figcaption>Generated class with methods-wrappers. mDB object is realization of interface marked with DBInterfaceRx.</figcaption></figure><p>To use DBMakeRxAnnotationProcessor You should add DBMakeRx annotation to method-query. This annotation gets the name of model-class. Generated method-wrapper will return Observable&lt;*model-class*&gt;. If You will not define model-class, generated method will return io.reactivex.Completable that is well for INSERT, DELETE and UPDATE queries those don’t return result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/865/1*EPux-sbUs2zCBDjpzOUn4Q.png" /><figcaption>Generated methods-wrappers for some query-methos from interface above</figcaption></figure><p>As you can see, model-class must have opened constructor that gets ResultSet, because generated method-wrapper creates objects of model-class automatically.</p><p>Generated method-wrapper will have same quantity and types of parameters as method-query annotated withDBMakeRx.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/672/1*ZcwOCU6-u5q_e4MpvKEEzw.png" /><figcaption>Params of method-wrapper correlates with params of method-query</figcaption></figure><p>All exceptions those will be thrown by method-query will be caught and sent to Observer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/869/1*i93DVMFD_p-VL3tphoGTDg.png" /><figcaption>Example of use of generated class with methods-wrappers</figcaption></figure><p>Of course, new SpendDBImpl(); or new SpendDBRx(mSpendDB); may be replaced for testing using Dagger or any other DI library.</p><p><a href="https://github.com/qwert2603/RetroBase">Source code of Retrobase with comments</a> and <a href="https://github.com/qwert2603/RetroBaseExample">working example</a> are open. Feel free to modify them!</p><p>The aim of Retrobase is not only make queries to database easier, but show how powerful may be Annotation Processing. There are a lot of code that can be written automatically!</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=ec32abfd4793" width="1" height="1" alt=""><hr><p><a href="https://medium.com/android-news/retrobase-like-retrofit-but-for-database-ec32abfd4793">Retrobase — like Retrofit, but for database</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[First]]></title>
            <link>https://qwert2603.medium.com/first-4c9f29447379?source=rss-d078c7360387------2</link>
            <guid isPermaLink="false">https://medium.com/p/4c9f29447379</guid>
            <dc:creator><![CDATA[Alexander Zhdanov]]></dc:creator>
            <pubDate>Mon, 28 Mar 2016 12:12:30 GMT</pubDate>
            <atom:updated>2016-03-28T12:12:30.264Z</atom:updated>
            <content:encoded><![CDATA[<p>I just want to see how my first story looks here. :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c9f29447379" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>