Upgrade to Pro — share decks privately, control downloads, hide ads and more …

뷰 실시간으로 동기화 하기

뷰 실시간으로 동기화 하기

2017년 7월 18일 All About Kotlin 행사 라이트닝 토크에서 발표했던 뷰 실시간으로 동기화하기 발표자료 입니다.

발표영상: https://www.youtube.com/watch?v=uEsEAAG7L9M&t=7s

Seungmin 마량

July 18, 2017
Tweet

More Decks by Seungmin 마량

Other Decks in Technology

Transcript

  1. 1. Interface - 1depth زӝചী ਬਊೞ׮. 2. Publish Subject (with

    RxJava) 3. ੹৉ ੉ߕ౟ (with EventBus) 4. Flux 5. ViewModel
  2. /* Activityীࢲ Interface ١۾ */ private void setAdapter() {
 adapter.setOnClickItemListener(position

    -> {
 // ࠭ زӝച ೞӝ
 });
 } /* Adapterীࢲ Interface ࢎਊ */ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
 ListItemView view = (ListItemView) holder.itemView;
 Item item = getItem(position);
 view.bind(item);
 view.setOnClickListener(v -> onClickItemListener.onClickItem(position));
 }
  3. 1. Interface - 1depth زӝചী ਬਊೞ׮. 2. Publish Subject (with

    RxJava) - 1ߣҗ ߑೱࢿ੉ ߈؀. ѐ֛ ੉೧о औ׮. Observable ࢎਊ੉ оמೞ׮. 3. ੹৉ ੉ߕ౟ (with EventBus) 4. Flux 5. ViewModel
  4. /* Activityীࢲ Subject ੉ߕ౟ ҳة */ private void setAdapter() {


    adapter.onClick(position -> {
 // ࠭ زӝച ೞӝ
 });
 } /* Adapterীࢲ Subject ੹࣠ */ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
 ListItemView view = (ListItemView) holder.itemView;
 Item item = getItem(position);
 view.bind(item);
 view.setOnClickListener(v -> clickSubject.onNext(position));
 }
  5. 1. Interface - 1depth زӝചী ਬਊೞ׮. 2. Publish Subject (with

    RxJava) - 1ߣҗ ߑೱࢿ੉ ߈؀. ѐ֛ ੉೧о औ׮. Observable ࢎਊ੉ оמೞ׮. 3. ੹৉ ੉ߕ౟ (with EventBus) - ݣܻ ڄয૓ ചݶ, ࢲ࠺झ р زӝചী ਬਊೞ׮. 4. Flux 5. ViewModel
  6. /* ੉ߕ౟ ࠁղӝ */ private void sendEvent(String parameter) {
 EventBus.getDefault().post(new

    Event()
 .setParam(parameter));
 } /* Event ఋੑ੄ ੉ߕ౟ ߉ӝ */ @Subscribe
 public void onEvent(Event e) {
 String parameter = e.getParam(); // ࠭ زӝച ೞӝ }
  7. 1. Interface - 1depth زӝചী ਬਊೞ׮. 2. Publish Subject (with

    RxJava) - 1ߣҗ ߑೱࢿ੉ ߈؀. ѐ֛ ੉೧о औ׮. Observable ࢎਊ੉ оמೞ׮. 3. ੹৉ ੉ߕ౟ (with EventBus) - ݣܻ ڄয૓ ചݶ, ࢲ࠺झ р زӝചী ਬਊೞ׮. 4. Flux - ׮নೠ ঘ࣌੉ ೞա੄ ੹৉߸ࣻী ৔ೱਸ ઴ ٸ ਬਊೞ׮. 5. ViewModel
  8. public class FluxStore {
 private static FluxStore mInstance;
 private String

    data;
 // ؘ੉ఠܳ Storeী ੷੢ೞҊ ؘ੉ఠо ߸ೠ ࢎपਸ ੹౵ೠ׮.
 public void setData(String data) {
 this.data = data; // ؘ੉ఠ ߸҃ ੉ߕ౟ܳ ࠁմ׮. ೙ਃೠ ࠭ীࢲ ੉ߕ౟ܳ ߉ই زӝച ೠ׮.
 EventBus.getDefault().post(new FluxEvent());
 } // FluxEventܳ ߉ח Ҕীࢲ ؘ੉ఠܳ оઉо ୭नਵ۽ ࠭ܳ јनೠ׮.
 public String getData() {
 return data;
 }
 }
  9. 1. Interface - 1depth زӝചী ਬਊೞ׮. 2. Publish Subject (with

    RxJava) - 1ߣҗ ߑೱࢿ੉ ߈؀. ѐ֛ ੉೧о औ׮. Observable ࢎਊ੉ оמೞ׮. 3. ੹৉ ੉ߕ౟ (with EventBus) - ݣܻ ڄয૓ ചݶ, ࢲ࠺झ р زӝചী ਬਊೞ׮. 4. Flux - ׮নೠ ঘ࣌੉ ೞա੄ ੹৉߸ࣻী ৔ೱਸ ઴ ٸ ਬਊೞ׮. 5. ViewModel - ViewModel ߧਤ উীࢲ ݽ؛ױਤ زӝചী ਬਊೞ׮.
  10. public abstract class BaseViewModel<T extends IdentifiableLong> {
 
 private final

    ConcurrentHashMap<Long, Subject<T, T>> subjects;
 // ೙ਃೠ ࠭ীࢲ ੉ ݫࣗ٘ܳ ࢎਊೞৈ Subjectܳ ҳةೠ׮. // ݽ؛੄ id߹۽ Subjectܳ ҳ࠙ೞৈ ҳةೠ׮.
 public Observable<T> observe(T item) {
 Subject<T, T> subject = subjects.get(item.getId());
 if (subject == null) {
 subject = new SerializedSubject<>(PublishSubject.create());
 subjects.put(item.getId(), subject);
 }
 return subject.observeOn(AndroidSchedulers.mainThread());
 }
 }
  11. public abstract class BaseViewModel<T extends IdentifiableLong> {
 // ߸҃ػ ݽ؛ਸ

    ੹࣠ೠ׮. idо э਷ ݽ؛࠭੄ Subject ҳة੉ ࠭ܳ јनೡ Ѫ੉׮.
 public void setValue(T item) {
 Subject<T, T> subject = subjects.get(item.getId());
 if (subject != null) subject.onNext(item);
 }
 }
  12. 1. Interface - 1depth زӝചী ਬਊೞ׮. 2. Publish Subject (with

    RxJava) - 1ߣҗ ߑೱࢿ੉ ߈؀. ѐ֛ ੉೧о औ׮. Observable ࢎਊ੉ оמೞ׮. 3. ੹৉ ੉ߕ౟ (with EventBus) - ݣܻ ڄয૓ ചݶ, ࢲ࠺झ р زӝചী ਬਊೞ׮. 4. Flux - ׮নೠ ঘ࣌੉ ೞա੄ ੹৉߸ࣻী ৔ೱਸ ઴ ٸ ਬਊೞ׮. 5. ViewModel - ViewModel ߧਤ উীࢲ ݽ؛ױਤ زӝചী ਬਊೞ׮.