diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/DiscoveryFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/DiscoveryFragment.java index e4d751bd9..a6db1e46b 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/DiscoveryFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/DiscoveryFragment.java @@ -1,32 +1,31 @@ package de.danoeh.antennapod.fragment; +import android.app.AlertDialog; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; +import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.GridView; import android.widget.ProgressBar; -import android.widget.Spinner; import android.widget.TextView; + import androidx.annotation.NonNull; -import com.google.android.material.appbar.MaterialToolbar; +import androidx.appcompat.widget.Toolbar; import androidx.fragment.app.Fragment; -import de.danoeh.antennapod.net.discovery.ItunesTopListLoader; -import de.danoeh.antennapod.net.discovery.PodcastSearchResult; -import org.greenrobot.eventbus.EventBus; +import com.google.android.material.appbar.MaterialToolbar; +import com.google.android.material.textfield.MaterialAutoCompleteTextView; +import com.google.android.material.textfield.TextInputLayout; -import de.danoeh.antennapod.R; -import de.danoeh.antennapod.activity.OnlineFeedViewActivity; -import de.danoeh.antennapod.adapter.itunes.ItunesAdapter; -import de.danoeh.antennapod.event.DiscoveryDefaultUpdateEvent; -import io.reactivex.disposables.Disposable; +import org.apache.commons.lang3.StringUtils; +import org.greenrobot.eventbus.EventBus; import java.util.ArrayList; import java.util.Arrays; @@ -34,13 +33,20 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Locale; +import java.util.Map; -import static android.content.Context.MODE_PRIVATE; +import de.danoeh.antennapod.R; +import de.danoeh.antennapod.activity.OnlineFeedViewActivity; +import de.danoeh.antennapod.adapter.itunes.ItunesAdapter; +import de.danoeh.antennapod.event.DiscoveryDefaultUpdateEvent; +import de.danoeh.antennapod.net.discovery.ItunesTopListLoader; +import de.danoeh.antennapod.net.discovery.PodcastSearchResult; +import io.reactivex.disposables.Disposable; /** * Searches iTunes store for top podcasts and displays results in a list. */ -public class DiscoveryFragment extends Fragment { +public class DiscoveryFragment extends Fragment implements Toolbar.OnMenuItemClickListener { private static final String TAG = "ItunesSearchFragment"; private SharedPreferences prefs; @@ -62,9 +68,16 @@ public class DiscoveryFragment extends Fragment { private List topList; private Disposable disposable; private String countryCode = "US"; + private boolean hidden; + private MaterialToolbar toolbar; + + public DiscoveryFragment() { + // Required empty public constructor + } /** * Replace adapter data with provided search results from SearchTask. + * * @param result List of Podcast objects containing search results */ private void updateData(List result) { @@ -83,28 +96,28 @@ public class DiscoveryFragment extends Fragment { } } - public DiscoveryFragment() { - // Required empty public constructor - } - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - prefs = getActivity().getSharedPreferences(ItunesTopListLoader.PREFS, MODE_PRIVATE); + prefs = getActivity().getSharedPreferences(ItunesTopListLoader.PREFS, Context.MODE_PRIVATE); countryCode = prefs.getString(ItunesTopListLoader.PREF_KEY_COUNTRY_CODE, Locale.getDefault().getCountry()); + hidden = prefs.getBoolean(ItunesTopListLoader.PREF_KEY_HIDDEN_DISCOVERY_COUNTRY, false); } @Override - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View root = inflater.inflate(R.layout.fragment_itunes_search, container, false); gridView = root.findViewById(R.id.gridView); adapter = new ItunesAdapter(getActivity(), new ArrayList<>()); gridView.setAdapter(adapter); - MaterialToolbar toolbar = root.findViewById(R.id.toolbar); + toolbar = root.findViewById(R.id.toolbar); toolbar.setNavigationOnClickListener(v -> getParentFragmentManager().popBackStack()); + toolbar.inflateMenu(R.menu.countries_menu); + MenuItem discoverHideItem = toolbar.getMenu().findItem(R.id.discover_hide_item); + discoverHideItem.setChecked(hidden); + toolbar.setOnMenuItemClickListener(this); //Show information about the podcast when the list item is clicked gridView.setOnItemClickListener((parent, view1, position, id) -> { @@ -117,57 +130,6 @@ public class DiscoveryFragment extends Fragment { startActivity(intent); }); - List countryCodeArray = new ArrayList(Arrays.asList(Locale.getISOCountries())); - HashMap countryCodeNames = new HashMap(); - for (String code: countryCodeArray) { - Locale locale = new Locale("", code); - String countryName = locale.getDisplayCountry(); - if (countryName != null) { - countryCodeNames.put(code, countryName); - } - } - - List countryNamesSort = new ArrayList(countryCodeNames.values()); - Collections.sort(countryNamesSort); - countryNamesSort.add(0, getResources().getString(R.string.discover_hide)); - - Spinner countrySpinner = root.findViewById(R.id.spinner_country); - ArrayAdapter dataAdapter = new ArrayAdapter(this.getContext(), - android.R.layout.simple_spinner_item, - countryNamesSort); - dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - countrySpinner.setAdapter(dataAdapter); - int pos = countryNamesSort.indexOf(countryCodeNames.get(countryCode)); - countrySpinner.setSelection(pos); - - countrySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView countrySpinner, View view, int position, long id) { - String countryName = (String) countrySpinner.getItemAtPosition(position); - - if (countryName.equals(getResources().getString(R.string.discover_hide))) { - countryCode = ItunesTopListLoader.DISCOVER_HIDE_FAKE_COUNTRY_CODE; - } else { - for (Object o : countryCodeNames.keySet()) { - if (countryCodeNames.get(o).equals(countryName)) { - countryCode = o.toString(); - break; - } - } - } - - prefs.edit() - .putString(ItunesTopListLoader.PREF_KEY_COUNTRY_CODE, countryCode) - .apply(); - - EventBus.getDefault().post(new DiscoveryDefaultUpdateEvent()); - loadToplist(countryCode); - } - - @Override - public void onNothingSelected(AdapterView parent) { - } - }); progressBar = root.findViewById(R.id.progressBar); txtvError = root.findViewById(R.id.txtvError); butRetry = root.findViewById(R.id.butRetry); @@ -197,7 +159,7 @@ public class DiscoveryFragment extends Fragment { txtvEmpty.setVisibility(View.GONE); progressBar.setVisibility(View.VISIBLE); - if (country.equals(ItunesTopListLoader.DISCOVER_HIDE_FAKE_COUNTRY_CODE)) { + if (hidden) { gridView.setVisibility(View.GONE); txtvError.setVisibility(View.VISIBLE); txtvError.setText(getResources().getString(R.string.discover_is_hidden)); @@ -221,4 +183,73 @@ public class DiscoveryFragment extends Fragment { }); } } + + @Override + public boolean onMenuItemClick(MenuItem item) { + if (super.onOptionsItemSelected(item)) { + return true; + } + final int itemId = item.getItemId(); + if (itemId == R.id.discover_hide_item) { + item.setChecked(!item.isChecked()); + hidden = item.isChecked(); + prefs.edit().putBoolean(ItunesTopListLoader.PREF_KEY_HIDDEN_DISCOVERY_COUNTRY, hidden).apply(); + + EventBus.getDefault().post(new DiscoveryDefaultUpdateEvent()); + loadToplist(countryCode); + return true; + } else if (itemId == R.id.discover_countries_item) { + + LayoutInflater inflater = getLayoutInflater(); + View selectCountryDialogView = inflater.inflate(R.layout.select_country_dialog, null); + AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); + builder.setView(selectCountryDialogView); + + List countryCodeArray = new ArrayList<>(Arrays.asList(Locale.getISOCountries())); + Map countryCodeNames = new HashMap<>(); + Map countryNameCodes = new HashMap<>(); + for (String code : countryCodeArray) { + Locale locale = new Locale("", code); + String countryName = locale.getDisplayCountry(); + countryCodeNames.put(code, countryName); + countryNameCodes.put(countryName, code); + } + + List countryNamesSort = new ArrayList<>(countryCodeNames.values()); + Collections.sort(countryNamesSort); + + ArrayAdapter dataAdapter = + new ArrayAdapter<>(this.getContext(), android.R.layout.simple_list_item_1, countryNamesSort); + TextInputLayout textInput = selectCountryDialogView.findViewById(R.id.country_text_input); + MaterialAutoCompleteTextView editText = (MaterialAutoCompleteTextView) textInput.getEditText(); + editText.setAdapter(dataAdapter); + editText.setText(countryCodeNames.get(countryCode)); + editText.setOnClickListener(view -> { + if (StringUtils.isEmpty(editText.getText().toString())) { + return; + } + editText.getText().clear(); + editText.postDelayed(editText::showDropDown, 100); + }); + + builder.setPositiveButton(android.R.string.ok, (dialogInterface, i) -> { + String countryName = editText.getText().toString(); + if (countryNameCodes.containsKey(countryName)) { + countryCode = countryNameCodes.get(countryName); + MenuItem discoverHideItem = toolbar.getMenu().findItem(R.id.discover_hide_item); + discoverHideItem.setChecked(false); + hidden = false; + } + + prefs.edit().putBoolean(ItunesTopListLoader.PREF_KEY_HIDDEN_DISCOVERY_COUNTRY, hidden).apply(); + prefs.edit().putString(ItunesTopListLoader.PREF_KEY_COUNTRY_CODE, countryCode).apply(); + + EventBus.getDefault().post(new DiscoveryDefaultUpdateEvent()); + loadToplist(countryCode); + }); + builder.show(); + return true; + } + return false; + } } diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/OnlineSearchFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/OnlineSearchFragment.java index 2198d130f..9e34c0a27 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/OnlineSearchFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/OnlineSearchFragment.java @@ -3,22 +3,26 @@ package de.danoeh.antennapod.fragment; import android.content.Context; import android.content.Intent; import android.os.Bundle; - -import android.widget.AbsListView; -import com.google.android.material.appbar.MaterialToolbar; -import androidx.fragment.app.Fragment; -import androidx.appcompat.widget.SearchView; - import android.util.Log; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; +import android.widget.AbsListView; import android.widget.Button; import android.widget.GridView; import android.widget.ProgressBar; import android.widget.TextView; + +import androidx.appcompat.widget.SearchView; +import androidx.fragment.app.Fragment; + +import com.google.android.material.appbar.MaterialToolbar; + +import java.util.ArrayList; +import java.util.List; + import de.danoeh.antennapod.R; import de.danoeh.antennapod.activity.MainActivity; import de.danoeh.antennapod.activity.OnlineFeedViewActivity; @@ -28,11 +32,6 @@ import de.danoeh.antennapod.net.discovery.PodcastSearcher; import de.danoeh.antennapod.net.discovery.PodcastSearcherRegistry; import io.reactivex.disposables.Disposable; -import java.util.ArrayList; -import java.util.List; - -import static android.view.View.INVISIBLE; - public class OnlineSearchFragment extends Fragment { private static final String TAG = "FyydSearchFragment"; @@ -95,7 +94,6 @@ public class OnlineSearchFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View root = inflater.inflate(R.layout.fragment_itunes_search, container, false); - root.findViewById(R.id.spinner_country).setVisibility(INVISIBLE); gridView = root.findViewById(R.id.gridView); adapter = new ItunesAdapter(getActivity(), new ArrayList<>()); gridView.setAdapter(adapter); diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/QuickFeedDiscoveryFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/QuickFeedDiscoveryFragment.java index f5bfed324..37bdc3c9e 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/QuickFeedDiscoveryFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/QuickFeedDiscoveryFragment.java @@ -114,7 +114,8 @@ public class QuickFeedDiscoveryFragment extends Fragment implements AdapterView. SharedPreferences prefs = getActivity().getSharedPreferences(ItunesTopListLoader.PREFS, MODE_PRIVATE); String countryCode = prefs.getString(ItunesTopListLoader.PREF_KEY_COUNTRY_CODE, Locale.getDefault().getCountry()); - if (countryCode.equals(ItunesTopListLoader.DISCOVER_HIDE_FAKE_COUNTRY_CODE)) { + boolean hidden = prefs.getBoolean(ItunesTopListLoader.PREF_KEY_HIDDEN_DISCOVERY_COUNTRY, false); + if (hidden) { errorTextView.setText(R.string.discover_is_hidden); errorView.setVisibility(View.VISIBLE); discoverGridLayout.setVisibility(View.GONE); diff --git a/app/src/main/res/layout/fragment_itunes_search.xml b/app/src/main/res/layout/fragment_itunes_search.xml index 369928d1d..d39d49c04 100644 --- a/app/src/main/res/layout/fragment_itunes_search.xml +++ b/app/src/main/res/layout/fragment_itunes_search.xml @@ -7,40 +7,22 @@ android:layout_height="match_parent" android:fitsSystemWindows="true"> - - - - - - - + android:theme="?attr/actionBarTheme" + app:navigationIcon="?homeAsUpIndicator" + app:title="@string/discover" /> + + + + + + + + + diff --git a/app/src/main/res/menu/countries_menu.xml b/app/src/main/res/menu/countries_menu.xml new file mode 100644 index 000000000..7e31b7467 --- /dev/null +++ b/app/src/main/res/menu/countries_menu.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/net/discovery/src/main/java/de/danoeh/antennapod/net/discovery/ItunesTopListLoader.java b/net/discovery/src/main/java/de/danoeh/antennapod/net/discovery/ItunesTopListLoader.java index 827a3202f..3dddf763c 100644 --- a/net/discovery/src/main/java/de/danoeh/antennapod/net/discovery/ItunesTopListLoader.java +++ b/net/discovery/src/main/java/de/danoeh/antennapod/net/discovery/ItunesTopListLoader.java @@ -26,8 +26,8 @@ public class ItunesTopListLoader { private static final String TAG = "ITunesTopListLoader"; private final Context context; public static final String PREF_KEY_COUNTRY_CODE = "country_code"; + public static final String PREF_KEY_HIDDEN_DISCOVERY_COUNTRY = "hidden_discovery_country"; public static final String PREFS = "CountryRegionPrefs"; - public static final String DISCOVER_HIDE_FAKE_COUNTRY_CODE = "00"; public static final String COUNTRY_CODE_UNSET = "99"; public ItunesTopListLoader(Context context) { diff --git a/ui/i18n/src/main/res/values/strings.xml b/ui/i18n/src/main/res/values/strings.xml index a6f5d8053..884ffab29 100644 --- a/ui/i18n/src/main/res/values/strings.xml +++ b/ui/i18n/src/main/res/values/strings.xml @@ -724,6 +724,7 @@ more ยป Suggestions by iTunes Results by %1$s + Country Add local folder