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 com.google.gson.Gson
import com.keylesspalace.tusky.appstore.EventHub
import com.keylesspalace.tusky.appstore.NewHomeTimelineStatusEvent
import com.keylesspalace.tusky.appstore.NewNotificationEvent
import com.keylesspalace.tusky.entity.Notification
import com.keylesspalace.tusky.entity.Status
import com.keylesspalace.tusky.network.MastodonApi
import com.keylesspalace.tusky.util.NotificationHelper
import io.reactivex.Flowable
@ -37,21 +39,39 @@ class ProfileStreamListener @Inject constructor(
// "event: notification"
// Then notification will be on the next line as:
// "data: {...}"
var hadNotification = false
var lastEventType: String? = null
linesSequence.forEach { line ->
Log.d(TAG, "new line: $line")
failedAttempts = 0
val parts = line.split(':', limit = 2)
if (parts.size > 1 && parts[0] == "data" && hadNotification) {
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))
val parts = line.split(": ", limit = 2)
if (parts.size < 2) {
return@forEach
}
when (parts[0]) {
"event" -> lastEventType = parts[1]
"data" -> when (lastEventType) {
null -> {
}
"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

@ -18,4 +18,5 @@ data class PreferenceChangedEvent(val preferenceKey: String) : Dispatchable
data class MainTabsChangedEvent(val newTabs: List<TabData>) : Dispatchable
data class PollVoteEvent(val statusId: String, val poll: Poll) : 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.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.keylesspalace.tusky.AccountListActivity;
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.FavoriteEvent;
import com.keylesspalace.tusky.appstore.MuteEvent;
import com.keylesspalace.tusky.appstore.NewHomeTimelineStatusEvent;
import com.keylesspalace.tusky.appstore.PreferenceChangedEvent;
import com.keylesspalace.tusky.appstore.ReblogEvent;
import com.keylesspalace.tusky.appstore.StatusComposedEvent;
@ -78,22 +95,6 @@ import java.util.concurrent.TimeUnit;
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 io.reactivex.Observable;
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 boolean isSwipeToRefreshEnabled = true;
private boolean isNeedRefresh;
private Status newStatusEventStatus;
public enum Kind {
HOME,
@ -523,6 +525,10 @@ public class TimelineFragment extends SFragment implements
handleStatusComposeEvent(status);
} else if (event instanceof PreferenceChangedEvent) {
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;
@ -1296,7 +1302,12 @@ public class TimelineFragment extends SFragment implements
if (isAdded()) {
adapter.notifyItemRangeInserted(position, count);
Context context = getContext();
if (position == 0 && context != null) {
if (
count >
0 && position == 0
&& context != null
&& statuses.get(0).asRightOrNull() != newStatusEventStatus
) {
if (isSwipeToRefreshEnabled)
recyclerView.scrollBy(0, Utils.dpToPx(context, -30));
else