package com.frodo.github.business.activity;

import com.fasterxml.jackson.core.type.TypeReference;
import com.frodo.app.android.core.task.AndroidFetchNetworkDataTask;
import com.frodo.app.android.core.toolbox.JsonConverter;
import com.frodo.app.framework.controller.AbstractModel;
import com.frodo.app.framework.controller.MainController;
import com.frodo.app.framework.net.NetworkTransport;
import com.frodo.app.framework.net.Request;
import com.frodo.app.framework.net.Response;
import com.frodo.github.bean.dto.response.Notification;
import com.frodo.github.common.Path;

import java.io.IOException;
import java.util.List;

import okhttp3.ResponseBody;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;

/**
 * Created by frodo on 16/6/10.
 */
public class NotificationsModel extends AbstractModel {

	public static final String TAG = NotificationsModel.class.getSimpleName();
	private AndroidFetchNetworkDataTask fetchReceivedEventsNetworkDataTask;

	public NotificationsModel(MainController controller) {
		super(controller);
	}

	public Observable<List<Notification>> loadNotifications() {
		return Observable.create(new Observable.OnSubscribe<Response>() {
			@Override
			public void call(Subscriber<? super Response> subscriber) {
				Request request = new Request.Builder()
						.method("GET")
						.relativeUrl(Path.Activity.NOTIFICATIONS)
						.build();
				final NetworkTransport networkTransport = getMainController().getNetworkTransport();
				networkTransport.setAPIUrl(Path.HOST_GITHUB);
				fetchReceivedEventsNetworkDataTask = new AndroidFetchNetworkDataTask(getMainController().getNetworkTransport(), request, subscriber);
				getMainController().getBackgroundExecutor().execute(fetchReceivedEventsNetworkDataTask);
			}
		}).flatMap(new Func1<Response, Observable<List<Notification>>>() {
			@Override
			public Observable<List<Notification>> call(Response response) {
				ResponseBody rb = (ResponseBody) response.getBody();
				try {
					List<Notification> notifications = JsonConverter.convert(rb.string(), new TypeReference<List<Notification>>() {
					});
					return Observable.just(notifications);
				} catch (IOException e) {
					return Observable.error(e);
				}
			}
		});
	}


}

@@ -42,62 +42,8 @@
 y;%0A%0A
-import com.fasterxml.jackson.core.type.TypeReference;%0A
 impo
@@ -577,38 +577,93 @@
 ort 
-okhttp3.ResponseBody;%0Aimport r
+com.google.gson.reflect.TypeToken;%0Aimport io.reactivex.Observable;%0Aimport io.reactive
 x.Ob
@@ -662,32 +662,39 @@
 tivex.Observable
+Emitter
 ;%0Aimport rx.Subs
@@ -690,48 +690,149 @@
 ort 
-rx.Subscriber;%0Aimport rx.functions.Func1
+io.reactivex.ObservableOnSubscribe;%0Aimport io.reactivex.ObservableSource;%0Aimport io.reactivex.functions.Function;%0Aimport okhttp3.ResponseBody
 ;%0A%0A/
@@ -1260,17 +1260,16 @@
 servable
-.
 OnSubscr
@@ -1294,28 +1294,25 @@
 %09%09%09@Override
-%0A%09%09%09
+ 
 public void 
@@ -1315,22 +1315,17 @@
 oid 
-call(S
+s
 ubscribe
 r%3C? 
@@ -1324,40 +1324,49 @@
 ribe
-r%3C? super Response%3E subscriber) 
+(ObservableEmitter%3CResponse%3E emitter)%0A%09%09%09
 %7B%0A%09%09
@@ -1756,24 +1756,21 @@
 equest, 
-subscrib
+emitt
 er);%0A%09%09%09
@@ -1885,17 +1885,20 @@
 new Func
-1
+tion
 %3CRespons
@@ -1902,32 +1902,38 @@
 onse, Observable
+Source
 %3CList%3CNotificati
@@ -1954,20 +1954,17 @@
 Override
-%0A%09%09%09
+ 
 public O
@@ -1964,32 +1964,38 @@
 ublic Observable
+Source
 %3CList%3CNotificati
@@ -2003,12 +2003,13 @@
 n%3E%3E 
-call
+apply
 (Res
@@ -2023,17 +2023,20 @@
 esponse)
- 
+%0A%09%09%09
 %7B%0A%09%09%09%09Re
@@ -2183,17 +2183,13 @@
 Type
-Reference
+Token
 %3CLis
@@ -2215,16 +2215,26 @@
 %7B%0A%09%09%09%09%09%7D
+.getType()
 );%0A%09%09%09%09%09

Update NotificationsModel.java