<?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 sourabh gupta on Medium]]></title>
        <description><![CDATA[Stories by sourabh gupta on Medium]]></description>
        <link>https://medium.com/@saurav12994?source=rss-26861b5a1973------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*r-WSxV5602njaqJe.jpg</url>
            <title>Stories by sourabh gupta on Medium</title>
            <link>https://medium.com/@saurav12994?source=rss-26861b5a1973------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 01:33:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@saurav12994/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[Hi DataStore ,Adieu Shared Prefs]]></title>
            <link>https://saurav12994.medium.com/hi-datastore-adieu-shared-prefs-3a1d19aa4b29?source=rss-26861b5a1973------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a1d19aa4b29</guid>
            <category><![CDATA[android-apps]]></category>
            <category><![CDATA[jetpack-datastore]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[sourabh gupta]]></dc:creator>
            <pubDate>Tue, 24 Aug 2021 15:05:09 GMT</pubDate>
            <atom:updated>2021-08-24T15:05:09.351Z</atom:updated>
            <content:encoded><![CDATA[<h3>Hi DataStore ,Adieu Shared Prefs</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/1*ysHx0jWoVyLTT6B_MvFleg.png" /></figure><h3><strong>What is JetPack DataStore?</strong></h3><p><em>Jetpack DataStore is a data storage solution that allows you to store key-value pairs or typed objects with protocol buffers. DataStore uses Kotlin coroutines and Flow to store data asynchronously, consistently, and transactionally.</em></p><h3>ShortComings in SharedPreferences</h3><p>SharedPreferences interface provides an API for managing a small collection of key-values preferences saved in the XML file.However there is some drawbacks in <strong>Shared Prefs</strong></p><ul><li><strong><em>Typed Safety :</em></strong> Type safety is not supported in SharedPrefs.It can led to run-time exceptions</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-dT4k2geYKH8F__20JX_sw.png" /></figure><p>It is easy to just call putBoolean instead of putInt for a preference key there is nothing to tell us that this is that gives us type safety</p><ul><li><strong><em>Transaction Api :</em></strong> SharedPrefs doesn’t provide transactional Api it can lead to conflicts if Multiple threads update data at same-time</li><li><strong><em>Safe to call from UI Thread : </em></strong>It is not not safe to call from UI thread,Shared prefs in case of <strong>commit()</strong> saves the data to in-memory storage first and then <strong>synchronously</strong> writes it to disk. Since IO operation is involved, main thread is blocked until the data is written to the disk.In case of apply() function writes data to disk on top of fsync() which runs naturally in blocking thread and it will wait for other task to be done</li><li><strong><em>Safe from Run-Time exceptions: </em></strong>Shared preferences has no way of signalling errors actually there was you’ll just get a run-time exception being thrown and even if you have a parsing error where the the share preferences file can’t be read even that is also a runtime exception you can’t really differentiate between these so you can actually get to handle them properly</li><li><strong><em>Handle Migrations: </em></strong>Shared preferences doesn’t handle migration so let’s say that you decide to change the type of one of your values from boolean to int for example well you’ll have to make all of the changes needed to migrate to this new version manually</li><li><strong><em>UnBundled: </em></strong>Share preferences is part of the android OS so this means that it’s hard to actually make changes and improve it</li></ul><h3>Types of DataStore</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/697/1*3GqBkrKw_UxRv-R0rbk9dw.jpeg" /></figure><p><strong>Preferences DataStore</strong> stores and accesses data using keys. This implementation does not require a predefined schema, and it does not provide type safety.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fGRsgXzTXxrgbPiki71TYA.png" /></figure><p><strong>Proto DataStore</strong> stores data as instances of a custom data type. This implementation requires you to define a schema using <a href="https://developers.google.com/protocol-buffers">protocol buffers</a>, but it provides type safety.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AGlVBz_fZBbtFfLjQSQFgQ.png" /></figure><h3>How DataStore solve drawbacks of Shared Prefs?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IrySBz3Wayx9ePvmEbEtBQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*HwyYmM8F_CmbGywMp_7h5g.jpeg" /></figure><h3>So, let’s begin to code!</h3><h3><strong>Preferences DataStore</strong></h3><p>Let’s implement the <strong>Preference DataStore </strong>in code.</p><p><strong>Step 1: Add dependency in build.gradle file of your app module</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rzKXSXLlm2sbyYR5RU4kRA.png" /></figure><p><strong>Step 2: Create the object of DataStore Preferences</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oRLOswiHIOF_hy5bi_2mZA.png" /></figure><p><strong>Step 3: Set Data in DataStore</strong></p><p>Preferences DataStore provides an <a href="https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/package-summary#edit">edit()</a> function that transactionally updates the data in a DataStore.Below method, saveUserName() suspend method is responsible for saving user name.<a href="https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/package-summary#edit">edit()</a> coroutine completes when the data has been persisted durably to disk.<a href="https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/package-summary#edit">edit()</a>can also throw an IO exception if something happened while trying to write the data on file by default datastore.edit is called on dispatchers.IO ifso like this it means that this call is safe to be called from the main thread</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4lX2QlJwyG93l_--OXzwmQ.png" /></figure><p><strong>Step 4: Read Data from DataStore</strong></p><p>Preferences DataStore does not use a predefined schema .DataStore used flow to expose the stored data in Preferences object. we must use corresponding key type function to define a key for each value that you need to store in the DataStore&lt;Preferences&gt; instance. we can handle the error by using the catch operator on the flow.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1TdqEQwLrXat4PevaoXGJg.png" /></figure><p>Supported DataTypes: Int , Long, Boolean , Float , String</p><h3><strong>Proto DataStore</strong></h3><p><strong>Step 1 :</strong></p><p>We have to make several changes in app level build.gradle</p><p>•Add the Protobuf plugin</p><p>•Add the Protobuf and Proto DataStore dependencies</p><p>•Configure Protobuf</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NHnwVz62yblqhP_mvTUmLQ.png" /></figure><p><strong>Step 2 : Create the Proto file</strong></p><p>we have to create proto file inside src/main/proto folder.The person message definition specifies four fields (name/value pairs), one for each piece of data that you want to include in this type of message. Each field has a name and a type.we can also specify composite types for your fields, including enumerations and other message types.As you can see, each field in the message definition has a unique number. These field numbers are used to identify your fields in the message binary format.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0CNqe3NC55vcuOI6GmUNjA.png" /></figure><p><strong>Step 3 :Create the serializer</strong></p><p>To tell DataStore how to read and write the data type we defined in the proto file, we need to implement a Serializer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/966/1*v3bRNvkeMVZi6yhrXGENyw.png" /></figure><p><strong>Step 4 : Create Datastore instance</strong></p><p>To create a DataStore instance we use the dataStore delegate, with the Context as receiver. The delegate has two mandatory parameters:</p><ul><li>The name of the file that DataStore will act on.</li><li>The serializer for the type used with DataStore.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ycn9mloBjkVsKzScxzGGKw.png" /></figure><p><strong>Step 5 : Read Data from DataStore</strong></p><p>For reading DataStore.data expose flow which you can use to read data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/930/1*u30DEzROubBOtiV9YXlQwQ.png" /></figure><p><strong>Step 6: Write Data in DataStore</strong></p><p>For writing, Proto DataStore provides an updateData() function that transactionally updates a stored object. updateData() gives you the current state of the data as an instance of your data type and updates the data transactionally in an atomic read-write-modify operation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8DR7ok5nYE_YsIwKaIkTrQ.png" /></figure><p><strong>References:</strong></p><ul><li><a href="https://developer.android.com/topic/libraries/architecture/datastore">https://developer.android.com/guide/topics/ui/shortcuts/creating-shortcuts</a></li></ul><h3>Summing-up</h3><p>I think this articles will help you to understand importance of DataStore.<br>This article surely motivate to you to use DataStore. Start using it from now.More you practice,More you learn.</p><p><strong>Keep Exploring and Keep Learning…………</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a1d19aa4b29" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Intro to App Shortcuts]]></title>
            <link>https://engg.glance.com/intro-to-app-shortcuts-eddc64aa7597?source=rss-26861b5a1973------2</link>
            <guid isPermaLink="false">https://medium.com/p/eddc64aa7597</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[shortcuts]]></category>
            <dc:creator><![CDATA[sourabh gupta]]></dc:creator>
            <pubDate>Sun, 14 Feb 2021 11:30:54 GMT</pubDate>
            <atom:updated>2021-11-19T04:52:42.205Z</atom:updated>
            <content:encoded><![CDATA[<p>Shortcuts are a way to quickly provide specific functionality or features to users. Each shortcut can refer to more than one intent.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*PM3F4wecK1TWiaU0DUnluQ.gif" /></figure><p>Types of Shortcuts</p><p><strong>Static Shortcuts — </strong>These types of shortcuts deliver consistent behavior to users throughout the lifetime of the application, and they’re not context-sensitive.For example, if the user wants to view their calendar or email in a specific way, using a static shortcut ensures that their experience in performing a routine task is consistent.</p><p><strong>Dynamic Shortcuts- </strong>it is used for actions in apps that are context-sensitive. For instance, if you build a game that allows the user to start from their current level on launch, the shortcut will need to be updated frequently</p><p><strong>Pinned Shortcuts- </strong>Pinned shortcuts are used to perform specific user-driven actions. These shortcuts will ensure the users can access specific functionalities or features with a single click from the home screen.</p><p>For example, a user might want to pin a specific website to the launcher. This is beneficial because it allows the user to perform a custom action, like navigating to the website in one step</p><p><strong>Limitation:</strong></p><ol><li>One app can publish utmost 5 shortcuts combining static and dynamic.</li><li>Most launcher supports only 4 shortcuts</li><li>Unlike static and dynamic shortcuts, pinned shortcuts appear in supported launchers as separate icons</li><li>Pinned shortcuts can only be created on devices with Android 8 and above.</li></ol><p><strong>Implementation</strong></p><p><strong>Static ShortCuts:-</strong></p><ol><li>In your app’s manifest file (AndroidManifest.xml), find an activity whose intent filters are set to the android.intent.action.MAIN action and the android.intent.category.LAUNCHER category.</li><li>Add a &lt;meta-data&gt; element to this activity that references the resource file where the app’s shortcuts are defined</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1001/1*gRoJUNzYVgbAkyTU0MvuaA.png" /></figure><p>3. Create a new resource file: res/xml/shortcuts.xml.</p><p>4. Inside this resource file, we can define one or more shortcuts. The file should start with a shortcuts tag, and inside that, we can add multiple shortcuts</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RzFpkix5g1rYPeS1iuWQBQ.png" /></figure><p>That’s all. We’ve created static shortcuts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*HVcF9fdqqJ2u0svsCedt4A.png" /></figure><p><strong>Dynamic Shortcuts:</strong></p><p>We can create publish,update and remove dynamic shortcuts through <strong>ShortcutManager </strong>API</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ppNmByVPgHQ21W0ygRMPew.png" /></figure><p><strong>Pinned ShortCuts:</strong></p><p>Similarly to creating a dynamic shortcut, we have to use the ShortcutManager API for pinned shortcuts as well.</p><ul><li>First, to check if the launcher supports pinned shortcuts, we need to use isRequestPinShortcutSupported()</li><li>Create a shortcutInfo Object.</li><li>we have to create a pending intent and pass both ShortcutInfo and the pending intent to the requestPinShortcut function on the ShortcutManager instance</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iTPUVwPupaAuifBfUuSMWA.png" /></figure><p><strong>References:</strong></p><ul><li><a href="https://developer.android.com/guide/topics/ui/shortcuts/creating-shortcuts">https://developer.android.com/guide/topics/ui/shortcuts/creating-shortcuts</a></li><li><a href="https://developer.android.com/guide/topics/ui/shortcuts/managing-shortcuts#update-shortcuts">https://developer.android.com/guide/topics/ui/shortcuts/managing-shortcuts#update-shortcuts</a></li></ul><h3>Summing-up</h3><p>I think this articles will help you to understand importance of App Shortcuts.<br>This article surely motivate to you to use App Shortcuts. Start using it from now.More you practice,More you learn.</p><p><strong>Keep Exploring and Keep Learning…………</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eddc64aa7597" width="1" height="1" alt=""><hr><p><a href="https://engg.glance.com/intro-to-app-shortcuts-eddc64aa7597">Intro to App Shortcuts</a> was originally published in <a href="https://engg.glance.com/">Glance</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Intro to RxJava]]></title>
            <link>https://saurav12994.medium.com/intro-to-rxjava-6e73077be848?source=rss-26861b5a1973------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e73077be848</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[learning]]></category>
            <category><![CDATA[rxjava]]></category>
            <dc:creator><![CDATA[sourabh gupta]]></dc:creator>
            <pubDate>Thu, 16 Jul 2020 20:30:41 GMT</pubDate>
            <atom:updated>2020-07-16T20:30:41.540Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Rxjava – AndroidPub" src="https://cdn-images-1.medium.com/proxy/1*DERNKsR4v95_V6jKke2hzw.png" /></figure><h3>What is RxJava?</h3><p>RxJava is a Java VM implementation of ReactiveX .It is library for composing asynchronous and event-based programs by using observable sequences.</p><h3>Why RxJava?</h3><ol><li>Remove <strong>Boilerplate code</strong></li><li>RxJava makes <strong>multi-threading super easy</strong>. Using imperative approach, moving a piece of code to background thread is hard work. However in RxJava, we could easily define what thread each part of the chain would be in.</li><li><strong>RxJava is composable</strong>, Rx operators can be combined to produce more complicated operations.</li><li>Rx operators <strong>can transform one type of data to another</strong>, reducing, mapping or expanding streams as needed.</li><li><strong>Error handling becomes much easier</strong> with RxJava. You don’t need to worry about adding try catch blocks everywhere.</li><li>The RxJava library was designed to give a control over a wide range of data, simultaneously on the events in the real-time. This allows us <strong>to build highly responsive mobile applications</strong>.</li></ol><h3>RxJava Vs RxAndroid</h3><p>RxAndroid is just a layer on top of RxJava which provides android specific support.There is new Scheduler class (AndroidSchedulers.mainThread()) in RxAndroid.</p><h4>If you study any Rx Java implemented code, all of the time, you will find three main constructs</h4><ol><li>Observables</li><li>Schedulers</li><li>Observers</li></ol><h3>Observables</h3><p><strong>Observables</strong> is part of code which is used to emit and process data. Many <strong>Subscribers</strong>(Observers) can subscribe to Single <strong>Observable</strong>.We can modify data emitted by observables by using <strong>Operators</strong></p><h3>Schedulers</h3><p>We can achieve Multithreading in RxJava using <strong>Schedulers</strong>.A scheduler can be recognized as a thread pool managing one or more threads. Whenever a Scheduler needs to execute a task, it will take a thread from its pool and run the task in that thread.RxJava works on single thread by default.We can specify thread on which <strong>Observable</strong> emit data and on which <strong>Observer</strong> observe data.</p><p><strong>Different types of Schedulers available in RxJava</strong></p><p><strong><em>Schedulers.io() :</em></strong></p><p>This can have a limitless thread pool. Used for non CPU intensive tasks. Such as database interactions, performing network communications and interactions with the file system.</p><p><strong><em>Schedulers.io() :</em></strong></p><p>This can have a limitless thread pool. Used for CPU intensive tasks. Such as image processing.</p><p><strong><em>AndroidSchedulers.mainThread()</em></strong></p><p>This is the main thread or the UI thread. This is where user interactions happen</p><p><strong><em>Schedulers.newThread()</em></strong></p><p>This scheduler creates a new thread for each unit of work scheduled.</p><p><strong><em>Schedulers.single()</em></strong></p><p>This scheduler has a single thread executing tasks one after another following the given order.</p><p><strong><em>Schedulers.trampoline()</em></strong></p><p>This scheduler executes tasks following first in, first out basics.We use this when implementing recurring tasks.</p><p><strong><em>Schedulers.from(Executor executor)</em></strong></p><p>This creates and returns a custom scheduler backed by the specified executor.</p><h3>Observers</h3><p>Observers is the one who receives data emitted by Observables.</p><p>One Observable can have many observers. An observable emit data, if there is <strong>at least one</strong> observer subscribed for the data. If there is no subscription observable will not emit data.</p><p>Main observer methods</p><ul><li>OnSubscribe():-Each time when observer subscribe to Observable ,OnSubscribe() Method called.</li><li>onNext() :- Each time an Observable emits data it calls to Observer’s onNext() method passing that data.</li><li>onError() :- If any error occurs Observable calls to Observer’s onError() method.</li><li>onComplete() :- Observable invokes Observer’s onComplete() method, when the data emission is over.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/0*hjr4mjs72CmA1-e0" /></figure><h3><strong>What is Operator?</strong></h3><p>Operator is used to modify and filter data emitted by Observables.if we apply multiple operator then it forms a chain ,Every Operator operate on data emitted by previous operator.Order is important in case of chain,</p><p>There are many operators in RxJava,Some of are given below.</p><ol><li><strong>Just Operator-</strong>The Just operator converts an item into an Observable that emits that item.</li><li><strong>From Array Operator-</strong>it creates an Observable from set of items using an Iterable, which means each item is emitted one at a time.</li><li><strong>Range Operator-</strong><a href="http://reactivex.io/documentation/operators/range.html">Range()</a> creates an Observable from a sequence of generated integers. The function generates sequence of integers by taking starting number and length. So the above same examples can be modified as Observable.range(1, 10).</li><li><strong>Create Operator-</strong>Creates an Observable from scratch and allows observer method to call programmatically.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jzOgrewM1XKSbDU_bf_mMg.png" /></figure><p>5.<strong> Map Operator- </strong>Transform the items emitted by an Observable by applying a function to each item</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2uBLTFrmt3T4rf0k" /></figure><p>6.<strong> Flat Map Operator-</strong>transform the items emitted by an Observable into Observables, then flatten the emissions from those into a single Observable</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/712/0*ybfJ-3EFyJO2GzyW" /></figure><p>7. <strong>Concat Map Operator-</strong>ConcatMap works almost the same as flatMap, but preserves the order of items. But concatMap has one big flaw: it waits for each observable to finish all the work until next one is processed</p><p>8. <strong>Buffer Operator-</strong>The Buffer operator transforms an Observable that emits items into an Observable that emits buffered collections of those items</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/0*0iKkW28R0SlgtaQg" /></figure><p>9. <strong>Filter operator-</strong>The Filter operator filters an Observable by only allowing items through that pass a test that you specify in the form of a predicate function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/686/0*04vewYayjkvXaiXl" /></figure><p>10. <strong>Distinct Operator-</strong>The Distinct operator filters an Observable by only allowing items through that have not already been emitted.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/816/0*FPP2q4MWU12jC5bc" /></figure><p>11. <strong>Skip Operator-</strong>You can ignore the first <em>n</em> items emitted by an Observable and attend only to those items that come after, by modifying the Observable with the Skip operator</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/808/0*xlvb_eBWJM6IN0dm" /></figure><p>12.<strong> Skip Last-</strong>You can ignore the final <em>n</em> items emitted by an Observable and attend only to those items that come before them, by modifying the Observable with the SkipLast operator.</p><h3>Summing-up</h3><p>I think this articles will help you to understand importance of RxJava.<br>This article surely motivate to you to refractor your old code to handling code to background tasks.Start using it from now.More you practice,More you learn.</p><p><strong>Keep Exploring and Keep Learning…………</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e73077be848" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>