Add support for "update" SSE event.

This commit is contained in:
charlag 2019-05-26 17:53:01 +02:00
parent 3b09b065c9
commit bc812a8f6c
3 changed files with 61 additions and 29 deletions

View File

@ -4,8 +4,10 @@ import android.content.Context
import android.util.Log import android.util.Log
import com.google.gson.Gson import com.google.gson.Gson
import com.keylesspalace.tusky.appstore.EventHub import com.keylesspalace.tusky.appstore.EventHub
import com.keylesspalace.tusky.appstore.NewHomeTimelineStatusEvent
import com.keylesspalace.tusky.appstore.NewNotificationEvent import com.keylesspalace.tusky.appstore.NewNotificationEvent
import com.keylesspalace.tusky.entity.Notification import com.keylesspalace.tusky.entity.Notification
import com.keylesspalace.tusky.entity.Status
import com.keylesspalace.tusky.network.MastodonApi import com.keylesspalace.tusky.network.MastodonApi
import com.keylesspalace.tusky.util.NotificationHelper import com.keylesspalace.tusky.util.NotificationHelper
import io.reactivex.Flowable import io.reactivex.Flowable
@ -37,21 +39,39 @@ class ProfileStreamListener @Inject constructor(
// "event: notification" // "event: notification"
// Then notification will be on the next line as: // Then notification will be on the next line as:
// "data: {...}" // "data: {...}"
var hadNotification = false var lastEventType: String? = null
linesSequence.forEach { line -> linesSequence.forEach { line ->
Log.d(TAG, "new line: $line")
failedAttempts = 0 failedAttempts = 0
val parts = line.split(':', limit = 2) val parts = line.split(": ", limit = 2)
if (parts.size > 1 && parts[0] == "data" && hadNotification) { if (parts.size < 2) {
val account = accountManager.activeAccount return@forEach
if (account != null) { }
val notification = gson.fromJson(parts[1], Notification::class.java) when (parts[0]) {
NotificationHelper.make(context, notification, account, true) "event" -> lastEventType = parts[1]
account.lastNotificationId = notification.id "data" -> when (lastEventType) {
accountManager.saveAccount(account) null -> {
eventHub.dispatch(NewNotificationEvent(notification)) }
"notification" -> {
Log.d(TAG, "new notification")
val account = accountManager.activeAccount
if (account != null) {
val notification = gson.fromJson(parts[1], Notification::class.java)
NotificationHelper.make(context, notification, account, true)
account.lastNotificationId = notification.id
accountManager.saveAccount(account)
eventHub.dispatch(NewNotificationEvent(notification))
}
}
"update" -> {
Log.d(TAG, "new update")
accountManager.activeAccount?.let { account ->
val status = gson.fromJson(parts[1], Status::class.java)
eventHub.dispatch(NewHomeTimelineStatusEvent(status))
}
}
} }
} }
hadNotification = line.startsWith("event: notification")
} }
} }
} }

View File

@ -19,3 +19,4 @@ data class MainTabsChangedEvent(val newTabs: List<TabData>) : Dispatchable
data class PollVoteEvent(val statusId: String, val poll: Poll) : Dispatchable data class PollVoteEvent(val statusId: String, val poll: Poll) : Dispatchable
data class DomainMuteEvent(val instance: String): Dispatchable data class DomainMuteEvent(val instance: String): Dispatchable
data class NewNotificationEvent(val notification: Notification) : Dispatchable data class NewNotificationEvent(val notification: Notification) : Dispatchable
data class NewHomeTimelineStatusEvent(val status: Status) : Dispatchable

View File

@ -26,6 +26,22 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.arch.core.util.Function;
import androidx.core.util.Pair;
import androidx.core.widget.ContentLoadingProgressBar;
import androidx.lifecycle.Lifecycle;
import androidx.recyclerview.widget.AsyncDifferConfig;
import androidx.recyclerview.widget.AsyncListDiffer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.keylesspalace.tusky.AccountListActivity; import com.keylesspalace.tusky.AccountListActivity;
import com.keylesspalace.tusky.BaseActivity; import com.keylesspalace.tusky.BaseActivity;
@ -37,6 +53,7 @@ import com.keylesspalace.tusky.appstore.DomainMuteEvent;
import com.keylesspalace.tusky.appstore.EventHub; import com.keylesspalace.tusky.appstore.EventHub;
import com.keylesspalace.tusky.appstore.FavoriteEvent; import com.keylesspalace.tusky.appstore.FavoriteEvent;
import com.keylesspalace.tusky.appstore.MuteEvent; import com.keylesspalace.tusky.appstore.MuteEvent;
import com.keylesspalace.tusky.appstore.NewHomeTimelineStatusEvent;
import com.keylesspalace.tusky.appstore.PreferenceChangedEvent; import com.keylesspalace.tusky.appstore.PreferenceChangedEvent;
import com.keylesspalace.tusky.appstore.ReblogEvent; import com.keylesspalace.tusky.appstore.ReblogEvent;
import com.keylesspalace.tusky.appstore.StatusComposedEvent; import com.keylesspalace.tusky.appstore.StatusComposedEvent;
@ -78,22 +95,6 @@ import java.util.concurrent.TimeUnit;
import javax.inject.Inject; import javax.inject.Inject;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.arch.core.util.Function;
import androidx.core.util.Pair;
import androidx.core.widget.ContentLoadingProgressBar;
import androidx.lifecycle.Lifecycle;
import androidx.recyclerview.widget.AsyncDifferConfig;
import androidx.recyclerview.widget.AsyncListDiffer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import at.connyduck.sparkbutton.helpers.Utils; import at.connyduck.sparkbutton.helpers.Utils;
import io.reactivex.Observable; import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
@ -119,6 +120,7 @@ public class TimelineFragment extends SFragment implements
private static final int LOAD_AT_ONCE = 30; private static final int LOAD_AT_ONCE = 30;
private boolean isSwipeToRefreshEnabled = true; private boolean isSwipeToRefreshEnabled = true;
private boolean isNeedRefresh; private boolean isNeedRefresh;
private Status newStatusEventStatus;
public enum Kind { public enum Kind {
HOME, HOME,
@ -523,6 +525,10 @@ public class TimelineFragment extends SFragment implements
handleStatusComposeEvent(status); handleStatusComposeEvent(status);
} else if (event instanceof PreferenceChangedEvent) { } else if (event instanceof PreferenceChangedEvent) {
onPreferenceChanged(((PreferenceChangedEvent) event).getPreferenceKey()); onPreferenceChanged(((PreferenceChangedEvent) event).getPreferenceKey());
} else if (event instanceof NewHomeTimelineStatusEvent && kind == Kind.HOME) {
this.newStatusEventStatus = ((NewHomeTimelineStatusEvent) event).getStatus();
this.statuses.add(0, new Either.Right<>(this.newStatusEventStatus));
this.updateAdapter();
} }
}); });
eventRegistered = true; eventRegistered = true;
@ -1296,7 +1302,12 @@ public class TimelineFragment extends SFragment implements
if (isAdded()) { if (isAdded()) {
adapter.notifyItemRangeInserted(position, count); adapter.notifyItemRangeInserted(position, count);
Context context = getContext(); Context context = getContext();
if (position == 0 && context != null) { if (
count >
0 && position == 0
&& context != null
&& statuses.get(0).asRightOrNull() != newStatusEventStatus
) {
if (isSwipeToRefreshEnabled) if (isSwipeToRefreshEnabled)
recyclerView.scrollBy(0, Utils.dpToPx(context, -30)); recyclerView.scrollBy(0, Utils.dpToPx(context, -30));
else else