Skip to content

Instantly share code, notes, and snippets.

View putrarifin's full-sized avatar
🌴
On vacation

Muh Arif Saputra putrarifin

🌴
On vacation
View GitHub Profile
@putrarifin
putrarifin / gist:028b52281ccda28eec390890bf75b8ef
Created October 14, 2020 07:09
WebView FileChooser/Camera Android
webView.settings.javaScriptEnabled = true
webView.settings.loadWithOverviewMode = true
webView.settings.useWideViewPort = true
webView.settings.domStorageEnabled = true
webView.settings.allowFileAccess=true
webView.settings.allowContentAccess=true
webView.settings.allowUniversalAccessFromFileURLs=true
webView.settings.allowFileAccessFromFileURLs=true
webView.settings.javaScriptCanOpenWindowsAutomatically=true
webView.loadUrl(Constants.URL)
@ExperimentalCoroutinesApi
class MainViewModel : ViewModel() {
private val _countState = MutableStateFlow(0)
val countState: StateFlow<Int> = _countState
fun incrementCount() {
_countState.value++
}
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun main() = runBlocking {
val stateFlow = MutableStateFlow<Int>(0)
// Observe values
val job = launch {
stateFlow.collect {
print("$it ")
class MainActivity : AppCompatActivity() {
private val viewModel by lazy {
ViewModelProvider(this)[MainViewModel::class.java]
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
private fun initView() {
button_plus.setOnClickListener(::incrementCounter)
button_minus.setOnClickListener(::decrementCounter)
}
private fun incrementCounter(view: View) {
viewModel.incrementCount()
}
private fun decrementCounter(view: View) {
private fun initCountObserver() {
lifecycleScope.launch {
viewModel.countState.collect { value ->
textview_count.text = "$value"
}
}
}
dependencies {
// Preferences DataStore
implementation "androidx.datastore:datastore-preferences:1.0.0-alpha01"
}
class SettingsManager(context: Context) {
private val dataStore = context.createDataStore(name = "settings_pref")
...
companion object {
val IS_DARK_MODE = preferencesKey<Boolean>("dark_mode")
}
suspend fun setUiMode(uiMode: UiMode) {
dataStore.edit { preferences ->
preferences[IS_DARK_MODE] = when (uiMode) {
UiMode.LIGHT -> false
UiMode.DARK -> true
}
}
}