Refreshing trending searches is now available.

This commit is contained in:
Alex Ning 2021-07-18 22:59:08 +08:00
parent 3ecb39bc3e
commit 1208a2d4f8
4 changed files with 39 additions and 8 deletions

View File

@ -15,6 +15,7 @@ import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout;
@ -62,6 +63,8 @@ public class TrendingActivity extends BaseActivity {
AppBarLayout appBarLayout; AppBarLayout appBarLayout;
@BindView(R.id.toolbar_trending_activity) @BindView(R.id.toolbar_trending_activity)
Toolbar toolbar; Toolbar toolbar;
@BindView(R.id.swipe_refresh_layout_trending_activity)
SwipeRefreshLayout swipeRefreshLayout;
@BindView(R.id.recycler_view_trending_activity) @BindView(R.id.recycler_view_trending_activity)
RecyclerView recyclerView; RecyclerView recyclerView;
@Inject @Inject
@ -154,10 +157,16 @@ public class TrendingActivity extends BaseActivity {
recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter); recyclerView.setAdapter(adapter);
swipeRefreshLayout.setEnabled(mSharedPreferences.getBoolean(SharedPreferencesUtils.PULL_TO_REFRESH, true));
swipeRefreshLayout.setOnRefreshListener(this::loadTrendingSearches);
loadTrendingSearches(); loadTrendingSearches();
} }
private void loadTrendingSearches() { private void loadTrendingSearches() {
swipeRefreshLayout.setRefreshing(true);
trendingSearches = null;
adapter.setTrendingSearches(null);
Handler handler = new Handler(); Handler handler = new Handler();
mOauthRetrofit.create(RedditAPI.class).getTrendingSearches(APIUtils.getOAuthHeader(mAccessToken)).enqueue(new Callback<String>() { mOauthRetrofit.create(RedditAPI.class).getTrendingSearches(APIUtils.getOAuthHeader(mAccessToken)).enqueue(new Callback<String>() {
@Override @Override
@ -190,20 +199,28 @@ public class TrendingActivity extends BaseActivity {
handler.post(() -> { handler.post(() -> {
trendingSearches = trendingSearchList; trendingSearches = trendingSearchList;
swipeRefreshLayout.setRefreshing(false);
adapter.setTrendingSearches(trendingSearches); adapter.setTrendingSearches(trendingSearches);
}); });
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
handler.post(() -> {
swipeRefreshLayout.setRefreshing(false);
});
} }
}); });
} else { } else {
handler.post(() -> {
swipeRefreshLayout.setRefreshing(false);
});
} }
} }
@Override @Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
handler.post(() -> {
swipeRefreshLayout.setRefreshing(false);
});
} }
}); });
} }
@ -222,6 +239,8 @@ public class TrendingActivity extends BaseActivity {
protected void applyCustomTheme() { protected void applyCustomTheme() {
coordinatorLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor()); coordinatorLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor());
applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar); applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar);
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(mCustomThemeWrapper.getCircularProgressBarBackground());
swipeRefreshLayout.setColorSchemeColors(mCustomThemeWrapper.getColorAccent());
} }
@Subscribe @Subscribe

View File

@ -180,8 +180,14 @@ public class TrendingSearchRecyclerViewAdapter extends RecyclerView.Adapter<Recy
} }
public void setTrendingSearches(ArrayList<TrendingSearch> trendingSearches) { public void setTrendingSearches(ArrayList<TrendingSearch> trendingSearches) {
this.trendingSearches = trendingSearches; if (trendingSearches != null) {
notifyDataSetChanged(); this.trendingSearches = trendingSearches;
notifyItemRangeInserted(0, trendingSearches.size());
} else {
int size = this.trendingSearches == null ? 0 : this.trendingSearches.size();
this.trendingSearches = null;
notifyItemRangeRemoved(0, size);
}
} }
class TrendingSearchViewHolder extends RecyclerView.ViewHolder { class TrendingSearchViewHolder extends RecyclerView.ViewHolder {

View File

@ -363,7 +363,6 @@ public interface RedditAPI {
@POST("/api/submit_gallery_post.json?resubmit=true&raw_json=1") @POST("/api/submit_gallery_post.json?resubmit=true&raw_json=1")
Call<String> submitGalleryPost(@HeaderMap Map<String, String> headers, @Body String body); Call<String> submitGalleryPost(@HeaderMap Map<String, String> headers, @Body String body);
//https://www.reddit.com/search.json?q=Olympics&source=trending
@GET("/api/trending_searches_v1.json?withAds=0&raw_json=1&gilding_detail=1") @GET("/api/trending_searches_v1.json?withAds=0&raw_json=1&gilding_detail=1")
Call<String> getTrendingSearches(@HeaderMap Map<String, String> headers); Call<String> getTrendingSearches(@HeaderMap Map<String, String> headers);
} }

View File

@ -33,10 +33,17 @@
</com.google.android.material.appbar.AppBarLayout> </com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/recycler_view_trending_activity" android:id="@+id/swipe_refresh_layout_trending_activity"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" /> app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_trending_activity"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>