<?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 Shalu T D on Medium]]></title>
        <description><![CDATA[Stories by Shalu T D on Medium]]></description>
        <link>https://medium.com/@shalutd?source=rss-24a1bf7756bd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*Duo6VZNtTTbOc7EhSY_RFg.jpeg</url>
            <title>Stories by Shalu T D on Medium</title>
            <link>https://medium.com/@shalutd?source=rss-24a1bf7756bd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 03:14:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shalutd/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[Automatic Update Prompts for Crashing Apps]]></title>
            <link>https://shalutd.medium.com/automatic-update-prompts-for-crashing-apps-7b87156d215?source=rss-24a1bf7756bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/7b87156d215</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[androiddev]]></category>
            <dc:creator><![CDATA[Shalu T D]]></dc:creator>
            <pubDate>Sat, 29 Apr 2023 00:52:41 GMT</pubDate>
            <atom:updated>2023-04-29T00:52:41.734Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yQjqToNGfsqZIyNn" /><figcaption>Photo by <a href="https://unsplash.com/@jplenio?utm_source=medium&amp;utm_medium=referral">Johannes Plenio</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>This is a new feature provided by the Play Store. Suppose the user is experiencing a crash with the old version of the app, Play store will prompt the user that the app crash will be fixed by updating to the new version. Please see the prompt screenshot below 👇</p><figure><img alt="Play store prompt" src="https://cdn-images-1.medium.com/max/662/1*RjLzDAeW7dbg_zAyukMiSw.png" /><figcaption>Photo from Android developer&#39;s blog</figcaption></figure><p>While taping the update button will redirect the user to the play store for updating the app.</p><h4><strong>Supported OS Version:-</strong></h4><p>It will be worked with any phones and tablets running Android 7.0 (SDK level 24) and above.</p><h4><strong>What do we need to do anything at the code level?</strong></h4><p>These new prompts don’t require any integration by you and are enabled automatically when Play determines that a newer version of your app has a statistically relevant, lower crash rate. Furthermore, since the dialog is shown by the Play Store and not your app, the update prompt can be shown even if your app crashes on startup.</p><p>I have just created this blog to inform everyone about this. Sill we need to figure out how can we set this play console. I will update this story once I get the play store configuration details.</p><p>I hope you got a little bit of knowledge about this. If you need to know more info on this, please check <a href="https://android-developers.googleblog.com/2023/04/automatic-update-prompts-for-crashing-apps.html">here</a></p><p>Now you are ready to go. Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7b87156d215" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exploring the Kotlin 1.5 Standard Library]]></title>
            <link>https://proandroiddev.com/exploring-the-kotlin-1-5-standard-library-a940b5910b9e?source=rss-24a1bf7756bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/a940b5910b9e</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Shalu T D]]></dc:creator>
            <pubDate>Sat, 21 Aug 2021 00:47:12 GMT</pubDate>
            <atom:updated>2021-08-26T12:33:03.463Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WhR0GNM_wrfEawQ4HfU2ZQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@clemhlrdt?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Clément Hélardot</a> on <a href="https://unsplash.com/s/photos/code?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>This article discusses some of the standard library changes that come under the <strong>Kotlin 1.5</strong> version. Please find each of them below.</p><h3>1. New collection functions</h3><p>Consider you have a list of objects, all with a nullable property, and you want to get the first non-null value of that property in the collection. Usually, you would first use <em>mapNotNull()</em> to get a collection of all non-null values and then call <em>firstOrNull()</em> to get the final value.</p><p>In Kotlin 1.5 we have functions <em>firstNotNullOf()</em>and <em>firstNotNullOfOrNull(). </em>The function<em> </em><em>firstNotNullOf() </em>returns first non-null value of the passed selector, or exception thrown if no element matches. The function<em> </em><em>firstNotNullOfOrNull() </em>returns the first non-null value of the passed selector, or null if no element matches.</p><p>The function<em>firstNotNullOf() </em>is the combination of <em>mapNotNull() </em>and <em>first(). The same way </em><em>firstNotNullOfOrNull() </em>is the combination of <em>mapNotNull() </em>and <em>firstOrNull().</em></p><p>Please check the below code for more information.</p><pre>data class Employee(val name: String, val designation: String?)</pre><pre>val employees = <em>listOf</em>(<br>    Employee(&quot;Felix&quot;, null),<br>    Employee(&quot;Vee&quot;, null),<br>    Employee(&quot;Ram&quot;, &quot;Developer&quot;),<br>    Employee(&quot;Vivek&quot;, &quot;Senior Developer&quot;),<br>    Employee(&quot;Joe&quot;, &quot;Developer&quot;),<br>)</pre><pre><em>// Old method</em><br>val firstDesignation = employees.<em>mapNotNull </em><strong>{ it</strong>.designation <strong>}</strong>.<em>firstOrNull</em>()</pre><pre><em>// Kotlin 1.5 method</em><br>val newfirstDesignation = employees.<em>firstNotNullOfOrNull </em><strong>{ it</strong>.designation <strong>}</strong></pre><h3><strong>2. Unsigned integer types Ubyte, UShort, UInt and ULong</strong></h3><p>These types of unsigned numbers are useful wherever you want to constrain a type to only contain non-negative values. They can also be more intuitive when you are working on bit and byte level, manipulating pixels in bitmaps, the bytes in a file, or other binary data.</p><ul><li>UByte: an unsigned 8-bit integer, ranges from 0 to 255</li><li>UShort: an unsigned 16-bit integer, ranges from 0 to 65535</li><li>UInt: an unsigned 32-bit integer, ranges from 0 to 2^32 - 1</li><li>ULong: an unsigned 64-bit integer, ranges from 0 to 2^64 - 1</li></ul><h3>3. Integer floor division and mod operator</h3><p>The new function <em>floorDiv()</em> provides floored division on integers. The floored division always rounds the result down.</p><pre>(5).<em>floorDiv</em>(3) -&gt; returns 1<br>(-5).<em>floorDiv</em>(3) -&gt; returns -2</pre><p>The remainder function, called ‘<strong>mod</strong>’ returns the remainder of the floored division. This is entirely different from the ‘<strong>%</strong>’ operator, called ‘<strong>rem</strong>’ in Kotlin.</p><p>Consider the below code,</p><pre>val a = -4<br>val b = 3</pre><pre><em>println</em>(-4 % 3)   -&gt; -1<br><em>println</em>(a.<em>mod</em>(b)) -&gt;  2</pre><p>Here, the ‘<strong>%</strong>’ operation returns remainder as -1. But <em>a.mod(b)</em> is the difference between <em>a</em> and <em>a.floorDiv(b) * b</em>. It’s either zero or has the same sign as <em>b</em>, while <em>a % b</em> can have a different one.</p><h3>4. char-to-integer conversion API</h3><p>For individual characters, Kotlin introduced functions that make their conversion into numbers more expressive. When you are converting a character into a number, you can mean one of two things: you either mean the char code for the character in question, or you mean the actual digit the character represents, parsing the number. Both of these can be concisely express with the new conversion API. It introduced Char conversions that are divided into the following sets of clearly named functions:</p><ul><li>Functions to get the integer code of Char and to construct Char from the given code:</li></ul><pre><strong>fun</strong> Char(code: Int): Char<br><strong>fun</strong> Char(code: UShort): Char<br><strong>val</strong> Char.code: Int</pre><ul><li>Functions to convert Char to the numeric value of the digit it represents:</li></ul><pre><strong>fun</strong> Char.digitToInt(radix: Int): Int<br><strong>fun</strong> Char.digitToIntOrNull(radix: Int): Int?</pre><ul><li>An extension function for Int to convert the non-negative single digit it represents to the corresponding Char representation:</li></ul><pre><strong>fun</strong> Int.digitToChar(radix: Int): Char</pre><h3>5. Char category in multiplatform code</h3><p>Provided a number of functions to check character properties available in common code. They help you when you want to check if a character is a digit, a letter, or either one of them, whether a letter is an upper, lower, or title case, and if its Unicode representation is defined or an ISO sequence.</p><pre>&#39;1&#39;.<em>isDigit</em>()          -&gt; true<br>&#39;A&#39;.<em>isLetter</em>()         -&gt; true<br>&#39;!&#39;.<em>isLetterOrDigit</em>()  -&gt; false<br>&#39;C&#39;.<em>isLowerCase</em>()      -&gt; false<br>&#39;C&#39;.<em>isUpperCase</em>()      -&gt; true<br>&#39;b&#39;.<em>isTitleCase</em>()      -&gt; false<br>&#39;?&#39;.<em>isDefined</em>()         -&gt; true<br>&#39;\n&#39;.<em>isISOControl</em>()    -&gt; true</pre><h3>6. String/Char case conversion functions</h3><p>Kotlin 1.5 introduced a new <strong>locale-agnostic API</strong> for uppercase/lowercase text conversion. Locale-agnostic means that no matter the language settings on the target system, you will get consistent case conversion results. So, the new API helps you avoid errors due to different locale settings. Please find the old and new methods below.</p><p><strong>String conversion functions:-</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i2scNesetbQlMqKNWV-8ZQ.png" /></figure><p><strong>Char conversion functions:-</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fb67ols_NodwG2rbQ2S3gg.png" /></figure><p>If you want locale-based conversion, you need to explicitly specify the locale as below.</p><pre>“kotlin”.uppercase(Locale.forLanguageTag(“tr-TR”)) </pre><h3>7. Strict version of String?.toBoolean()</h3><p>Introduced String.toBooleanStrict() and String.toBooleanStrictOrNull() functions. The functionString.toBooleanStrict() throws exception for all inputs other than that of ‘true’ or ‘false’ literals. This is case sensitive too. As it is case-sensitive, the example &quot;true&quot;.toBooleanStrict() gives output as true but &quot;True&quot;.toBooleanStrict() throws exceptions.</p><p>String.toBooleanStrictOrNull() will not throw an exception, instead, it gives null for all inputs other than that of ‘true’ or ‘false’ literals. This is also case-sensitive.</p><p>&quot;true&quot;.toBooleanStrictOrNull() -&gt; returns true</p><p>&quot;True&quot;.toBooleanStrictOrNull() -&gt; returns null</p><p>I hope you learned some of the Kotlin standard library changes here. If you need to know more about this, you can use the <a href="https://kotlinlang.org/docs/whatsnew15.html#standard-library">documentation</a>.</p><p>If you need any help, join me on <a href="https://www.linkedin.com/in/shalutd/">LinkedIn</a> and <a href="https://github.com/shalutd">Github</a>. If you liked this article, share it with fellow Kotlin developers.</p><p>Now you are ready to go. Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a940b5910b9e" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/exploring-the-kotlin-1-5-standard-library-a940b5910b9e">Exploring the Kotlin 1.5 Standard Library</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[Explore Kotlin Annotations]]></title>
            <link>https://proandroiddev.com/explore-kotlin-annotations-d52eddd7e7b6?source=rss-24a1bf7756bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/d52eddd7e7b6</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[life]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Shalu T D]]></dc:creator>
            <pubDate>Mon, 16 Nov 2020 08:01:08 GMT</pubDate>
            <atom:updated>2020-11-17T09:39:44.537Z</atom:updated>
            <content:encoded><![CDATA[<h4>Kotlin annotations which help in programming with Java</h4><figure><img alt="Kotlin Annotations explore" src="https://cdn-images-1.medium.com/max/1024/1*jYvicNC-2KCMmkzeuh0Hwg.png" /></figure><p>This article discusses the Kotlin annotations used while developing the Android app. Here we will explore the annotations <strong>@JvmStatic, @JvmOverloads, and @JvmField</strong>.</p><p>As Google suggests Kotlin as the official language for Android development, most of the people are in the process of migrating existing Java code to Kotlin code. But we can’t convert all source code from Java to Kotlin in a single sprint itself.</p><p>So, we want to convert some part of the code (Classes, enums, etc) to Kotlin and mix with the existing java code. If you are in this process, you should know these annotations.</p><h3>@JvmStatic</h3><p>In Kotlin, the package-level functions are represented as static methods. Also, in Kotlin, you can make static methods for functions that are defined in some companion object or named object by using the <strong>@JvmStati</strong>c <strong>annotation</strong>.</p><blockquote>@Target([AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER])<br><strong>annotation class </strong>JvmStatic</blockquote><p>It specifies that an additional static method needs to be generated from this element if it’s a function. If this element is a property, additional static getter/setter methods should be generated.</p><p>Consider the below class:</p><pre><strong>class </strong>ClassName {</pre><pre><strong>companion object </strong>{<br>        @JvmStatic<br>        <strong>fun </strong>iAmStatic() {<br>            <em>//body of Static function<br>        </em>}</pre><pre>        <strong>fun </strong>iAmNonStatic() {<br>            <em>//body of Non static function<br>        </em>}<br>    }<br>}</pre><p>Here, <strong>iAmStatic() </strong>is a static function and <strong>iAmNonStatic() </strong>is a non-static function. Suppose you are calling the above methods from the Java class. It will behave like below:</p><pre>ClassName.iAmStatic(); <em>// works fine<br></em>ClassName.iAmNonStatic(); <em>// compile error <br></em>ClassName.Companion.iAmStaticMethod(); <em>// works fine<br></em>ClassName.Companion.iAmNonStaticMethod(); <em>// other way to work</em></pre><p>As for the case of named objects, the same procedure is applicable:</p><pre><strong>object </strong>ObjectName {<br>    @JvmStatic <strong>fun </strong>iAmStatic() {<br>        <em>//body of Static function<br>    </em>}<br>    <strong>fun </strong>iAmNonStatic() {<br>        <em>//body of Non static function<br>    </em>}<br>}</pre><p>Call the above methods from Java as below:</p><pre>ObjectName.iAmStatic(); <em>// works fine<br></em>ObjectName.iAmNonStatic(); <em>// compile error<br></em>ObjectName.INSTANCE.iAmStatic(); <em>// works<br></em>ObjectName.INSTANCE.iAmNonStatic(); <em>// works</em></pre><h3>@JvmOverloads</h3><blockquote>@Target([AnnotationTarget.FUNCTION, AnnotationTarget.CONSTRUCTOR]) <strong>annotation class </strong>JvmOverloads</blockquote><p>It instructs the Kotlin compiler to generate overloads for this function that substitute default parameter values. If a method has N parameters and M of which have default values, M overloads are generated: the first one takes N-1 parameters (all but the last one that takes a default value), the second takes N-2 parameters, and so on.</p><p>Consider a class having two fields, one is initialized and the other is not initialized.</p><pre><strong>data class </strong>Customer( <strong>val </strong>name: String, <strong>val </strong>dob: Date = Date())</pre><p>Here in this class, by default, the current date will be taken as the date of birth of the customer if we are not passing the second argument while creating the customer object. So, if we are calling from Kotlin, the following code will run without any compile error.</p><pre><strong>val </strong>custOne = Customer(<strong>&quot;Don&quot;</strong>)<br><strong>val </strong>custTwo = Customer(<strong>&quot;Don&quot;</strong>, Date())</pre><p>But if we are calling the same from the Java then you have to pass all the parameters otherwise you will get a compile error:</p><pre>Customer custOne = new Customer(<strong>&quot;Don&quot;</strong>); <em>//Here we are passing only      one argument, so we will get compile error</em></pre><pre>Customer custTwo = new Customer(<strong>&quot;Don&quot;</strong>, new Date());<em>//No error</em></pre><p>In order to make use of the default value while creating an object from Java, we can use the annotation <strong>@JvmOverloads. </strong>Now, after using the annotation, the Kotlin code will be:</p><pre>Customer @JvmOverloads <strong>constructor</strong>( <strong>val </strong>name: String, <strong>val </strong>dob: Date = Date())</pre><p>Now, calling from Java, you don’t need to pass all the parameters:</p><pre>Customer custOne = new Customer(<strong>&quot;Don&quot;</strong>); <em>//No Error<br></em>Customer custTwo = new Customer(<strong>&quot;Don&quot;</strong>, new Date());<em>//No error</em></pre><h3>@JvmField</h3><blockquote>@Target([AnnotationTarget.FIELD]) <strong>annotation</strong> <strong>class</strong> JvmField</blockquote><p>It instructs the Kotlin compiler not to generate getters/setters for this property and expose it as a field.</p><p>Consider the below Java class:</p><pre><strong>public class </strong>Customer {<br>    <strong>public </strong>String name;<br>    <strong>public </strong>Date dob;<br>    <br>    <strong>public </strong>getName() {<br>        <strong>return this</strong>.name;<br>    }</pre><pre>    <strong>public </strong>getDob() {<br>        <strong>return this</strong>.dob;<br>    }<br>}</pre><p>The corresponding Kotlin code will be as below :</p><pre><strong>data class </strong>Customer( <strong>val </strong>name: String, <strong>val </strong>dob: Date = Date())</pre><p>If you are accessing the properties of the class, then the code in Kotlin is:</p><pre><strong>val customer </strong>= Customer(<strong>&quot;Don&quot;</strong>, Date())<br><strong>val name </strong>= <strong>customer</strong>.<em>name<br></em><strong>val dob </strong>= <strong>customer</strong>.<em>dob</em></pre><p>But in Java, we have to use the getter method as below:</p><pre>Customer customer = new Customer(<strong>&quot;Don&quot;</strong>, new Date());<br>String name = customer.getName();<br>Date dob = customer.getDob();</pre><p>If you want a particular field to be used as a normal field and not as a getter or setter then you have to tell the compiler not to generate any getter and setter for the same and this can be achieved by using the <strong>@JvmField </strong>annotation. So, the Kotlin code after using the <strong>@JvmField</strong> annotation will be:</p><pre><strong>data class </strong>Customer(@JvmField <strong>val </strong>name: String, <strong>val </strong>dob: Date = Date())</pre><p>Now, you can access the field from Java as below:</p><pre>Customer customer = new Customer(<strong>&quot;Don&quot;</strong>, new Date());<br>String name = customer.name;</pre><p>And you are ready to go. Thanks for reading!</p><p><strong>Reference:</strong></p><p><a href="https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html">Calling Kotlin from Java</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d52eddd7e7b6" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/explore-kotlin-annotations-d52eddd7e7b6">Explore Kotlin Annotations</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[Welcome DataStore, Good-Bye SharedPreferences — Part 3]]></title>
            <link>https://proandroiddev.com/welcome-datastore-good-bye-sharedpreferences-part-3-7bd46203e91?source=rss-24a1bf7756bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/7bd46203e91</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[jetpack-datastore]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Shalu T D]]></dc:creator>
            <pubDate>Sat, 31 Oct 2020 12:26:38 GMT</pubDate>
            <atom:updated>2020-11-17T09:40:59.827Z</atom:updated>
            <content:encoded><![CDATA[<h3>Welcome DataStore, Good-Bye SharedPreferences — Part 3</h3><h4>Migrate existing SharedPreferences data to DataStore</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F0fMORFZy2P_z_b2l2OZyw.jpeg" /></figure><p>Hey Android devs, we have discussed Preferences DataStore and Proto DataStore in the earlier part of this series. If you already have an Android app, probably you are using SharedPreferences. So, here we will discuss how we can migrate such existing SharedPreferences data to DataStore.</p><p>Before going to it, if anyone of you missed reading the previous article, it’s better to read it first before going through the current one. You can read it here👇</p><ul><li><a href="https://medium.com/@shalutd007/welcome-datastore-good-bye-sharedpreferences-4bf68e70efdb">Welcome DataStore, Good-Bye SharedPreferences</a></li><li><a href="https://medium.com/swlh/welcome-datastore-good-bye-sharedpreferences-fdeb831a1e58">Welcome DataStore, Good-Bye SharedPreferences</a></li></ul><p>Okay! for migration, Android provides a class called <a href="https://developer.android.com/reference/kotlin/androidx/datastore/migrations/SharedPreferencesMigration">SharedPreferencesMigration</a>. To migrate toDataStore, when creating the DataStore instance, just pass the existing old SharedPreferences name. So, the migration will be performed automatically for you.</p><p>Migrations are run before any data access can occur in DataStore. We need to make sure the migration is completed successfully before DataStore.data emits any values and before DataStore.updateData() can update the data.</p><blockquote>We can migrate to PreferencesDataStore or ProtoDataStore based on your requirement.</blockquote><h3>How can we migrate to Preferences DataStore?</h3><p>When migrating to Preferences DataStore, you should create the instance of SharedPreferencesMigration by just pass the context and the name used to construct your SharedPreferences. It can be done at the creation time of the DataStore instance as below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e80bc24d908dbeadf2c2a8e2cf7958e3/href">https://medium.com/media/e80bc24d908dbeadf2c2a8e2cf7958e3/href</a></iframe><h3>How can we migrate to Proto DataStore?</h3><p>When migrating to Proto DataStore, you should have to implement a mapping function that defines how to migrate from the key-value pairs used by SharedPreferences to the DataStore schema you defined. You can see the implementation code below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a88545ee0cd736ec8b8d89387ca09707/href">https://medium.com/media/a88545ee0cd736ec8b8d89387ca09707/href</a></iframe><p>Here also, we are creating the instance of SharedPreferencesMigration using the name of the SharedPreferences, and implement a mapping method that copying the values from SharedPreferences to DataStore schema.</p><p>Then, create the Proto DataStore object with the instance of SharedPreferencesMigration .</p><h3>Let&#39;s code, the migration to Preferences DataStore!</h3><p>Now, we will discuss how to migrate existing SharedPreferences data to <strong>Preferences DataStore</strong>. The code pushed at the GitHub <a href="https://github.com/shalutd/DataStoreDemo">repository</a>, you can freely download the code.</p><p>Here, we have a manager class MigrationManager which is responsible for the migration. You can see the class below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e9f5770ba385212535c76e7a05e99e00/href">https://medium.com/media/e9f5770ba385212535c76e7a05e99e00/href</a></iframe><p>As we said earlier, while creating the DataStore object, we need to pass the instance of SharedPreferencesMigration class. This instance is created using the SharedPreference’s name <strong>my_preference </strong>and the <strong>context</strong>. The same name should be given to the newly creating DataStore object.</p><p>After conversion, we can access the DataStore object data as we have discussed in earlier stories. In this manager class, the <strong>userDetailsFlow</strong> variable exposes a Flow of UserDetailsfrom the DataStore object.</p><p>Okay, now let us check how our MigrateToPrefDataStoreActivity is doing his job here. Initially, we are creating a SharedPreferences with the name <strong>my_preference, </strong>which storing FirstName (Roberto) and LastName (Bagio). Stored FirstName and LastName will be displayed on the screen.</p><p>The MigrateToPrefDataStoreActivity can be shown below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94f59a14dd5fe85fbe717922d06e3e4f/href">https://medium.com/media/94f59a14dd5fe85fbe717922d06e3e4f/href</a></iframe><p>While taping the Migrate button, it will create the MigrationManager object and so migration happening. After migration, we will read the migrated value from the DataStore through theMigrationManager , and also the value from the SharedPreferences will be displayed on the screen as below.</p><figure><img alt="Migrated screen" src="https://cdn-images-1.medium.com/max/442/1*l5N5KZzKDM2HiuArwxi9Tg.png" /></figure><p>Here, you can see the existing SharedPrefernes values become empty and stored in the datastore object.</p><p>So, Migration is over here. Thanks for reading!</p><h3>Resources:</h3><ul><li><a href="https://developer.android.com/reference/kotlin/androidx/datastore/migrations/SharedPreferencesMigration">SharedPreferencesMigration | Android Developers</a></li><li><a href="https://github.com/shalutd/DataStoreDemo">shalutd/DataStoreDemo</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7bd46203e91" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/welcome-datastore-good-bye-sharedpreferences-part-3-7bd46203e91">Welcome DataStore, Good-Bye SharedPreferences — Part 3</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[Welcome DataStore, Good-Bye SharedPreferences]]></title>
            <link>https://proandroiddev.com/welcome-datastore-good-bye-sharedpreferences-fdeb831a1e58?source=rss-24a1bf7756bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/fdeb831a1e58</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[flow]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[jetpack]]></category>
            <dc:creator><![CDATA[Shalu T D]]></dc:creator>
            <pubDate>Sun, 04 Oct 2020 09:43:54 GMT</pubDate>
            <atom:updated>2020-11-20T08:10:31.214Z</atom:updated>
            <content:encoded><![CDATA[<h3>Welcome DataStore, Good-Bye SharedPreferences -Part 2</h3><h4>Everything to know about ProtoDataStore</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F0fMORFZy2P_z_b2l2OZyw.jpeg" /></figure><p>Hey Android Folks, we got an idea about the Jetpack DataStore and Preferences Datastore in the <a href="https://medium.com/@shalutd007/welcome-datastore-good-bye-sharedpreferences-4bf68e70efdb">previous article</a>. While reading you might think what is the need of replacing the Sharedpreference with Preferences Datastore. Both Sharedpreferences and Preferences Datastore says about the key-value pair storing without thinking about storing data as instances of a custom data type and so, type safety. If you missed reading the previous article, it&#39;s better to read it first before going through the current one. You can read it 👇</p><p><a href="https://medium.com/@shalutd007/welcome-datastore-good-bye-sharedpreferences-4bf68e70efdb">Welcome DataStore, Good-Bye SharedPreferences</a></p><p>Now, let us discuss the Proto DataStore. As we know, we can’t store instances of a custom data type using SharedPreferences. But we can achieve the same with <strong>Proto DataStore.</strong></p><h3>Proto DataStore</h3><ul><li>It stores and retrieves instances as custom data types.</li><li>Defines schema using <a href="https://developers.google.com/protocol-buffers">Protocol buffers</a>.</li><li>They are faster, smaller, simpler, and less ambiguous than XML and other similar data formats</li></ul><h3>What are protocol buffers?</h3><p>Protocol buffers are Google’s language-neutral, platform-neutral, extensible mechanism for serializing structured data. Protocol buffers currently support generated code in Java, Python, Objective-C, and C++.</p><h3>So, let’s begin to code!</h3><p>Now, we will discuss how to implement the <strong>Proto DataStore</strong>. The code pushed at the GitHub <a href="https://github.com/shalutd/DataStoreDemo">repository</a>, you can freely download the code. This repository describes a sample Android app that stores the background color of the screen provided by the user. Users can choose a <strong>Black</strong> or <strong>White </strong>color.</p><h4>Step1: Configure build.gradle</h4><p>Configure build.gradle file of your app module as below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/559db87fcc9bbb01c42d4d53839f2ada/href">https://medium.com/media/559db87fcc9bbb01c42d4d53839f2ada/href</a></iframe><h4>Step2: Define a schema in proto file</h4><p>We should create a predefined schema in a proto file in the app/src/main/proto/ directory. So, create a new file called user_info.proto in the path app/src/main/proto as below. To learn more about defining a proto schema, see the <a href="https://developers.google.com/protocol-buffers/docs/proto3">protobuf language guide</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/db8c83b883f67bd0d774113940f35725/href">https://medium.com/media/db8c83b883f67bd0d774113940f35725/href</a></iframe><p>Once you’re created the above schema in user_info.proto, Rebuild<strong><em> </em></strong>your Gradle project. You can see that UserSettings.java will be automatically generated from the above proto schema.</p><h4>Step 3: Create Serializer for Proto class</h4><p>We will create a class which implements Serializer&lt;T&gt;, where T is the type defined in the proto file. In our app, UserSettings is the type. This serializer class tells DataStore how to read and write your data type. Below shows our serializer class named ProtoSettingsSerializer. 👇</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca9c34ee9f09bccf12b1dcac81a9dfcd/href">https://medium.com/media/ca9c34ee9f09bccf12b1dcac81a9dfcd/href</a></iframe><h4>Step 4<strong>: Create a Proto DataStore</strong></h4><p>We’ll use the Context.createDataStore() extension function to create an instance of DataStore&lt;UserSettings&gt;. The filename parameter tells DataStore which file to use to store the data, and the serializer parameter tells DataStore the name of the serializer class. You can see the Datastore creation in ProtoSettingsManagerclass below. 👇</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/09aa8bba01d2640301bb37c69e48aa34/href">https://medium.com/media/09aa8bba01d2640301bb37c69e48aa34/href</a></iframe><h4>Step 4: <strong>Read from Proto DataStore</strong></h4><p>As we have seen in the previous <a href="https://medium.com/@shalutd007/welcome-datastore-good-bye-sharedpreferences-4bf68e70efdb">article</a>, here we are using DataStore.data to expose a Flow of the appropriate property from the stored object. In the ProtoSettingsManagerclass, the userSettingsFlow variable exposes Flow of the UserSettings. We can access this variable from the activity later.</p><h4>Step 5: Write to Proto DataStore</h4><p>The Proto DataStore provides an updateData()function that transactionally updates a stored object. The above mentioned suspend method updateColor() in the ProtoSettingsManagerclass can be used to update the background color. Here, the argument to the method UserSettings.BgColor is a property mentioned in the user_info.proto proto schema.</p><h4><strong>Step 5: Reading and writing color value from activity.</strong></h4><p>In the ProtoDataStoreActivityshown below 👇, we have two buttons called <strong>Black</strong> and <strong>White</strong>. While taping <strong>Black</strong> button, it will update the DataStore object with the value UserSettings.BgColor.COLOR_BLACK but UserSettings.BgColor.COLOR_WHITE value for <strong>White</strong> button. While writing the color value, it will automatically invoke the protoSettingsManager.userSettingsFlow.collectsection and changing the background color based on the receiving value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df0eb804a31b3094ab65e9a41da498b3/href">https://medium.com/media/df0eb804a31b3094ab65e9a41da498b3/href</a></iframe><h4><strong>Step 6: Run the sample app!</strong></h4><p>You will get the below screen when you run the app. Here, it shows a screen with a white background. You can change the background color by tapping the Black button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*SVaPfIM65C2hKu5n-1rCyw.png" /></figure><p>Hopefully, this post helps clarify some concepts and lets you implement the DataStore storage solution.</p><p>If you have time, read the next part of this article below. 👇</p><p><a href="https://proandroiddev.com/welcome-datastore-good-bye-sharedpreferences-part-3-7bd46203e91">Welcome DataStore, Good-Bye SharedPreferences — Part 3</a></p><h4><strong>Resources</strong></h4><ul><li><a href="https://developer.android.com/topic/libraries/architecture/datastore">App Architecture: Data Layer - DataStore - Android Developers | App architecture</a></li><li><a href="https://github.com/shalutd/DataStoreDemo">shalutd/DataStoreDemo</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fdeb831a1e58" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/welcome-datastore-good-bye-sharedpreferences-fdeb831a1e58">Welcome DataStore, Good-Bye SharedPreferences</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[Welcome DataStore, Good-Bye SharedPreferences]]></title>
            <link>https://proandroiddev.com/welcome-datastore-good-bye-sharedpreferences-4bf68e70efdb?source=rss-24a1bf7756bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/4bf68e70efdb</guid>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[jetpack]]></category>
            <category><![CDATA[data-stores]]></category>
            <category><![CDATA[flow]]></category>
            <dc:creator><![CDATA[Shalu T D]]></dc:creator>
            <pubDate>Sat, 19 Sep 2020 12:39:19 GMT</pubDate>
            <atom:updated>2020-11-17T09:46:49.394Z</atom:updated>
            <content:encoded><![CDATA[<h3>Welcome DataStore, Good-Bye SharedPreferences -Part 1</h3><h4>Everything to know about DataStore and Preferences DataStore</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F0fMORFZy2P_z_b2l2OZyw.jpeg" /></figure><p>In this article series, we will discuss the new Jetpack library called <strong>DataStore</strong>.</p><p>Jetpack DataStore is a data storage solution that allows you to store key-value pairs or typed objects with <a href="https://developers.google.com/protocol-buffers">protocol buffers</a>. DataStore uses Kotlin’s coroutines and Flow to store data asynchronously, consistently, and transactionally. It will be going to replace SharedPreferences<strong>. </strong>If you are currently using SharedPreference in your projects, please try to migrate to datastore.</p><blockquote>DataStore is ideal for small, simple datasets and does not support partial updates or referential integrity. For this, we need to use Room Database.</blockquote><blockquote>There are two types of DataStore. <strong>Preferences DataStore</strong> and <strong>Proto DataStore</strong>.</blockquote><ul><li><strong>Preference DataStore</strong>, like SharedPreferences, it stores and access data using keys, has no way to define a schema or to ensure that keys are accessed with the correct type.</li><li><strong>Proto DataStore, </strong>stores data as instances of a custom data type. Here, we need to define a schema using <a href="https://developers.google.com/protocol-buffers">Protocol buffers</a>. Using Protobufs allows persisting <strong>strongly typed data</strong>. They are faster, smaller, simpler, and less ambiguous than XML and other similar data formats.</li></ul><p>The current series of this article focuses on the <strong>Preference DataStore </strong>only. We will discuss <strong>Proto DataStore </strong>in the<strong> </strong>next part of this article<strong>.</strong></p><h3>So, let’s begin to code!</h3><p>Now, we will discuss how to implement the <strong>Preference DataStore </strong>in code. We will discuss the code pushed at the GitHub <a href="https://github.com/shalutd/DataStoreDemo">repository</a>, you can freely download the code.</p><p>This repository describes a sample Android app that stores the background color of the screen provided by the user. Users can choose a <strong>Black</strong> or <strong>White </strong>color.</p><h4><strong>Step1: Add datastore dependency in build.gradle file of your app module</strong></h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c937adcfd059b251864b7f7cf73c3c36/href">https://medium.com/media/c937adcfd059b251864b7f7cf73c3c36/href</a></iframe><h4><strong>Step 2: Create a Preferences DataStore</strong></h4><p>Its implementation uses DataStore and Preferences classes. Create the DataStore object with the Context.createDataStore() extension function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/645831945a9bd051fb2b4a3d1f6047bb/href">https://medium.com/media/645831945a9bd051fb2b4a3d1f6047bb/href</a></iframe><h4><strong>Step 3: Read data from Preferences DataStore</strong></h4><p>DataStore exposes the stored data inFlow in Preferences object. Because Preferences DataStore does not use a predefined schema, you must use Preferences.preferencesKey() to define a key for each value that you need to store in the DataStore&lt;Preferences&gt; instance. The userPreferencesFlow object gets data from the data property of the datastore object. The stored color value can be either <strong><em>android.R.color.white</em></strong> or <strong><em>android.R.color.black.</em></strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/37398164aa6afc059a4fe0eadc771ffc/href">https://medium.com/media/37398164aa6afc059a4fe0eadc771ffc/href</a></iframe><h4><strong>Step 4: Write data to a Preferences DataStore</strong></h4><p>Preferences DataStore provides a method called edit() that transactionally updates the data in a DataStore. Below method, updateColor() suspend method is responsible for updating the color value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d5c976a3fc5d338d9f576fe90d798d56/href">https://medium.com/media/d5c976a3fc5d338d9f576fe90d798d56/href</a></iframe><h4><strong>Step 5: Reading and writing color value from activity.</strong></h4><p>In the PrefDataStoreActivity, we have two buttons called <strong>Black</strong> and <strong>White</strong>. when the user taping the black or white button, the color value <strong><em>android.R.color.white</em></strong> or <strong><em>android.R.color.black </em></strong>will be stored to datastore object. Here, we have a Flow variable, which will collect the current color value from the datastore and it&#39;s using for changing the background color of the screen.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36b0d110ca4ea7ae92be95585c6f540c/href">https://medium.com/media/36b0d110ca4ea7ae92be95585c6f540c/href</a></iframe><h4><strong>Step 6: Run the sample app!</strong></h4><p>You will get the below screen when you run the app. Here, it shows a screen with a white background. You can change the background color by tapping the Black button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/504/1*tpM5ATQfVN-oam90qglIuw.png" /></figure><p>That’s it 😍. We have implemented PreferencesDataStore now. 😍</p><p>Now, you can see the next article of the series which discusses the <strong>Proto DataStore</strong> here</p><p><a href="https://medium.com/swlh/welcome-datastore-good-bye-sharedpreferences-fdeb831a1e58">Welcome DataStore, Good-Bye SharedPreferences</a></p><h3>Resources</h3><ul><li><a href="https://developer.android.com/topic/libraries/architecture/datastore">App Architecture: Data Layer - DataStore - Android Developers | App architecture</a></li><li><a href="https://github.com/shalutd/DataStoreDemo">shalutd/DataStoreDemo</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4bf68e70efdb" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/welcome-datastore-good-bye-sharedpreferences-4bf68e70efdb">Welcome DataStore, Good-Bye SharedPreferences</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>
    </channel>
</rss>