package com.frodo.github.business.activity;

import android.support.v4.util.Pair;

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.GithubEvent;
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 EventsModel extends AbstractModel {
	public static final String TAG = EventsModel.class.getSimpleName();
	private AndroidFetchNetworkDataTask fetchReceivedEventsNetworkDataTask;

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

	public Observable<List<GithubEvent>> loadAccountEvents(String accountUser) {
		return loadEventsWithReactor(Path.replace(Path.Activity.USER_EVENTS, new Pair<>("username", accountUser)));
	}

	public Observable<List<GithubEvent>> loadReceivedEvents(final String username) {
		return loadEventsWithReactor(Path.replace(Path.Activity.RECEIVED_EVENTS, new Pair<>("username", username)));
	}

	private Observable<List<GithubEvent>> loadEventsWithReactor(final String path) {
		return Observable.create(new Observable.OnSubscribe<Response>() {
			@Override
			public void call(Subscriber<? super Response> subscriber) {
				Request request = new Request.Builder()
						.method("GET")
						.relativeUrl(path)
						.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<GithubEvent>>>() {
			@Override
			public Observable<List<GithubEvent>> call(Response response) {
				ResponseBody rb = (ResponseBody) response.getBody();
				try {
					List<GithubEvent> events = JsonConverter.convert(rb.string(), new TypeReference<List<GithubEvent>>() {
					});
					return Observable.just(events);
				} catch (IOException e) {
					return Observable.error(e);
				}
			}
		});
	}
}

@@ -80,62 +80,8 @@
 r;%0A%0A
-import com.fasterxml.jackson.core.type.TypeReference;%0A
 impo
@@ -614,38 +614,93 @@
 ort 
-okhttp3.ResponseBody;%0Aimport r
+com.google.gson.reflect.TypeToken;%0Aimport io.reactivex.Observable;%0Aimport io.reactive
 x.Ob
@@ -699,32 +699,39 @@
 tivex.Observable
+Emitter
 ;%0Aimport rx.Subs
@@ -727,48 +727,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/
@@ -1685,17 +1685,16 @@
 servable
-.
 OnSubscr
@@ -1719,28 +1719,25 @@
 %09%09%09@Override
-%0A%09%09%09
+ 
 public void 
@@ -1740,22 +1740,17 @@
 oid 
-call(S
+s
 ubscribe
 r%3C? 
@@ -1749,40 +1749,49 @@
 ribe
-r%3C? super Response%3E subscriber) 
+(ObservableEmitter%3CResponse%3E emitter)%0A%09%09%09
 %7B%0A%09%09
@@ -2158,24 +2158,21 @@
 equest, 
-subscrib
+emitt
 er);%0A%09%09%09
@@ -2287,17 +2287,20 @@
 new Func
-1
+tion
 %3CRespons
@@ -2304,32 +2304,38 @@
 onse, Observable
+Source
 %3CList%3CGithubEven
@@ -2355,20 +2355,17 @@
 Override
-%0A%09%09%09
+ 
 public O
@@ -2365,32 +2365,38 @@
 ublic Observable
+Source
 %3CList%3CGithubEven
@@ -2403,12 +2403,13 @@
 t%3E%3E 
-call
+apply
 (Res
@@ -2423,17 +2423,20 @@
 esponse)
- 
+%0A%09%09%09
 %7B%0A%09%09%09%09Re
@@ -2575,17 +2575,13 @@
 Type
-Reference
+Token
 %3CLis
@@ -2606,16 +2606,26 @@
 %7B%0A%09%09%09%09%09%7D
+.getType()
 );%0A%09%09%09%09%09

Update EventsModel.java