Skip to content

Commit f34af1e

Browse files
author
maskara
committed
With support for bundled notifications
1 parent 59abd02 commit f34af1e

File tree

4 files changed

+101
-18
lines changed

4 files changed

+101
-18
lines changed

app/src/main/java/fr/free/nrw/commons/mwapi/ApacheHttpClientMediaWikiApi.java

+4-14
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import fr.free.nrw.commons.BuildConfig;
4242
import fr.free.nrw.commons.PageTitle;
4343
import fr.free.nrw.commons.notification.Notification;
44+
import fr.free.nrw.commons.notification.NotificationUtils;
4445
import in.yuvi.http.fluent.Http;
4546
import io.reactivex.Observable;
4647
import io.reactivex.Single;
@@ -50,6 +51,8 @@
5051
import static fr.free.nrw.commons.notification.NotificationType.UNKNOWN;
5152
import static fr.free.nrw.commons.notification.NotificationUtils.getNotificationFromApiResult;
5253
import static fr.free.nrw.commons.notification.NotificationUtils.getNotificationType;
54+
import static fr.free.nrw.commons.notification.NotificationUtils.getNotificationsFromBundle;
55+
import static fr.free.nrw.commons.notification.NotificationUtils.isBundledNotification;
5356
import static fr.free.nrw.commons.notification.NotificationUtils.isCommonsNotification;
5457

