From 983cfeb279c064879760596e9b6d689c188d6f9d Mon Sep 17 00:00:00 2001 From: Miguel Juarez Lopez Date: Thu, 24 Oct 2024 12:10:00 -0400 Subject: [PATCH 1/6] [ANDR][Example] Add config settings page --- platform/jvm/gradle-test-app/build.gradle.kts | 1 + .../ConfigurationSettingsFragment.kt | 52 +++++++++++++++++++ .../bitdrift/gradletestapp/FirstFragment.kt | 9 +++- .../src/main/res/layout/fragment_first.xml | 8 +++ .../src/main/res/navigation/nav_graph.xml | 8 +++ .../src/main/res/values/strings.xml | 2 + 6 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 platform/jvm/gradle-test-app/src/main/java/io/bitdrift/gradletestapp/ConfigurationSettingsFragment.kt diff --git a/platform/jvm/gradle-test-app/build.gradle.kts b/platform/jvm/gradle-test-app/build.gradle.kts index 1399e746..4c525292 100644 --- a/platform/jvm/gradle-test-app/build.gradle.kts +++ b/platform/jvm/gradle-test-app/build.gradle.kts @@ -15,6 +15,7 @@ dependencies { implementation("androidx.constraintlayout:constraintlayout:2.1.4") implementation("androidx.navigation:navigation-fragment-ktx:2.5.3") implementation("androidx.navigation:navigation-ui-ktx:2.5.3") + implementation("androidx.preference:preference-ktx:1.2.1") implementation("com.apollographql.apollo3:apollo-runtime:3.8.3") implementation("com.apollographql.apollo3:apollo-runtime:3.8.3") implementation("com.jakewharton.timber:timber:5.0.1") diff --git a/platform/jvm/gradle-test-app/src/main/java/io/bitdrift/gradletestapp/ConfigurationSettingsFragment.kt b/platform/jvm/gradle-test-app/src/main/java/io/bitdrift/gradletestapp/ConfigurationSettingsFragment.kt new file mode 100644 index 00000000..814e840a --- /dev/null +++ b/platform/jvm/gradle-test-app/src/main/java/io/bitdrift/gradletestapp/ConfigurationSettingsFragment.kt @@ -0,0 +1,52 @@ +package io.bitdrift.gradletestapp + +import android.content.Intent +import android.content.pm.PackageManager +import android.os.Bundle +import androidx.preference.EditTextPreference +import androidx.preference.Preference +import androidx.preference.PreferenceCategory +import androidx.preference.PreferenceFragmentCompat +import kotlin.system.exitProcess + +class ConfigurationSettingsFragment : PreferenceFragmentCompat() { + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { + val context = preferenceManager.context + val screen = preferenceManager.createPreferenceScreen(context) + + val apiUrlPref = EditTextPreference(context) + apiUrlPref.key = "apiUrl" + apiUrlPref.title = "API URL" + apiUrlPref.summary = "App needs to be restarted for changes to take effect" + + val apiKeyPref = EditTextPreference(context) + apiKeyPref.key = "apiKey" + apiKeyPref.title = "API Key" + apiKeyPref.summary = "App needs to be restarted for changes to take effect" + + val backendCategory = PreferenceCategory(context) + backendCategory.key = "control_plane_category" + backendCategory.title = "Control Plane Configuration" + + screen.addPreference(backendCategory) + backendCategory.addPreference(apiUrlPref) + backendCategory.addPreference(apiKeyPref) + + val restartPreference = Preference(context) + restartPreference.key = "restart" + restartPreference.title = "Restart the App" + restartPreference.setOnPreferenceClickListener { + val launchIntent = context.packageManager.getLaunchIntentForPackage(context.packageName) + val restartIntent = Intent.makeRestartActivityTask(launchIntent!!.component) + // Required for API 34 and later + // Ref: https://developer.android.com/about/versions/14/behavior-changes-14#safer-intents + restartIntent.setPackage(context.packageName); + context.startActivity(restartIntent) + exitProcess(0) + } + + screen.addPreference(restartPreference) + + preferenceScreen = screen + } +} \ No newline at end of file diff --git a/platform/jvm/gradle-test-app/src/main/java/io/bitdrift/gradletestapp/FirstFragment.kt b/platform/jvm/gradle-test-app/src/main/java/io/bitdrift/gradletestapp/FirstFragment.kt index 9379f24d..0cb96736 100644 --- a/platform/jvm/gradle-test-app/src/main/java/io/bitdrift/gradletestapp/FirstFragment.kt +++ b/platform/jvm/gradle-test-app/src/main/java/io/bitdrift/gradletestapp/FirstFragment.kt @@ -100,7 +100,11 @@ class FirstFragment : Fragment() { setContent { // In Compose world MaterialTheme { - Text("Hello from Compose!") + Text( + text = "Text in Compose", + style = MaterialTheme.typography.h6, + color = MaterialTheme.colors.secondary, + ) } } } @@ -110,6 +114,9 @@ class FirstFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + binding.btnNavigateConfiguration.setOnClickListener { + findNavController().navigate(R.id.action_FirstFragment_to_ConfigFragment) + } binding.btnCopySessionUrl.setOnClickListener(this::copySessionUrl) binding.btnStartNewSession.setOnClickListener(this::startNewSession) binding.btnTempDeviceCode.setOnClickListener(this::getTempDeviceCode) diff --git a/platform/jvm/gradle-test-app/src/main/res/layout/fragment_first.xml b/platform/jvm/gradle-test-app/src/main/res/layout/fragment_first.xml index b57783af..6061c6c3 100644 --- a/platform/jvm/gradle-test-app/src/main/res/layout/fragment_first.xml +++ b/platform/jvm/gradle-test-app/src/main/res/layout/fragment_first.xml @@ -11,6 +11,14 @@ tools:context=".FirstFragment" > +