Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Dagger & rxjava & retrofit

3,620 views

Published on

about using android third party library
Dagger, Rxjava, Retrofit

Published in: Engineering
  • Be the first to comment

Dagger & rxjava & retrofit

  1. 1. Dagger & RxJava & Retrofit Ted
  2. 2. what is Dagger Dagger is a Dependency Injector
  3. 3. example1 public class School { public Teacher teacher = new Teacher(); }
  4. 4. Cons? ● school 裡會有很多teacher,但是如果沒有 setter 代表school必須跟teacher綁定 ● how to test?
  5. 5. example2 public class School { public Teacher teacher ; public void setTeacher(Teacher t){ teacher = t; } }
  6. 6. Pros ● now we can set whoever we want. ● and easy to test
  7. 7. TestSample s = new Schoole(); s.setTeacher(new MockTeacher()); assert.equals(s.getTeacherName(),”balabala”);
  8. 8. so...how to use dagger ● source (Module) ● target (class)
  9. 9. Source (Module) create a @Module tell the module you are going to @Inject to which class @Provides a type to that class
  10. 10. Target(class) @Inject the Modules to class auto @Inject to field or get type by code
  11. 11. example1 demo and explain https://github. com/nightbear1009/Dagger_StepByStep repository = feature/simpleDagger
  12. 12. example2 demo and explain https://github. com/nightbear1009/Dagger_StepByStep repository = feature/plusIfNecessary
  13. 13. what’s the meaning of plus? AppGraph NetworkModules AccountModules
  14. 14. what’s the meaning of plus? AppGraph NetworkModules AccountModules LoginGraph LoginHelperModules UserLogin App + Login Graph
  15. 15. what’s the meaning of plus? UserLogout AppGraph NetworkModules AccountModules
  16. 16. what about test override
  17. 17. example3 https://github. com/nightbear1009/Dagger_StepByStep repository = feature/testTeacher
  18. 18. Rxjava Funtional Programing
  19. 19. 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
  20. 20. 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
  21. 21. 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
  22. 22. filter rx.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
  23. 23. toSortedList rx.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
  24. 24. 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
  25. 25. Observable.just vs Observable.from 一次丟全部資料和一次丟一筆資料的差別?
  26. 26. without publish + connect D s url1 D s url2 D s url3 D s2 url1 D s2 url2 D s2 url3
  27. 27. publish + connect D s url1 D s2 url1 D s url2 D s2 url2 D s url3 D s2 url3
  28. 28. refcount 還沒時間看XD
  29. 29. Pros & Cons Pros logic will be clear Cons high leaning curve lots of callback(with retrolamda maybe better)
  30. 30. Ingram 說 http://ingramchen. io/blog/2014/10/retromlambda.html
  31. 31. 華生大大 說 https://twitter. com/jakewharton/status/3858989968 84971520
  32. 32. 過了幾個月後 華生大大又說..
  33. 33. 華生大大 again
  34. 34. 都是別人說,現在換我說XD rxjava的確算是較大型的lib,learning curve比較 高,有很多東西需要花時間學習&了解. but如果我們只是用來處理 api + data handle 而 不用來處理複雜的邏輯 or data combining... (https://github.com/nightbear1009/RxAndroid- Login-Sample ), 我相信會是個不錯的選擇
  35. 35. Retrofit three steps 1. create interface 2. create api 3. handleData
  36. 36. retrofit 1. build in gson 2. no need to implement parcelable 3. easy to test
  37. 37. test
  38. 38. test more with gradle + dagger 前情提要: randy: 有沒有可能寫出 XXXListView.get(url) ; 然後就可以畫出layout的 辦法? https://github. com/nightbear1009/SmallDaggerRetr ofitRxjavaSample
  39. 39. 當然,目前的版本還有很多高耦合的問題必須解 決,ex : adapter 但或許是個將view 封裝的一個 方向?
  40. 40. so… what can we do with these lib Demo Time again
  41. 41. 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)

×