From cfcd50cb76f24abae461ae116e10dafe88428ac3 Mon Sep 17 00:00:00 2001 From: Grishka Date: Fri, 8 Apr 2022 00:11:45 +0300 Subject: [PATCH] Update polls throughout the app when voting --- .../android/events/PollUpdatedEvent.java | 13 ++ .../fragments/AccountTimelineFragment.java | 19 +-- .../fragments/BaseStatusListFragment.java | 6 +- .../fragments/HomeTimelineFragment.java | 27 ---- .../fragments/NotificationsListFragment.java | 39 ++++-- .../android/fragments/StatusListFragment.java | 127 ++++++++++-------- .../android/fragments/ThreadFragment.java | 23 +--- .../discover/DiscoverPostsFragment.java | 16 --- .../PollFooterStatusDisplayItem.java | 1 - 9 files changed, 122 insertions(+), 149 deletions(-) create mode 100644 mastodon/src/main/java/org/joinmastodon/android/events/PollUpdatedEvent.java diff --git a/mastodon/src/main/java/org/joinmastodon/android/events/PollUpdatedEvent.java b/mastodon/src/main/java/org/joinmastodon/android/events/PollUpdatedEvent.java new file mode 100644 index 00000000..f72565bd --- /dev/null +++ b/mastodon/src/main/java/org/joinmastodon/android/events/PollUpdatedEvent.java @@ -0,0 +1,13 @@ +package org.joinmastodon.android.events; + +import org.joinmastodon.android.model.Poll; + +public class PollUpdatedEvent{ + public String accountID; + public Poll poll; + + public PollUpdatedEvent(String accountID, Poll poll){ + this.accountID=accountID; + this.poll=poll; + } +} diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/AccountTimelineFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/AccountTimelineFragment.java index 5d65436b..751b1a7e 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/AccountTimelineFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/AccountTimelineFragment.java @@ -4,14 +4,10 @@ import android.app.Activity; import android.os.Bundle; import android.view.View; -import com.squareup.otto.Subscribe; - import org.joinmastodon.android.R; import org.joinmastodon.android.api.requests.accounts.GetAccountStatuses; import org.joinmastodon.android.api.session.AccountSessionManager; -import org.joinmastodon.android.events.StatusCountersUpdatedEvent; import org.joinmastodon.android.events.StatusCreatedEvent; -import org.joinmastodon.android.events.StatusDeletedEvent; import org.joinmastodon.android.model.Account; import org.joinmastodon.android.model.Status; import org.parceler.Parcels; @@ -73,20 +69,7 @@ public class AccountTimelineFragment extends StatusListFragment{ loadData(); } - @Override - @Subscribe - public void onStatusCountersUpdated(StatusCountersUpdatedEvent ev){ - super.onStatusCountersUpdated(ev); - } - - @Override - @Subscribe - public void onStatusDeleted(StatusDeletedEvent ev){ - super.onStatusDeleted(ev); - } - - @Subscribe - public void onStatusCreated(StatusCreatedEvent ev){ + protected void onStatusCreated(StatusCreatedEvent ev){ if(!AccountSessionManager.getInstance().isSelf(accountID, ev.status.account)) return; if(filter==GetAccountStatuses.Filter.DEFAULT){ diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/BaseStatusListFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/BaseStatusListFragment.java index 1b810c92..af6f9dbd 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/BaseStatusListFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/BaseStatusListFragment.java @@ -18,9 +18,11 @@ import android.view.ViewGroup; import android.view.WindowInsets; import android.widget.Toolbar; +import org.joinmastodon.android.E; import org.joinmastodon.android.R; import org.joinmastodon.android.api.requests.accounts.GetAccountRelationships; import org.joinmastodon.android.api.requests.polls.SubmitPollVote; +import org.joinmastodon.android.events.PollUpdatedEvent; import org.joinmastodon.android.model.Account; import org.joinmastodon.android.model.DisplayItemsParent; import org.joinmastodon.android.model.Poll; @@ -344,7 +346,7 @@ public abstract class BaseStatusListFragment exten public abstract void onItemClick(String id); - protected void updatePoll(String itemID, Poll poll){ + protected void updatePoll(String itemID, Status status, Poll poll){ int firstOptionIndex=-1, footerIndex=-1; int i=0; for(StatusDisplayItem item:displayItems){ @@ -412,7 +414,7 @@ public abstract class BaseStatusListFragment exten .setCallback(new Callback<>(){ @Override public void onSuccess(Poll result){ - updatePoll(parentID, result); + E.post(new PollUpdatedEvent(accountID, result)); } @Override 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 f10e5a9f..976221e1 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeTimelineFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeTimelineFragment.java @@ -1,8 +1,6 @@ package org.joinmastodon.android.fragments; import android.app.Activity; -import android.content.DialogInterface; -import android.content.Intent; import android.content.res.ColorStateList; import android.content.res.Configuration; import android.os.Bundle; @@ -11,35 +9,22 @@ import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; -import android.view.ViewGroup; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.Toolbar; import com.squareup.otto.Subscribe; -import org.joinmastodon.android.E; -import org.joinmastodon.android.MainActivity; import org.joinmastodon.android.R; -import org.joinmastodon.android.api.requests.oauth.RevokeOauthToken; -import org.joinmastodon.android.api.requests.timelines.GetHomeTimeline; -import org.joinmastodon.android.api.session.AccountSession; import org.joinmastodon.android.api.session.AccountSessionManager; -import org.joinmastodon.android.events.StatusCountersUpdatedEvent; import org.joinmastodon.android.events.StatusCreatedEvent; -import org.joinmastodon.android.events.StatusDeletedEvent; import org.joinmastodon.android.model.Status; -import org.joinmastodon.android.ui.M3AlertDialogBuilder; import org.joinmastodon.android.ui.utils.UiUtils; -import org.parceler.Parcels; -import java.util.ArrayList; import java.util.Collections; import java.util.List; import me.grishka.appkit.Nav; -import me.grishka.appkit.api.Callback; -import me.grishka.appkit.api.ErrorResponse; import me.grishka.appkit.api.SimpleCallback; public class HomeTimelineFragment extends StatusListFragment{ @@ -121,16 +106,4 @@ public class HomeTimelineFragment extends StatusListFragment{ Toolbar toolbar=getToolbar(); toolbar.addView(logo, new Toolbar.LayoutParams(Gravity.CENTER)); } - - @Override - @Subscribe - public void onStatusCountersUpdated(StatusCountersUpdatedEvent ev){ - super.onStatusCountersUpdated(ev); - } - - @Override - @Subscribe - public void onStatusDeleted(StatusDeletedEvent ev){ - super.onStatusDeleted(ev); - } } diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/NotificationsListFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/NotificationsListFragment.java index 34b158bc..8894d768 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/NotificationsListFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/NotificationsListFragment.java @@ -8,8 +8,12 @@ import android.graphics.RectF; import android.os.Bundle; import android.view.View; +import com.squareup.otto.Subscribe; + +import org.joinmastodon.android.E; import org.joinmastodon.android.R; import org.joinmastodon.android.api.session.AccountSessionManager; +import org.joinmastodon.android.events.PollUpdatedEvent; import org.joinmastodon.android.model.Notification; import org.joinmastodon.android.model.Poll; import org.joinmastodon.android.model.Status; @@ -37,6 +41,18 @@ import me.grishka.appkit.utils.V; public class NotificationsListFragment extends BaseStatusListFragment{ private boolean onlyMentions; + @Override + public void onCreate(Bundle savedInstanceState){ + super.onCreate(savedInstanceState); + E.register(this); + } + + @Override + public void onDestroy(){ + super.onDestroy(); + E.unregister(this); + } + @Override public void onAttach(Activity activity){ super.onAttach(activity); @@ -136,15 +152,6 @@ public class NotificationsListFragment extends BaseStatusListFragment{ + protected EventListener eventListener=new EventListener(); + protected List buildDisplayItems(Status s){ return StatusDisplayItem.buildItems(this, s, accountID, s, knownAccounts, false, true); } @@ -33,13 +37,13 @@ public abstract class StatusListFragment extends BaseStatusListFragment{ @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); - E.register(this); + E.register(eventListener); } @Override public void onDestroy(){ super.onDestroy(); - E.unregister(this); + E.unregister(eventListener); } @Override @@ -55,59 +59,7 @@ public abstract class StatusListFragment extends BaseStatusListFragment{ Nav.go(getActivity(), ThreadFragment.class, args); } - @Override - protected void updatePoll(String itemID, Poll poll){ - Status status=getContentStatusByID(itemID); - if(status==null) - return; - status.poll=poll; - super.updatePoll(itemID, poll); - } - - @Subscribe - public void onStatusCountersUpdated(StatusCountersUpdatedEvent ev){ - for(Status s:data){ - if(s.getContentStatus().id.equals(ev.id)){ - s.update(ev); - for(int i=0;i{ } return null; } + + public class EventListener{ + + @Subscribe + public void onStatusCountersUpdated(StatusCountersUpdatedEvent ev){ + for(Status s:data){ + if(s.getContentStatus().id.equals(ev.id)){ + s.update(ev); + for(int i=0;i