dagger & rxjava & retrofit
Post on 02-Jul-2015
1.125 Views
Preview:
DESCRIPTION
TRANSCRIPT
Dagger & RxJava & Retrofit
Ted
what is Dagger
Dagger is a Dependency Injector
example1
public class School { public Teacher teacher = new Teacher();}
Cons?
● school 裡會有很多teacher,但是如果沒有setter 代表school必須跟teacher綁定
● how to test?
example2
public class School { public Teacher teacher ;
public void setTeacher(Teacher t){teacher = t;
}}
Pros
● now we can set whoever we want.● and easy to test
TestSample
s = new Schoole();s.setTeacher(new MockTeacher());assert.equals(s.getTeacherName(),”balabala”);
so...how to use dagger
● source (Module)● target (class)
Source (Module)
create a @Moduletell the module you are going to @Inject to which class@Provides a type to that class
Target(class)
@Inject the Modules to classauto @Inject to field or get type by code
example1
demo and explain
https://github.com/nightbear1009/Dagger_StepBySteprepository = feature/simpleDagger
example2
demo and explain
https://github.com/nightbear1009/Dagger_StepBySteprepository = feature/plusIfNecessary
what’s the meaning of plus?
AppGraph
NetworkModules
AccountModules
what’s the meaning of plus?
AppGraph
NetworkModules
AccountModules
LoginGraph
LoginHelperModules
App + Login GraphUserLogin
what’s the meaning of plus?
AppGraph
NetworkModules
AccountModules
UserLogout
what about test
override
example3
https://github.com/nightbear1009/Dagger_StepBySteprepository = feature/testTeacher
Rxjava
Funtional Programing
forEach rx.Observable.from("1","2","3").forEach(new Action1<String>() {
@Override
public void call(String s) {
Log.d("Ted","s "+s);
}
});
s 1
s 2
s 3
map rx.Observable.from("1","2","3").map(new Func1<String, Boolean>() {
@Override
public Boolean call(String s) {
return s.equals("1");
}
}).subscribe(new Action1<Object>() {
@Override
public void call(Object o) {
Log.d("Ted","o "+o);
}
});
o true
o false
o false
flapMap rx.Observable.from("1","2","3").flatMap(new Func1<String, rx.Observable<?>>() {
@Override
public rx.Observable<?> call(String s) {
Log.d("Ted","num "+s);
return rx.Observable.from(s);
}
}).subscribe(new Action1<Object>() {
@Override
public void call(Object o) {
}
});
num 1
num 2
num 3
filterrx.Observable.from("1","2","3").filter(new Func1<String, Boolean>() {
@Override
public Boolean call(String s) {
return s.equals("1");
}
}).subscribe(new Action1<String>() {
@Override
public void call(String s) {
Log.d("Ted","s "+s);
}
});
s 1
toSortedListrx.Observable.from("1","3","2").toSortedList(new Func2<String, String, Integer>() {
@Override
public Integer call(String s, String s2) {
return Integer.valueOf(s2) - Integer.valueOf(s);
}
}).subscribe(new Action1<List<String>>() {
@Override
public void call(List<String> strings) {
for(String s :strings){
Log.d("Ted","s "+s);
}
}
});
s 3
s 2
s 1
groupby
false integer 1 false integer 3
true integer 2 true integer 2 false integer 3
false integer 5 true integer 6 false integer 7
true integer 8 false integer 9
Observable.just vs Observable.from
一次丟全部資料和一次丟一筆資料的差別?
without publish + connect D s url1 D s url2 D s url3 D s2 url1 D s2 url2 D s2 url3
publish + connect
D s url1 D s2 url1 D s url2 D s2 url2 D s url3 D s2 url3
refcount
還沒時間看XD
Pros & Cons
Proslogic will be clear
Conshigh leaning curvelots of callback(with retrolamda maybe better)
Ingram 說
http://ingramchen.io/blog/2014/10/retromlambda.html
華生大大 說
https://twitter.com/jakewharton/status/385898996884971520
過了幾個月後 華生大大又說..
華生大大 again
都是別人說,現在換我說XD
rxjava的確算是較大型的lib,learning curve比較高,有很多東西需要花時間學習&了解.but如果我們只是用來處理 api + data handle 而不用來處理複雜的邏輯 or data combining... (https://github.com/nightbear1009/RxAndroid-Login-Sample ),我相信會是個不錯的選擇
Retrofit
three steps1. create interface2. create api3. handleData
retrofit
1. build in gson2. no need to implement parcelable3. easy to test
test
test more with gradle + dagger
前情提要:
randy: 有沒有可能寫出 XXXListView.get(url) ; 然後就可以畫出layout的辦法?https://github.com/nightbear1009/SmallDaggerRetrofitRxjavaSample
當然,目前的版本還有很多高耦合的問題必須解決,ex : adapter 但或許是個將view 封裝的一個方向?
so… what can we do with these lib
Demo Time again
Conclusion
● use Retrofit to handle Network api● use dagger to injection every retrofit’s
adapter(so we can mock the api data)
● use rxjava to handle every event including api call back(so our logic will be clearly)
top related