From 6cbe406cefd37a9ac61e652b9965412311cd6cd1 Mon Sep 17 00:00:00 2001 From: sk Date: Thu, 19 Oct 2023 00:04:54 +0200 Subject: [PATCH] load new posts on reload --- .../android/api/CacheController.java | 2 ++ .../fragments/HomeTimelineFragment.java | 16 ++++++++------ .../settings/SettingsAboutAppFragment.java | 22 +++++++++++++++---- mastodon/src/main/res/values/strings_sk.xml | 2 ++ 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/api/CacheController.java b/mastodon/src/main/java/org/joinmastodon/android/api/CacheController.java index 0a743a13a9..832fdcb402 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/api/CacheController.java +++ b/mastodon/src/main/java/org/joinmastodon/android/api/CacheController.java @@ -120,6 +120,8 @@ public void putHomeTimeline(List posts, boolean clear){ values.put("time", s.createdAt.getEpochSecond()); db.insertWithOnConflict("home_timeline", null, values, SQLiteDatabase.CONFLICT_REPLACE); } + if(!clear) + db.delete("home_timeline", "`id` NOT IN (SELECT `id` FROM `home_timeline` ORDER BY `time` DESC LIMIT ?)", new String[]{"1000"}); }); } diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeTimelineFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeTimelineFragment.java index e4e185fa91..46b3e58428 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeTimelineFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeTimelineFragment.java @@ -61,7 +61,7 @@ public void onSuccess(CacheablePaginatedResponse> result){ maxID=result.maxID; AccountSessionManager.get(accountID).filterStatuses(result.items, getFilterContext()); onDataLoaded(result.items, !empty); - if(result.isFromCache()) + if(result.isFromCache() && GlobalUserPreferences.loadNewPosts) loadNewPosts(); } }); @@ -74,8 +74,9 @@ public void onViewCreated(View view, Bundle savedInstanceState){ list.addOnScrollListener(new RecyclerView.OnScrollListener(){ @Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy){ - if(parent != null && parent.isNewPostsBtnShown() && list.getChildAdapterPosition(list.getChildAt(0))<=getMainAdapterOffset()){ - parent.hideNewPostsButton(); + if(parent!=null && list.getChildAdapterPosition(list.getChildAt(0))<=getMainAdapterOffset()){ + if(parent.isNewPostsBtnShown()) parent.hideNewPostsButton(); + else if(!dataLoading && GlobalUserPreferences.loadNewPosts) loadNewPosts(); } } }); @@ -87,7 +88,7 @@ protected void onShown(){ if(!getArguments().getBoolean("noAutoLoad")){ if(!loaded && !dataLoading){ loadData(); - }else if(!dataLoading){ + }else if(!dataLoading && GlobalUserPreferences.loadNewPosts){ loadNewPosts(); } } @@ -121,7 +122,6 @@ public void onStatusCreated(Status status){ } private void loadNewPosts(){ - if (!GlobalUserPreferences.loadNewPosts) return; dataLoading=true; // we only care about the data that was actually retrieved from the timeline api since // user-created statuses are probably in the wrong position @@ -156,12 +156,14 @@ public void onSuccess(List result){ } if(!toAddUnfiltered.isEmpty()) AccountSessionManager.getInstance().getAccount(accountID).getCacheController().putHomeTimeline(toAddUnfiltered, false); + refreshDone(); } @Override public void onError(ErrorResponse error){ currentRequest=null; dataLoading=false; + refreshDone(); } }) .exec(accountID); @@ -328,8 +330,8 @@ public void onRefresh(){ currentRequest=null; dataLoading=false; } - if (parent != null) parent.hideNewPostsButton(); - super.onRefresh(); + if(parent!=null) parent.hideNewPostsButton(); + loadNewPosts(); } @Override diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsAboutAppFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsAboutAppFragment.java index 0ec9c7177a..ea92d5153d 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsAboutAppFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsAboutAppFragment.java @@ -16,7 +16,6 @@ import org.joinmastodon.android.ui.utils.UiUtils; import java.util.List; -import java.util.Objects; import androidx.recyclerview.widget.RecyclerView; import me.grishka.appkit.imageloader.ImageCache; @@ -26,23 +25,32 @@ public class SettingsAboutAppFragment extends BaseSettingsFragment{ private ListItem mediaCacheItem; + private AccountSession session; + private boolean timelineCacheCleared=false; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setTitle(getString(R.string.about_app, getString(R.string.sk_app_name))); - AccountSession s=AccountSessionManager.get(accountID); + session=AccountSessionManager.get(accountID); onDataLoaded(List.of( new ListItem<>(R.string.sk_settings_donate, 0, R.drawable.ic_fluent_heart_24_regular, i->UiUtils.openHashtagTimeline(getActivity(), accountID, getString(R.string.donate_hashtag))), new ListItem<>(R.string.sk_settings_contribute, 0, R.drawable.ic_fluent_open_24_regular, i->UiUtils.launchWebBrowser(getActivity(), getString(R.string.repo_url))), - new ListItem<>(R.string.settings_tos, 0, R.drawable.ic_fluent_open_24_regular, i->UiUtils.launchWebBrowser(getActivity(), "https://"+s.domain+"/terms")), + new ListItem<>(R.string.settings_tos, 0, R.drawable.ic_fluent_open_24_regular, i->UiUtils.launchWebBrowser(getActivity(), "https://"+session.domain+"/terms")), new ListItem<>(R.string.settings_privacy_policy, 0, R.drawable.ic_fluent_open_24_regular, i->UiUtils.launchWebBrowser(getActivity(), getString(R.string.privacy_policy_url)), 0, true), - mediaCacheItem=new ListItem<>(R.string.settings_clear_cache, 0, this::onClearMediaCacheClick) + mediaCacheItem=new ListItem<>(R.string.settings_clear_cache, 0, this::onClearMediaCacheClick), + new ListItem<>(getString(R.string.sk_settings_clear_timeline_cache), session.domain, this::onClearTimelineCacheClick) )); updateMediaCacheItem(); } + @Override + protected void onHidden(){ + super.onHidden(); + if(timelineCacheCleared) getActivity().recreate(); + } + @Override protected void doLoadData(int offset, int count){} @@ -74,6 +82,12 @@ private void onClearMediaCacheClick(ListItem item){ }); } + private void onClearTimelineCacheClick(ListItem item){ + session.getCacheController().putHomeTimeline(List.of(), true); + Toast.makeText(getContext(), R.string.sk_message_cache_cleared, Toast.LENGTH_SHORT).show(); + timelineCacheCleared=true; + } + private void updateMediaCacheItem(){ long size=ImageCache.getInstance(getActivity()).getDiskCache().size(); mediaCacheItem.subtitle=UiUtils.formatFileSize(getActivity(), size, false); diff --git a/mastodon/src/main/res/values/strings_sk.xml b/mastodon/src/main/res/values/strings_sk.xml index 98da35c28b..25b92cfc81 100644 --- a/mastodon/src/main/res/values/strings_sk.xml +++ b/mastodon/src/main/res/values/strings_sk.xml @@ -412,4 +412,6 @@ Underlined links Set as default Edit alt text + Clear home timeline cache + Cleared message cache \ No newline at end of file