From d7435c0d2f4a908a9cda58444e203af4d2b9b63e Mon Sep 17 00:00:00 2001
From: romanvht <romanvht@gmail.com>
Date: Sat, 26 Oct 2024 01:58:40 +0400
Subject: [PATCH] Fix AppSelectionFragment

---
 .../byedpi/fragments/AppSelectionFragment.kt  |  1 +
 .../byedpi/fragments/MainSettingsFragment.kt  | 61 +++++++++----------
 app/src/main/res/layout/app_selection.xml     |  1 +
 app/src/main/res/xml/main_settings.xml        |  3 +-
 4 files changed, 33 insertions(+), 33 deletions(-)

diff --git a/app/src/main/java/io/github/dovecoteescapee/byedpi/fragments/AppSelectionFragment.kt b/app/src/main/java/io/github/dovecoteescapee/byedpi/fragments/AppSelectionFragment.kt
index 483177d..cb22c85 100644
--- a/app/src/main/java/io/github/dovecoteescapee/byedpi/fragments/AppSelectionFragment.kt
+++ b/app/src/main/java/io/github/dovecoteescapee/byedpi/fragments/AppSelectionFragment.kt
@@ -66,6 +66,7 @@ class AppSelectionFragment : Fragment() {
             }
             recyclerView.adapter = adapter
             progressBar.visibility = View.GONE
+            searchView.visibility = View.VISIBLE
         }
     }
 
diff --git a/app/src/main/java/io/github/dovecoteescapee/byedpi/fragments/MainSettingsFragment.kt b/app/src/main/java/io/github/dovecoteescapee/byedpi/fragments/MainSettingsFragment.kt
index e9b4563..a90a2a7 100644
--- a/app/src/main/java/io/github/dovecoteescapee/byedpi/fragments/MainSettingsFragment.kt
+++ b/app/src/main/java/io/github/dovecoteescapee/byedpi/fragments/MainSettingsFragment.kt
@@ -52,7 +52,7 @@ class MainSettingsFragment : PreferenceFragmentCompat() {
                 true
             }
 
-        val accessibilityStatusPref = findPreferenceNotNull<Preference>("accessibility_service_status")
+        val accessibilityStatus = findPreferenceNotNull<Preference>("accessibility_service_status")
         val switchCommandLineSettings = findPreferenceNotNull<SwitchPreference>("byedpi_enable_cmd_settings")
 
         val uiSettings = findPreferenceNotNull<Preference>("byedpi_ui_settings")
@@ -67,10 +67,11 @@ class MainSettingsFragment : PreferenceFragmentCompat() {
 
         switchCommandLineSettings.setOnPreferenceChangeListener { _, newValue ->
             setByeDpiSettingsMode(newValue as Boolean)
+            updatePreferences()
             true
         }
 
-        accessibilityStatusPref.setOnPreferenceClickListener {
+        accessibilityStatus.setOnPreferenceClickListener {
             val intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
             startActivity(intent)
             true
@@ -78,13 +79,14 @@ class MainSettingsFragment : PreferenceFragmentCompat() {
 
         findPreferenceNotNull<Preference>("version").summary = BuildConfig.VERSION_NAME
 
-        updateAccessibilityStatus(accessibilityStatusPref)
+        updateAccessibilityStatus()
         updatePreferences()
     }
 
     override fun onResume() {
         super.onResume()
         sharedPreferences?.registerOnSharedPreferenceChangeListener(preferenceListener)
+        updateAccessibilityStatus()
         updatePreferences()
     }
 
@@ -94,34 +96,30 @@ class MainSettingsFragment : PreferenceFragmentCompat() {
     }
 
     private fun updatePreferences() {
-        val mode = findPreferenceNotNull<ListPreference>("byedpi_mode")
-            .value.let { Mode.fromString(it) }
+        val mode = findPreferenceNotNull<ListPreference>("byedpi_mode").value.let { Mode.fromString(it) }
         val dns = findPreferenceNotNull<EditTextPreference>("dns_ip")
         val ipv6 = findPreferenceNotNull<SwitchPreference>("ipv6_enable")
-
-        val applist_type = findPreferenceNotNull<ListPreference>("applist_type")
-        val selected_apps = findPreferenceNotNull<Preference>("selected_apps")
-        val accessibilityStatusPref = findPreferenceNotNull<Preference>("accessibility_service_status")
-
-        updateAccessibilityStatus(accessibilityStatusPref)
+        val applistType = findPreferenceNotNull<ListPreference>("applist_type")
+        val selectedApps = findPreferenceNotNull<Preference>("selected_apps")
 
         when (mode) {
             Mode.VPN -> {
                 dns.isVisible = true
                 ipv6.isVisible = true
-                when (applist_type.value) {
+
+                when (applistType.value) {
                     "disable" -> {
-                        applist_type.isVisible = true
-                        selected_apps.isVisible = false
+                        applistType.isVisible = true
+                        selectedApps.isVisible = false
                     }
                     "blacklist", "whitelist" -> {
-                        applist_type.isVisible = true
-                        selected_apps.isVisible = true
+                        applistType.isVisible = true
+                        selectedApps.isVisible = true
                     }
                     else -> {
-                        applist_type.isVisible = true
-                        selected_apps.isVisible = false
-                        Log.w(TAG, "Unexpected applist_type value: ${applist_type.value}")
+                        applistType.isVisible = true
+                        selectedApps.isVisible = false
+                        Log.w(TAG, "Unexpected applistType value: ${applistType.value}")
                     }
                 }
             }
@@ -129,23 +127,22 @@ class MainSettingsFragment : PreferenceFragmentCompat() {
             Mode.Proxy -> {
                 dns.isVisible = false
                 ipv6.isVisible = false
-                applist_type.isVisible = false
-                selected_apps.isVisible = false
+                applistType.isVisible = false
+                selectedApps.isVisible = false
             }
         }
     }
 
-    private fun updateAccessibilityStatus(preference: Preference?) {
-        preference?.let {
-            val isEnabled = AccessibilityUtils.isAccessibilityServiceEnabled(
-                requireContext(),
-                AutoStartAccessibilityService::class.java
-            )
-            it.summary = if (isEnabled) {
-                getString(R.string.accessibility_service_enabled)
-            } else {
-                getString(R.string.accessibility_service_disabled)
-            }
+    private fun updateAccessibilityStatus() {
+        val accessibilityStatus = findPreferenceNotNull<Preference>("accessibility_service_status")
+        val isEnabled = AccessibilityUtils.isAccessibilityServiceEnabled(
+            requireContext(),
+            AutoStartAccessibilityService::class.java
+        )
+        accessibilityStatus.summary = if (isEnabled) {
+            getString(R.string.accessibility_service_enabled)
+        } else {
+            getString(R.string.accessibility_service_disabled)
         }
     }
 }
diff --git a/app/src/main/res/layout/app_selection.xml b/app/src/main/res/layout/app_selection.xml
index 02ffc35..3a2eec3 100644
--- a/app/src/main/res/layout/app_selection.xml
+++ b/app/src/main/res/layout/app_selection.xml
@@ -8,6 +8,7 @@
         android:id="@+id/searchView"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
+        android:visibility="gone"
         android:queryHint="@string/search_apps" />
 
     <LinearLayout
diff --git a/app/src/main/res/xml/main_settings.xml b/app/src/main/res/xml/main_settings.xml
index 7ebd9e6..90635eb 100644
--- a/app/src/main/res/xml/main_settings.xml
+++ b/app/src/main/res/xml/main_settings.xml
@@ -44,7 +44,8 @@
 
         <Preference
             android:key="selected_apps"
-            android:title="@string/apps_select" />
+            android:title="@string/apps_select"
+            app:fragment="io.github.dovecoteescapee.byedpi.fragments.AppSelectionFragment" />
 
     </androidx.preference.PreferenceCategory>