reverting to the old code. This is weird

This commit is contained in:
LucasGGamerM 2023-01-22 12:40:26 -03:00
parent bed71fd9e0
commit 135a98224f
2 changed files with 10 additions and 96 deletions

View File

@ -12,7 +12,6 @@ import android.util.Log;
import org.joinmastodon.android.BuildConfig;
import org.joinmastodon.android.MastodonApp;
import org.joinmastodon.android.api.requests.notifications.GetNotifications;
import org.joinmastodon.android.api.requests.timelines.GetConversationsTimeline;
import org.joinmastodon.android.api.requests.timelines.GetHomeTimeline;
import org.joinmastodon.android.api.session.AccountSessionManager;
import org.joinmastodon.android.model.CacheablePaginatedResponse;
@ -127,79 +126,6 @@ public class CacheController{
});
}
public void getConversationsTimeline(String maxID, int count, boolean forceReload, Callback<CacheablePaginatedResponse<List<Status>>> callback){
cancelDelayedClose();
databaseThread.postRunnable(()->{
try{
List<Filter> filters=AccountSessionManager.getInstance().getAccount(accountID).wordFilters.stream().filter(f->f.context.contains(Filter.FilterContext.HOME)).collect(Collectors.toList());
if(!forceReload){
SQLiteDatabase db=getOrOpenDatabase();
try(Cursor cursor=db.query("conversations_timeline", new String[]{"json", "flags"}, maxID==null ? null : "`id`<?", maxID==null ? null : new String[]{maxID}, null, null, "`id` DESC", count+"")){
if(cursor.getCount()==count){
ArrayList<Status> result=new ArrayList<>();
cursor.moveToFirst();
String newMaxID;
outer:
do{
Status status=MastodonAPIController.gson.fromJson(cursor.getString(0), Status.class);
status.postprocess();
int flags=cursor.getInt(1);
status.hasGapAfter=((flags & POST_FLAG_GAP_AFTER)!=0);
newMaxID=status.id;
for(Filter filter:filters){
if(filter.matches(status))
continue outer;
}
result.add(status);
}while(cursor.moveToNext());
String _newMaxID=newMaxID;
uiHandler.post(()->callback.onSuccess(new CacheablePaginatedResponse<>(result, _newMaxID, true)));
return;
}
}catch(IOException x){
Log.w(TAG, "getConversationsTimeline: corrupted status object in database", x);
}
}
new GetConversationsTimeline(maxID, null, count, null)
.setCallback(new Callback<>(){
@Override
public void onSuccess(List<Status> result){
callback.onSuccess(new CacheablePaginatedResponse<>(result.stream().filter(new StatusFilterPredicate(filters)).collect(Collectors.toList()), result.isEmpty() ? null : result.get(result.size()-1).id, false));
putConversationsTimeline(result, maxID==null);
}
@Override
public void onError(ErrorResponse error){
callback.onError(error);
}
})
.exec(accountID);
}catch(SQLiteException x){
Log.w(TAG, x);
uiHandler.post(()->callback.onError(new MastodonErrorResponse(x.getLocalizedMessage(), 500, x)));
}finally{
closeDelayed();
}
}, 0);
}
public void putConversationsTimeline(List<Status> posts, boolean clear){
runOnDbThread((db)->{
if(clear)
db.delete("conversations_timeline", null, null);
ContentValues values=new ContentValues(3);
for(Status s:posts){
values.put("id", s.id);
values.put("json", MastodonAPIController.gson.toJson(s));
int flags=0;
if(s.hasGapAfter)
flags|=POST_FLAG_GAP_AFTER;
values.put("flags", flags);
db.insertWithOnConflict("conversations_timeline", null, values, SQLiteDatabase.CONFLICT_REPLACE);
}
});
}
public void getNotifications(String maxID, int count, boolean onlyMentions, boolean onlyPosts, boolean forceReload, Callback<PaginatedResponse<List<Notification>>> callback){
cancelDelayedClose();
databaseThread.postRunnable(()->{
@ -393,18 +319,15 @@ public class CacheController{
)""");
createRecentSearchesTable(db);
createPostsNotificationsTable(db);
createConversationsTimelineTable(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
if(oldVersion==1){
createRecentSearchesTable(db);
createConversationsTimelineTable(db);
}
if(oldVersion==2){
createPostsNotificationsTable(db);
createConversationsTimelineTable(db);
}
}
@ -426,15 +349,6 @@ public class CacheController{
`type` INTEGER NOT NULL
)""");
}
private void createConversationsTimelineTable(SQLiteDatabase db){
db.execSQL("""
CREATE TABLE `conversations_timeline` (
`id` VARCHAR(25) NOT NULL PRIMARY KEY,
`json` TEXT NOT NULL,
`flags` INTEGER NOT NULL DEFAULT 0
)""");
}
}
@FunctionalInterface

View File

@ -15,6 +15,7 @@ import org.joinmastodon.android.events.StatusCreatedEvent;
import org.joinmastodon.android.model.CacheablePaginatedResponse;
import org.joinmastodon.android.model.Filter;
import org.joinmastodon.android.model.Status;
import org.joinmastodon.android.model.StatusPrivacy;
import org.joinmastodon.android.ui.displayitems.GapStatusDisplayItem;
import org.joinmastodon.android.ui.displayitems.StatusDisplayItem;
import org.joinmastodon.android.utils.StatusFilterPredicate;
@ -43,18 +44,17 @@ public class ConversationsTimelineFragment extends FabStatusListFragment {
private List<Status> filterPosts(List<Status> items) {
// Disabling this for DMs, because there are no boosts on DMs, and most of them are replies
// return items.stream().filter(i ->
// (GlobalUserPreferences.showReplies || i.inReplyToId == null) &&
// (GlobalUserPreferences.showBoosts || i.reblog == null)
// ).collect(Collectors.toList());
return items;
return items.stream().filter(i ->
(i.visibility == StatusPrivacy.DIRECT)
).collect(Collectors.toList());
// return items;
}
@Override
protected void doLoadData(int offset, int count){
AccountSessionManager.getInstance()
.getAccount(accountID).getCacheController()
.getConversationsTimeline(offset>0 ? maxID : null, count, refreshing, new SimpleCallback<>(this){
.getHomeTimeline(offset>0 ? maxID : null, count, refreshing, new SimpleCallback<>(this){
@Override
public void onSuccess(CacheablePaginatedResponse<List<Status>> result){
if(getActivity()==null)
@ -127,7 +127,7 @@ public class ConversationsTimelineFragment extends FabStatusListFragment {
if(!toAdd.isEmpty()){
prependItems(toAdd, true);
if (parent != null) parent.showNewPostsButton();
AccountSessionManager.getInstance().getAccount(accountID).getCacheController().putConversationsTimeline(toAdd, false);
// AccountSessionManager.getInstance().getAccount(accountID).getCacheController().putHomTimeline(toAdd, false);
}
}
@ -166,7 +166,7 @@ public class ConversationsTimelineFragment extends FabStatusListFragment {
Status gapStatus=getStatusByID(gap.parentID);
if(gapStatus!=null){
gapStatus.hasGapAfter=false;
AccountSessionManager.getInstance().getAccount(accountID).getCacheController().putConversationsTimeline(Collections.singletonList(gapStatus), false);
// AccountSessionManager.getInstance().getAccount(accountID).getCacheController().putConversationsTimeline(Collections.singletonList(gapStatus), false);
}
}else{
Set<String> idsBelowGap=new HashSet<>();
@ -178,7 +178,7 @@ public class ConversationsTimelineFragment extends FabStatusListFragment {
}else if(s.id.equals(gap.parentID)){
belowGap=true;
s.hasGapAfter=false;
AccountSessionManager.getInstance().getAccount(accountID).getCacheController().putConversationsTimeline(Collections.singletonList(s), false);
// AccountSessionManager.getInstance().getAccount(accountID).getCacheController().putConversationsTimeline(Collections.singletonList(s), false);
}else{
gapPostIndex++;
}
@ -213,7 +213,7 @@ public class ConversationsTimelineFragment extends FabStatusListFragment {
adapter.notifyItemChanged(getMainAdapterOffset()+gapPos);
adapter.notifyItemRangeInserted(getMainAdapterOffset()+gapPos+1, targetList.size()-1);
}
AccountSessionManager.getInstance().getAccount(accountID).getCacheController().putConversationsTimeline(insertedPosts, false);
// AccountSessionManager.getInstance().getAccount(accountID).getCacheController().putConversationsTimeline(insertedPosts, false);
}
}