1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
package de.danoeh.antennapod.fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.net.discovery.ItunesTopListLoader;
import de.danoeh.antennapod.net.discovery.PodcastSearchResult;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.activity.OnlineFeedViewActivity;
import de.danoeh.antennapod.adapter.FeedDiscoverAdapter;
import de.danoeh.antennapod.event.DiscoveryDefaultUpdateEvent;
import io.reactivex.disposables.Disposable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import static android.content.Context.MODE_PRIVATE;
public class QuickFeedDiscoveryFragment extends Fragment implements AdapterView.OnItemClickListener {
private static final String TAG = "FeedDiscoveryFragment";
private static final int NUM_SUGGESTIONS = 12;
private Disposable disposable;
private FeedDiscoverAdapter adapter;
private GridView discoverGridLayout;
private TextView errorTextView;
private TextView poweredByTextView;
private LinearLayout errorView;
private Button errorRetry;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View root = inflater.inflate(R.layout.quick_feed_discovery, container, false);
View discoverMore = root.findViewById(R.id.discover_more);
discoverMore.setOnClickListener(v ->
((MainActivity) getActivity()).loadChildFragment(new DiscoveryFragment()));
discoverGridLayout = root.findViewById(R.id.discover_grid);
errorView = root.findViewById(R.id.discover_error);
errorTextView = root.findViewById(R.id.discover_error_txtV);
errorRetry = root.findViewById(R.id.discover_error_retry_btn);
poweredByTextView = root.findViewById(R.id.discover_powered_by_itunes);
adapter = new FeedDiscoverAdapter((MainActivity) getActivity());
discoverGridLayout.setAdapter(adapter);
discoverGridLayout.setOnItemClickListener(this);
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
float screenWidthDp = displayMetrics.widthPixels / displayMetrics.density;
if (screenWidthDp > 600) {
discoverGridLayout.setNumColumns(6);
} else {
discoverGridLayout.setNumColumns(4);
}
// Fill with dummy elements to have a fixed height and
// prevent the UI elements below from jumping on slow connections
List<PodcastSearchResult> dummies = new ArrayList<>();
for (int i = 0; i < NUM_SUGGESTIONS; i++) {
dummies.add(PodcastSearchResult.dummy());
}
adapter.updateData(dummies);
loadToplist();
EventBus.getDefault().register(this);
return root;
}
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
if (disposable != null) {
disposable.dispose();
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
@SuppressWarnings("unused")
public void onDiscoveryDefaultUpdateEvent(DiscoveryDefaultUpdateEvent event) {
loadToplist();
}
private void loadToplist() {
errorView.setVisibility(View.GONE);
errorRetry.setVisibility(View.INVISIBLE);
errorRetry.setText(R.string.retry_label);
poweredByTextView.setVisibility(View.VISIBLE);
ItunesTopListLoader loader = new ItunesTopListLoader(getContext());
SharedPreferences prefs = getActivity().getSharedPreferences(ItunesTopListLoader.PREFS, MODE_PRIVATE);
String countryCode = prefs.getString(ItunesTopListLoader.PREF_KEY_COUNTRY_CODE,
Locale.getDefault().getCountry());
if (prefs.getBoolean(ItunesTopListLoader.PREF_KEY_HIDDEN_DISCOVERY_COUNTRY, false)) {
errorTextView.setText(R.string.discover_is_hidden);
errorView.setVisibility(View.VISIBLE);
discoverGridLayout.setVisibility(View.GONE);
errorRetry.setVisibility(View.GONE);
poweredByTextView.setVisibility(View.GONE);
return;
}
//noinspection ConstantConditions
if (BuildConfig.FLAVOR.equals("free") && prefs.getBoolean(ItunesTopListLoader.PREF_KEY_NEEDS_CONFIRM, true)) {
errorTextView.setText("");
errorView.setVisibility(View.VISIBLE);
discoverGridLayout.setVisibility(View.VISIBLE);
errorRetry.setVisibility(View.VISIBLE);
errorRetry.setText(R.string.discover_confirm);
poweredByTextView.setVisibility(View.VISIBLE);
errorRetry.setOnClickListener(v -> {
prefs.edit().putBoolean(ItunesTopListLoader.PREF_KEY_NEEDS_CONFIRM, false).apply();
loadToplist();
});
return;
}
disposable = loader.loadToplist(countryCode, NUM_SUGGESTIONS)
.subscribe(
podcasts -> {
errorView.setVisibility(View.GONE);
if (podcasts.size() == 0) {
errorTextView.setText(getResources().getText(R.string.search_status_no_results));
errorView.setVisibility(View.VISIBLE);
discoverGridLayout.setVisibility(View.INVISIBLE);
} else {
discoverGridLayout.setVisibility(View.VISIBLE);
adapter.updateData(podcasts);
}
}, error -> {
Log.e(TAG, Log.getStackTraceString(error));
errorTextView.setText(error.getLocalizedMessage());
errorView.setVisibility(View.VISIBLE);
discoverGridLayout.setVisibility(View.INVISIBLE);
errorRetry.setVisibility(View.VISIBLE);
errorRetry.setOnClickListener((listener) -> loadToplist());
});
}
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
PodcastSearchResult podcast = adapter.getItem(position);
if (TextUtils.isEmpty(podcast.feedUrl)) {
return;
}
Intent intent = new Intent(getActivity(), OnlineFeedViewActivity.class);
intent.putExtra(OnlineFeedViewActivity.ARG_FEEDURL, podcast.feedUrl);
startActivity(intent);
}
}
|