From 5a7cbdfaca008e9049aaa3a0506e23aabd1a66a7 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Fri, 14 Jun 2019 10:34:55 +0200 Subject: [PATCH] Added authentication and episode filter dialogs --- .../PreferenceControllerFlavorHelper.java | 3 +- .../activity/FeedSettingsActivity.java | 91 ---------- .../dialog/EpisodeFilterDialog.java | 71 ++++++++ .../fragment/FeedSettingsFragment.java | 38 +++- .../main/res/layout/episode_filter_dialog.xml | 96 ++++++++++ app/src/main/res/layout/feedsettings.xml | 169 ------------------ app/src/main/res/xml/feed_settings.xml | 2 +- 7 files changed, 207 insertions(+), 263 deletions(-) create mode 100644 app/src/main/java/de/danoeh/antennapod/dialog/EpisodeFilterDialog.java create mode 100644 app/src/main/res/layout/episode_filter_dialog.xml diff --git a/app/src/free/java/de/danoeh/antennapod/preferences/PreferenceControllerFlavorHelper.java b/app/src/free/java/de/danoeh/antennapod/preferences/PreferenceControllerFlavorHelper.java index ff39ed389..e096f883f 100644 --- a/app/src/free/java/de/danoeh/antennapod/preferences/PreferenceControllerFlavorHelper.java +++ b/app/src/free/java/de/danoeh/antennapod/preferences/PreferenceControllerFlavorHelper.java @@ -1,11 +1,12 @@ package de.danoeh.antennapod.preferences; import de.danoeh.antennapod.core.preferences.UserPreferences; +import de.danoeh.antennapod.fragment.preferences.PlaybackPreferencesFragment; /** * Implements functions from PreferenceController that are flavor dependent. */ -class PreferenceControllerFlavorHelper { +public class PreferenceControllerFlavorHelper { public static void setupFlavoredUI(PlaybackPreferencesFragment ui) { ui.findPreference(UserPreferences.PREF_CAST_ENABLED).setEnabled(false); diff --git a/app/src/main/java/de/danoeh/antennapod/activity/FeedSettingsActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/FeedSettingsActivity.java index 2a7e9d5c4..fd526d1db 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/FeedSettingsActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/FeedSettingsActivity.java @@ -40,18 +40,6 @@ public class FeedSettingsActivity extends AppCompatActivity { private ImageView imgvBackground; private TextView txtvAuthorHeader; - /* - private EditText etxtUsername; - private EditText etxtPassword; - private EditText etxtFilterText; - private RadioButton rdoFilterInclude; - private RadioButton rdoFilterExclude; - private CheckBox cbxAutoDownload; - private CheckBox cbxKeepUpdated; - private Spinner spnAutoDelete; - private boolean filterInclude = true; - */ - @Override protected void onCreate(Bundle savedInstanceState) { setTheme(UserPreferences.getTheme()); @@ -69,22 +57,6 @@ public class FeedSettingsActivity extends AppCompatActivity { // https://github.com/bumptech/glide/issues/529 imgvBackground.setColorFilter(new LightingColorFilter(0xff828282, 0x000000)); - /* - etxtUsername = findViewById(R.id.etxtUsername); - etxtPassword = findViewById(R.id.etxtPassword); - etxtFilterText = findViewById(R.id.etxtEpisodeFilterText); - rdoFilterInclude = findViewById(R.id.radio_filter_include); - rdoFilterInclude.setOnClickListener(v -> { - filterInclude = true; - filterTextChanged = true; - }); - rdoFilterExclude = findViewById(R.id.radio_filter_exclude); - rdoFilterExclude.setOnClickListener(v -> { - filterInclude = false; - filterTextChanged = true; - }); - */ - disposable = Maybe.create((MaybeOnSubscribe) emitter -> { Feed feed = DBReader.getFeed(feedId); if (feed != null) { @@ -150,69 +122,6 @@ public class FeedSettingsActivity extends AppCompatActivity { .into(imgvBackground); } - /* - private void setupPrefs(Feed feed) { - FeedPreferences prefs = feed.getPreferences(); - - etxtUsername.setText(prefs.getUsername()); - etxtPassword.setText(prefs.getPassword()); - - etxtUsername.addTextChangedListener(authTextWatcher); - etxtPassword.addTextChangedListener(authTextWatcher); - - FeedFilter filter = prefs.getFilter(); - if (filter.includeOnly()) { - etxtFilterText.setText(filter.getIncludeFilter()); - rdoFilterInclude.setChecked(true); - rdoFilterExclude.setChecked(false); - filterInclude = true; - } else if (filter.excludeOnly()) { - etxtFilterText.setText(filter.getExcludeFilter()); - rdoFilterInclude.setChecked(false); - rdoFilterExclude.setChecked(true); - filterInclude = false; - } else { - Log.d(TAG, "No filter set"); - rdoFilterInclude.setChecked(false); - rdoFilterExclude.setChecked(false); - etxtFilterText.setText(""); - } - etxtFilterText.addTextChangedListener(filterTextWatcher); - - } - - @Override - protected void onPause() { - super.onPause(); - if (feed != null) { - FeedPreferences prefs = feed.getPreferences(); - if (authInfoChanged) { - Log.d(TAG, "Auth info changed, saving credentials"); - prefs.setUsername(etxtUsername.getText().toString()); - prefs.setPassword(etxtPassword.getText().toString()); - } - if (filterTextChanged) { - Log.d(TAG, "Filter info changed, saving..."); - String filterText = etxtFilterText.getText().toString(); - String includeString = ""; - String excludeString = ""; - if (filterInclude) { - includeString = filterText; - } else { - excludeString = filterText; - } - prefs.setFilter(new FeedFilter(includeString, excludeString)); - } - if (authInfoChanged || autoDeleteChanged || filterTextChanged) { - DBWriter.setFeedPreferences(prefs); - } - authInfoChanged = false; - autoDeleteChanged = false; - filterTextChanged = false; - } - } - */ - @Override public void onDestroy() { super.onDestroy(); diff --git a/app/src/main/java/de/danoeh/antennapod/dialog/EpisodeFilterDialog.java b/app/src/main/java/de/danoeh/antennapod/dialog/EpisodeFilterDialog.java new file mode 100644 index 000000000..ba778be56 --- /dev/null +++ b/app/src/main/java/de/danoeh/antennapod/dialog/EpisodeFilterDialog.java @@ -0,0 +1,71 @@ +package de.danoeh.antennapod.dialog; + +import android.app.Dialog; +import android.content.Context; +import android.os.Bundle; +import android.widget.Button; +import android.widget.EditText; +import android.widget.RadioButton; +import de.danoeh.antennapod.R; +import de.danoeh.antennapod.core.feed.FeedFilter; + +/** + * Displays a dialog with a text box for filtering episodes and two radio buttons for exclusion/inclusion + */ +public abstract class EpisodeFilterDialog extends Dialog { + + private final FeedFilter initialFilter; + + public EpisodeFilterDialog(Context context, FeedFilter filter) { + super(context); + this.initialFilter = filter; + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.episode_filter_dialog); + final EditText etxtEpisodeFilterText = findViewById(R.id.etxtEpisodeFilterText); + final RadioButton radioInclude = findViewById(R.id.radio_filter_include); + final RadioButton radioExclude = findViewById(R.id.radio_filter_exclude); + final Button butConfirm = findViewById(R.id.butConfirm); + final Button butCancel = findViewById(R.id.butCancel); + + setTitle(R.string.episode_filters_label); + setOnCancelListener(dialog -> onCancelled()); + + if (initialFilter.includeOnly()) { + radioInclude.setChecked(true); + etxtEpisodeFilterText.setText(initialFilter.getIncludeFilter()); + } else if(initialFilter.excludeOnly()) { + radioExclude.setChecked(true); + etxtEpisodeFilterText.setText(initialFilter.getExcludeFilter()); + } else { + radioExclude.setChecked(false); + radioInclude.setChecked(false); + etxtEpisodeFilterText.setText(""); + } + + + butCancel.setOnClickListener(v -> cancel()); + butConfirm.setOnClickListener(v -> { + + String includeString = ""; + String excludeString = ""; + if (radioInclude.isChecked()) { + includeString = etxtEpisodeFilterText.getText().toString(); + } else { + excludeString = etxtEpisodeFilterText.getText().toString(); + } + + onConfirmed(new FeedFilter(includeString, excludeString)); + dismiss(); + }); + } + + protected void onCancelled() { + + } + + protected abstract void onConfirmed(FeedFilter filter); +} diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java index 02e264eec..6a321658f 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java @@ -5,16 +5,21 @@ import android.content.DialogInterface; import android.os.Bundle; import android.support.v14.preference.SwitchPreference; import android.support.v7.preference.ListPreference; +import android.support.v7.preference.Preference; import android.support.v7.preference.PreferenceFragmentCompat; import de.danoeh.antennapod.R; import de.danoeh.antennapod.activity.FeedSettingsActivity; import de.danoeh.antennapod.core.dialog.ConfirmationDialog; import de.danoeh.antennapod.core.feed.Feed; +import de.danoeh.antennapod.core.feed.FeedFilter; import de.danoeh.antennapod.core.feed.FeedPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.storage.DBWriter; +import de.danoeh.antennapod.dialog.AuthenticationDialog; +import de.danoeh.antennapod.dialog.EpisodeFilterDialog; public class FeedSettingsFragment extends PreferenceFragmentCompat { + private static final CharSequence PREF_EPISODE_FILTER = "episodeFilter"; private Feed feed; private FeedPreferences feedPreferences; @@ -28,11 +33,42 @@ public class FeedSettingsFragment extends PreferenceFragmentCompat { setupAutoDownloadPreference(); setupKeepUpdatedPreference(); setupAutoDeletePreference(); + setupAuthentificationPreference(); + setupEpisodeFilterPreference(); updateAutoDeleteSummary(); updateAutoDownloadEnabled(); } + private void setupEpisodeFilterPreference() { + findPreference(PREF_EPISODE_FILTER).setOnPreferenceClickListener(preference -> { + new EpisodeFilterDialog(getContext(), feedPreferences.getFilter()) { + @Override + protected void onConfirmed(FeedFilter filter) { + feedPreferences.setFilter(filter); + feed.savePreferences(); + } + }.show(); + return false; + }); + } + + private void setupAuthentificationPreference() { + findPreference("authentication").setOnPreferenceClickListener(preference -> { + new AuthenticationDialog(getContext(), + R.string.authentication_label, true, false, + feedPreferences.getUsername(), feedPreferences.getPassword()) { + @Override + protected void onConfirmed(String username, String password, boolean saveUsernamePassword) { + feedPreferences.setUsername(username); + feedPreferences.setPassword(password); + feed.savePreferences(); + } + }.show(); + return false; + }); + } + private void setupAutoDeletePreference() { ListPreference autoDeletePreference = (ListPreference) findPreference("autoDelete"); autoDeletePreference.setOnPreferenceChangeListener((preference, newValue) -> { @@ -106,7 +142,7 @@ public class FeedSettingsFragment extends PreferenceFragmentCompat { private void updateAutoDownloadEnabled() { if (feed != null && feed.getPreferences() != null) { boolean enabled = feed.getPreferences().getAutoDownload() && UserPreferences.isEnableAutodownload(); - findPreference("filters").setEnabled(enabled); + findPreference(PREF_EPISODE_FILTER).setEnabled(enabled); } } diff --git a/app/src/main/res/layout/episode_filter_dialog.xml b/app/src/main/res/layout/episode_filter_dialog.xml new file mode 100644 index 000000000..7a5c47ee6 --- /dev/null +++ b/app/src/main/res/layout/episode_filter_dialog.xml @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + + + + +