Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
load new posts on reload
Browse files Browse the repository at this point in the history
  • Loading branch information
sk22 committed Oct 18, 2023
1 parent 3097bc7 commit 6cbe406
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ public void putHomeTimeline(List<Status> 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"});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void onSuccess(CacheablePaginatedResponse<List<Status>> result){
maxID=result.maxID;
AccountSessionManager.get(accountID).filterStatuses(result.items, getFilterContext());
onDataLoaded(result.items, !empty);
if(result.isFromCache())
if(result.isFromCache() && GlobalUserPreferences.loadNewPosts)
loadNewPosts();
}
});
Expand All @@ -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();
}
}
});
Expand All @@ -87,7 +88,7 @@ protected void onShown(){
if(!getArguments().getBoolean("noAutoLoad")){
if(!loaded && !dataLoading){
loadData();
}else if(!dataLoading){
}else if(!dataLoading && GlobalUserPreferences.loadNewPosts){
loadNewPosts();
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -156,12 +156,14 @@ public void onSuccess(List<Status> 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);
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,23 +25,32 @@

public class SettingsAboutAppFragment extends BaseSettingsFragment<Void>{
private ListItem<Void> 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){}

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions mastodon/src/main/res/values/strings_sk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,6 @@
<string name="sk_settings_underlined_links">Underlined links</string>
<string name="sk_set_as_default">Set as default</string>
<string name="sk_edit_alt_text">Edit alt text</string>
<string name="sk_settings_clear_timeline_cache">Clear home timeline cache</string>
<string name="sk_message_cache_cleared">Cleared message cache</string>
</resources>

0 comments on commit 6cbe406

Please sign in to comment.