From 01ad0c8c9148ca1f6fac8bba6d0ffc17807c72eb Mon Sep 17 00:00:00 2001 From: knightshade Date: Mon, 26 Feb 2018 00:34:35 +0530 Subject: [PATCH] Notifications list retained across orientation change. Fixes #1198 --- app/build.gradle | 4 +-- .../notification/NotificationActivity.java | 36 +++++++++++++------ .../NotificationWorkerFragment.java | 29 +++++++++++++++ 3 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/fr/free/nrw/commons/notification/NotificationWorkerFragment.java diff --git a/app/build.gradle b/app/build.gradle index c021e28ac..6f225dc2f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -49,7 +49,7 @@ dependencies { compile 'com.facebook.stetho:stetho:1.5.0' testCompile 'junit:junit:4.12' - testCompile 'org.robolectric:robolectric:3.4' + testCompile 'org.robolectric:robolectric:3.7.1' testCompile 'com.squareup.okhttp3:mockwebserver:3.8.1' androidTestCompile 'com.squareup.okhttp3:mockwebserver:3.8.1' @@ -78,7 +78,7 @@ dependencies { androidTestImplementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" testImplementation 'junit:junit:4.12' - testImplementation 'org.robolectric:robolectric:3.4' + testImplementation 'org.robolectric:robolectric:3.7.1' testImplementation 'org.mockito:mockito-all:1.10.19' testImplementation 'com.squareup.okhttp3:mockwebserver:3.8.1' diff --git a/app/src/main/java/fr/free/nrw/commons/notification/NotificationActivity.java b/app/src/main/java/fr/free/nrw/commons/notification/NotificationActivity.java index 4b4ca3007..da9b60ba8 100644 --- a/app/src/main/java/fr/free/nrw/commons/notification/NotificationActivity.java +++ b/app/src/main/java/fr/free/nrw/commons/notification/NotificationActivity.java @@ -1,6 +1,7 @@ package fr.free.nrw.commons.notification; import android.annotation.SuppressLint; +import android.app.FragmentManager; import android.content.Context; import android.content.Intent; import android.net.Uri; @@ -10,7 +11,6 @@ import android.support.v7.widget.RecyclerView; import com.pedrogomez.renderers.RVRendererAdapter; -import java.util.Collections; import java.util.List; import javax.inject.Inject; @@ -35,12 +35,16 @@ public class NotificationActivity extends NavigationBaseActivity { @Inject NotificationController controller; + private static final String TAG_NOTIFICATION_WORKER_FRAGMENT = "NotificationWorkerFragment"; + private NotificationWorkerFragment mNotificationWorkerFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification); ButterKnife.bind(this); + mNotificationWorkerFragment = (NotificationWorkerFragment) getFragmentManager() + .findFragmentByTag(TAG_NOTIFICATION_WORKER_FRAGMENT); initListView(); initDrawer(); } @@ -55,14 +59,18 @@ public class NotificationActivity extends NavigationBaseActivity { private void addNotifications() { Timber.d("Add notifications"); - Observable.fromCallable(() -> controller.getNotifications()) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(notificationList -> { - Collections.reverse(notificationList); - Timber.d("Number of notifications is %d", notificationList.size()); - setAdapter(notificationList); - }, throwable -> Timber.e(throwable, "Error occurred while loading notifications")); + if(mNotificationWorkerFragment == null){ + Observable.fromCallable(() -> controller.getNotifications()) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(notificationList -> { + Timber.d("Number of notifications is %d", notificationList.size()); + initializeAndSetNotificationList(notificationList); + setAdapter(notificationList); + }, throwable -> Timber.e(throwable, "Error occurred while loading notifications")); + } else { + setAdapter(mNotificationWorkerFragment.getNotificationList()); + } } private void handleUrl(String url) { @@ -85,4 +93,12 @@ public class NotificationActivity extends NavigationBaseActivity { Intent intent = new Intent(context, NotificationActivity.class); context.startActivity(intent); } -} + + private void initializeAndSetNotificationList(List notificationList){ + FragmentManager fm = getFragmentManager(); + mNotificationWorkerFragment = new NotificationWorkerFragment(); + fm.beginTransaction().add(mNotificationWorkerFragment, TAG_NOTIFICATION_WORKER_FRAGMENT) + .commit(); + mNotificationWorkerFragment.setNotificationList(notificationList); + } +} \ No newline at end of file diff --git a/app/src/main/java/fr/free/nrw/commons/notification/NotificationWorkerFragment.java b/app/src/main/java/fr/free/nrw/commons/notification/NotificationWorkerFragment.java new file mode 100644 index 000000000..bb85fc696 --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/notification/NotificationWorkerFragment.java @@ -0,0 +1,29 @@ +package fr.free.nrw.commons.notification; + +import android.app.Fragment; +import android.os.Bundle; +import android.support.annotation.Nullable; + +import java.util.List; + +/** + * Created by knightshade on 25/2/18. + */ + +public class NotificationWorkerFragment extends Fragment { + private List notificationList; + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setRetainInstance(true); + } + + public void setNotificationList(List notificationList){ + this.notificationList = notificationList; + } + + public List getNotificationList(){ + return notificationList; + } +}