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

Android Architecture Components

Viraj Tank
September 06, 2017

Android Architecture Components

Slides from my presentation on AAC at,

Android Summit, Washington D.C. 2017, 24-Aug-2017
- http://androidsummit.org/

Viraj Tank

September 06, 2017
Tweet

More Decks by Viraj Tank

Other Decks in Programming

Transcript

  1. - Lifecycle changes - Configuration changes Reloading the view Data

    call may get interrupted Context Leak View state management Problem Summary
  2. Activity class MyActiviy : Activity() { private val myPresenterFactory =

    MyPresenterFactory() private lateinit var myPresenter: MyPresenter override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) myPresenter = myPresenterFactory.getPresenter() } class MyActiviy : Activity() { private val myPresenterFactory = MyPresenterFactory() private lateinit var myPresenter: MyPresenter override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) myPresenter = myPresenterFactory.getPresenter() } override fun onStart() { super.onStart() myPresenter.bind(this) } override fun onStop() { myPresenter.unBind() super.onStop() } class MyActiviy : Activity() { private val myPresenterFactory = MyPresenterFactory() private lateinit var myPresenter: MyPresenter override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) myPresenter = myPresenterFactory.getPresenter() } override fun onStart() { super.onStart() myPresenter.bind(this) } override fun onStop() { myPresenter.unBind() super.onStop() } override fun onDestroy() { if (!isChangingConfigurations) { myPresenter.dispose() } super.onDestroy() } }
  3. Fragment class MyFragment : Fragment() { private val myPresenter =

    MyPresenter() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) retainInstance = true } class MyFragment : Fragment() { private val myPresenter = MyPresenter() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) retainInstance = true } override fun onStart() { super.onStart() myPresenter.bind(this) } override fun onStop() { myPresenter.unBind() super.onStop() } class MyFragment : Fragment() { private val myPresenter = MyPresenter() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) retainInstance = true } override fun onStart() { super.onStart() myPresenter.bind(this) } override fun onStop() { myPresenter.unBind() super.onStop() } override fun onDestroy() { myPresenter.dispose() super.onDestroy() } }
  4. Presenter class MyPresenter { private val hotObservable = BehaviorSubject.create<Model>() private

    val viewSubscriptions = CompositeDisposable() private val dataSubscriptions = CompositeDisposable() class MyPresenter { private val hotObservable = BehaviorSubject.create<Model>() private val viewSubscriptions = CompositeDisposable() private val dataSubscriptions = CompositeDisposable() fun bind(view: MyFragment) { viewSubscriptions.add(hotObservable .observeOn(AndroidSchedulers.mainThread()) .subscribe({ this.view.updateView(it) })) } class MyPresenter { private val hotObservable = BehaviorSubject.create<Model>() private val viewSubscriptions = CompositeDisposable() private val dataSubscriptions = CompositeDisposable() fun bind(view: MyFragment) { viewSubscriptions.add(hotObservable .observeOn(AndroidSchedulers.mainThread()) .subscribe({ this.view.updateView(it) })) } fun unBind() { viewSubscriptions.clear() this.view = null }
  5. Presenter class MyPresenter { fun getData() { dataSubscriptions.add(myDataSource.getData()) .subscribeOn(Schedulers.computation()) .observeOn(Schedulers.computation())

    .subscribe({ modelData -> hotObservable.onNext(modelData) }) } class MyPresenter { init { getData() } fun getData() { dataSubscriptions.add(myDataSource.getData()) .subscribeOn(Schedulers.computation()) .observeOn(Schedulers.computation()) .subscribe({ modelData -> hotObservable.onNext(modelData) }) } class MyPresenter { init { getData() } fun getData() { dataSubscriptions.add(myDataSource.getData()) .subscribeOn(Schedulers.computation()) .observeOn(Schedulers.computation()) .subscribe({ modelData -> hotObservable.onNext(modelData) }) } fun dispose() { dataSubscriptions.dispose() } }
  6. Unidirectional Dataflow data class MyViewState(val myData: String = "", val

    loadingState: Boolean = false) data class MyViewState(val myData: String = "", val loadingState: Boolean = false) class MyPresenter { private var myViewState = MyViewState() private val hotObservable = BehaviorSubject.create<MyViewState>() data class MyViewState(val myData: String = "", val loadingState: Boolean = false) class MyPresenter { private var myViewState = MyViewState() private val hotObservable = BehaviorSubject.create<MyViewState>() fun getData() { myViewState = myViewState.copy(myData = data, loadingState = fals hotObservable.onNext(myViewState) } }
  7. isConsumed data class MyViewState(val isConsumed: Boolean = true, val errorMessage:

    String = "") data class MyViewState(val isConsumed: Boolean = true, val errorMessage: String = "") class MyPresenter { private var myViewState = MyViewState() private val hotObservable = BehaviorSubject.create<MyViewState>() data class MyViewState(val isConsumed: Boolean = true, val errorMessage: String = "") class MyPresenter { private var myViewState = MyViewState() private val hotObservable = BehaviorSubject.create<MyViewState>() fun getData() { myViewState = myViewState.copy(isConsumed = false, errorMessage = "Error") hotObservable.onNext(myViewState) } data class MyViewState(val isConsumed: Boolean = true, val errorMessage: String = "") class MyPresenter { private var myViewState = MyViewState() private val hotObservable = BehaviorSubject.create<MyViewState>() fun getData() { myViewState = myViewState.copy(isConsumed = false, errorMessage = "Error") hotObservable.onNext(myViewState) } class MyFragment : Fragment() { fun updateView(myViewState: MyViewState) { if (!myViewState.isConsumed) { showError() myPresenter.errorConsumed() } } } data class MyViewState(val isConsumed: Boolean = true, val errorMessage: String = "") class MyPresenter { private var myViewState = MyViewState() private val hotObservable = BehaviorSubject.create<MyViewState>() fun getData() { myViewState = myViewState.copy(isConsumed = false, errorMessage = "Error") hotObservable.onNext(myViewState) } fun errorConsumed() { myViewState = myViewState.copy(true, "") hotObservable.onNext(myViewState) } } class MyFragment : Fragment() { fun updateView(myViewState: MyViewState) { if (!myViewState.isConsumed) { showError() myPresenter.errorConsumed() } } }
  8. Activity class MyActiviy : LifecycleActivity() { private val myViewModelFactory =

    MyViewModelFactory() private lateinit var myViewModel: MyViewModel override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) myViewModel = ViewModelProviders.of(this, myViewModelFactory) .get(MyViewModel::class.java) myViewModel.myLiveData.observe(this, Observer { /* Update View */ }) } }
  9. Fragment class MyFragment : LifecycleFragment() { private val myViewModelFactory =

    MyViewModelFactory() private lateinit var myViewModel: MyViewModel override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) myViewModel = ViewModelProviders.of(this, myViewModelFactory) .get(MyViewModel::class.java) myViewModel.myLiveData.observe(this, Observer { /* Update View */ }) } }
  10. ViewModel class MyViewModel : ViewModel() { private val dataSubscription =

    CompositeDisposable() private var myViewState = MyViewState() val myLiveData = MutableLiveData<MyViewState>() init { getData() } fun getData() { dataSubscriptions.add(myDataSource.getData()) .subscribeOn(Schedulers.computation()) .observeOn(Schedulers.computation()) .subscribe({ data -> myViewState = myViewState.copy(myData = data) myLiveData.postValue(myViewState) }) } override fun onCleared() { dataSubscription.dispose() super.onCleared() } }
  11. - Lifecycle changes - Configuration changes Reloading the view Data

    call may get interrupted Context Leak Recreating view state