Skip to content

Commit a9629c6

Browse files
cypheropmaskaravivek
authored andcommitted
Added option in the action bar menu to view archived notifications (commons-app#2422)
* changed layout and implemented archived notificaitons feature * set different texts for toolbar,menu option and no notification text in archived * modified the startup intent for NotificationsActivity * disabled swipe on archived * commit * fixed navigation drawer on notification activity * handled on back pressed * updated strings.xml * removed TODO * some minor changes * set progress bar visibility * some code quality changes * commit * some code quality changes * removing unused import statements
1 parent cb3a570 commit a9629c6

File tree

10 files changed

+178
-72
lines changed

10 files changed

+178
-72
lines changed

app/src/main/java/fr/free/nrw/commons/contributions/MainActivity.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
292292
final View notification = notificationsMenuItem.getActionView();
293293
notificationCount = notification.findViewById(R.id.notification_count_badge);
294294
notification.setOnClickListener(view -> {
295-
NotificationActivity.startYourself(MainActivity.this);
295+
NotificationActivity.startYourself(MainActivity.this, "unread");
296296
});
297297
this.menu = menu;
298298
updateMenuItem();
@@ -302,7 +302,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
302302

303303
@SuppressLint("CheckResult")
304304
private void setNotificationCount() {
305-
Observable.fromCallable(() -> notificationController.getNotifications())
305+
Observable.fromCallable(() -> notificationController.getNotifications(false))
306306
.subscribeOn(Schedulers.io())
307307
.observeOn(AndroidSchedulers.mainThread())
308308
.subscribe(this::initNotificationViews,
@@ -344,7 +344,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
344344
switch (item.getItemId()) {
345345
case R.id.notifications:
346346
// Starts notification activity on click to notification icon
347-
NotificationActivity.startYourself(this);
347+
NotificationActivity.startYourself(this, "unread");
348348
return true;
349349
case R.id.list_sheet:
350350
if (contributionsActivityPagerAdapter.getItem(1) != null) {

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ public String edit(String editToken, String processedPageContent, String filenam
273273
}
274274

275275

276+
276277
@Override
277278
@Nullable
278279
public String appendEdit(String editToken, String processedPageContent, String filename, String summary) throws IOException {
@@ -557,16 +558,22 @@ public String revisionsByFilename(String filename) throws IOException {
557558

558559
@Override
559560
@NonNull
560-
public List<Notification> getNotifications() {
561+
public List<Notification> getNotifications(boolean archived) {
561562
CustomApiResult notificationNode = null;
563+
String notfilter;
562564
try {
565+
if (archived) {
566+
notfilter = "read";
567+
}else {
568+
notfilter = "!read";
569+
}
563570
notificationNode = api.action("query")
564571
.param("notprop", "list")
565572
.param("format", "xml")
566573
.param("meta", "notifications")
567574
.param("notformat", "model")
568575
.param("notwikis", "wikidatawiki|commonswiki|enwiki")
569-
.param("notfilter","!read")
576+
.param("notfilter", notfilter)
570577
.get()
571578
.getNode("/api/query/notifications/list");
572579
} catch (IOException e) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public interface MediaWikiApi {
8181
Observable<String> allCategories(String filter, int searchCatsLimit);
8282

8383
@NonNull
84-
List<Notification> getNotifications() throws IOException;
84+
List<Notification> getNotifications(boolean archived) throws IOException;
8585

8686
@NonNull
8787
boolean markNotificationAsRead(Notification notification) throws IOException;

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

Lines changed: 95 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@
55
import android.content.Intent;
66
import android.net.Uri;
77
import android.os.Bundle;
8-
import android.support.constraint.ConstraintLayout;
98
import android.support.design.widget.Snackbar;
109
import android.support.v7.widget.DividerItemDecoration;
1110
import android.support.v7.widget.LinearLayoutManager;
1211
import android.support.v7.widget.RecyclerView;
12+
import android.view.Menu;
13+
import android.view.MenuInflater;
14+
import android.view.MenuItem;
1315
import android.view.View;
1416
import android.widget.ProgressBar;
1517
import android.widget.RelativeLayout;
18+
import android.widget.TextView;
1619
import android.widget.Toast;
20+
import android.support.v7.widget.Toolbar;
1721

1822
import com.pedrogomez.renderers.RVRendererAdapter;
1923

@@ -47,7 +51,9 @@ public class NotificationActivity extends NavigationBaseActivity {
4751
@BindView(R.id.container)
4852
RelativeLayout relativeLayout;
4953
@BindView(R.id.no_notification_background)
50-
ConstraintLayout no_notification;
54+
RelativeLayout no_notification;
55+
@BindView(R.id.toolbar)
56+
Toolbar toolbar;
5157
/* @BindView(R.id.swipe_bg)
5258
TextView swipe_bg;*/
5359
@Inject
@@ -57,6 +63,8 @@ public class NotificationActivity extends NavigationBaseActivity {
5763
private NotificationWorkerFragment mNotificationWorkerFragment;
5864
private RVRendererAdapter<Notification> adapter;
5965
private List<Notification> notificationList;
66+
MenuItem notificationmenuitem;
67+
TextView nonotificationtext;
6068

6169
@Override
6270
protected void onCreate(Bundle savedInstanceState) {
@@ -67,6 +75,8 @@ protected void onCreate(Bundle savedInstanceState) {
6775
.findFragmentByTag(TAG_NOTIFICATION_WORKER_FRAGMENT);
6876
initListView();
6977
initDrawer();
78+
nonotificationtext = (TextView)this.findViewById(R.id.no_notification_text);
79+
setPageTitle();
7080
}
7181

7282
@SuppressLint("CheckResult")
@@ -84,6 +94,7 @@ public void removeNotification(Notification notification) {
8494

8595
snackbar.show();
8696
if (notificationList.size()==0){
97+
setEmptyView();
8798
relativeLayout.setVisibility(View.GONE);
8899
no_notification.setVisibility(View.VISIBLE);
89100
}
@@ -108,27 +119,34 @@ private void initListView() {
108119
recyclerView.setLayoutManager(new LinearLayoutManager(this));
109120
DividerItemDecoration itemDecor = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
110121
recyclerView.addItemDecoration(itemDecor);
111-
refresh();
122+
if (getIntent().getStringExtra("title").equals("read")) {
123+
refresh(true);
124+
} else {
125+
refresh(false);
126+
}
112127
}
113128

114-
private void refresh() {
129+
private void refresh(boolean archived) {
115130
if (!NetworkUtils.isInternetConnectionEstablished(this)) {
116131
progressBar.setVisibility(View.GONE);
117132
Snackbar.make(relativeLayout, R.string.no_internet, Snackbar.LENGTH_INDEFINITE)
118-
.setAction(R.string.retry, view -> refresh()).show();
133+
.setAction(R.string.retry, view -> refresh(archived)).show();
119134
} else {
120-
progressBar.setVisibility(View.VISIBLE);
121-
addNotifications();
135+
addNotifications(archived);
122136
}
137+
progressBar.setVisibility(View.VISIBLE);
138+
no_notification.setVisibility(View.GONE);
139+
relativeLayout.setVisibility(View.VISIBLE);
123140
}
124141

125142
@SuppressLint("CheckResult")
126-
private void addNotifications() {
143+
private void addNotifications(boolean archived) {
127144
Timber.d("Add notifications");
128145
if (mNotificationWorkerFragment == null) {
129146
Observable.fromCallable(() -> {
130147
progressBar.setVisibility(View.VISIBLE);
131-
return controller.getNotifications();
148+
return controller.getNotifications(archived);
149+
132150
})
133151
.subscribeOn(Schedulers.io())
134152
.observeOn(AndroidSchedulers.mainThread())
@@ -137,10 +155,12 @@ private void addNotifications() {
137155
Timber.d("Number of notifications is %d", notificationList.size());
138156
this.notificationList = notificationList;
139157
if (notificationList.size()==0){
158+
setEmptyView();
140159
relativeLayout.setVisibility(View.GONE);
141160
no_notification.setVisibility(View.VISIBLE);
142161
} else {
143162
setAdapter(notificationList);
163+
} if (notificationmenuitem != null) {
144164
}
145165
progressBar.setVisibility(View.GONE);
146166
}, throwable -> {
@@ -154,6 +174,31 @@ private void addNotifications() {
154174
}
155175
}
156176

177+
@Override
178+
public boolean onCreateOptionsMenu(Menu menu) {
179+
MenuInflater inflater = getMenuInflater();
180+
inflater.inflate(R.menu.menu_notifications, menu);
181+
notificationmenuitem = menu.findItem(R.id.archived);
182+
setMenuItemTitle();
183+
return true;
184+
}
185+
186+
@Override
187+
public boolean onOptionsItemSelected(MenuItem item) {
188+
// Handle item selection
189+
switch (item.getItemId()) {
190+
case R.id.archived:
191+
if (item.getTitle().equals(getString(R.string.menu_option_archived))) {
192+
NotificationActivity.startYourself(NotificationActivity.this, "read");
193+
}else if (item.getTitle().equals(getString(R.string.menu_option_unread))) {
194+
onBackPressed();
195+
}
196+
return true;
197+
default:
198+
return super.onOptionsItemSelected(item);
199+
}
200+
}
201+
157202
private void handleUrl(String url) {
158203
if (url == null || url.equals("")) {
159204
return;
@@ -167,9 +212,18 @@ private void setAdapter(List<Notification> notificationList) {
167212
/*progressBar.setVisibility(View.GONE);
168213
recyclerView.setVisibility(View.GONE);*/
169214
relativeLayout.setVisibility(View.GONE);
215+
setEmptyView();
170216
no_notification.setVisibility(View.VISIBLE);
171217
return;
172218
}
219+
220+
boolean isarchivedvisible;
221+
if (getIntent().getStringExtra("title").equals("read")) {
222+
isarchivedvisible = true;
223+
} else {
224+
isarchivedvisible = false;
225+
}
226+
173227
notificationAdapterFactory = new NotificationAdapterFactory(new NotificationRenderer.NotificationClicked() {
174228
@Override
175229
public void notificationClicked(Notification notification) {
@@ -182,16 +236,45 @@ public void markNotificationAsRead(Notification notification) {
182236
Timber.d("Notification to mark as read %s", notification.notificationId);
183237
removeNotification(notification);
184238
}
185-
});
239+
}, isarchivedvisible);
186240
adapter = notificationAdapterFactory.create(notificationList);
187241
relativeLayout.setVisibility(View.VISIBLE);
188242
no_notification.setVisibility(View.GONE);
189243
recyclerView.setAdapter(adapter);
190244
}
191245

192-
public static void startYourself(Context context) {
246+
public static void startYourself(Context context, String title) {
193247
Intent intent = new Intent(context, NotificationActivity.class);
194-
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
248+
intent.putExtra("title", title);
249+
195250
context.startActivity(intent);
196251
}
252+
253+
private void setPageTitle() {
254+
if (getSupportActionBar() != null) {
255+
if (getIntent().getStringExtra("title").equals("read")) {
256+
getSupportActionBar().setTitle(R.string.archived_notifications);
257+
} else {
258+
getSupportActionBar().setTitle(R.string.notifications);
259+
}
260+
}
261+
}
262+
263+
private void setEmptyView() {
264+
if (getIntent().getStringExtra("title").equals("read")) {
265+
nonotificationtext.setText(R.string.no_archived_notification);
266+
}else {
267+
nonotificationtext.setText(R.string.no_notification);
268+
}
269+
}
270+
271+
private void setMenuItemTitle() {
272+
if (getIntent().getStringExtra("title").equals("read")) {
273+
notificationmenuitem.setTitle(R.string.menu_option_unread);
274+
275+
}else {
276+
notificationmenuitem.setTitle(R.string.menu_option_archived);
277+
278+
}
279+
}
197280
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515

1616
class NotificationAdapterFactory {
1717
private NotificationRenderer.NotificationClicked listener;
18+
private boolean isarchivedvisible = false;
1819

19-
NotificationAdapterFactory(@NonNull NotificationRenderer.NotificationClicked listener) {
20+
NotificationAdapterFactory(@NonNull NotificationRenderer.NotificationClicked listener, boolean isarchivedvisible) {
2021
this.listener = listener;
22+
this.isarchivedvisible = isarchivedvisible;
2123
}
2224

2325
public RVRendererAdapter<Notification> create(List<Notification> notifications) {
2426
RendererBuilder<Notification> builder = new RendererBuilder<Notification>()
25-
.bind(Notification.class, new NotificationRenderer(listener));
27+
.bind(Notification.class, new NotificationRenderer(listener, isarchivedvisible));
2628
ListAdapteeCollection<Notification> collection = new ListAdapteeCollection<>(
2729
notifications != null ? notifications : Collections.<Notification>emptyList());
2830
return new RVRendererAdapter<>(builder, collection);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ public NotificationController(MediaWikiApi mediaWikiApi, SessionManager sessionM
2525
this.sessionManager = sessionManager;
2626
}
2727

28-
public List<Notification> getNotifications() throws IOException {
28+
public List<Notification> getNotifications(boolean archived) throws IOException {
2929
if (mediaWikiApi.validateLogin()) {
30-
return mediaWikiApi.getNotifications();
30+
return mediaWikiApi.getNotifications(archived);
3131
} else {
3232
Boolean authTokenValidated = sessionManager.revalidateAuthToken();
3333
if (authTokenValidated != null && authTokenValidated) {
34-
return mediaWikiApi.getNotifications();
34+
return mediaWikiApi.getNotifications(archived);
3535
}
3636
}
3737
return new ArrayList<>();

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ public class NotificationRenderer extends Renderer<Notification> {
3535
LinearLayout bottomLayout;
3636

3737
private NotificationClicked listener;
38+
private boolean isarchivedvisible = false;
3839

3940

40-
NotificationRenderer(NotificationClicked listener) {
41+
NotificationRenderer(NotificationClicked listener, boolean isarchivedvisible) {
4142
this.listener = listener;
43+
this.isarchivedvisible = isarchivedvisible;
4244
}
4345
@OnClick(R.id.bottom)
4446
void onBottomLayoutClicked(){
@@ -59,7 +61,11 @@ protected void hookListeners(View rootView) {
5961
protected View inflate(LayoutInflater layoutInflater, ViewGroup viewGroup) {
6062
View inflatedView = layoutInflater.inflate(R.layout.item_notification, viewGroup, false);
6163
ButterKnife.bind(this, inflatedView);
62-
64+
if (isarchivedvisible) {
65+
swipeLayout.setSwipeEnabled(false);
66+
}else {
67+
swipeLayout.setSwipeEnabled(true);
68+
}
6369
swipeLayout.addDrag(SwipeLayout.DragEdge.Top, bottomLayout);
6470
swipeLayout.addRevealListener(R.id.bottom_wrapper_child1, (child, edge, fraction, distance) -> {
6571
View star = child.findViewById(R.id.star);

0 commit comments

Comments
 (0)