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

Android Traditional vs Jetpack in Architecture ...

Android Traditional vs Jetpack in Architecture Components

2018년 11월 24일 GDG DevFest Busan 2018 에서 발표한 내용입니다. 전통적인 방식과 비교하며 Jetpack에 포함된 Architecture Components 라이브러리들을 살펴봅니다.

Seungmin 마량

November 24, 2018
Tweet

More Decks by Seungmin 마량

Other Decks in Technology

Transcript

  1. ݾର 1. ࣁ࣌ ݾ಴ 2. What is Jetpack? 3. What

    is Architecture Components? 4. Lifecycle 5. LiveData 6. ViewModel 7. Room 8. Navigation 9. WorkManager 10.DataBinding 11.Paging 12.੿ܻ 13.݃ޖܻ Korea
  2. ੉ߣ ࣁ࣌ਸ ా೧ ৈ۞ٜ࠙੉ ঳যщਵݶ જѷח Ѫ ࣁ࣌ ݾ಴ 1.Jetpack੉

    ޖ঺ੋо 2.Architecture Componentsо ޖ঺ੋо 3.Architecture Components ппী ؀ೠ ੉೧
  3. UI

  4. Architecture Components੉ۆ? জਸ ౡౡೞҊ, పझ౟ оמೞҊ, ਬ૑ࠁࣻ ೞӝ औب۾ ذח

    ۄ੉࠳۞ܻ ݽ਺ Jetpack੄ ௾ ஠పҊܻ ઺ ೞա ୹୊: https://developer.android.com/topic/libraries/architecture/
  5. public class BasePresenter { protected CompositeDisposable compositeDisposable; public void onDestroy()

    { compositeDisposable.dispose(); } } Traditional Presenterীࢲ dispose()ܳ Fragment onDestroyী ݏ୶য ࣻ೯೧ঠ ೠ׮
  6. public class BasePresenter { protected CompositeDisposable compositeDisposable; public void onDestroy()

    { compositeDisposable.dispose(); } } public class UsersFragment { @Override public void onDestroy() { presenter.onDestroy(); } } Traditional Presenterীࢲ dispose()ܳ Fragment onDestroyী ݏ୶য ࣻ೯೧ঠ ೠ׮
  7. public class BasePresenter { protected CompositeDisposable compositeDisposable; public void onDestroy()

    { compositeDisposable.dispose(); } } public class UsersFragment { @Override public void onDestroy() { presenter.onDestroy(); } } Traditional Presenterীࢲ dispose()ܳ Fragment onDestroyী ݏ୶য ࣻ೯೧ঠ ೠ׮ ܻ࠙ػ Class੄ ੘সਸ ೞա੄ Lifecycleী ݏ୶য ز੘ೞب۾ ೧ঠೠ׮
  8. public class UsersPresenter implements LifecycleObserver { protected CompositeDisposable compositeDisposable; @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)

    void onDestroy() { compositeDisposable.dispose(); } } Jetpack ৻ࠗ Classীࢲ Lifecycleী ݏ୶য ੘সਸ ࣻ೯ೡ ࣻ ੓׮
  9. class MyLocationListener { private Lifecycle lifecycle; public MyLocationListener(Lifecycle lifecycle) {

    this.lifecycle = lifecycle; } public void enable() { if (lifecycle.getCurrentState().isAtLeast(Lifecycle.State.STARTED)) { // connect if not connected } } } Jetpack
  10. class MyLocationListener { private Lifecycle lifecycle; public MyLocationListener(Lifecycle lifecycle) {

    this.lifecycle = lifecycle; } public void enable() { if (lifecycle.getCurrentState().isAtLeast(Lifecycle.State.STARTED)) { // connect if not connected } } } Jetpack Lifecycle Wrapping Class
  11. class MyLocationListener { private Lifecycle lifecycle; public MyLocationListener(Lifecycle lifecycle) {

    this.lifecycle = lifecycle; } public void enable() { if (lifecycle.getCurrentState().isAtLeast(Lifecycle.State.STARTED)) { // connect if not connected } } } Jetpack ৻ࠗ Classীࢲ Lifecycleܳ х૑
  12. LiveDataਸ ঱ઁ ࢎਊೞաਃ? 1. Data झझ۽ Lifecycle х૑ 2. Lifecycleী

    ٮܲ ݫݽܻܼ ١੄ Observable Side Effect ੗ز ߑ૑
  13. public class UsersPresenter extends BasePresenter { private View view; private

    UserRepository repository; public void listUsers() { repository.listUsers() .observeOn(AndroidSchedulers.mainThread()) .subscribe(users -> { view.onListUsers(users); }); } } Traditional
  14. public class UsersPresenter extends BasePresenter { private View view; private

    UserRepository repository; public void listUsers() { repository.listUsers() .observeOn(AndroidSchedulers.mainThread()) .subscribe(users -> { view.onListUsers(users); }); } } Traditional Observable ࠺زӝ
  15. public class UsersPresenter extends BasePresenter { private View view; private

    UserRepository repository; public void listUsers() { repository.listUsers() .observeOn(AndroidSchedulers.mainThread()) .subscribe(users -> { view.onListUsers(users); }); } } Traditional
  16. public class UsersPresenter extends BasePresenter { private View view; private

    UserRepository repository; public void listUsers() { repository.listUsers() .observeOn(AndroidSchedulers.mainThread()) .subscribe(users -> { view.onListUsers(users); }); } } Traditional ܻࠛӝ ੹ী Viewо Destroy ػ׮ݶ?
  17. public class UsersPresenter extends BasePresenter { private View view; private

    UserRepository repository; private MutableLiveData<List<User>> users = new MutableLiveData<>(); public MutableLiveData<List<User>> getUsers() { return users; } public void listUsers() { repository.listUsers() .observeOn(AndroidSchedulers.mainThread()) .subscribe(users -> { this.users.postValue(users); }); } } Jetpack
  18. public class UsersPresenter extends BasePresenter { private View view; private

    UserRepository repository; private MutableLiveData<List<User>> users = new MutableLiveData<>(); public MutableLiveData<List<User>> getUsers() { return users; } public void listUsers() { repository.listUsers() .observeOn(AndroidSchedulers.mainThread()) .subscribe(users -> { this.users.postValue(users); }); } } Jetpack Observable ࠺زӝ
  19. public class UsersPresenter extends BasePresenter { private View view; private

    UserRepository repository; private MutableLiveData<List<User>> users = new MutableLiveData<>(); public MutableLiveData<List<User>> getUsers() { return users; } public void listUsers() { repository.listUsers() .observeOn(AndroidSchedulers.mainThread()) .subscribe(users -> { this.users.postValue(users); }); } } Jetpack LiveData۽ Dataܳ ֈӟ׮
  20. public class UsersFragment extends BaseFragment implements UsersPresenter.View { private void

    setPresenter() { presenter.getUsers().observe(this, users -> adapter.refresh(users)); presenter.listUsers(); } } Jetpack
  21. public class UsersFragment extends BaseFragment implements UsersPresenter.View { private void

    setPresenter() { presenter.getUsers().observe(this, users -> adapter.refresh(users)); presenter.listUsers(); } } Jetpack Observe
  22. public class UsersFragment extends BaseFragment implements UsersPresenter.View { private void

    setPresenter() { presenter.getUsers().observe(this, users -> adapter.refresh(users)); presenter.listUsers(); } } Jetpack
  23. public class UsersFragment extends BaseFragment implements UsersPresenter.View { private void

    setPresenter() { presenter.getUsers().observe(this, users -> adapter.refresh(users)); presenter.listUsers(); } } Jetpack Lifecycle
  24. public class UsersFragment extends BaseFragment implements UsersPresenter.View { private void

    setPresenter() { presenter.getUsers().observe(this, users -> adapter.refresh(users)); presenter.listUsers(); } } Jetpack Users Dataо झझ۽ Lifecycleਸ х૑ Viewо Destroy غݶ observeܳ ੗زਵ۽ Րח׮
  25. ViewModelਸ ঱ઁ ࢎਊೞաਃ? 1. Orientation ١੄ UI ߸ചীب Dataܳ ਬ૑

    2. ೞա੄ ചݶীࢲ ৈ۞ Component р Dataܳ औѱ ҕਬ
  26. public class UsersFragment extends BaseFragment implements UsersPresenter.View { @Override public

    void onActivityCreated(@Nullable Bundle savedInstanceState) { presenter = new UsersPresenter(this); presenter.listUsers(); } } Traditional
  27. public class UsersFragment extends BaseFragment implements UsersPresenter.View { @Override public

    void onActivityCreated(@Nullable Bundle savedInstanceState) { presenter = new UsersPresenter(this); presenter.listUsers(); } } Traditional
  28. public class UsersFragment extends BaseFragment implements UsersPresenter.View { @Override public

    void onActivityCreated(@Nullable Bundle savedInstanceState) { presenter = new UsersPresenter(this); presenter.listUsers(); } } Traditional Orientation ഥ੹ द ੤ࢤࢿ ߂ ੤ਃ୒
  29. public class UsersFragment extends BaseFragment implements UsersPresenter.View { private void

    setPresenter() { presenter = ViewModelProviders.of(activity).get(UsersPresenter.class); presenter.listUsers(); } } Jetpack
  30. public class UsersFragment extends BaseFragment implements UsersPresenter.View { private void

    setPresenter() { presenter = ViewModelProviders.of(activity).get(UsersPresenter.class); presenter.listUsers(); } } Jetpack
  31. public class UsersFragment extends BaseFragment implements UsersPresenter.View { private void

    setPresenter() { presenter = ViewModelProviders.of(activity).get(UsersPresenter.class); presenter.listUsers(); } } Jetpack activity scope ղীࢲ э਷ ё୓ ߈ജ
  32. public class UsersFragment extends BaseFragment implements UsersPresenter.View { private void

    setPresenter() { presenter = ViewModelProviders.of(activity).get(UsersPresenter.class); presenter.listUsers(); } } Jetpack Orientation ഥ੹ೞৈب э਷ Presenter ё୓ীࢲ ੤ਃ୒ ߑ૑
  33. public class MemoTypeFragment extends BaseFragment { private void setButton() {

    btnConfirm.setOnClickListener(v -> { MemoActivity activity = ((MemoActivity) getActivity()); activity.showMemo(inputText.getText().toString()); }); } } public class MemoActivity extends BaseActivity { public void showMemo(String memo) { displayFragment.showMemo(memo); } } public class MemoDisplayFragment extends BaseFragment { public void showMemo(String memo) { txtMemo.setText(memo); } } Traditional
  34. public class MemoTypeFragment extends BaseFragment { private void setButton() {

    btnConfirm.setOnClickListener(v -> { MemoActivity activity = ((MemoActivity) getActivity()); activity.showMemo(inputText.getText().toString()); }); } } public class MemoActivity extends BaseActivity { public void showMemo(String memo) { displayFragment.showMemo(memo); } } public class MemoDisplayFragment extends BaseFragment { public void showMemo(String memo) { txtMemo.setText(memo); } } Traditional
  35. public class MemoTypeFragment extends BaseFragment { private void setButton() {

    btnConfirm.setOnClickListener(v -> { MemoActivity activity = ((MemoActivity) getActivity()); activity.showMemo(inputText.getText().toString()); }); } } public class MemoActivity extends BaseActivity { public void showMemo(String memo) { displayFragment.showMemo(memo); } } public class MemoDisplayFragment extends BaseFragment { public void showMemo(String memo) { txtMemo.setText(memo); } } Traditional
  36. public class MemoTypeFragment extends BaseFragment { private void setButton() {

    btnConfirm.setOnClickListener(v -> { MemoActivity activity = ((MemoActivity) getActivity()); activity.showMemo(inputText.getText().toString()); }); } } public class MemoActivity extends BaseActivity { public void showMemo(String memo) { displayFragment.showMemo(memo); } } public class MemoDisplayFragment extends BaseFragment { public void showMemo(String memo) { txtMemo.setText(memo); } } Traditional
  37. public class MemoTypeFragment extends BaseFragment { private void setButton() {

    btnConfirm.setOnClickListener(v -> { MemoActivity activity = ((MemoActivity) getActivity()); activity.showMemo(inputText.getText().toString()); }); } } public class MemoActivity extends BaseActivity { public void showMemo(String memo) { displayFragment.showMemo(memo); } } public class MemoDisplayFragment extends BaseFragment { public void showMemo(String memo) { txtMemo.setText(memo); } } Traditional ࠗݽ Activityܳ Ѣ୛ Dataܳ ੹׳
  38. public class MemoTypeFragment extends BaseFragment { private void setButton() {

    viewModel = ViewModelProviders.of(activity).get(MemoViewModel.class); btnConfirm.setOnClickListener(v -> viewModel.setMemo(inputText.getText().toString())); } } public class MemoViewModel extends ViewModel { private MutableLiveData<String> memo = new MutableLiveData<>(); public void setMemo(String memo) { this.memo.postValue(memo); } } public class MemoDisplayFragment extends BaseFragment { public void onActivityCreated(@Nullable Bundle savedInstanceState) { viewModel = ViewModelProviders.of(activity).get(MemoViewModel.class); viewModel.getMemo().observe(this, memo -> txtMemo.setText(memo)); } } Jetpack
  39. public class MemoTypeFragment extends BaseFragment { private void setButton() {

    viewModel = ViewModelProviders.of(activity).get(MemoViewModel.class); btnConfirm.setOnClickListener(v -> viewModel.setMemo(inputText.getText().toString())); } } public class MemoViewModel extends ViewModel { private MutableLiveData<String> memo = new MutableLiveData<>(); public void setMemo(String memo) { this.memo.postValue(memo); } } public class MemoDisplayFragment extends BaseFragment { public void onActivityCreated(@Nullable Bundle savedInstanceState) { viewModel = ViewModelProviders.of(activity).get(MemoViewModel.class); viewModel.getMemo().observe(this, memo -> txtMemo.setText(memo)); } } Jetpack
  40. public class MemoTypeFragment extends BaseFragment { private void setButton() {

    viewModel = ViewModelProviders.of(activity).get(MemoViewModel.class); btnConfirm.setOnClickListener(v -> viewModel.setMemo(inputText.getText().toString())); } } public class MemoViewModel extends ViewModel { private MutableLiveData<String> memo = new MutableLiveData<>(); public void setMemo(String memo) { this.memo.postValue(memo); } } public class MemoDisplayFragment extends BaseFragment { public void onActivityCreated(@Nullable Bundle savedInstanceState) { viewModel = ViewModelProviders.of(activity).get(MemoViewModel.class); viewModel.getMemo().observe(this, memo -> txtMemo.setText(memo)); } } Jetpack
  41. public class MemoTypeFragment extends BaseFragment { private void setButton() {

    viewModel = ViewModelProviders.of(activity).get(MemoViewModel.class); btnConfirm.setOnClickListener(v -> viewModel.setMemo(inputText.getText().toString())); } } public class MemoViewModel extends ViewModel { private MutableLiveData<String> memo = new MutableLiveData<>(); public void setMemo(String memo) { this.memo.postValue(memo); } } public class MemoDisplayFragment extends BaseFragment { public void onActivityCreated(@Nullable Bundle savedInstanceState) { viewModel = ViewModelProviders.of(activity).get(MemoViewModel.class); viewModel.getMemo().observe(this, memo -> txtMemo.setText(memo)); } } Jetpack
  42. public class MemoTypeFragment extends BaseFragment { private void setButton() {

    viewModel = ViewModelProviders.of(activity).get(MemoViewModel.class); btnConfirm.setOnClickListener(v -> viewModel.setMemo(inputText.getText().toString())); } } public class MemoViewModel extends ViewModel { private MutableLiveData<String> memo = new MutableLiveData<>(); public void setMemo(String memo) { this.memo.postValue(memo); } } public class MemoDisplayFragment extends BaseFragment { public void onActivityCreated(@Nullable Bundle savedInstanceState) { viewModel = ViewModelProviders.of(activity).get(MemoViewModel.class); viewModel.getMemo().observe(this, memo -> txtMemo.setText(memo)); } } Jetpack э਷ activity scopeীࢲ ೞա੄ ViewModel ё୓۽ Dataܳ ੹׳
  43. @Entity public class User { @PrimaryKey private long id; private

    String name; @ColumnInfo(name = "avatar_url") private String avatarUrl; @ColumnInfo(name = "created_at") private Date createdAt; @ColumnInfo(name = "updated_at") private Date updatedAt; } Jetpack
  44. @Entity public class User { @PrimaryKey private long id; private

    String name; @ColumnInfo(name = "avatar_url") private String avatarUrl; @ColumnInfo(name = "created_at") private Date createdAt; @ColumnInfo(name = "updated_at") private Date updatedAt; } Jetpack Annotationਵ۽ ప੉࠶ ੿੄
  45. @Dao public interface UserDao { @Query("SELECT * FROM user") Observable<List<User>>

    listUsers(); @Query("SELECT * FROM user WHERE id = :userId") User getUser(long userId); @Insert long insert(User user); @Update void update(User user); @Delete void delete(User user); } Jetpack
  46. @Dao public interface UserDao { @Query("SELECT * FROM user") Observable<List<User>>

    listUsers(); @Query("SELECT * FROM user WHERE id = :userId") User getUser(long userId); @Insert long insert(User user); @Update void update(User user); @Delete void delete(User user); } Jetpack Annotationਵ۽ Query ੿੄
  47. @Dao public interface UserDao { @Query("SELECT * FROM user") Observable<List<User>>

    listUsers(); @Query("SELECT * FROM user WHERE id = :userId") User getUser(long userId); @Insert long insert(User user); @Update void update(User user); @Delete void delete(User user); } Jetpack Rx, LiveData ૑ਗ
  48. WorkManager੉ۆ? OS ߡ੹ী ࢚ҙহ੉ ࠁܨ оמೠ ࠺زӝ ੘সਸ झநે݂ ೠ׮

    ex) ইஜ 9दী ୽੹ ઺੉ۄݶ জਸ সؘ੉౟ ೞৈۄ…
  49. Architecture Components੉ۆ? জਸ ౡౡೞҊ, పझ౟ оמೞҊ, ਬ૑ࠁࣻ ೞӝ औب۾ ذח

    ۄ੉࠳۞ܻ ݽ਺ Jetpack੄ ௾ ஠పҊܻ ઺ ೞա ୹୊: https://developer.android.com/topic/libraries/architecture/