This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| plugins { | |
| ... | |
| id "com.google.protobuf" version "0.8.17" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Proto DataStore | |
| implementation "androidx.datastore:datastore:1.0.0" | |
| // protobuf | |
| implementation "com.google.protobuf:protobuf-javalite:3.19.4" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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] ?: "" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| suspend fun storeUserInfo(age: Int, name: String) { | |
| context.dataStore.edit { preferences -> | |
| preferences[USER_AGE_KEY] = age | |
| preferences[USER_NAME_KEY] = name | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val USER_AGE_KEY = intPreferencesKey("USER_AGE") | |
| val USER_NAME_KEY = stringPreferencesKey("USER_NAME") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val Context.dataStore: DataStore<Preferences> by preferencesDataStore("user_prefs") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| implementation "androidx.datastore:datastore-preferences:1.0.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| setContent { | |
| AutoScrollTheme { | |
| Surface( | |
| modifier = Modifier.fillMaxSize(), | |
| color = ThemeColor | |
| ) { | |
| AutoScrollingLazyRow(list = (1..8).take(4)) { | |
| LazyListItem(text = "Item $it") | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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( |