5558
/**
@@ -447,23 +450,10 @@ public List<Notification> getNotifications() {
447450
return new ArrayList<>();
448451
}
449452

450-
List<Notification> notifications = new ArrayList<>();
451-
452453
NodeList childNodes = notificationNode.getDocument().getChildNodes();
453-
454-
for (int i = 0; i < childNodes.getLength(); i++) {
455-
Node node = childNodes.item(i);
456-
if (isCommonsNotification(node)
457-
&& !getNotificationType(node).equals(UNKNOWN)
458-
&& !getNotificationType(node).equals(THANK_YOU_EDIT)) {
459-
notifications.add(getNotificationFromApiResult(context, node));
460-
}
461-
}
462-
463-
return notifications;
454+
return NotificationUtils.getNotificationsFromList(context, childNodes);
464455
}
465456

466-
467457
@Override
468458
public boolean existingFile(String fileSha1) throws IOException {
469459
return api.action("query")

app/src/main/java/fr/free/nrw/commons/notification/NotificationActivity.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
import android.support.v7.widget.DividerItemDecoration;
1010
import android.support.v7.widget.LinearLayoutManager;
1111
import android.support.v7.widget.RecyclerView;
12+
import android.view.View;
13+
import android.widget.ProgressBar;
1214

1315
import com.pedrogomez.renderers.RVRendererAdapter;
1416

17+
import java.util.Collections;
1518
import java.util.List;
1619

1720
import javax.inject.Inject;
@@ -34,6 +37,8 @@ public class NotificationActivity extends NavigationBaseActivity {
3437
NotificationAdapterFactory notificationAdapterFactory;
3538

3639
@BindView(R.id.listView) RecyclerView recyclerView;
40+
@BindView(R.id.progressBar)
41+
ProgressBar progressBar;
3742

3843
@Inject NotificationController controller;
3944

@@ -63,14 +68,21 @@ private void addNotifications() {
6368
Timber.d("Add notifications");
6469

6570
if(mNotificationWorkerFragment == null){
66-
Observable.fromCallable(() -> controller.getNotifications())
71+
Observable.fromCallable(() -> {
72+
progressBar.setVisibility(View.VISIBLE);
73+
return controller.getNotifications();
74+
})
6775
.subscribeOn(Schedulers.io())
6876
.observeOn(AndroidSchedulers.mainThread())
6977
.subscribe(notificationList -> {
78+
Collections.reverse(notificationList);
7079
Timber.d("Number of notifications is %d", notificationList.size());
71-
initializeAndSetNotificationList(notificationList);
7280
setAdapter(notificationList);
73-
}, throwable -> Timber.e(throwable, "Error occurred while loading notifications"));
81+
progressBar.setVisibility(View.GONE);
82+
}, throwable -> {
83+
Timber.e(throwable, "Error occurred while loading notifications");
84+
progressBar.setVisibility(View.GONE);
85+
});
7486
} else {
7587
setAdapter(mNotificationWorkerFragment.getNotificationList());
7688
}

app/src/main/java/fr/free/nrw/commons/notification/NotificationUtils.java

+76-1
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@
22

33
import android.annotation.SuppressLint;
44
import android.content.Context;
5+
import android.support.annotation.NonNull;
56

67
import org.w3c.dom.Element;
78
import org.w3c.dom.Node;
89
import org.w3c.dom.NodeList;
910

11+
import java.util.ArrayList;
12+
import java.util.List;
13+
1014
import javax.annotation.Nullable;
1115

1216
import fr.free.nrw.commons.BuildConfig;
1317
import fr.free.nrw.commons.R;
1418

19+
import static fr.free.nrw.commons.notification.NotificationType.THANK_YOU_EDIT;
20+
import static fr.free.nrw.commons.notification.NotificationType.UNKNOWN;
21+
1522
public class NotificationUtils {
1623

1724
private static final String COMMONS_WIKI = "commonswiki";
@@ -30,6 +37,56 @@ public static NotificationType getNotificationType(Node document) {
3037
return NotificationType.handledValueOf(type);
3138
}
3239

40+
public static List<Notification> getNotificationsFromBundle(Context context, Node document) {
41+
Element bundledNotifications = getBundledNotifications(document);
42+
NodeList childNodes = bundledNotifications.getChildNodes();
43+
44+
List<Notification> notifications = new ArrayList<>();
45+
for (int i = 0; i < childNodes.getLength(); i++) {
46+
Node node = childNodes.item(i);
47+
if (isUsefulNotification(node)) {
48+
notifications.add(getNotificationFromApiResult(context, node));
49+
}
50+
}
51+
return notifications;
52+
}
53+
54+
@NonNull
55+
public static List<Notification> getNotificationsFromList(Context context, NodeList childNodes) {
56+
List<Notification> notifications = new ArrayList<>();
57+
for (int i = 0; i < childNodes.getLength(); i++) {
58+
Node node = childNodes.item(i);
59+
if (isUsefulNotification(node)) {
60+
if (isBundledNotification(node)) {
61+
notifications.addAll(getNotificationsFromBundle(context, node));
62+
} else {
63+
notifications.add(getNotificationFromApiResult(context, node));
64+
}
65+
}
66+
}
67+
68+
return notifications;
69+
}
70+
71+
private static boolean isUsefulNotification(Node node) {
72+
return isCommonsNotification(node)
73+
&& !getNotificationType(node).equals(UNKNOWN)
74+
&& !getNotificationType(node).equals(THANK_YOU_EDIT);
75+
}
76+
77+
public static boolean isBundledNotification(Node document) {
78+
Element bundleElement = getBundledNotifications(document);
79+
if (bundleElement == null) {
80+
return false;
81+
}
82+
83+
return bundleElement.getChildNodes().getLength() > 0;
84+
}
85+
86+
private static Element getBundledNotifications(Node document) {
87+
return (Element) getNode(document, "bundledNotifications");
88+
}
89+
3390
public static Notification getNotificationFromApiResult(Context context, Node document) {
3491
NotificationType type = getNotificationType(document);
3592

@@ -43,7 +100,7 @@ public static Notification getNotificationFromApiResult(Context context, Node do
43100
notificationText = context.getString(R.string.notifications_thank_you_edit);
44101
break;
45102
case EDIT_USER_TALK:
46-
notificationText = getNotificationHeader(document);
103+
notificationText = getNotificationText(document);
47104
break;
48105
case MENTION:
49106
notificationText = getMentionMessage(context, document);
@@ -56,6 +113,14 @@ public static Notification getNotificationFromApiResult(Context context, Node do
56113
return new Notification(type, notificationText, getTimestamp(document), description, link, iconUrl);
57114
}
58115

116+
private static String getNotificationText(Node document) {
117+
String notificationBody = getNotificationBody(document);
118+
if (notificationBody == null || notificationBody.trim().equals("")) {
119+
return getNotificationHeader(document);
120+
}
121+
return notificationBody;
122+
}
123+
59124
private static String getNotificationHeader(Node document) {
60125
Node body = getNode(getModel(document), "header");
61126
if (body != null) {
@@ -66,6 +131,16 @@ private static String getNotificationHeader(Node document) {
66131
}
67132
}
68133

134+
private static String getNotificationBody(Node document) {
135+
Node body = getNode(getModel(document), "body");
136+
if (body != null) {
137+
String textContent = body.getTextContent();
138+
return textContent.replace("<strong>", "").replace("</strong>", "");
139+
} else {
140+
return "";
141+
}
142+
}
143+
69144
private static String getMentionDescription(Node document) {
70145
Node body = getNode(getModel(document), "body");
71146
return body != null ? body.getTextContent() : "";

app/src/main/res/layout/activity_notification.xml

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
android:layout_width="match_parent"
1616
android:layout_height="wrap_content" />
1717

18+
<ProgressBar
19+
android:id="@+id/progressBar"
20+
android:layout_width="match_parent"
21+
android:layout_centerHorizontal="true"
22+
android:layout_centerVertical="true"
23+
android:layout_height="wrap_content" />
1824
<android.support.v7.widget.RecyclerView
1925
android:id="@+id/listView"
2026
android:layout_width="match_parent"

0 commit comments

Comments
 (0)