Timeline refactor (#2175)
* Move Timeline files into their own package * Introduce TimelineViewModel, add coroutines * Simplify StatusViewData * Handle timeilne fetch errors * Rework filters, fix ViewThreadFragment * Fix NotificationsFragment * Simplify Notifications and Thread, handle pin * Redo loading in TimelineViewModel * Improve error handling in TimelineViewModel * Rewrite actions in TimelineViewModel * Apply feedback after timeline factoring review * Handle initial failure in timeline correctly
This commit is contained in:
parent
0a992480c2
commit
44a5b42cac
58 changed files with 3956 additions and 3618 deletions
|
|
@ -58,6 +58,7 @@ import com.keylesspalace.tusky.appstore.BlockEvent;
|
|||
import com.keylesspalace.tusky.appstore.BookmarkEvent;
|
||||
import com.keylesspalace.tusky.appstore.EventHub;
|
||||
import com.keylesspalace.tusky.appstore.FavoriteEvent;
|
||||
import com.keylesspalace.tusky.appstore.PinEvent;
|
||||
import com.keylesspalace.tusky.appstore.PreferenceChangedEvent;
|
||||
import com.keylesspalace.tusky.appstore.ReblogEvent;
|
||||
import com.keylesspalace.tusky.db.AccountEntity;
|
||||
|
|
@ -83,6 +84,7 @@ import com.keylesspalace.tusky.util.StatusDisplayOptions;
|
|||
import com.keylesspalace.tusky.util.ViewDataUtils;
|
||||
import com.keylesspalace.tusky.view.BackgroundMessageView;
|
||||
import com.keylesspalace.tusky.view.EndlessOnScrollListener;
|
||||
import com.keylesspalace.tusky.viewdata.AttachmentViewData;
|
||||
import com.keylesspalace.tusky.viewdata.NotificationViewData;
|
||||
import com.keylesspalace.tusky.viewdata.StatusViewData;
|
||||
|
||||
|
|
@ -92,6 +94,7 @@ import java.util.Collections;
|
|||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
|
@ -311,35 +314,6 @@ public class NotificationsFragment extends SFragment implements
|
|||
.show();
|
||||
}
|
||||
|
||||
private void handleFavEvent(FavoriteEvent event) {
|
||||
Pair<Integer, Notification> posAndNotification =
|
||||
findReplyPosition(event.getStatusId());
|
||||
if (posAndNotification == null) return;
|
||||
//noinspection ConstantConditions
|
||||
setFavouriteForStatus(posAndNotification.first,
|
||||
posAndNotification.second.getStatus(),
|
||||
event.getFavourite());
|
||||
}
|
||||
|
||||
private void handleBookmarkEvent(BookmarkEvent event) {
|
||||
Pair<Integer, Notification> posAndNotification =
|
||||
findReplyPosition(event.getStatusId());
|
||||
if (posAndNotification == null) return;
|
||||
//noinspection ConstantConditions
|
||||
setBookmarkForStatus(posAndNotification.first,
|
||||
posAndNotification.second.getStatus(),
|
||||
event.getBookmark());
|
||||
}
|
||||
|
||||
private void handleReblogEvent(ReblogEvent event) {
|
||||
Pair<Integer, Notification> posAndNotification = findReplyPosition(event.getStatusId());
|
||||
if (posAndNotification == null) return;
|
||||
//noinspection ConstantConditions
|
||||
setReblogForStatus(posAndNotification.first,
|
||||
posAndNotification.second.getStatus(),
|
||||
event.getReblog());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
|
|
@ -386,11 +360,13 @@ public class NotificationsFragment extends SFragment implements
|
|||
.to(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
|
||||
.subscribe(event -> {
|
||||
if (event instanceof FavoriteEvent) {
|
||||
handleFavEvent((FavoriteEvent) event);
|
||||
setFavouriteForStatus(((FavoriteEvent) event).getStatusId(), ((FavoriteEvent) event).getFavourite());
|
||||
} else if (event instanceof BookmarkEvent) {
|
||||
handleBookmarkEvent((BookmarkEvent) event);
|
||||
setBookmarkForStatus(((BookmarkEvent) event).getStatusId(), ((BookmarkEvent) event).getBookmark());
|
||||
} else if (event instanceof ReblogEvent) {
|
||||
handleReblogEvent((ReblogEvent) event);
|
||||
setReblogForStatus(((ReblogEvent) event).getStatusId(), ((ReblogEvent) event).getReblog());
|
||||
} else if (event instanceof PinEvent) {
|
||||
setPinForStatus(((PinEvent) event).getStatusId(), ((PinEvent) event).getPinned());
|
||||
} else if (event instanceof BlockEvent) {
|
||||
removeAllByAccountId(((BlockEvent) event).getAccountId());
|
||||
} else if (event instanceof PreferenceChangedEvent) {
|
||||
|
|
@ -423,34 +399,21 @@ public class NotificationsFragment extends SFragment implements
|
|||
final Notification notification = notifications.get(position).asRight();
|
||||
final Status status = notification.getStatus();
|
||||
Objects.requireNonNull(status, "Reblog on notification without status");
|
||||
timelineCases.reblog(status, reblog)
|
||||
timelineCases.reblog(status.getId(), reblog)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.to(autoDisposable(from(this)))
|
||||
.subscribe(
|
||||
(newStatus) -> setReblogForStatus(position, status, reblog),
|
||||
(newStatus) -> setReblogForStatus(status.getId(), reblog),
|
||||
(t) -> Log.d(getClass().getSimpleName(),
|
||||
"Failed to reblog status: " + status.getId(), t)
|
||||
);
|
||||
}
|
||||
|
||||
private void setReblogForStatus(int position, Status status, boolean reblog) {
|
||||
status.setReblogged(reblog);
|
||||
|
||||
if (status.getReblog() != null) {
|
||||
status.getReblog().setReblogged(reblog);
|
||||
}
|
||||
|
||||
NotificationViewData.Concrete viewdata = (NotificationViewData.Concrete) notifications.getPairedItem(position);
|
||||
|
||||
StatusViewData.Builder viewDataBuilder = new StatusViewData.Builder(viewdata.getStatusViewData());
|
||||
viewDataBuilder.setReblogged(reblog);
|
||||
|
||||
NotificationViewData.Concrete newViewData = new NotificationViewData.Concrete(
|
||||
viewdata.getType(), viewdata.getId(), viewdata.getAccount(),
|
||||
viewDataBuilder.createStatusViewData());
|
||||
|
||||
notifications.setPairedItem(position, newViewData);
|
||||
updateAdapter();
|
||||
private void setReblogForStatus(String statusId, boolean reblog) {
|
||||
updateStatus(statusId, (s) -> {
|
||||
s.setReblogged(reblog);
|
||||
return s;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -458,34 +421,21 @@ public class NotificationsFragment extends SFragment implements
|
|||
final Notification notification = notifications.get(position).asRight();
|
||||
final Status status = notification.getStatus();
|
||||
|
||||
timelineCases.favourite(status, favourite)
|
||||
timelineCases.favourite(status.getId(), favourite)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.to(autoDisposable(from(this)))
|
||||
.subscribe(
|
||||
(newStatus) -> setFavouriteForStatus(position, status, favourite),
|
||||
(newStatus) -> setFavouriteForStatus(status.getId(), favourite),
|
||||
(t) -> Log.d(getClass().getSimpleName(),
|
||||
"Failed to favourite status: " + status.getId(), t)
|
||||
);
|
||||
}
|
||||
|
||||
private void setFavouriteForStatus(int position, Status status, boolean favourite) {
|
||||
status.setFavourited(favourite);
|
||||
|
||||
if (status.getReblog() != null) {
|
||||
status.getReblog().setFavourited(favourite);
|
||||
}
|
||||
|
||||
NotificationViewData.Concrete viewdata = (NotificationViewData.Concrete) notifications.getPairedItem(position);
|
||||
|
||||
StatusViewData.Builder viewDataBuilder = new StatusViewData.Builder(viewdata.getStatusViewData());
|
||||
viewDataBuilder.setFavourited(favourite);
|
||||
|
||||
NotificationViewData.Concrete newViewData = new NotificationViewData.Concrete(
|
||||
viewdata.getType(), viewdata.getId(), viewdata.getAccount(),
|
||||
viewDataBuilder.createStatusViewData());
|
||||
|
||||
notifications.setPairedItem(position, newViewData);
|
||||
updateAdapter();
|
||||
private void setFavouriteForStatus(String statusId, boolean favourite) {
|
||||
updateStatus(statusId, (s) -> {
|
||||
s.setFavourited(favourite);
|
||||
return s;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -493,63 +443,38 @@ public class NotificationsFragment extends SFragment implements
|
|||
final Notification notification = notifications.get(position).asRight();
|
||||
final Status status = notification.getStatus();
|
||||
|
||||
timelineCases.bookmark(status, bookmark)
|
||||
timelineCases.bookmark(status.getActionableId(), bookmark)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.to(autoDisposable(from(this)))
|
||||
.subscribe(
|
||||
(newStatus) -> setBookmarkForStatus(position, status, bookmark),
|
||||
(newStatus) -> setBookmarkForStatus(status.getId(), bookmark),
|
||||
(t) -> Log.d(getClass().getSimpleName(),
|
||||
"Failed to bookmark status: " + status.getId(), t)
|
||||
);
|
||||
}
|
||||
|
||||
private void setBookmarkForStatus(int position, Status status, boolean bookmark) {
|
||||
status.setBookmarked(bookmark);
|
||||
|
||||
if (status.getReblog() != null) {
|
||||
status.getReblog().setBookmarked(bookmark);
|
||||
}
|
||||
|
||||
NotificationViewData.Concrete viewdata = (NotificationViewData.Concrete) notifications.getPairedItem(position);
|
||||
|
||||
StatusViewData.Builder viewDataBuilder = new StatusViewData.Builder(viewdata.getStatusViewData());
|
||||
viewDataBuilder.setBookmarked(bookmark);
|
||||
|
||||
NotificationViewData.Concrete newViewData = new NotificationViewData.Concrete(
|
||||
viewdata.getType(), viewdata.getId(), viewdata.getAccount(),
|
||||
viewDataBuilder.createStatusViewData());
|
||||
|
||||
notifications.setPairedItem(position, newViewData);
|
||||
updateAdapter();
|
||||
private void setBookmarkForStatus(String statusId, boolean bookmark) {
|
||||
updateStatus(statusId, (s) -> {
|
||||
s.setBookmarked(bookmark);
|
||||
return s;
|
||||
});
|
||||
}
|
||||
|
||||
public void onVoteInPoll(int position, @NonNull List<Integer> choices) {
|
||||
final Notification notification = notifications.get(position).asRight();
|
||||
final Status status = notification.getStatus();
|
||||
|
||||
timelineCases.voteInPoll(status, choices)
|
||||
final Status status = notification.getStatus().getActionableStatus();
|
||||
timelineCases.voteInPoll(status.getId(), status.getPoll().getId(), choices)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.to(autoDisposable(from(this)))
|
||||
.subscribe(
|
||||
(newPoll) -> setVoteForPoll(position, newPoll),
|
||||
(newPoll) -> setVoteForPoll(status, newPoll),
|
||||
(t) -> Log.d(TAG,
|
||||
"Failed to vote in poll: " + status.getId(), t)
|
||||
);
|
||||
}
|
||||
|
||||
private void setVoteForPoll(int position, Poll poll) {
|
||||
|
||||
NotificationViewData.Concrete viewdata = (NotificationViewData.Concrete) notifications.getPairedItem(position);
|
||||
|
||||
StatusViewData.Builder viewDataBuilder = new StatusViewData.Builder(viewdata.getStatusViewData());
|
||||
viewDataBuilder.setPoll(poll);
|
||||
|
||||
NotificationViewData.Concrete newViewData = new NotificationViewData.Concrete(
|
||||
viewdata.getType(), viewdata.getId(), viewdata.getAccount(),
|
||||
viewDataBuilder.createStatusViewData());
|
||||
|
||||
notifications.setPairedItem(position, newViewData);
|
||||
updateAdapter();
|
||||
private void setVoteForPoll(Status status, Poll poll) {
|
||||
updateStatus(status.getId(), (s) -> s.copyWithPoll(poll));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -562,13 +487,17 @@ public class NotificationsFragment extends SFragment implements
|
|||
public void onViewMedia(int position, int attachmentIndex, @Nullable View view) {
|
||||
Notification notification = notifications.get(position).asRightOrNull();
|
||||
if (notification == null || notification.getStatus() == null) return;
|
||||
super.viewMedia(attachmentIndex, notification.getStatus(), view);
|
||||
Status status = notification.getStatus();
|
||||
super.viewMedia(attachmentIndex, AttachmentViewData.list(status), view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewThread(int position) {
|
||||
Notification notification = notifications.get(position).asRight();
|
||||
super.viewThread(notification.getStatus());
|
||||
Status status = notification.getStatus();
|
||||
if (status == null) return;
|
||||
;
|
||||
super.viewThread(status.getActionableId(), status.getActionableStatus().getUrl());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -579,30 +508,19 @@ public class NotificationsFragment extends SFragment implements
|
|||
|
||||
@Override
|
||||
public void onExpandedChange(boolean expanded, int position) {
|
||||
NotificationViewData.Concrete old =
|
||||
(NotificationViewData.Concrete) notifications.getPairedItem(position);
|
||||
StatusViewData.Concrete statusViewData =
|
||||
new StatusViewData.Builder(old.getStatusViewData())
|
||||
.setIsExpanded(expanded)
|
||||
.createStatusViewData();
|
||||
NotificationViewData notificationViewData = new NotificationViewData.Concrete(old.getType(),
|
||||
old.getId(), old.getAccount(), statusViewData);
|
||||
notifications.setPairedItem(position, notificationViewData);
|
||||
updateAdapter();
|
||||
updateViewDataAt(position, (vd) -> vd.copyWithExpanded(expanded));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onContentHiddenChange(boolean isShowing, int position) {
|
||||
NotificationViewData.Concrete old =
|
||||
(NotificationViewData.Concrete) notifications.getPairedItem(position);
|
||||
StatusViewData.Concrete statusViewData =
|
||||
new StatusViewData.Builder(old.getStatusViewData())
|
||||
.setIsShowingSensitiveContent(isShowing)
|
||||
.createStatusViewData();
|
||||
NotificationViewData notificationViewData = new NotificationViewData.Concrete(old.getType(),
|
||||
old.getId(), old.getAccount(), statusViewData);
|
||||
notifications.setPairedItem(position, notificationViewData);
|
||||
updateAdapter();
|
||||
updateViewDataAt(position, (vd) -> vd.copyWithShowingContent(isShowing));
|
||||
}
|
||||
|
||||
private void setPinForStatus(String statusId, boolean pinned) {
|
||||
updateStatus(statusId, status -> {
|
||||
status.copyWithPinned(pinned);
|
||||
return status;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -628,42 +546,74 @@ public class NotificationsFragment extends SFragment implements
|
|||
|
||||
@Override
|
||||
public void onContentCollapsedChange(boolean isCollapsed, int position) {
|
||||
if (position < 0 || position >= notifications.size()) {
|
||||
Log.e(TAG, String.format("Tried to access out of bounds status position: %d of %d", position, notifications.size() - 1));
|
||||
return;
|
||||
}
|
||||
updateViewDataAt(position, (vd) -> vd.copyWIthCollapsed(isCollapsed));
|
||||
;
|
||||
}
|
||||
|
||||
NotificationViewData notification = notifications.getPairedItem(position);
|
||||
if (!(notification instanceof NotificationViewData.Concrete)) {
|
||||
Log.e(TAG, String.format(
|
||||
"Expected NotificationViewData.Concrete, got %s instead at position: %d of %d",
|
||||
notification == null ? "null" : notification.getClass().getSimpleName(),
|
||||
private void updateStatus(String statusId, Function<Status, Status> mapper) {
|
||||
int index = CollectionsKt.indexOfFirst(this.notifications, (s) -> s.isRight() &&
|
||||
s.asRight().getStatus() != null &&
|
||||
s.asRight().getStatus().getId().equals(statusId));
|
||||
if (index == -1) return;
|
||||
|
||||
// We have quite some graph here:
|
||||
//
|
||||
// Notification --------> Status
|
||||
// ^
|
||||
// |
|
||||
// StatusViewData
|
||||
// ^
|
||||
// |
|
||||
// NotificationViewData -----+
|
||||
//
|
||||
// So if we have "new" status we need to update all references to be sure that data is
|
||||
// up-to-date:
|
||||
// 1. update status
|
||||
// 2. update notification
|
||||
// 3. update statusViewData
|
||||
// 4. update notificationViewData
|
||||
|
||||
Status oldStatus = notifications.get(index).asRight().getStatus();
|
||||
NotificationViewData.Concrete oldViewData =
|
||||
(NotificationViewData.Concrete) this.notifications.getPairedItem(index);
|
||||
Status newStatus = mapper.apply(oldStatus);
|
||||
Notification newNotification = this.notifications.get(index).asRight()
|
||||
.copyWithStatus(newStatus);
|
||||
StatusViewData.Concrete newStatusViewData =
|
||||
Objects.requireNonNull(oldViewData.getStatusViewData()).copyWithStatus(newStatus);
|
||||
NotificationViewData.Concrete newViewData = oldViewData.copyWithStatus(newStatusViewData);
|
||||
|
||||
notifications.set(index, new Either.Right<>(newNotification));
|
||||
notifications.setPairedItem(index, newViewData);
|
||||
|
||||
updateAdapter();
|
||||
}
|
||||
|
||||
private void updateViewDataAt(int position,
|
||||
Function<StatusViewData.Concrete, StatusViewData.Concrete> mapper) {
|
||||
if (position < 0 || position >= notifications.size()) {
|
||||
String message = String.format(
|
||||
Locale.getDefault(),
|
||||
"Tried to access out of bounds status position: %d of %d",
|
||||
position,
|
||||
notifications.size() - 1
|
||||
));
|
||||
);
|
||||
Log.e(TAG, message);
|
||||
return;
|
||||
}
|
||||
NotificationViewData someViewData = this.notifications.getPairedItem(position);
|
||||
if (!(someViewData instanceof NotificationViewData.Concrete)) {
|
||||
return;
|
||||
}
|
||||
NotificationViewData.Concrete oldViewData = (NotificationViewData.Concrete) someViewData;
|
||||
StatusViewData.Concrete oldStatusViewData = oldViewData.getStatusViewData();
|
||||
if (oldStatusViewData == null) return;
|
||||
|
||||
StatusViewData.Concrete status = ((NotificationViewData.Concrete) notification).getStatusViewData();
|
||||
StatusViewData.Concrete updatedStatus = new StatusViewData.Builder(status)
|
||||
.setCollapsed(isCollapsed)
|
||||
.createStatusViewData();
|
||||
NotificationViewData.Concrete newViewData =
|
||||
oldViewData.copyWithStatus(mapper.apply(oldStatusViewData));
|
||||
notifications.setPairedItem(position, newViewData);
|
||||
|
||||
NotificationViewData.Concrete concreteNotification = (NotificationViewData.Concrete) notification;
|
||||
NotificationViewData updatedNotification = new NotificationViewData.Concrete(
|
||||
concreteNotification.getType(),
|
||||
concreteNotification.getId(),
|
||||
concreteNotification.getAccount(),
|
||||
updatedStatus
|
||||
);
|
||||
notifications.setPairedItem(position, updatedNotification);
|
||||
updateAdapter();
|
||||
|
||||
// Since we cannot notify to the RecyclerView right away because it may be scrolling
|
||||
// we run this when the RecyclerView is done doing measurements and other calculations.
|
||||
// To test this is not bs: try getting a notification while scrolling, without wrapping
|
||||
// notifyItemChanged in a .post() call. App will crash.
|
||||
recyclerView.post(() -> adapter.notifyItemChanged(position, notification));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -844,8 +794,11 @@ public class NotificationsFragment extends SFragment implements
|
|||
for (Either<Placeholder, Notification> either : notifications) {
|
||||
Notification notification = either.asRightOrNull();
|
||||
if (notification != null && notification.getId().equals(notificationId)) {
|
||||
super.viewThread(notification.getStatus());
|
||||
return;
|
||||
Status status = notification.getStatus();
|
||||
if (status != null) {
|
||||
super.viewThread(status.getActionableId(), status.getActionableStatus().getUrl());
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
Log.w(TAG, "Didn't find a notification for ID: " + notificationId);
|
||||
|
|
@ -951,7 +904,7 @@ public class NotificationsFragment extends SFragment implements
|
|||
}
|
||||
|
||||
Disposable notificationCall = mastodonApi.notifications(fromId, uptoId, LOAD_AT_ONCE, showNotificationsFilter ? notificationFilter : null)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.to(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
|
||||
.subscribe(
|
||||
response -> {
|
||||
|
|
|
|||
|
|
@ -24,7 +24,6 @@ import android.content.Intent;
|
|||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Environment;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
|
|
@ -33,7 +32,6 @@ import android.widget.Toast;
|
|||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.widget.PopupMenu;
|
||||
import androidx.core.app.ActivityOptionsCompat;
|
||||
|
|
@ -55,8 +53,6 @@ import com.keylesspalace.tusky.db.AccountEntity;
|
|||
import com.keylesspalace.tusky.db.AccountManager;
|
||||
import com.keylesspalace.tusky.di.Injectable;
|
||||
import com.keylesspalace.tusky.entity.Attachment;
|
||||
import com.keylesspalace.tusky.entity.Filter;
|
||||
import com.keylesspalace.tusky.entity.PollOption;
|
||||
import com.keylesspalace.tusky.entity.Status;
|
||||
import com.keylesspalace.tusky.network.MastodonApi;
|
||||
import com.keylesspalace.tusky.network.TimelineCases;
|
||||
|
|
@ -64,20 +60,14 @@ import com.keylesspalace.tusky.util.LinkHelper;
|
|||
import com.keylesspalace.tusky.view.MuteAccountDialog;
|
||||
import com.keylesspalace.tusky.viewdata.AttachmentViewData;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
||||
import kotlin.Unit;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
||||
import static autodispose2.AutoDispose.autoDisposable;
|
||||
import static autodispose2.androidx.lifecycle.AndroidLifecycleScopeProvider.from;
|
||||
|
|
@ -96,11 +86,6 @@ public abstract class SFragment extends Fragment implements Injectable {
|
|||
|
||||
private BottomSheetActivity bottomSheetActivity;
|
||||
|
||||
private static List<Filter> filters;
|
||||
private boolean filterRemoveRegex;
|
||||
private Matcher filterRemoveRegexMatcher;
|
||||
private static final Matcher alphanumeric = Pattern.compile("^\\w+$").matcher("");
|
||||
|
||||
@Inject
|
||||
public MastodonApi mastodonApi;
|
||||
@Inject
|
||||
|
|
@ -131,9 +116,8 @@ public abstract class SFragment extends Fragment implements Injectable {
|
|||
bottomSheetActivity.viewAccount(status.getAccount().getId());
|
||||
}
|
||||
|
||||
protected void viewThread(Status status) {
|
||||
Status actionableStatus = status.getActionableStatus();
|
||||
bottomSheetActivity.viewThread(actionableStatus.getId(), actionableStatus.getUrl());
|
||||
protected void viewThread(String statusId, @Nullable String statusUrl) {
|
||||
bottomSheetActivity.viewThread(statusId, statusUrl);
|
||||
}
|
||||
|
||||
protected void viewAccount(String accountId) {
|
||||
|
|
@ -149,7 +133,7 @@ public abstract class SFragment extends Fragment implements Injectable {
|
|||
Status actionableStatus = status.getActionableStatus();
|
||||
Status.Visibility replyVisibility = actionableStatus.getVisibility();
|
||||
String contentWarning = actionableStatus.getSpoilerText();
|
||||
Status.Mention[] mentions = actionableStatus.getMentions();
|
||||
List<Status.Mention> mentions = actionableStatus.getMentions();
|
||||
Set<String> mentionedUsernames = new LinkedHashSet<>();
|
||||
mentionedUsernames.add(actionableStatus.getAccount().getUsername());
|
||||
String loggedInUsername = null;
|
||||
|
|
@ -316,11 +300,11 @@ public abstract class SFragment extends Fragment implements Injectable {
|
|||
return true;
|
||||
}
|
||||
case R.id.pin: {
|
||||
timelineCases.pin(status, !status.isPinned());
|
||||
timelineCases.pin(status.getId(), !status.isPinned());
|
||||
return true;
|
||||
}
|
||||
case R.id.status_mute_conversation: {
|
||||
timelineCases.muteConversation(status, status.getMuted() == null || !status.getMuted())
|
||||
timelineCases.muteConversation(status.getId(), status.getMuted() == null || !status.getMuted())
|
||||
.onErrorReturnItem(status)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.to(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
|
||||
|
|
@ -335,12 +319,12 @@ public abstract class SFragment extends Fragment implements Injectable {
|
|||
|
||||
private void onMute(String accountId, String accountUsername) {
|
||||
MuteAccountDialog.showMuteAccountDialog(
|
||||
this.getActivity(),
|
||||
accountUsername,
|
||||
(notifications, duration) -> {
|
||||
timelineCases.mute(accountId, notifications, duration);
|
||||
return Unit.INSTANCE;
|
||||
}
|
||||
this.getActivity(),
|
||||
accountUsername,
|
||||
(notifications, duration) -> {
|
||||
timelineCases.mute(accountId, notifications, duration);
|
||||
return Unit.INSTANCE;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -352,7 +336,7 @@ public abstract class SFragment extends Fragment implements Injectable {
|
|||
.show();
|
||||
}
|
||||
|
||||
private static boolean accountIsInMentions(AccountEntity account, Status.Mention[] mentions) {
|
||||
private static boolean accountIsInMentions(AccountEntity account, List<Status.Mention> mentions) {
|
||||
if (account == null) {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -368,20 +352,18 @@ public abstract class SFragment extends Fragment implements Injectable {
|
|||
return false;
|
||||
}
|
||||
|
||||
protected void viewMedia(int urlIndex, Status status, @Nullable View view) {
|
||||
final Status actionable = status.getActionableStatus();
|
||||
final Attachment active = actionable.getAttachments().get(urlIndex);
|
||||
Attachment.Type type = active.getType();
|
||||
protected void viewMedia(int urlIndex, List<AttachmentViewData> attachments, @Nullable View view) {
|
||||
final AttachmentViewData active = attachments.get(urlIndex);
|
||||
Attachment.Type type = active.getAttachment().getType();
|
||||
switch (type) {
|
||||
case GIFV:
|
||||
case VIDEO:
|
||||
case IMAGE:
|
||||
case AUDIO: {
|
||||
final List<AttachmentViewData> attachments = AttachmentViewData.list(actionable);
|
||||
final Intent intent = ViewMediaActivity.newIntent(getContext(), attachments,
|
||||
urlIndex);
|
||||
if (view != null) {
|
||||
String url = active.getUrl();
|
||||
String url = active.getAttachment().getUrl();
|
||||
ViewCompat.setTransitionName(view, url);
|
||||
ActivityOptionsCompat options =
|
||||
ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(),
|
||||
|
|
@ -394,7 +376,7 @@ public abstract class SFragment extends Fragment implements Injectable {
|
|||
}
|
||||
default:
|
||||
case UNKNOWN: {
|
||||
LinkHelper.openLink(active.getUrl(), getContext());
|
||||
LinkHelper.openLink(active.getAttachment().getUrl(), getContext());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -510,83 +492,4 @@ public abstract class SFragment extends Fragment implements Injectable {
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
|
||||
public void reloadFilters(boolean forceRefresh) {
|
||||
if (filters != null && !forceRefresh) {
|
||||
applyFilters(forceRefresh);
|
||||
return;
|
||||
}
|
||||
|
||||
mastodonApi.getFilters().enqueue(new Callback<List<Filter>>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<List<Filter>> call, @NonNull Response<List<Filter>> response) {
|
||||
filters = response.body();
|
||||
if (response.isSuccessful() && filters != null) {
|
||||
applyFilters(forceRefresh);
|
||||
} else {
|
||||
Log.e(TAG, "Error getting filters from server");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(@NonNull Call<List<Filter>> call, @NonNull Throwable t) {
|
||||
Log.e(TAG, "Error getting filters from server", t);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected boolean filterIsRelevant(@NonNull Filter filter) {
|
||||
// Called when building local filter expression
|
||||
// Override to select relevant filters for your fragment
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void refreshAfterApplyingFilters() {
|
||||
// Called after filters are updated
|
||||
// Override to refresh your fragment
|
||||
}
|
||||
|
||||
@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
|
||||
public boolean shouldFilterStatus(Status status) {
|
||||
|
||||
if (filterRemoveRegex && status.getPoll() != null) {
|
||||
for (PollOption option : status.getPoll().getOptions()) {
|
||||
if (filterRemoveRegexMatcher.reset(option.getTitle()).find()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (filterRemoveRegex && (filterRemoveRegexMatcher.reset(status.getActionableStatus().getContent()).find()
|
||||
|| (!status.getSpoilerText().isEmpty() && filterRemoveRegexMatcher.reset(status.getActionableStatus().getSpoilerText()).find())));
|
||||
}
|
||||
|
||||
private void applyFilters(boolean refresh) {
|
||||
List<String> tokens = new ArrayList<>();
|
||||
for (Filter filter : filters) {
|
||||
if (filterIsRelevant(filter)) {
|
||||
tokens.add(filterToRegexToken(filter));
|
||||
}
|
||||
}
|
||||
filterRemoveRegex = !tokens.isEmpty();
|
||||
if (filterRemoveRegex) {
|
||||
filterRemoveRegexMatcher = Pattern.compile(TextUtils.join("|", tokens), Pattern.CASE_INSENSITIVE).matcher("");
|
||||
}
|
||||
if (refresh) {
|
||||
refreshAfterApplyingFilters();
|
||||
}
|
||||
}
|
||||
|
||||
private static String filterToRegexToken(Filter filter) {
|
||||
String phrase = filter.getPhrase();
|
||||
String quotedPhrase = Pattern.quote(phrase);
|
||||
return (filter.getWholeWord() && alphanumeric.reset(phrase).matches()) ? // "whole word" should only apply to alphanumeric filters, #1543
|
||||
String.format("(^|\\W)%s($|\\W)", quotedPhrase) :
|
||||
quotedPhrase;
|
||||
}
|
||||
|
||||
public static void flushFilters() {
|
||||
filters = null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -28,7 +28,6 @@ import android.view.ViewGroup;
|
|||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.arch.core.util.Function;
|
||||
import androidx.core.util.Pair;
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
import androidx.preference.PreferenceManager;
|
||||
import androidx.recyclerview.widget.DividerItemDecoration;
|
||||
|
|
@ -48,6 +47,7 @@ import com.keylesspalace.tusky.appstore.BlockEvent;
|
|||
import com.keylesspalace.tusky.appstore.BookmarkEvent;
|
||||
import com.keylesspalace.tusky.appstore.EventHub;
|
||||
import com.keylesspalace.tusky.appstore.FavoriteEvent;
|
||||
import com.keylesspalace.tusky.appstore.PinEvent;
|
||||
import com.keylesspalace.tusky.appstore.ReblogEvent;
|
||||
import com.keylesspalace.tusky.appstore.StatusComposedEvent;
|
||||
import com.keylesspalace.tusky.appstore.StatusDeletedEvent;
|
||||
|
|
@ -56,6 +56,7 @@ import com.keylesspalace.tusky.entity.Filter;
|
|||
import com.keylesspalace.tusky.entity.Poll;
|
||||
import com.keylesspalace.tusky.entity.Status;
|
||||
import com.keylesspalace.tusky.interfaces.StatusActionListener;
|
||||
import com.keylesspalace.tusky.network.FilterModel;
|
||||
import com.keylesspalace.tusky.network.MastodonApi;
|
||||
import com.keylesspalace.tusky.settings.PrefKeys;
|
||||
import com.keylesspalace.tusky.util.CardViewMode;
|
||||
|
|
@ -64,6 +65,7 @@ import com.keylesspalace.tusky.util.PairedList;
|
|||
import com.keylesspalace.tusky.util.StatusDisplayOptions;
|
||||
import com.keylesspalace.tusky.util.ViewDataUtils;
|
||||
import com.keylesspalace.tusky.view.ConversationLineItemDecoration;
|
||||
import com.keylesspalace.tusky.viewdata.AttachmentViewData;
|
||||
import com.keylesspalace.tusky.viewdata.StatusViewData;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
|
@ -73,7 +75,9 @@ import java.util.Locale;
|
|||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import autodispose2.androidx.lifecycle.AndroidLifecycleScopeProvider;
|
||||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
||||
import kotlin.collections.CollectionsKt;
|
||||
|
||||
import static autodispose2.AutoDispose.autoDisposable;
|
||||
import static autodispose2.androidx.lifecycle.AndroidLifecycleScopeProvider.from;
|
||||
|
|
@ -86,6 +90,8 @@ public final class ViewThreadFragment extends SFragment implements
|
|||
public MastodonApi mastodonApi;
|
||||
@Inject
|
||||
public EventHub eventHub;
|
||||
@Inject
|
||||
public FilterModel filterModel;
|
||||
|
||||
private SwipeRefreshLayout swipeRefreshLayout;
|
||||
private RecyclerView recyclerView;
|
||||
|
|
@ -163,7 +169,7 @@ public final class ViewThreadFragment extends SFragment implements
|
|||
recyclerView.addItemDecoration(new ConversationLineItemDecoration(context));
|
||||
alwaysShowSensitiveMedia = accountManager.getActiveAccount().getAlwaysShowSensitiveMedia();
|
||||
alwaysOpenSpoiler = accountManager.getActiveAccount().getAlwaysOpenSpoiler();
|
||||
reloadFilters(false);
|
||||
reloadFilters();
|
||||
|
||||
recyclerView.setAdapter(adapter);
|
||||
|
||||
|
|
@ -190,6 +196,8 @@ public final class ViewThreadFragment extends SFragment implements
|
|||
handleReblogEvent((ReblogEvent) event);
|
||||
} else if (event instanceof BookmarkEvent) {
|
||||
handleBookmarkEvent((BookmarkEvent) event);
|
||||
} else if (event instanceof PinEvent) {
|
||||
handlePinEvent(((PinEvent) event));
|
||||
} else if (event instanceof BlockEvent) {
|
||||
removeAllByAccountId(((BlockEvent) event).getAccountId());
|
||||
} else if (event instanceof StatusComposedEvent) {
|
||||
|
|
@ -203,13 +211,8 @@ public final class ViewThreadFragment extends SFragment implements
|
|||
public void onRevealPressed() {
|
||||
boolean allExpanded = allExpanded();
|
||||
for (int i = 0; i < statuses.size(); i++) {
|
||||
StatusViewData.Concrete newViewData =
|
||||
new StatusViewData.Concrete.Builder(statuses.getPairedItem(i))
|
||||
.setIsExpanded(!allExpanded)
|
||||
.createStatusViewData();
|
||||
statuses.setPairedItem(i, newViewData);
|
||||
updateViewData(i, statuses.getPairedItem(i).copyWithExpanded(!allExpanded));
|
||||
}
|
||||
adapter.setStatuses(statuses.getPairedCopy());
|
||||
updateRevealIcon();
|
||||
}
|
||||
|
||||
|
|
@ -239,11 +242,11 @@ public final class ViewThreadFragment extends SFragment implements
|
|||
public void onReblog(final boolean reblog, final int position) {
|
||||
final Status status = statuses.get(position);
|
||||
|
||||
timelineCases.reblog(statuses.get(position), reblog)
|
||||
timelineCases.reblog(statuses.get(position).getId(), reblog)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.to(autoDisposable(from(this)))
|
||||
.subscribe(
|
||||
(newStatus) -> updateStatus(position, newStatus),
|
||||
this::replaceStatus,
|
||||
(t) -> Log.d(TAG,
|
||||
"Failed to reblog status: " + status.getId(), t)
|
||||
);
|
||||
|
|
@ -253,11 +256,11 @@ public final class ViewThreadFragment extends SFragment implements
|
|||
public void onFavourite(final boolean favourite, final int position) {
|
||||
final Status status = statuses.get(position);
|
||||
|
||||
timelineCases.favourite(statuses.get(position), favourite)
|
||||
timelineCases.favourite(statuses.get(position).getId(), favourite)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.to(autoDisposable(from(this)))
|
||||
.subscribe(
|
||||
(newStatus) -> updateStatus(position, newStatus),
|
||||
this::replaceStatus,
|
||||
(t) -> Log.d(TAG,
|
||||
"Failed to favourite status: " + status.getId(), t)
|
||||
);
|
||||
|
|
@ -267,32 +270,29 @@ public final class ViewThreadFragment extends SFragment implements
|
|||
public void onBookmark(final boolean bookmark, final int position) {
|
||||
final Status status = statuses.get(position);
|
||||
|
||||
timelineCases.bookmark(statuses.get(position), bookmark)
|
||||
timelineCases.bookmark(statuses.get(position).getId(), bookmark)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.to(autoDisposable(from(this)))
|
||||
.subscribe(
|
||||
(newStatus) -> updateStatus(position, newStatus),
|
||||
this::replaceStatus,
|
||||
(t) -> Log.d(TAG,
|
||||
"Failed to bookmark status: " + status.getId(), t)
|
||||
);
|
||||
}
|
||||
|
||||
private void updateStatus(int position, Status status) {
|
||||
private void replaceStatus(Status status) {
|
||||
updateStatus(status.getId(), (__) -> status);
|
||||
}
|
||||
|
||||
private void updateStatus(String statusId, Function<Status, Status> mapper) {
|
||||
int position = indexOfStatus(statusId);
|
||||
|
||||
if (position >= 0 && position < statuses.size()) {
|
||||
|
||||
Status actionableStatus = status.getActionableStatus();
|
||||
|
||||
StatusViewData.Concrete viewData = new StatusViewData.Builder(statuses.getPairedItem(position))
|
||||
.setReblogged(actionableStatus.getReblogged())
|
||||
.setReblogsCount(actionableStatus.getReblogsCount())
|
||||
.setFavourited(actionableStatus.getFavourited())
|
||||
.setBookmarked(actionableStatus.getBookmarked())
|
||||
.setFavouritesCount(actionableStatus.getFavouritesCount())
|
||||
.createStatusViewData();
|
||||
statuses.setPairedItem(position, viewData);
|
||||
|
||||
adapter.setItem(position, viewData, true);
|
||||
|
||||
Status oldStatus = statuses.get(position);
|
||||
Status newStatus = mapper.apply(oldStatus);
|
||||
StatusViewData.Concrete oldViewData = statuses.getPairedItem(position);
|
||||
statuses.set(position, newStatus);
|
||||
updateViewData(position, oldViewData.copyWithStatus(newStatus));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -304,7 +304,7 @@ public final class ViewThreadFragment extends SFragment implements
|
|||
@Override
|
||||
public void onViewMedia(int position, int attachmentIndex, @NonNull View view) {
|
||||
Status status = statuses.get(position);
|
||||
super.viewMedia(attachmentIndex, status, view);
|
||||
super.viewMedia(attachmentIndex, AttachmentViewData.list(status), view);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -314,7 +314,7 @@ public final class ViewThreadFragment extends SFragment implements
|
|||
// If already viewing this thread, don't reopen it.
|
||||
return;
|
||||
}
|
||||
super.viewThread(status);
|
||||
super.viewThread(status.getActionableId(), status.getActionableStatus().getUrl());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -325,21 +325,22 @@ public final class ViewThreadFragment extends SFragment implements
|
|||
|
||||
@Override
|
||||
public void onExpandedChange(boolean expanded, int position) {
|
||||
StatusViewData.Concrete newViewData =
|
||||
new StatusViewData.Builder(statuses.getPairedItem(position))
|
||||
.setIsExpanded(expanded)
|
||||
.createStatusViewData();
|
||||
statuses.setPairedItem(position, newViewData);
|
||||
adapter.setItem(position, newViewData, true);
|
||||
updateViewData(
|
||||
position,
|
||||
statuses.getPairedItem(position).copyWithExpanded(expanded)
|
||||
);
|
||||
updateRevealIcon();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onContentHiddenChange(boolean isShowing, int position) {
|
||||
StatusViewData.Concrete newViewData =
|
||||
new StatusViewData.Builder(statuses.getPairedItem(position))
|
||||
.setIsShowingSensitiveContent(isShowing)
|
||||
.createStatusViewData();
|
||||
updateViewData(
|
||||
position,
|
||||
statuses.getPairedItem(position).copyWithShowingContent(isShowing)
|
||||
);
|
||||
}
|
||||
|
||||
private void updateViewData(int position, StatusViewData.Concrete newViewData) {
|
||||
statuses.setPairedItem(position, newViewData);
|
||||
adapter.setItem(position, newViewData, true);
|
||||
}
|
||||
|
|
@ -365,28 +366,11 @@ public final class ViewThreadFragment extends SFragment implements
|
|||
|
||||
@Override
|
||||
public void onContentCollapsedChange(boolean isCollapsed, int position) {
|
||||
if (position < 0 || position >= statuses.size()) {
|
||||
Log.e(TAG, String.format("Tried to access out of bounds status position: %d of %d", position, statuses.size() - 1));
|
||||
return;
|
||||
}
|
||||
|
||||
StatusViewData.Concrete status = statuses.getPairedItem(position);
|
||||
if (status == null) {
|
||||
// Statuses PairedList contains a base type of StatusViewData.Concrete and also doesn't
|
||||
// check for null values when adding values to it although this doesn't seem to be an issue.
|
||||
Log.e(TAG, String.format(
|
||||
"Expected StatusViewData.Concrete, got null instead at position: %d of %d",
|
||||
position,
|
||||
statuses.size() - 1
|
||||
));
|
||||
return;
|
||||
}
|
||||
|
||||
StatusViewData.Concrete updatedStatus = new StatusViewData.Builder(status)
|
||||
.setCollapsed(isCollapsed)
|
||||
.createStatusViewData();
|
||||
statuses.setPairedItem(position, updatedStatus);
|
||||
recyclerView.post(() -> adapter.setItem(position, updatedStatus, true));
|
||||
adapter.setItem(
|
||||
position,
|
||||
statuses.getPairedItem(position).copyWIthCollapsed(isCollapsed),
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -412,28 +396,21 @@ public final class ViewThreadFragment extends SFragment implements
|
|||
public void onVoteInPoll(int position, @NonNull List<Integer> choices) {
|
||||
final Status status = statuses.get(position).getActionableStatus();
|
||||
|
||||
setVoteForPoll(position, status.getPoll().votedCopy(choices));
|
||||
setVoteForPoll(status.getId(), status.getPoll().votedCopy(choices));
|
||||
|
||||
timelineCases.voteInPoll(status, choices)
|
||||
timelineCases.voteInPoll(status.getId(), status.getPoll().getId(), choices)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.to(autoDisposable(from(this)))
|
||||
.subscribe(
|
||||
(newPoll) -> setVoteForPoll(position, newPoll),
|
||||
(newPoll) -> setVoteForPoll(status.getId(), newPoll),
|
||||
(t) -> Log.d(TAG,
|
||||
"Failed to vote in poll: " + status.getId(), t)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
private void setVoteForPoll(int position, Poll newPoll) {
|
||||
|
||||
StatusViewData.Concrete viewData = statuses.getPairedItem(position);
|
||||
|
||||
StatusViewData.Concrete newViewData = new StatusViewData.Builder(viewData)
|
||||
.setPoll(newPoll)
|
||||
.createStatusViewData();
|
||||
statuses.setPairedItem(position, newViewData);
|
||||
adapter.setItem(position, newViewData, true);
|
||||
private void setVoteForPoll(String statusId, Poll newPoll) {
|
||||
updateStatus(statusId, s -> s.copyWithPoll(newPoll));
|
||||
}
|
||||
|
||||
private void removeAllByAccountId(String accountId) {
|
||||
|
|
@ -530,7 +507,7 @@ public final class ViewThreadFragment extends SFragment implements
|
|||
|
||||
ArrayList<Status> ancestors = new ArrayList<>();
|
||||
for (Status status : unfilteredAncestors)
|
||||
if (!shouldFilterStatus(status))
|
||||
if (!filterModel.shouldFilterStatus(status))
|
||||
ancestors.add(status);
|
||||
|
||||
// Insert newly fetched ancestors
|
||||
|
|
@ -560,7 +537,7 @@ public final class ViewThreadFragment extends SFragment implements
|
|||
|
||||
ArrayList<Status> descendants = new ArrayList<>();
|
||||
for (Status status : unfilteredDescendants)
|
||||
if (!shouldFilterStatus(status))
|
||||
if (!filterModel.shouldFilterStatus(status))
|
||||
descendants.add(status);
|
||||
|
||||
// Insert newly fetched descendants
|
||||
|
|
@ -581,71 +558,31 @@ public final class ViewThreadFragment extends SFragment implements
|
|||
}
|
||||
|
||||
private void handleFavEvent(FavoriteEvent event) {
|
||||
Pair<Integer, Status> posAndStatus = findStatusAndPos(event.getStatusId());
|
||||
if (posAndStatus == null) return;
|
||||
|
||||
boolean favourite = event.getFavourite();
|
||||
posAndStatus.second.setFavourited(favourite);
|
||||
|
||||
if (posAndStatus.second.getReblog() != null) {
|
||||
posAndStatus.second.getReblog().setFavourited(favourite);
|
||||
}
|
||||
|
||||
StatusViewData.Concrete viewdata = statuses.getPairedItem(posAndStatus.first);
|
||||
|
||||
StatusViewData.Builder viewDataBuilder = new StatusViewData.Builder((viewdata));
|
||||
viewDataBuilder.setFavourited(favourite);
|
||||
|
||||
StatusViewData.Concrete newViewData = viewDataBuilder.createStatusViewData();
|
||||
|
||||
statuses.setPairedItem(posAndStatus.first, newViewData);
|
||||
adapter.setItem(posAndStatus.first, newViewData, true);
|
||||
updateStatus(event.getStatusId(), (s) -> {
|
||||
s.setFavourited(event.getFavourite());
|
||||
return s;
|
||||
});
|
||||
}
|
||||
|
||||
private void handleReblogEvent(ReblogEvent event) {
|
||||
Pair<Integer, Status> posAndStatus = findStatusAndPos(event.getStatusId());
|
||||
if (posAndStatus == null) return;
|
||||
|
||||
boolean reblog = event.getReblog();
|
||||
posAndStatus.second.setReblogged(reblog);
|
||||
|
||||
if (posAndStatus.second.getReblog() != null) {
|
||||
posAndStatus.second.getReblog().setReblogged(reblog);
|
||||
}
|
||||
|
||||
StatusViewData.Concrete viewdata = statuses.getPairedItem(posAndStatus.first);
|
||||
|
||||
StatusViewData.Builder viewDataBuilder = new StatusViewData.Builder((viewdata));
|
||||
viewDataBuilder.setReblogged(reblog);
|
||||
|
||||
StatusViewData.Concrete newViewData = viewDataBuilder.createStatusViewData();
|
||||
|
||||
statuses.setPairedItem(posAndStatus.first, newViewData);
|
||||
adapter.setItem(posAndStatus.first, newViewData, true);
|
||||
updateStatus(event.getStatusId(), (s) -> {
|
||||
s.setReblogged(event.getReblog());
|
||||
return s;
|
||||
});
|
||||
}
|
||||
|
||||
private void handleBookmarkEvent(BookmarkEvent event) {
|
||||
Pair<Integer, Status> posAndStatus = findStatusAndPos(event.getStatusId());
|
||||
if (posAndStatus == null) return;
|
||||
|
||||
boolean bookmark = event.getBookmark();
|
||||
posAndStatus.second.setBookmarked(bookmark);
|
||||
|
||||
if (posAndStatus.second.getReblog() != null) {
|
||||
posAndStatus.second.getReblog().setBookmarked(bookmark);
|
||||
}
|
||||
|
||||
StatusViewData.Concrete viewdata = statuses.getPairedItem(posAndStatus.first);
|
||||
|
||||
StatusViewData.Builder viewDataBuilder = new StatusViewData.Builder((viewdata));
|
||||
viewDataBuilder.setBookmarked(bookmark);
|
||||
|
||||
StatusViewData.Concrete newViewData = viewDataBuilder.createStatusViewData();
|
||||
|
||||
statuses.setPairedItem(posAndStatus.first, newViewData);
|
||||
adapter.setItem(posAndStatus.first, newViewData, true);
|
||||
updateStatus(event.getStatusId(), (s) -> {
|
||||
s.setBookmarked(event.getBookmark());
|
||||
return s;
|
||||
});
|
||||
}
|
||||
|
||||
private void handlePinEvent(PinEvent event) {
|
||||
updateStatus(event.getStatusId(), (s) -> s.copyWithPinned(event.getPinned()));
|
||||
}
|
||||
|
||||
|
||||
private void handleStatusComposedEvent(StatusComposedEvent event) {
|
||||
Status eventStatus = event.getStatus();
|
||||
if (eventStatus.getInReplyToId() == null) return;
|
||||
|
|
@ -671,23 +608,16 @@ public final class ViewThreadFragment extends SFragment implements
|
|||
}
|
||||
|
||||
private void handleStatusDeletedEvent(StatusDeletedEvent event) {
|
||||
Pair<Integer, Status> posAndStatus = findStatusAndPos(event.getStatusId());
|
||||
if (posAndStatus == null) return;
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
int pos = posAndStatus.first;
|
||||
statuses.remove(pos);
|
||||
adapter.removeItem(pos);
|
||||
int index = this.indexOfStatus(event.getStatusId());
|
||||
if (index != -1) {
|
||||
statuses.remove(index);
|
||||
adapter.removeItem(index);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Pair<Integer, Status> findStatusAndPos(@NonNull String statusId) {
|
||||
for (int i = 0; i < statuses.size(); i++) {
|
||||
if (statusId.equals(statuses.get(i).getId())) {
|
||||
return new Pair<>(i, statuses.get(i));
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
private int indexOfStatus(String statusId) {
|
||||
return CollectionsKt.indexOfFirst(this.statuses, (s) -> s.getId().equals(statusId));
|
||||
}
|
||||
|
||||
private void updateRevealIcon() {
|
||||
|
|
@ -710,13 +640,25 @@ public final class ViewThreadFragment extends SFragment implements
|
|||
ViewThreadActivity.REVEAL_BUTTON_REVEAL);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean filterIsRelevant(@NonNull Filter filter) {
|
||||
return filter.getContext().contains(Filter.THREAD);
|
||||
private void reloadFilters() {
|
||||
mastodonApi.getFilters()
|
||||
.to(autoDisposable(AndroidLifecycleScopeProvider.from(this)))
|
||||
.subscribe(
|
||||
(filters) -> {
|
||||
List<Filter> relevantFilters = CollectionsKt.filter(
|
||||
filters,
|
||||
(f) -> f.getContext().contains(Filter.THREAD)
|
||||
);
|
||||
filterModel.initWithFilters(relevantFilters);
|
||||
|
||||
recyclerView.post(this::applyFilters);
|
||||
},
|
||||
(t) -> Log.e(TAG, "Failed to load filters", t)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void refreshAfterApplyingFilters() {
|
||||
onRefresh();
|
||||
private void applyFilters() {
|
||||
CollectionsKt.removeAll(this.statuses, filterModel::shouldFilterStatus);
|
||||
adapter.setStatuses(this.statuses.getPairedCopy());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue