package im.angry.openeuicc.ui import android.content.Intent import android.net.Uri import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.datastore.preferences.core.Preferences import androidx.lifecycle.lifecycleScope import androidx.preference.CheckBoxPreference import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import im.angry.openeuicc.common.R import im.angry.openeuicc.util.* import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking class SettingsFragment: PreferenceFragmentCompat() { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.pref_settings, rootKey) findPreference("pref_info_app_version") ?.summary = requireContext().selfAppVersion findPreference("pref_info_source_code") ?.setOnPreferenceClickListener { startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(it.summary.toString()))) true } findPreference("pref_advanced_logs") ?.setOnPreferenceClickListener { startActivity(Intent(requireContext(), LogsActivity::class.java)) true } findPreference("pref_notifications_download") ?.bindBooleanFlow(preferenceRepository.notificationDownloadFlow, PreferenceKeys.NOTIFICATION_DOWNLOAD) findPreference("pref_notifications_delete") ?.bindBooleanFlow(preferenceRepository.notificationDeleteFlow, PreferenceKeys.NOTIFICATION_DELETE) findPreference("pref_notifications_switch") ?.bindBooleanFlow(preferenceRepository.notificationSwitchFlow, PreferenceKeys.NOTIFICATION_SWITCH) findPreference("pref_advanced_disable_safeguard_removable_esim") ?.bindBooleanFlow(preferenceRepository.disableSafeguardFlow, PreferenceKeys.DISABLE_SAFEGUARD_REMOVABLE_ESIM) findPreference("pref_advanced_verbose_logging") ?.bindBooleanFlow(preferenceRepository.verboseLoggingFlow, PreferenceKeys.VERBOSE_LOGGING) } override fun onStart() { super.onStart() setupRootViewInsets(requireView().requireViewById(androidx.preference.R.id.recycler_view)) } private fun CheckBoxPreference.bindBooleanFlow(flow: Flow, key: Preferences.Key) { lifecycleScope.launch { flow.collect { isChecked = it } } setOnPreferenceChangeListener { _, newValue -> runBlocking { preferenceRepository.updatePreference(key, newValue as Boolean) } true } } }