Notifications list retained across orientation change.

Fixes #1198
This commit is contained in:
knightshade 2018-02-26 00:34:35 +05:30
parent 215c97e132
commit 01ad0c8c91
3 changed files with 57 additions and 12 deletions

View file

@ -49,7 +49,7 @@ dependencies {
compile 'com.facebook.stetho:stetho:1.5.0' compile 'com.facebook.stetho:stetho:1.5.0'
testCompile 'junit:junit:4.12' 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' testCompile 'com.squareup.okhttp3:mockwebserver:3.8.1'
androidTestCompile '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" androidTestImplementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
testImplementation 'junit:junit:4.12' 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 'org.mockito:mockito-all:1.10.19'
testImplementation 'com.squareup.okhttp3:mockwebserver:3.8.1' testImplementation 'com.squareup.okhttp3:mockwebserver:3.8.1'

View file

@ -1,6 +1,7 @@
package fr.free.nrw.commons.notification; package fr.free.nrw.commons.notification;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.FragmentManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
@ -10,7 +11,6 @@ import android.support.v7.widget.RecyclerView;
import com.pedrogomez.renderers.RVRendererAdapter; import com.pedrogomez.renderers.RVRendererAdapter;
import java.util.Collections;
import java.util.List; import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
@ -35,12 +35,16 @@ public class NotificationActivity extends NavigationBaseActivity {
@Inject NotificationController controller; @Inject NotificationController controller;
private static final String TAG_NOTIFICATION_WORKER_FRAGMENT = "NotificationWorkerFragment";
private NotificationWorkerFragment mNotificationWorkerFragment;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification); setContentView(R.layout.activity_notification);
ButterKnife.bind(this); ButterKnife.bind(this);
mNotificationWorkerFragment = (NotificationWorkerFragment) getFragmentManager()
.findFragmentByTag(TAG_NOTIFICATION_WORKER_FRAGMENT);
initListView(); initListView();
initDrawer(); initDrawer();
} }
@ -55,14 +59,18 @@ public class NotificationActivity extends NavigationBaseActivity {
private void addNotifications() { private void addNotifications() {
Timber.d("Add notifications"); Timber.d("Add notifications");
if(mNotificationWorkerFragment == null){
Observable.fromCallable(() -> controller.getNotifications()) Observable.fromCallable(() -> controller.getNotifications())
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(notificationList -> { .subscribe(notificationList -> {
Collections.reverse(notificationList);
Timber.d("Number of notifications is %d", notificationList.size()); Timber.d("Number of notifications is %d", notificationList.size());
initializeAndSetNotificationList(notificationList);
setAdapter(notificationList); setAdapter(notificationList);
}, throwable -> Timber.e(throwable, "Error occurred while loading notifications")); }, throwable -> Timber.e(throwable, "Error occurred while loading notifications"));
} else {
setAdapter(mNotificationWorkerFragment.getNotificationList());
}
} }
private void handleUrl(String url) { private void handleUrl(String url) {
@ -85,4 +93,12 @@ public class NotificationActivity extends NavigationBaseActivity {
Intent intent = new Intent(context, NotificationActivity.class); Intent intent = new Intent(context, NotificationActivity.class);
context.startActivity(intent); context.startActivity(intent);
} }
private void initializeAndSetNotificationList(List<Notification> notificationList){
FragmentManager fm = getFragmentManager();
mNotificationWorkerFragment = new NotificationWorkerFragment();
fm.beginTransaction().add(mNotificationWorkerFragment, TAG_NOTIFICATION_WORKER_FRAGMENT)
.commit();
mNotificationWorkerFragment.setNotificationList(notificationList);
}
} }

View file

@ -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<Notification> notificationList;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
public void setNotificationList(List<Notification> notificationList){
this.notificationList = notificationList;
}
public List<Notification> getNotificationList(){
return notificationList;
}
}