This file contains 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
// inisialisasi | |
companion object { | |
private val NAME_KEY = stringPreferencesKey("NAME_KEY") | |
} | |
private val Context._dataStore: DataStore<Preferences> by preferencesDataStore( | |
name = "sampingan_data_store", | |
produceMigrations = ::sharedPreferencesMigration | |
) | |
private fun sharedPreferencesMigration(context: Context) = |
This file contains 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
fun <T> DataStore<Preferences>.getValueFlow( | |
key: Preferences.Key<T>, | |
defaultValue: T, | |
): Flow<T> { | |
return this.data | |
.catch { exception -> | |
if (exception is IOException) { | |
emit(emptyPreferences()) | |
} else { | |
throw exception |
This file contains 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
// inisialisasi | |
companion object { | |
private val NAME_KEY = stringPreferencesKey("NAME_KEY") | |
} | |
private val Context._dataStore: DataStore<Preferences> by preferencesDataStore( | |
name = "sampingan_data_store", | |
produceMigrations = ::sharedPreferencesMigration | |
) |
This file contains 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 MainActivity : AppCompatActivity() { | |
private lateinit var bmiBrain: BmiBrain | |
// initialize class | |
bmiBrain = BmiBrain(this) | |
// load value | |
binding.apply { | |
lifecycleScope.launch { | |
val name = async { bmiBrain.getName() } |
This file contains 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-alpha08" |
This file contains 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 MainViewModel(private val dataStore: DataStore<Preferences>) : ViewModel() { | |
private val _name = MutableLiveData<String>() | |
val name: LiveData<String> | |
get() = _name | |
fun fetch() { | |
viewModelScope.launch { | |
dataStore.data | |
.catch { _name.postValue(it.message) } |
This file contains 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 MainActivity : AppCompatActivity() { | |
// initialize view model, in this case using koin extension | |
private val viewModel: MainViewModel by viewModel() | |
// observe value | |
viewModel.name.observe(this, ::setName) | |
private fun setName(name: String?) = binding.edtName.setText(name) | |
// fetch data on the first time / activtity created |
This file contains 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
@Test | |
fun `Join Test`() { | |
runBlocking { | |
val job : Job = launch { println(Thread.currentThread().name) } | |
job.join() | |
} | |
} | |
@Test | |
fun `JoinAll Test`() { |
OlderNewer