Skip to content

Instantly share code, notes, and snippets.

View cp-hardik-p's full-sized avatar

hardik parmar cp-hardik-p

View GitHub Profile
plugins {
...
id "com.google.protobuf" version "0.8.17"
}
// Proto DataStore
implementation "androidx.datastore:datastore:1.0.0"
// protobuf
implementation "com.google.protobuf:protobuf-javalite:3.19.4"
class SaveUserInfo(private val context: Context) {
// Create the dataStore and give it a name same as user_pref
// Create some keys we will use them to store and retrieve the data
companion object {
val Context.dataStore: DataStore<Preferences> by preferencesDataStore("user_prefs")
val USER_AGE_KEY = intPreferencesKey("USER_AGE")
val USER_NAME_KEY = stringPreferencesKey("USER_NAME")
}
// get the user's age
val userAgeFlow: Flow<Int> = context.dataStore.data.map { preferences ->
preferences[USER_AGE_KEY] ?: 0
}
// get the user's name
val userNameFlow: Flow<String> = context.dataStore.data.map { preferences ->
preferences[USER_NAME_KEY] ?: ""
}
suspend fun storeUserInfo(age: Int, name: String) {
context.dataStore.edit { preferences ->
preferences[USER_AGE_KEY] = age
preferences[USER_NAME_KEY] = name
}
}
val USER_AGE_KEY = intPreferencesKey("USER_AGE")
val USER_NAME_KEY = stringPreferencesKey("USER_NAME")
val Context.dataStore: DataStore<Preferences> by preferencesDataStore("user_prefs")
implementation "androidx.datastore:datastore-preferences:1.0.0"
setContent {
AutoScrollTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = ThemeColor
) {
AutoScrollingLazyRow(list = (1..8).take(4)) {
LazyListItem(text = "Item $it")
}
}
private const val SCROLL_DX = 24f
private const val REQUIRED_CARD_COUNT = 8
private class AutoScrollItem<T>(
val id: String = UUID.randomUUID().toString(),
val data: T
)
@Composable
fun <T : Any> AutoScrollingLazyRow(