Update Notification Adapter to support status created timestamp refresh (#1130)
* Update Notification Adapter to support status created timestamp refresh * Format the code
This commit is contained in:
parent
2de0a690ae
commit
c3df44eaef
|
@ -124,12 +124,24 @@ public class NotificationsAdapter extends RecyclerView.Adapter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
|
||||||
|
bindViewHolder(viewHolder,position,null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position, @NonNull List payloads) {
|
||||||
|
bindViewHolder(viewHolder,position,payloads);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void bindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position, @Nullable List payloads){
|
||||||
|
Object payloadForHolder = payloads!=null&&!payloads.isEmpty()?payloads.get(0):null;
|
||||||
if (position < this.dataSource.getItemCount()) {
|
if (position < this.dataSource.getItemCount()) {
|
||||||
NotificationViewData notification = dataSource.getItemAt(position);
|
NotificationViewData notification = dataSource.getItemAt(position);
|
||||||
if (notification instanceof NotificationViewData.Placeholder) {
|
if (notification instanceof NotificationViewData.Placeholder) {
|
||||||
|
if (payloadForHolder==null) {
|
||||||
NotificationViewData.Placeholder placeholder = ((NotificationViewData.Placeholder) notification);
|
NotificationViewData.Placeholder placeholder = ((NotificationViewData.Placeholder) notification);
|
||||||
PlaceholderViewHolder holder = (PlaceholderViewHolder) viewHolder;
|
PlaceholderViewHolder holder = (PlaceholderViewHolder) viewHolder;
|
||||||
holder.setup(statusListener, placeholder.isLoading());
|
holder.setup(statusListener, placeholder.isLoading());
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
NotificationViewData.Concrete concreteNotificaton =
|
NotificationViewData.Concrete concreteNotificaton =
|
||||||
|
@ -141,14 +153,14 @@ public class NotificationsAdapter extends RecyclerView.Adapter {
|
||||||
StatusViewHolder holder = (StatusViewHolder) viewHolder;
|
StatusViewHolder holder = (StatusViewHolder) viewHolder;
|
||||||
StatusViewData.Concrete status = concreteNotificaton.getStatusViewData();
|
StatusViewData.Concrete status = concreteNotificaton.getStatusViewData();
|
||||||
holder.setupWithStatus(status,
|
holder.setupWithStatus(status,
|
||||||
statusListener, mediaPreviewEnabled);
|
statusListener, mediaPreviewEnabled,payloadForHolder);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case FAVOURITE:
|
case FAVOURITE:
|
||||||
case REBLOG: {
|
case REBLOG: {
|
||||||
StatusNotificationViewHolder holder = (StatusNotificationViewHolder) viewHolder;
|
StatusNotificationViewHolder holder = (StatusNotificationViewHolder) viewHolder;
|
||||||
StatusViewData.Concrete statusViewData = concreteNotificaton.getStatusViewData();
|
StatusViewData.Concrete statusViewData = concreteNotificaton.getStatusViewData();
|
||||||
|
if (payloadForHolder==null) {
|
||||||
if (statusViewData == null) {
|
if (statusViewData == null) {
|
||||||
holder.showNotificationContent(false);
|
holder.showNotificationContent(false);
|
||||||
} else {
|
} else {
|
||||||
|
@ -166,18 +178,29 @@ public class NotificationsAdapter extends RecyclerView.Adapter {
|
||||||
holder.setupButtons(notificationActionListener,
|
holder.setupButtons(notificationActionListener,
|
||||||
concreteNotificaton.getAccount().getId(),
|
concreteNotificaton.getAccount().getId(),
|
||||||
concreteNotificaton.getId());
|
concreteNotificaton.getId());
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
if (payloadForHolder instanceof List)
|
||||||
|
for (Object item:payloads) {
|
||||||
|
if (StatusBaseViewHolder.Key.KEY_CREATED.equals(item)){
|
||||||
|
holder.setCreatedAt(statusViewData.getCreatedAt());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case FOLLOW: {
|
case FOLLOW: {
|
||||||
|
if (payloadForHolder==null) {
|
||||||
FollowViewHolder holder = (FollowViewHolder) viewHolder;
|
FollowViewHolder holder = (FollowViewHolder) viewHolder;
|
||||||
holder.setMessage(concreteNotificaton.getAccount(), bidiFormatter);
|
holder.setMessage(concreteNotificaton.getAccount(), bidiFormatter);
|
||||||
holder.setupButtons(notificationActionListener, concreteNotificaton.getAccount().getId());
|
holder.setupButtons(notificationActionListener, concreteNotificaton.getAccount().getId());
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@Override
|
@Override
|
||||||
public int getItemCount() {
|
public int getItemCount() {
|
||||||
return dataSource.getItemCount();
|
return dataSource.getItemCount();
|
||||||
|
|
|
@ -47,6 +47,7 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder {
|
||||||
public static class Key {
|
public static class Key {
|
||||||
public static final String KEY_CREATED = "created";
|
public static final String KEY_CREATED = "created";
|
||||||
}
|
}
|
||||||
|
|
||||||
private TextView displayName;
|
private TextView displayName;
|
||||||
private TextView username;
|
private TextView username;
|
||||||
private ImageButton replyButton;
|
private ImageButton replyButton;
|
||||||
|
@ -542,6 +543,7 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder {
|
||||||
boolean mediaPreviewEnabled) {
|
boolean mediaPreviewEnabled) {
|
||||||
this.setupWithStatus(status, listener, mediaPreviewEnabled, null);
|
this.setupWithStatus(status, listener, mediaPreviewEnabled, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setupWithStatus(StatusViewData.Concrete status, final StatusActionListener listener,
|
protected void setupWithStatus(StatusViewData.Concrete status, final StatusActionListener listener,
|
||||||
boolean mediaPreviewEnabled, @Nullable Object payloads) {
|
boolean mediaPreviewEnabled, @Nullable Object payloads) {
|
||||||
if (payloads == null) {
|
if (payloads == null) {
|
||||||
|
@ -584,8 +586,7 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder {
|
||||||
// fetches another one from its delegate because it checks that it's set so we remove it
|
// fetches another one from its delegate because it checks that it's set so we remove it
|
||||||
// and let RecyclerView ask for a new delegate.
|
// and let RecyclerView ask for a new delegate.
|
||||||
itemView.setAccessibilityDelegate(null);
|
itemView.setAccessibilityDelegate(null);
|
||||||
}
|
} else {
|
||||||
else{
|
|
||||||
if (payloads instanceof List)
|
if (payloads instanceof List)
|
||||||
for (Object item : (List) payloads) {
|
for (Object item : (List) payloads) {
|
||||||
if (Key.KEY_CREATED.equals(item)) {
|
if (Key.KEY_CREATED.equals(item)) {
|
||||||
|
|
|
@ -31,6 +31,7 @@ import com.google.android.material.tabs.TabLayout;
|
||||||
import com.keylesspalace.tusky.MainActivity;
|
import com.keylesspalace.tusky.MainActivity;
|
||||||
import com.keylesspalace.tusky.R;
|
import com.keylesspalace.tusky.R;
|
||||||
import com.keylesspalace.tusky.adapter.NotificationsAdapter;
|
import com.keylesspalace.tusky.adapter.NotificationsAdapter;
|
||||||
|
import com.keylesspalace.tusky.adapter.StatusBaseViewHolder;
|
||||||
import com.keylesspalace.tusky.appstore.BlockEvent;
|
import com.keylesspalace.tusky.appstore.BlockEvent;
|
||||||
import com.keylesspalace.tusky.appstore.EventHub;
|
import com.keylesspalace.tusky.appstore.EventHub;
|
||||||
import com.keylesspalace.tusky.appstore.FavoriteEvent;
|
import com.keylesspalace.tusky.appstore.FavoriteEvent;
|
||||||
|
@ -57,9 +58,11 @@ import com.keylesspalace.tusky.viewdata.NotificationViewData;
|
||||||
import com.keylesspalace.tusky.viewdata.StatusViewData;
|
import com.keylesspalace.tusky.viewdata.StatusViewData;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
@ -78,6 +81,7 @@ import androidx.recyclerview.widget.RecyclerView;
|
||||||
import androidx.recyclerview.widget.SimpleItemAnimator;
|
import androidx.recyclerview.widget.SimpleItemAnimator;
|
||||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
||||||
import at.connyduck.sparkbutton.helpers.Utils;
|
import at.connyduck.sparkbutton.helpers.Utils;
|
||||||
|
import io.reactivex.Observable;
|
||||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||||
import kotlin.Unit;
|
import kotlin.Unit;
|
||||||
import kotlin.collections.CollectionsKt;
|
import kotlin.collections.CollectionsKt;
|
||||||
|
@ -929,7 +933,45 @@ public class NotificationsFragment extends SFragment implements
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean areContentsTheSame(NotificationViewData oldItem, NotificationViewData newItem) {
|
public boolean areContentsTheSame(NotificationViewData oldItem, NotificationViewData newItem) {
|
||||||
return oldItem.deepEquals(newItem);
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public Object getChangePayload(@NonNull NotificationViewData oldItem, @NonNull NotificationViewData newItem) {
|
||||||
|
if (oldItem.deepEquals(newItem)) {
|
||||||
|
//If items are equal - update timestamp only
|
||||||
|
List<String> payload = new ArrayList<>();
|
||||||
|
payload.add(StatusBaseViewHolder.Key.KEY_CREATED);
|
||||||
|
return payload;
|
||||||
|
} else
|
||||||
|
// If items are different - update a whole view holder
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
startUpdateTimestamp();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start to update adapter every minute to refresh timestamp
|
||||||
|
* If setting absoluteTimeView is false
|
||||||
|
* Auto dispose observable on pause
|
||||||
|
*/
|
||||||
|
private void startUpdateTimestamp() {
|
||||||
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
||||||
|
boolean useAbsoluteTime = preferences.getBoolean("absoluteTimeView", false);
|
||||||
|
if (!useAbsoluteTime) {
|
||||||
|
Observable.interval(1, TimeUnit.MINUTES)
|
||||||
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
|
.as(autoDisposable(from(this, Lifecycle.Event.ON_PAUSE)))
|
||||||
|
.subscribe(
|
||||||
|
interval -> updateAdapter()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue