Add auto-complete to discovery country selection (#6139)

This commit is contained in:
Simon Rusinov 2022-10-15 14:29:39 +05:00 committed by GitHub
parent 8ff9dd829a
commit 4c30d8ff7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 165 additions and 113 deletions

View File

@ -1,32 +1,31 @@
package de.danoeh.antennapod.fragment; package de.danoeh.antennapod.fragment;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button; import android.widget.Button;
import android.widget.GridView; import android.widget.GridView;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import com.google.android.material.appbar.MaterialToolbar; import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import de.danoeh.antennapod.net.discovery.ItunesTopListLoader; import com.google.android.material.appbar.MaterialToolbar;
import de.danoeh.antennapod.net.discovery.PodcastSearchResult; import com.google.android.material.textfield.MaterialAutoCompleteTextView;
import org.greenrobot.eventbus.EventBus; import com.google.android.material.textfield.TextInputLayout;
import de.danoeh.antennapod.R; import org.apache.commons.lang3.StringUtils;
import de.danoeh.antennapod.activity.OnlineFeedViewActivity; import org.greenrobot.eventbus.EventBus;
import de.danoeh.antennapod.adapter.itunes.ItunesAdapter;
import de.danoeh.antennapod.event.DiscoveryDefaultUpdateEvent;
import io.reactivex.disposables.Disposable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -34,13 +33,20 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; 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. * 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 static final String TAG = "ItunesSearchFragment";
private SharedPreferences prefs; private SharedPreferences prefs;
@ -62,9 +68,16 @@ public class DiscoveryFragment extends Fragment {
private List<PodcastSearchResult> topList; private List<PodcastSearchResult> topList;
private Disposable disposable; private Disposable disposable;
private String countryCode = "US"; 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. * Replace adapter data with provided search results from SearchTask.
*
* @param result List of Podcast objects containing search results * @param result List of Podcast objects containing search results
*/ */
private void updateData(List<PodcastSearchResult> result) { private void updateData(List<PodcastSearchResult> result) {
@ -83,28 +96,28 @@ public class DiscoveryFragment extends Fragment {
} }
} }
public DiscoveryFragment() {
// Required empty public constructor
}
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(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()); countryCode = prefs.getString(ItunesTopListLoader.PREF_KEY_COUNTRY_CODE, Locale.getDefault().getCountry());
hidden = prefs.getBoolean(ItunesTopListLoader.PREF_KEY_HIDDEN_DISCOVERY_COUNTRY, false);
} }
@Override @Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle savedInstanceState) {
// Inflate the layout for this fragment // Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_itunes_search, container, false); View root = inflater.inflate(R.layout.fragment_itunes_search, container, false);
gridView = root.findViewById(R.id.gridView); gridView = root.findViewById(R.id.gridView);
adapter = new ItunesAdapter(getActivity(), new ArrayList<>()); adapter = new ItunesAdapter(getActivity(), new ArrayList<>());
gridView.setAdapter(adapter); gridView.setAdapter(adapter);
MaterialToolbar toolbar = root.findViewById(R.id.toolbar); toolbar = root.findViewById(R.id.toolbar);
toolbar.setNavigationOnClickListener(v -> getParentFragmentManager().popBackStack()); 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 //Show information about the podcast when the list item is clicked
gridView.setOnItemClickListener((parent, view1, position, id) -> { gridView.setOnItemClickListener((parent, view1, position, id) -> {
@ -117,57 +130,6 @@ public class DiscoveryFragment extends Fragment {
startActivity(intent); 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); progressBar = root.findViewById(R.id.progressBar);
txtvError = root.findViewById(R.id.txtvError); txtvError = root.findViewById(R.id.txtvError);
butRetry = root.findViewById(R.id.butRetry); butRetry = root.findViewById(R.id.butRetry);
@ -197,7 +159,7 @@ public class DiscoveryFragment extends Fragment {
txtvEmpty.setVisibility(View.GONE); txtvEmpty.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE); progressBar.setVisibility(View.VISIBLE);
if (country.equals(ItunesTopListLoader.DISCOVER_HIDE_FAKE_COUNTRY_CODE)) { if (hidden) {
gridView.setVisibility(View.GONE); gridView.setVisibility(View.GONE);
txtvError.setVisibility(View.VISIBLE); txtvError.setVisibility(View.VISIBLE);
txtvError.setText(getResources().getString(R.string.discover_is_hidden)); 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;
}
} }

View File

@ -3,22 +3,26 @@ package de.danoeh.antennapod.fragment;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; 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.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.Button; import android.widget.Button;
import android.widget.GridView; import android.widget.GridView;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; 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.R;
import de.danoeh.antennapod.activity.MainActivity; import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.activity.OnlineFeedViewActivity; 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 de.danoeh.antennapod.net.discovery.PodcastSearcherRegistry;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
import java.util.ArrayList;
import java.util.List;
import static android.view.View.INVISIBLE;
public class OnlineSearchFragment extends Fragment { public class OnlineSearchFragment extends Fragment {
private static final String TAG = "FyydSearchFragment"; private static final String TAG = "FyydSearchFragment";
@ -95,7 +94,6 @@ public class OnlineSearchFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment // Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_itunes_search, container, false); 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); gridView = root.findViewById(R.id.gridView);
adapter = new ItunesAdapter(getActivity(), new ArrayList<>()); adapter = new ItunesAdapter(getActivity(), new ArrayList<>());
gridView.setAdapter(adapter); gridView.setAdapter(adapter);

View File

@ -114,7 +114,8 @@ public class QuickFeedDiscoveryFragment extends Fragment implements AdapterView.
SharedPreferences prefs = getActivity().getSharedPreferences(ItunesTopListLoader.PREFS, MODE_PRIVATE); SharedPreferences prefs = getActivity().getSharedPreferences(ItunesTopListLoader.PREFS, MODE_PRIVATE);
String countryCode = prefs.getString(ItunesTopListLoader.PREF_KEY_COUNTRY_CODE, String countryCode = prefs.getString(ItunesTopListLoader.PREF_KEY_COUNTRY_CODE,
Locale.getDefault().getCountry()); 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); errorTextView.setText(R.string.discover_is_hidden);
errorView.setVisibility(View.VISIBLE); errorView.setVisibility(View.VISIBLE);
discoverGridLayout.setVisibility(View.GONE); discoverGridLayout.setVisibility(View.GONE);

View File

@ -7,40 +7,22 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true"> android:fitsSystemWindows="true">
<LinearLayout <com.google.android.material.appbar.MaterialToolbar
android:id="@+id/browsing" android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:minHeight="?attr/actionBarSize"
android:orientation="horizontal" android:orientation="horizontal"
android:layout_alignParentTop="true"> android:theme="?attr/actionBarTheme"
app:navigationIcon="?homeAsUpIndicator"
<com.google.android.material.appbar.MaterialToolbar app:title="@string/discover" />
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>
<GridView <GridView
android:id="@+id/gridView" android:id="@+id/gridView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_below="@id/browsing" android:layout_below="@id/toolbar"
android:clipToPadding="false" android:clipToPadding="false"
android:columnWidth="400dp" android:columnWidth="400dp"
android:gravity="center" android:gravity="center"

View File

@ -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>

View File

@ -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>

View File

@ -26,8 +26,8 @@ public class ItunesTopListLoader {
private static final String TAG = "ITunesTopListLoader"; private static final String TAG = "ITunesTopListLoader";
private final Context context; private final Context context;
public static final String PREF_KEY_COUNTRY_CODE = "country_code"; 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 PREFS = "CountryRegionPrefs";
public static final String DISCOVER_HIDE_FAKE_COUNTRY_CODE = "00";
public static final String COUNTRY_CODE_UNSET = "99"; public static final String COUNTRY_CODE_UNSET = "99";
public ItunesTopListLoader(Context context) { public ItunesTopListLoader(Context context) {

View File

@ -724,6 +724,7 @@
<string name="discover_more">more »</string> <string name="discover_more">more »</string>
<string name="discover_powered_by_itunes">Suggestions by iTunes</string> <string name="discover_powered_by_itunes">Suggestions by iTunes</string>
<string name="search_powered_by">Results by %1$s</string> <string name="search_powered_by">Results by %1$s</string>
<string name="country">Country</string>
<!-- Local feeds --> <!-- Local feeds -->
<string name="add_local_folder">Add local folder</string> <string name="add_local_folder">Add local folder</string>