#3468 Switch from RvRenderer to AdapterDelegates - replace NotificationRenderer

This commit is contained in:
Sean Mac Gillicuddy 2020-05-13 12:13:31 +01:00
parent 1c50591887
commit 8a39fddde8
6 changed files with 54 additions and 176 deletions

View file

@ -0,0 +1,9 @@
package fr.free.nrw.commons.notification
import fr.free.nrw.commons.upload.categories.BaseDelegateAdapter
internal class NotificatinAdapter(onNotificationClicked: (Notification) -> Unit) :
BaseDelegateAdapter<Notification>(
notificationDelegate(onNotificationClicked),
areItemsTheSame = { oldItem, newItem -> oldItem.notificationId == newItem.notificationId }
)

View file

@ -11,16 +11,6 @@ data class Notification(var notificationType: NotificationType,
var link: String,
var iconUrl: String,
var notificationId: String) {
override fun toString(): String {
return "Notification" +
"notificationType='" + notificationType + '\'' +
", notificationText='" + notificationText + '\'' +
", date='" + date + '\'' +
", link='" + link + '\'' +
", iconUrl='" + iconUrl + '\'' +
", notificationId='" + notificationId + '\'' +
'}'
}
companion object {
@JvmStatic

View file

@ -13,23 +13,13 @@ import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.snackbar.Snackbar;
import com.pedrogomez.renderers.RVRendererAdapter;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import javax.inject.Inject;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.google.android.material.snackbar.Snackbar;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.theme.NavigationBaseActivity;
@ -40,6 +30,11 @@ import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import javax.inject.Inject;
import kotlin.Unit;
import timber.log.Timber;
/**
@ -47,7 +42,6 @@ import timber.log.Timber;
*/
public class NotificationActivity extends NavigationBaseActivity {
NotificationAdapterFactory notificationAdapterFactory;
@BindView(R.id.listView)
RecyclerView recyclerView;
@BindView(R.id.progressBar)
@ -64,7 +58,7 @@ public class NotificationActivity extends NavigationBaseActivity {
private static final String TAG_NOTIFICATION_WORKER_FRAGMENT = "NotificationWorkerFragment";
private NotificationWorkerFragment mNotificationWorkerFragment;
private RVRendererAdapter<Notification> adapter;
private NotificatinAdapter adapter;
private List<Notification> notificationList;
MenuItem notificationMenuItem;
@ -89,7 +83,7 @@ public class NotificationActivity extends NavigationBaseActivity {
.subscribe(result -> {
if (result) {
notificationList.remove(notification);
setAdapter(notificationList);
setItems(notificationList);
adapter.notifyDataSetChanged();
Snackbar snackbar = Snackbar
.make(relativeLayout, getString(R.string.notification_mark_read), Snackbar.LENGTH_LONG);
@ -102,7 +96,7 @@ public class NotificationActivity extends NavigationBaseActivity {
}
} else {
adapter.notifyDataSetChanged();
setAdapter(notificationList);
setItems(notificationList);
Toast.makeText(NotificationActivity.this, getString(R.string.some_error), Toast.LENGTH_SHORT).show();
}
}, throwable -> {
@ -126,6 +120,13 @@ public class NotificationActivity extends NavigationBaseActivity {
} else {
refresh(false);
}
adapter = new NotificatinAdapter(item -> {
Timber.d("Notification clicked %s", item.getLink());
handleUrl(item.getLink());
removeNotification(item);
return Unit.INSTANCE;
});
recyclerView.setAdapter(this.adapter);
}
private void refresh(boolean archived) {
@ -158,7 +159,7 @@ public class NotificationActivity extends NavigationBaseActivity {
relativeLayout.setVisibility(View.GONE);
no_notification.setVisibility(View.VISIBLE);
} else {
setAdapter(notificationList);
setItems(notificationList);
}
progressBar.setVisibility(View.GONE);
}, throwable -> {
@ -168,7 +169,7 @@ public class NotificationActivity extends NavigationBaseActivity {
}));
} else {
notificationList = mNotificationWorkerFragment.getNotificationList();
setAdapter(notificationList);
setItems(notificationList);
}
}
@ -204,7 +205,7 @@ public class NotificationActivity extends NavigationBaseActivity {
Utils.handleWebUrl(this, Uri.parse(url));
}
private void setAdapter(List<Notification> notificationList) {
private void setItems(List<Notification> notificationList) {
if (notificationList == null || notificationList.isEmpty()) {
ViewUtil.showShortSnackbar(relativeLayout, R.string.no_notifications);
/*progressBar.setVisibility(View.GONE);
@ -214,32 +215,9 @@ public class NotificationActivity extends NavigationBaseActivity {
no_notification.setVisibility(View.VISIBLE);
return;
}
boolean isarchivedvisible;
if (getIntent().getStringExtra("title").equals("read")) {
isarchivedvisible = true;
} else {
isarchivedvisible = false;
}
notificationAdapterFactory = new NotificationAdapterFactory(new NotificationRenderer.NotificationClicked() {
@Override
public void notificationClicked(Notification notification) {
Timber.d("Notification clicked %s", notification.getLink());
handleUrl(notification.getLink());
removeNotification(notification);
}
@Override
public void markNotificationAsRead(Notification notification) {
Timber.d("Notification to mark as read %s", notification.getNotificationId());
removeNotification(notification);
}
}, isarchivedvisible);
adapter = notificationAdapterFactory.create(notificationList);
relativeLayout.setVisibility(View.VISIBLE);
no_notification.setVisibility(View.GONE);
recyclerView.setAdapter(adapter);
adapter.setItems(notificationList);
}
public static void startYourself(Context context, String title) {

View file

@ -0,0 +1,24 @@
package fr.free.nrw.commons.notification
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateLayoutContainer
import fr.free.nrw.commons.R
import kotlinx.android.synthetic.main.activity_login.title
import kotlinx.android.synthetic.main.item_notification.*
import org.wikipedia.util.StringUtil
fun notificationDelegate(onNotificationClicked: (Notification) -> Unit) =
adapterDelegateLayoutContainer<Notification, Notification>(R.layout.item_notification) {
containerView.setOnClickListener { onNotificationClicked(item) }
bind {
title.text = item.processedNotificationText
time.text = item.date
}
}
private val Notification.processedNotificationText: CharSequence
get() = notificationText.trim()
.replace("(^\\s*)|(\\s*$)".toRegex(), "")
.let { StringUtil.fromHtml(it).toString() }
.let { if (it.length > 280) "${it.substring(0, 279)}..." else it } + " "

View file

@ -1,32 +0,0 @@
package fr.free.nrw.commons.notification;
import androidx.annotation.NonNull;
import com.pedrogomez.renderers.ListAdapteeCollection;
import com.pedrogomez.renderers.RVRendererAdapter;
import com.pedrogomez.renderers.RendererBuilder;
import java.util.Collections;
import java.util.List;
/**
* Created by root on 19.12.2017.
*/
class NotificationAdapterFactory {
private NotificationRenderer.NotificationClicked listener;
private boolean isarchivedvisible = false;
NotificationAdapterFactory(@NonNull NotificationRenderer.NotificationClicked listener, boolean isarchivedvisible) {
this.listener = listener;
this.isarchivedvisible = isarchivedvisible;
}
public RVRendererAdapter<Notification> create(List<Notification> notifications) {
RendererBuilder<Notification> builder = new RendererBuilder<Notification>()
.bind(Notification.class, new NotificationRenderer(listener, isarchivedvisible));
ListAdapteeCollection<Notification> collection = new ListAdapteeCollection<>(
notifications != null ? notifications : Collections.<Notification>emptyList());
return new RVRendererAdapter<>(builder, collection);
}
}

View file

@ -1,91 +0,0 @@
package fr.free.nrw.commons.notification;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.pedrogomez.renderers.Renderer;
import org.wikipedia.util.StringUtil;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import fr.free.nrw.commons.R;
/**
* Created by root on 19.12.2017.
*/
public class NotificationRenderer extends Renderer<Notification> {
@BindView(R.id.title)
TextView title;
@BindView(R.id.time)
TextView time;
@BindView(R.id.icon)
ImageView icon;
/*@BindView(R.id.bottom)
LinearLayout bottomLayout;*/
private NotificationClicked listener;
private boolean isarchivedvisible = false;
NotificationRenderer(NotificationClicked listener, boolean isarchivedvisible) {
this.listener = listener;
this.isarchivedvisible = isarchivedvisible;
}
@OnClick(R.id.notification_view)
void onNotificationClicked() {
listener.notificationClicked(getContent());
}
@Override
protected void setUpView(View rootView) {
}
@Override
protected void hookListeners(View rootView) {
}
@Override
protected View inflate(LayoutInflater layoutInflater, ViewGroup viewGroup) {
View inflatedView = layoutInflater.inflate(R.layout.item_notification, viewGroup, false);
ButterKnife.bind(this, inflatedView);
return inflatedView;
}
@Override
public void render() {
Notification notification = getContent();
setTitle(notification.getNotificationText());
time.setText(notification.getDate());
}
/**
* Cleans up the notification text and sets it as the title
* Clean up is required to fix escaped HTML string and extra white spaces at the beginning of the notification
*
* @param notificationText
*/
private void setTitle(String notificationText) {
notificationText = notificationText.trim().replaceAll("(^\\s*)|(\\s*$)", "");
notificationText = StringUtil.fromHtml(notificationText).toString();
if (notificationText.length() > 280) {
notificationText = notificationText.substring(0, 279);
notificationText = notificationText.concat("...");
}
notificationText = notificationText.concat(" ");
title.setText(notificationText);
}
public interface NotificationClicked {
void notificationClicked(Notification notification);
void markNotificationAsRead(Notification notification);
}
}