diff options
author | Simon Rusinov <mrpinkolik@gmail.com> | 2022-10-15 14:29:39 +0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-15 11:29:39 +0200 |
commit | 4c30d8ff7ff855e3cc25caf383620857a4319c79 (patch) | |
tree | df25c3213e007ce39f724e95e8fba38cfe5479c7 /app | |
parent | 8ff9dd829aa24ef7f0581dadcf3726a64f289955 (diff) | |
download | AntennaPod-4c30d8ff7ff855e3cc25caf383620857a4319c79.zip |
Add auto-complete to discovery country selection (#6139)
Diffstat (limited to 'app')
6 files changed, 163 insertions, 112 deletions
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<PodcastSearchResult> 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<PodcastSearchResult> 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<String> countryCodeArray = new ArrayList<String>(Arrays.asList(Locale.getISOCountries())); - HashMap<String, String> countryCodeNames = new HashMap<String, String>(); - for (String code: countryCodeArray) { - Locale locale = new Locale("", code); - String countryName = locale.getDisplayCountry(); - if (countryName != null) { - countryCodeNames.put(code, countryName); - } - } - - List<String> countryNamesSort = new ArrayList<String>(countryCodeNames.values()); - Collections.sort(countryNamesSort); - countryNamesSort.add(0, getResources().getString(R.string.discover_hide)); - - Spinner countrySpinner = root.findViewById(R.id.spinner_country); - ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(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<String> countryCodeArray = new ArrayList<>(Arrays.asList(Locale.getISOCountries())); + Map<String, String> countryCodeNames = new HashMap<>(); + Map<String, String> 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<String> countryNamesSort = new ArrayList<>(countryCodeNames.values()); + Collections.sort(countryNamesSort); + + ArrayAdapter<String> 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"> - <LinearLayout - android:id="@+id/browsing" + <com.google.android.material.appbar.MaterialToolbar + android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" + android:layout_alignParentTop="true" + android:minHeight="?attr/actionBarSize" android:orientation="horizontal" - android:layout_alignParentTop="true"> - - <com.google.android.material.appbar.MaterialToolbar - android:id="@+id/toolbar" - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:minHeight="?attr/actionBarSize" - android:theme="?attr/actionBarTheme" - app:navigationIcon="?homeAsUpIndicator" - app:title="@string/discover" /> - - <android.widget.Spinner - android:id="@+id/spinner_country" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:layout_gravity="right|end" - android:gravity="right|end" - android:isScrollContainer="true" - android:minHeight="?attr/actionBarSize" - android:spinnerMode="dropdown" - android:textAlignment="textEnd" /> - - </LinearLayout> + android:theme="?attr/actionBarTheme" + app:navigationIcon="?homeAsUpIndicator" + app:title="@string/discover" /> <GridView android:id="@+id/gridView" android:layout_width="match_parent" android:layout_height="match_parent" - android:layout_below="@id/browsing" + android:layout_below="@id/toolbar" android:clipToPadding="false" android:columnWidth="400dp" android:gravity="center" diff --git a/app/src/main/res/layout/select_country_dialog.xml b/app/src/main/res/layout/select_country_dialog.xml new file mode 100644 index 000000000..4b77492d3 --- /dev/null +++ b/app/src/main/res/layout/select_country_dialog.xml @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout + xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical"> + + <com.google.android.material.textfield.TextInputLayout + android:id="@+id/country_text_input" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_margin="20dp" + android:hint="@string/country" + style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"> + + <AutoCompleteTextView + android:layout_width="match_parent" + android:layout_height="wrap_content" /> + + </com.google.android.material.textfield.TextInputLayout> + +</LinearLayout> 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 @@ +<?xml version="1.0" encoding="utf-8"?> +<menu xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto"> + + <item + android:id="@+id/discover_hide_item" + android:checkable="true" + android:enabled="true" + android:title="@string/discover_hide" + android:visible="true" + app:showAsAction="never" /> + <item + android:id="@+id/discover_countries_item" + android:enabled="true" + android:title="@string/country" + android:visible="true" /> +</menu>
\ No newline at end of file |