<?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 Bryan Abergas on Medium]]></title>
        <description><![CDATA[Stories by Bryan Abergas on Medium]]></description>
        <link>https://medium.com/@bryanabergas?source=rss-b089536d69b8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*1pEdWZxIlzEUmguQ.</url>
            <title>Stories by Bryan Abergas on Medium</title>
            <link>https://medium.com/@bryanabergas?source=rss-b089536d69b8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 02:33:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bryanabergas/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[Compose Multiplatform — DataStore Implementation]]></title>
            <link>https://medium.com/@bryanabergas/compose-multiplatform-datastore-implementation-722e0f3ae264?source=rss-b089536d69b8------2</link>
            <guid isPermaLink="false">https://medium.com/p/722e0f3ae264</guid>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[compose-multiplatform]]></category>
            <dc:creator><![CDATA[Bryan Abergas]]></dc:creator>
            <pubDate>Sat, 07 Dec 2024 12:42:34 GMT</pubDate>
            <atom:updated>2024-12-07T12:42:34.795Z</atom:updated>
            <content:encoded><![CDATA[<h3>Compose Multiplatform — DataStore Implementation</h3><p>I was not able to find a tutorial for this one, so I wanted to share how I managed to do it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5GHxQ6C8XgN_E7F6BGLeew.png" /></figure><p>So what is DataStore?</p><p><strong>DataStore</strong> is a library provided by Android for storing small amounts of data locally on a device. Think of it as a modern and more efficient way to save data like user preferences, settings, or even small structured data. It’s like the upgraded version of SharedPreferences but better in terms of performance, safety, and flexibility.</p><p>As a start implement these two libraries.</p><pre>androidx.datastore:datastore-preferences:&lt;version&gt;<br>androidx.datastore:datastore:&lt;version&gt;</pre><p>Make sure to put these two libraries under commonMain.dependencies</p><p>Once done, create a DataStore file under commonMain directory e.g.:</p><pre>commonMain<br>-&gt;com<br>-&gt;-&gt;mobile<br>-&gt;-&gt;-&gt;app<br>-&gt;-&gt;-&gt;-&gt;DataStore.kt</pre><p>Make sure to create the same file in android and ios directories with matching directories named DataStore.&lt;platform&gt;.kt</p><p>Once done, go back to commonMain directory and add these line to the DataStore.kt</p><pre>expect fun createDataStore(): DataStore&lt;Preferences&gt;<br><br>lateinit var dataStore: DataStore&lt;Preferences&gt;<br><br>internal const val dataStoreFileName = &quot;application.preferences_pb&quot;</pre><p>and in android directory DataStore.android.kt</p><pre>actual fun createDataStore(): DataStore&lt;Preferences&gt; =<br>    createWithPath(produceFile = {<br>        AppContext.get().filesDir.resolve(dataStoreFileName).absolutePath.toPath()<br>    })<br>        .also { dataStore = it }</pre><p>also in ios directory DataStore.ios.kt</p><pre>@OptIn(ExperimentalForeignApi::class)<br>actual fun createDataStore(): DataStore&lt;Preferences&gt; = createWithPath(produceFile = {<br>    val documentDirectory: NSURL? = NSFileManager.defaultManager.URLForDirectory(<br>        directory = NSDocumentDirectory,<br>        inDomain = NSUserDomainMask,<br>        appropriateForURL = null,<br>        create = false,<br>        error = null,<br>    )<br>    (requireNotNull(documentDirectory).path + &quot;/$dataStoreFileName&quot;).toPath()<br>})</pre><p>We have added an annotation because we’re using a foreign API iniOSwhich is thetoPath() , this library should be available when the dependencies of DataStore is added.</p><p>We have come this far and we’re missing something right? and yes it is the AppContext.get() which is used in the DataStore.android.kt , there are a lot of variation of how to include the context in this part, so I will leave this one for you to write up your own implementation.</p><p>I think the only question we’re left with is how to use it, and here’s my implementation.</p><pre>private val preferenceDataStore: DataStore&lt;Preferences&gt; by lazy {<br>    createDataStore()<br>}<br><br>class AppDataStoreRepository() {<br><br>    private val INFO = stringPreferencesKey(&quot;some_info&quot;)<br><br>    val information: Flow&lt;String&gt; = preferenceDataStore.data.map { preference -&gt;<br>        preference[INFO] = it<br>    }<br><br>    suspend fun saveInformation(info: String) {<br>        preferenceDataStore.edit {<br>            it[INFO] = info<br>        }<br>    }<br>}</pre><p>If there are any improvements or implementations that you have found, please feel free to share it as well.</p><p>I wasn’t lucky enough to find one so I created what I needed.</p><p>This is my first time sharing something as well, so thank you for reading this.</p><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=722e0f3ae264" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>