Skip to content

Commit 80cc8b7

Browse files
Merge pull request commons-app#1202 from knight-shade/Fixes-#1198
Fixes commons-app#1198 Now notifications list retained
2 parents e412c9a + ff54d03 commit 80cc8b7

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

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

+26-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fr.free.nrw.commons.notification;
22

33
import android.annotation.SuppressLint;
4+
import android.app.FragmentManager;
45
import android.content.Context;
56
import android.content.Intent;
67
import android.net.Uri;
@@ -12,7 +13,6 @@
1213

1314
import com.pedrogomez.renderers.RVRendererAdapter;
1415

15-
import java.util.Collections;
1616
import java.util.List;
1717

1818
import javax.inject.Inject;
@@ -39,12 +39,16 @@ public class NotificationActivity extends NavigationBaseActivity {
3939

4040
@Inject NotificationController controller;
4141

42+
private static final String TAG_NOTIFICATION_WORKER_FRAGMENT = "NotificationWorkerFragment";
43+
private NotificationWorkerFragment mNotificationWorkerFragment;
4244

4345
@Override
4446
protected void onCreate(Bundle savedInstanceState) {
4547
super.onCreate(savedInstanceState);
4648
setContentView(R.layout.activity_notification);
4749
ButterKnife.bind(this);
50+
mNotificationWorkerFragment = (NotificationWorkerFragment) getFragmentManager()
51+
.findFragmentByTag(TAG_NOTIFICATION_WORKER_FRAGMENT);
4852
initListView();
4953
initDrawer();
5054
}
@@ -60,14 +64,18 @@ private void initListView() {
6064
private void addNotifications() {
6165
Timber.d("Add notifications");
6266

63-
Observable.fromCallable(() -> controller.getNotifications())
64-
.subscribeOn(Schedulers.io())
65-
.observeOn(AndroidSchedulers.mainThread())
66-
.subscribe(notificationList -> {
67-
Collections.reverse(notificationList);
68-
Timber.d("Number of notifications is %d", notificationList.size());
69-
setAdapter(notificationList);
70-
}, throwable -> Timber.e(throwable, "Error occurred while loading notifications"));
67+
if(mNotificationWorkerFragment == null){
68+
Observable.fromCallable(() -> controller.getNotifications())
69+
.subscribeOn(Schedulers.io())
70+
.observeOn(AndroidSchedulers.mainThread())
71+
.subscribe(notificationList -> {
72+
Timber.d("Number of notifications is %d", notificationList.size());
73+
initializeAndSetNotificationList(notificationList);
74+
setAdapter(notificationList);
75+
}, throwable -> Timber.e(throwable, "Error occurred while loading notifications"));
76+
} else {
77+
setAdapter(mNotificationWorkerFragment.getNotificationList());
78+
}
7179
}
7280

7381
private void handleUrl(String url) {
@@ -98,4 +106,12 @@ public static void startYourself(Context context) {
98106
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
99107
context.startActivity(intent);
100108
}
101-
}
109+
110+
private void initializeAndSetNotificationList(List<Notification> notificationList){
111+
FragmentManager fm = getFragmentManager();
112+
mNotificationWorkerFragment = new NotificationWorkerFragment();
113+
fm.beginTransaction().add(mNotificationWorkerFragment, TAG_NOTIFICATION_WORKER_FRAGMENT)
114+
.commit();
115+
mNotificationWorkerFragment.setNotificationList(notificationList);
116+
}
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package fr.free.nrw.commons.notification;
2+
3+
import android.app.Fragment;
4+
import android.os.Bundle;
5+
import android.support.annotation.Nullable;
6+
7+
import java.util.List;
8+
9+
/**
10+
* Created by knightshade on 25/2/18.
11+
*/
12+
13+
public class NotificationWorkerFragment extends Fragment {
14+
private List<Notification> notificationList;
15+
16+
@Override
17+
public void onCreate(@Nullable Bundle savedInstanceState) {
18+
super.onCreate(savedInstanceState);
19+
setRetainInstance(true);
20+
}
21+
22+
public void setNotificationList(List<Notification> notificationList){
23+
this.notificationList = notificationList;
24+
}
25+
26+
public List<Notification> getNotificationList(){
27+
return notificationList;
28+
}
29+
}

0 commit comments

Comments
 (0)