Merge pull request #2679 from ByteHamster/preference-search

Preference search
This commit is contained in:
Martin Fietz 2018-05-07 19:24:16 +02:00 committed by GitHub
commit 36ac93e031
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 78 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.3'
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,41 @@ 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.index()
.addBreadcrumb(getTitleOfPage(R.xml.preferences_user_interface))
.addFile(R.xml.preferences_user_interface);
searchPreference.index()
.addBreadcrumb(getTitleOfPage(R.xml.preferences_playback))
.addFile(R.xml.preferences_playback);
searchPreference.index()
.addBreadcrumb(getTitleOfPage(R.xml.preferences_network))
.addFile(R.xml.preferences_network);
searchPreference.index()
.addBreadcrumb(getTitleOfPage(R.xml.preferences_storage))
.addFile(R.xml.preferences_storage);
searchPreference.index()
.addBreadcrumb(getTitleOfPage(R.xml.preferences_network))
.addBreadcrumb(R.string.automation)
.addBreadcrumb(getTitleOfPage(R.xml.preferences_autodownload))
.addFile(R.xml.preferences_autodownload);
searchPreference.index()
.addBreadcrumb(getTitleOfPage(R.xml.preferences_integrations))
.addBreadcrumb(getTitleOfPage(R.xml.preferences_gpodder))
.addFile(R.xml.preferences_gpodder);
searchPreference.index()
.addBreadcrumb(getTitleOfPage(R.xml.preferences_integrations))
.addBreadcrumb(getTitleOfPage(R.xml.preferences_flattr))
.addFile(R.xml.preferences_flattr);
}
public PreferenceFragmentCompat openScreen(int preferences, AppCompatActivity activity) {
PreferenceFragmentCompat prefFragment = new PreferenceActivity.MainFragment();
Bundle args = new Bundle();
@ -566,7 +603,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 +1189,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