Skip to content

Instantly share code, notes, and snippets.

@crazygit
Last active July 25, 2023 22:31
Show Gist options
  • Save crazygit/a99503df175434914717b86fa5a3e10c to your computer and use it in GitHub Desktop.
Save crazygit/a99503df175434914717b86fa5a3e10c to your computer and use it in GitHub Desktop.
Jetpack DataStore in MVVM
import com.github.crazygit.tikheart.TikHeartApplication
import com.github.crazygit.tikheart.data.dao.UserInfoDao
import com.github.crazygit.tikheart.data.model.UserInfo
import com.github.crazygit.tikheart.utilities.LocalStorage
object LocalRepository {
private val localStorage = LocalStorage(TikHeartApplication.appContext)
private val localUserDao = UserInfoDao(localStorage)
fun getUserInfo() = localUserDao.getUserInfo()
suspend fun saveUser(userInfo: UserInfo) = localUserDao.saveUserInfo(userInfo)
suspend fun removeUserInfo() = localUserDao.removeUserInfo()
}
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.preferencesKey
import androidx.datastore.preferences.createDataStore
import kotlinx.coroutines.flow.map
class LocalStorage(appContext: Context) {
val dataStore: DataStore<Preferences> = appContext.createDataStore(
name = "AppDataStore"
)
suspend inline fun <reified T> save(key: Preferences.Key<T>, value: Any) {
dataStore.edit {
it[key] = value as T
}
}
inline fun <reified T> get(key: Preferences.Key<T>, default: T) = dataStore.data.map {
it[key] ?: default
}
companion object PreferenceKeys {
val USER_INFO_KEY = preferencesKey<String>("user_info")
}
}
import android.content.Context
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.github.crazygit.tikheart.utilities.LocalStorage
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class LocalStorageTest {
private val appContext: Context = InstrumentationRegistry.getInstrumentation().targetContext
private lateinit var localStorage: LocalStorage
@Before
fun init() {
localStorage = LocalStorage(appContext)
}
@Test
fun testLocalStorage() = runBlocking {
val data = "hello"
localStorage.save(LocalStorage.USER_INFO_KEY, data)
assertThat(localStorage.get(LocalStorage.USER_INFO_KEY, "").first()).isEqualTo(data)
}
}
import com.github.crazygit.tikheart.utilities.LocalStorage
import com.github.crazygit.tikheart.data.model.UserInfo
import com.google.gson.Gson
import com.google.gson.JsonSyntaxException
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
class UserInfoDao(private val localStorage: LocalStorage) {
fun getUserInfo(): Flow<UserInfo?> = localStorage.get(LocalStorage.USER_INFO_KEY, "").map {
try {
Gson().fromJson(it, UserInfo::class.java)
} catch (e: JsonSyntaxException) {
null
}
}
suspend fun saveUserInfo(userInfo: UserInfo) =
localStorage.save(LocalStorage.USER_INFO_KEY, Gson().toJson(userInfo))
suspend fun removeUserInfo() = localStorage.save(LocalStorage.USER_INFO_KEY, "")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment