Skip to content

Instantly share code, notes, and snippets.

@bhaskar253
Created April 10, 2025 17:46
Show Gist options
  • Save bhaskar253/5f77900e94b60c84942337de63a99838 to your computer and use it in GitHub Desktop.
Save bhaskar253/5f77900e94b60c84942337de63a99838 to your computer and use it in GitHub Desktop.
Experimental code for work
package com.example.experimental
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.preferencesDataStore
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class MainActivity : AppCompatActivity() {
private val prefsFlow: Flow<MyPreferences> by lazy {
dataStore.data.map {
val dialogShown = it[PreferencesKeys.DIALOG_SHOWN] ?: false
MyPreferences(dialogShown)
}
}
private suspend fun updateDialogShown(dialogShown: Boolean) {
dataStore.edit {
it[PreferencesKeys.DIALOG_SHOWN] = dialogShown
}
}
private lateinit var state: Bundle
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
state = savedInstanceState ?: Bundle()
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
lifecycleScope.launch(Dispatchers.IO) {
prefsFlow.collect {
if (!it.dialogShown) {
updateDialogShown(true)
withContext(Dispatchers.Main) {
val dialog = AlertDialog.Builder(this@MainActivity).apply {
setMessage("Experiment Successful")
setPositiveButton("OK") { _, _ ->
state.putBoolean("ACTIVITY_STARTED", true)
Intent(Settings.ACTION_SETTINGS).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}.also { intnt ->
startActivity(intnt)
}
}
setNegativeButton("Cancel") { _, _ -> }
}.also { dlg ->
dlg.create()
}
dialog.show()
}
}
}
}
}
override fun onResume() {
super.onResume()
if (::state.isInitialized && state.getBoolean("ACTIVITY_STARTED", false)) {
state.putBoolean("ACTIVITY_STARTED", false)
Toast.makeText(this@MainActivity, "Back from Settings", Toast.LENGTH_LONG).show()
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
if (::state.isInitialized) {
Toast.makeText(this@MainActivity, "Saving something", Toast.LENGTH_SHORT).show()
outState.putAll(state)
}
}
}
data class MyPreferences(val dialogShown: Boolean)
private const val MY_PREFERENCES_NAME = "my_preferences"
private val Context.dataStore by preferencesDataStore(
name = MY_PREFERENCES_NAME
)
private object PreferencesKeys {
val DIALOG_SHOWN = booleanPreferencesKey("dialog_shown")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment