Add search function to preferences

This commit is contained in:
ByteHamster 2018-05-06 23:46:45 +02:00
parent d7d614f33f
commit f8950ae9b8
6 changed files with 62 additions and 12 deletions

View File

@ -173,6 +173,7 @@ dependencies {
implementation "com.github.AntennaPod:AntennaPod-AudioPlayer:$audioPlayerVersion"
implementation 'com.github.mfietz:fyydlin:v0.3'
implementation 'com.github.ByteHamster:SearchPreference:v1.0.2'
androidTestImplementation "com.jayway.android.robotium:robotium-solo:$robotiumSoloVersion"
}

View File

@ -14,6 +14,9 @@ import android.widget.FrameLayout;
import java.lang.ref.WeakReference;
import com.bytehamster.lib.preferencesearch.SearchPreference;
import com.bytehamster.lib.preferencesearch.SearchPreferenceResult;
import com.bytehamster.lib.preferencesearch.SearchPreferenceResultListener;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.preferences.PreferenceController;
@ -22,7 +25,7 @@ import de.danoeh.antennapod.preferences.PreferenceController;
* PreferenceActivity for API 11+. In order to change the behavior of the preference UI, see
* PreferenceController.
*/
public class PreferenceActivity extends AppCompatActivity {
public class PreferenceActivity extends AppCompatActivity implements SearchPreferenceResultListener {
public static final String PARAM_RESOURCE = "resource";
private static WeakReference<PreferenceActivity> instance;
@ -35,6 +38,11 @@ public class PreferenceActivity extends AppCompatActivity {
this.fragment = fragment;
}
@Override
public PreferenceFragmentCompat getFragment() {
return fragment;
}
@Override
public Preference findPreference(CharSequence key) {
return fragment.findPreference(key);
@ -76,12 +84,21 @@ public class PreferenceActivity extends AppCompatActivity {
// since the MainFragment depends on the preferenceController already being created
preferenceController = new PreferenceController(preferenceUI);
showPreferenceScreen(R.xml.preferences, false);
}
private void showPreferenceScreen(int screen, boolean addHistory) {
PreferenceFragmentCompat prefFragment = new MainFragment();
preferenceUI.setFragment(prefFragment);
Bundle args = new Bundle();
args.putInt(PARAM_RESOURCE, R.xml.preferences);
args.putInt(PARAM_RESOURCE, screen);
prefFragment.setArguments(args);
getSupportFragmentManager().beginTransaction().replace(R.id.content, prefFragment).commit();
if (addHistory) {
getSupportFragmentManager().beginTransaction().replace(R.id.content, prefFragment)
.addToBackStack(getString(PreferenceController.getTitleOfPage(screen))).commit();
} else {
getSupportFragmentManager().beginTransaction().replace(R.id.content, prefFragment).commit();
}
}
@Override
@ -111,6 +128,12 @@ public class PreferenceActivity extends AppCompatActivity {
}
}
@Override
public void onSearchResultClicked(SearchPreferenceResult result) {
showPreferenceScreen(result.getResourceFile(), true);
result.highlight(preferenceUI.getFragment());
}
public static class MainFragment extends PreferenceFragmentCompat {
private int screen;
@ -136,7 +159,7 @@ public class PreferenceActivity extends AppCompatActivity {
super.onResume();
PreferenceActivity activity = instance.get();
if(activity != null && activity.preferenceController != null) {
activity.setTitle(activity.preferenceController.getTitleOfPage(screen));
activity.setTitle(PreferenceController.getTitleOfPage(screen));
activity.preferenceUI.setFragment(this);
activity.preferenceController.onResume(screen);
}

View File

@ -37,6 +37,7 @@ import android.widget.Toast;
import com.afollestad.materialdialogs.MaterialDialog;
import com.bytehamster.lib.preferencesearch.SearchPreference;
import de.danoeh.antennapod.activity.AboutActivity;
import de.danoeh.antennapod.activity.ImportExportActivity;
import de.danoeh.antennapod.activity.MediaplayerActivity;
@ -489,6 +490,7 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc
private void setupMainScreen() {
final AppCompatActivity activity = ui.getActivity();
setupSearch();
ui.findPreference(PREF_SCREEN_USER_INTERFACE).setOnPreferenceClickListener(preference -> {
openScreen(R.xml.preferences_user_interface, activity);
return true;
@ -555,6 +557,25 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc
});
}
private void setupSearch() {
final AppCompatActivity activity = ui.getActivity();
SearchPreference searchPreference = (SearchPreference) ui.findPreference("searchPreference");
searchPreference.setActivity(activity);
searchPreference.setFragmentContainerViewId(R.id.content);
searchPreference.setBreadcrumbsEnabled(true);
searchPreference.addResourceFileToIndex(R.xml.preferences_user_interface, getTitleOfPage(R.xml.preferences_user_interface));
searchPreference.addResourceFileToIndex(R.xml.preferences_playback, getTitleOfPage(R.xml.preferences_playback));
searchPreference.addResourceFileToIndex(R.xml.preferences_network, getTitleOfPage(R.xml.preferences_network));
searchPreference.addResourceFileToIndex(R.xml.preferences_storage, getTitleOfPage(R.xml.preferences_storage));
String integrations = activity.getString(getTitleOfPage(R.xml.preferences_integrations));
String flattr = activity.getString(getTitleOfPage(R.xml.preferences_flattr));
String gpodder = activity.getString(getTitleOfPage(R.xml.preferences_gpodder));
searchPreference.addResourceFileToIndex(R.xml.preferences_flattr, integrations + " > " + flattr);
searchPreference.addResourceFileToIndex(R.xml.preferences_gpodder, integrations + " > " + gpodder);
}
public PreferenceFragmentCompat openScreen(int preferences, AppCompatActivity activity) {
PreferenceFragmentCompat prefFragment = new PreferenceActivity.MainFragment();
Bundle args = new Bundle();
@ -566,7 +587,7 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc
return prefFragment;
}
public int getTitleOfPage(int preferences) {
public static int getTitleOfPage(int preferences) {
switch (preferences) {
case R.xml.preferences_network:
return R.string.network_pref;
@ -1152,6 +1173,7 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc
public interface PreferenceUI {
void setFragment(PreferenceFragmentCompat fragment);
PreferenceFragmentCompat getFragment();
/**
* Finds a preference based on its key.

View File

@ -1,10 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<com.bytehamster.lib.preferencesearch.SearchPreference
android:key="searchPreference" />
<Preference
android:key="prefScreenInterface"
android:title="@string/user_interface_label"
android:icon="?attr/type_video" />
android:key="prefScreenInterface"
android:title="@string/user_interface_label"
android:icon="?attr/type_video" />
<Preference
android:key="prefScreenPlayback"

View File

@ -37,12 +37,12 @@ subprojects {
}
project.ext {
compileSdkVersion = 25
compileSdkVersion = 26
buildToolsVersion = "27.0.3"
minSdkVersion = 14
targetSdkVersion = 26
supportVersion = "25.3.1"
supportVersion = "26.1.0"
commonsioVersion = "2.5"
commonslangVersion = "3.6"
commonstextVersion = "1.3"

View File

@ -12,7 +12,7 @@ dependencies:
- ~/.android
- ~/android
pre:
- echo y | android update sdk --no-ui --all --filter "tool,extra-android-m2repository,extra-android-support,extra-google-google_play_services,extra-google-m2repository,android-25"
- echo y | android update sdk --no-ui --all --filter "tool,extra-android-m2repository,extra-android-support,extra-google-google_play_services,extra-google-m2repository,android-26"
- echo y | android update sdk --no-ui --all --filter "build-tools-27.0.3"
override:
- echo override dependencies
@ -20,4 +20,4 @@ dependencies:
test:
override:
- ./gradlew assembleDebug -PdisablePreDex:
timeout: 1800
timeout: 1800