diff options
author | Alex Ning <chineseperson5@gmail.com> | 2021-07-18 15:22:10 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2021-07-18 15:22:10 +0000 |
commit | 03f8741f3d3d3c9c581c7d7c3a6901a3492d5240 (patch) | |
tree | 2589e92532025b6d26a4c64298d828305d377ef4 /app | |
parent | 78c8452ae4f5eaa8c58f26c8729f7d0c74c1cb91 (diff) | |
download | infinity-for-reddit-03f8741f3d3d3c9c581c7d7c3a6901a3492d5240.tar infinity-for-reddit-03f8741f3d3d3c9c581c7d7c3a6901a3492d5240.tar.gz infinity-for-reddit-03f8741f3d3d3c9c581c7d7c3a6901a3492d5240.tar.bz2 infinity-for-reddit-03f8741f3d3d3c9c581c7d7c3a6901a3492d5240.tar.lz infinity-for-reddit-03f8741f3d3d3c9c581c7d7c3a6901a3492d5240.tar.xz infinity-for-reddit-03f8741f3d3d3c9c581c7d7c3a6901a3492d5240.tar.zst infinity-for-reddit-03f8741f3d3d3c9c581c7d7c3a6901a3492d5240.zip |
Add a refresh menu item in TrendingActivity.
Diffstat (limited to 'app')
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/activities/TrendingActivity.java | 38 | ||||
-rw-r--r-- | app/src/main/res/menu/trending_activity.xml | 10 |
2 files changed, 45 insertions, 3 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/TrendingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/TrendingActivity.java index 79d8b24e..fa9ac0f0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/TrendingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/TrendingActivity.java @@ -6,6 +6,8 @@ import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.util.DisplayMetrics; +import android.view.Menu; +import android.view.MenuItem; import android.view.View; import android.view.Window; import android.view.WindowManager; @@ -97,6 +99,7 @@ public class TrendingActivity extends BaseActivity { Executor mExecutor; private String mAccessToken; private String mAccountName; + private boolean isRefreshing = false; private ArrayList<TrendingSearch> trendingSearches; private TrendingSearchRecyclerViewAdapter adapter; @@ -170,7 +173,7 @@ public class TrendingActivity extends BaseActivity { recyclerView.setAdapter(adapter); swipeRefreshLayout.setEnabled(mSharedPreferences.getBoolean(SharedPreferencesUtils.PULL_TO_REFRESH, true)); - swipeRefreshLayout.setOnRefreshListener(this::loadTrendingSearches); + swipeRefreshLayout.setOnRefreshListener(this::fetchTrendingSearches); if (savedInstanceState != null) { trendingSearches = savedInstanceState.getParcelableArrayList(TRENDING_SEARCHES_STATE); @@ -178,11 +181,16 @@ public class TrendingActivity extends BaseActivity { if (trendingSearches != null) { adapter.setTrendingSearches(trendingSearches); } else { - loadTrendingSearches(); + fetchTrendingSearches(); } } - private void loadTrendingSearches() { + private void fetchTrendingSearches() { + if (isRefreshing) { + return; + } + isRefreshing = true; + errorLinearLayout.setVisibility(View.GONE); Glide.with(this).clear(errorImageView); swipeRefreshLayout.setRefreshing(true); @@ -222,12 +230,14 @@ public class TrendingActivity extends BaseActivity { trendingSearches = trendingSearchList; swipeRefreshLayout.setRefreshing(false); adapter.setTrendingSearches(trendingSearches); + isRefreshing = false; }); } catch (JSONException e) { e.printStackTrace(); handler.post(() -> { swipeRefreshLayout.setRefreshing(false); showErrorView(R.string.error_parse_trending_search); + isRefreshing = false; }); } }); @@ -235,6 +245,7 @@ public class TrendingActivity extends BaseActivity { handler.post(() -> { swipeRefreshLayout.setRefreshing(false); showErrorView(R.string.error_fetch_trending_search); + isRefreshing = false; }); } } @@ -244,6 +255,7 @@ public class TrendingActivity extends BaseActivity { handler.post(() -> { swipeRefreshLayout.setRefreshing(false); showErrorView(R.string.error_fetch_trending_search); + isRefreshing = false; }); } }); @@ -256,6 +268,26 @@ public class TrendingActivity extends BaseActivity { } @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.trending_activity, menu); + applyMenuItemTheme(menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + if (item.getItemId() == android.R.id.home) { + finish(); + return true; + } else if (item.getItemId() == R.id.action_refresh_trending_activity) { + fetchTrendingSearches(); + return true; + } + + return false; + } + + @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); outState.putParcelableArrayList(TRENDING_SEARCHES_STATE, trendingSearches); diff --git a/app/src/main/res/menu/trending_activity.xml b/app/src/main/res/menu/trending_activity.xml new file mode 100644 index 00000000..2bd2db1c --- /dev/null +++ b/app/src/main/res/menu/trending_activity.xml @@ -0,0 +1,10 @@ +<?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/action_refresh_trending_activity" + android:orderInCategory="1" + android:title="@string/action_refresh" + android:icon="@drawable/ic_refresh_24dp" + app:showAsAction="ifRoom" /> +</menu>
\ No newline at end of file |