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

MVVM & Nested Fragment: Views - ViewModel Contracts

MVVM & Nested Fragment: Views - ViewModel Contracts

As smartphones get more and more powerful and with bigger screens, the complexity of tasks that people want to be able to do with them follow the same growth. Google has become very opinionated about how to do architecture and best practices, but examples are often overly simplified and not very dynamic.

Fragments are making a comeback, combined with LiveData and ViewModels. As we adopt these architecture components into our apps, how can we build support for complex use cases with multiple fragments on screen? Should each fragment have a ViewModel? What about custom views?

ViewModel Contracts propose the idea of having fragments (and potentially custom views) declare a ViewModel & LiveData via interface to abstract out all of the communication details between nested fragments and their parents. In this talk, we explain in detail how to implement them, how architecture components deal with ViewModels, ViewModelFactories and how we can reuse them.

Dfd02975cecba29510f6f8464e5bc916?s=128

Marcos Paulo Souza Damasceno

November 25, 2019
Tweet

More Decks by Marcos Paulo Souza Damasceno

Other Decks in Programming

Transcript

  1. MV V M & N E S T E D

    F R AG M E N T/ V I E W S - V I E W M O D E L CO N T R AC TS M A R C O S DA M A S C E N O S R . A N D R O I D E NG I N E E R @marcospaulosd B R E T E R I C K S O N A N D R O I D E NG I N E E R I NG M A N AG E R linkedin.com/in/breterickson/
  2. None
  3. None
  4. A N D R O I D H O N

    E YCO M B R E L E A S E D O N F E B , 2 01 1
  5. A N D R O I D H O N

    E YCO M B R E L E A S E D O N F E B , 2 01 1 C O M P L E X A N D DY N A M I C V I E W S B EC A M E MO R E PO P U L A R
  6. A N D R O I D H O N

    E YCO M B R E L E A S E D O N F E B , 2 01 1 C O M P L E X A N D DY N A M I C V I E W S B EC A M E MO R E PO P U L A R H E L LO F R AG M E N TS
  7. A N D R O I D U I CO

    M P L E X I T Y TA B L E TS A R E N OW H E R E TO B E S E E N B U T P H O N E S A R E B I G
  8. A N D R O I D U I CO

    M P L E X I T Y TA B L E TS A R E N OW H E R E TO B E S E E N B U T P H O N E S A R E B I G A P P S A R E B EC O M I NG C O N S TA N T LY C O M P L E X
  9. A N D R O I D U I CO

    M P L E X I T Y TA B L E TS A R E N OW H E R E TO B E S E E N B U T P H O N E S A R E B I G A P P S A R E B EC O M I NG C O N S TA N T LY C O M P L E X
  10. H O W D O W E CO M M

    U N IC AT E B E T W E E N S O M A N Y D I F F E R E N T V I E W S A N D F R AG M E N T S
  11. CO M M U N I C AT I O

    N B E T W E E N F R AG M E N T S I N T E N T B R OA D C A S T/ B U N D L E S
  12. CO M M U N I C AT I O

    N B E T W E E N F R AG M E N T S C A L L B AC K S & I N T E R FAC E S O NC L IC K L I ST E N E R I M P L BY AC T IV I T Y - C A L L BAC K H E L L C A ST AC T IV I T Y I N TO A N I N T E R FAC E I N S I D E T H E F R AG M E N T - W H AT I F I WA N T TO R E U S E I T 
 I N S I D E A NOT H E R F R AG M E N T V I E W CO N T R O L L E R S J U G G L E CO M M U N I C AT IO N LOG IC
  13. CO M M U N I C AT I O

    N B E T W E E N F R AG M E N T S R X S U B J EC TS I N J EC T I N G A N R X S U B J EC T I N TO A F R AG M E N T ’ S V M / P R E S E N T E R M AY E X PO S E M U TA B L E VA L U E S E V E RY W H E R E R X S U B J EC T S A R E N OT FAU LT TO L E R A N T - AT T H E E N D, A FA NC Y A LT E R N AT IV E TO E V E N T B U S - O N E E R R O R E M I T T E D 
 A N D YO U ’ R E D O N E D I F F IC U LT TO D E B U G & U N D E R S TA N D F LOW A S CO M P L E X I T Y I NC R E A S E S
  14. P O P U L A R A R C

    H I T EC T U R E S
  15. P O P U L A R A R C

    H I T EC T U R E S D O M A I N - DATA
  16. P O P U L A R A R C

    H I T EC T U R E S D O M A I N - DATA DATA L AY E R R E P O S I TO RY DATA S TO R E A P I C L I E N T
  17. P O P U L A R A R C

    H I T EC T U R E S D O M A I N - DATA DATA L AY E R R E P O S I TO RY DATA S TO R E A P I C L I E N T D O M A I N I N T E R FAC E S U S E C A S E S
  18. P O P U L A R A R C

    H I T EC T U R E S D O M A I N - DATA DATA L AY E R R E P O S I TO RY DATA S TO R E A P I C L I E N T D O M A I N I N T E R FAC E S U S E C A S E S U I V I E W M O D E L S F R AG M E N TS C U STO M V I E W S
  19. P O P U L A R A R C

    H I T EC T U R E S U I V I E W M O D E L S F R AG M E N TS C U STO M V I E W S
  20. U I V I E W M O D E

    L S F R AG M E N TS C U STO M V I E W S
  21. U I

  22. U I D O U BT S S H O

    U L D I H AV E O N E V I E W MO D E L FO R M U LT I P L E F R AG M E N T S ?
  23. U I D O U BT S S H O

    U L D I H AV E O N E V I E W MO D E L FO R M U LT I P L E F R AG M E N T S ? I S I T O K TO H AV E V I E W MO D E L S FO R C U STO M V I E W S ?
  24. U I D O U BT S S H O

    U L D I H AV E O N E V I E W MO D E L FO R M U LT I P L E F R AG M E N T S ? I S I T O K TO H AV E V I E W MO D E L S FO R C U STO M V I E W S ? S H O U L D W E S H A R E V I E W MO D E L S AC R O S S M U LT I P L E U I E L E M E N T S ?
  25. I N T R O D U C I N

    G
  26. V I E W M O D E L CO

    N T R AC T S I N T R O D U C I N G
  27. B U T B E F O R E

  28. B U T B E F O R E L

    E T ’ S A N A LYZ E A S I M P L E P R O B L E M
  29. A R EG I ST R AT IO N F

    LO W
  30. R EG I ST R AT IO N F LOW

    : F E AT U R E R EQ U I R E M E N TS
  31. R EG I ST R AT IO N F LOW

    : F E AT U R E R EQ U I R E M E N TS U S E R N A M E
  32. R EG I ST R AT IO N F LOW

    : F E AT U R E R EQ U I R E M E N TS E M A I L U S E R N A M E
  33. R EG I ST R AT IO N F LOW

    : F E AT U R E R EQ U I R E M E N TS I N T E R E S TS E M A I L U S E R N A M E
  34. R EG I ST R AT IO N F LOW

    : F E AT U R E R EQ U I R E M E N TS I N T E R E S TS E M A I L U S E R N A M E
  35. B A S I C I M P L E

    M E N TAT I O N M U LT I - AC T IV I T Y ?
  36. B A S I C I M P L E

    M E N TAT I O N M U LT I - AC T IV I T Y ?
  37. B A S I C I M P L E

    M E N TAT I O N M U LT I - AC T IV I T Y ? C A L L B AC K I N T E R FAC E S
  38. RegistrationActivity

  39. RegistrationActivity RegistrationViewModel L I V E D ATA

  40. RegistrationActivity UsernameFragment RegistrationViewModel L I V E D ATA

  41. RegistrationActivity UsernameFragment UsernameCallback RegistrationViewModel L I V E D ATA

  42. F E AT U R E C R E E

    P ! ! ! ! !
  43. P E R S I S T E D R

    EG I S T R AT I O N P R O G R E S S U I F E AT U R E C R E E P ! ! ! ! !
  44. P E R S I S T E D R

    EG I S T R AT I O N P R O G R E S S U I F E AT U R E C R E E P ! ! ! ! !
  45. P E R S I S T E D R

    EG I S T R AT I O N P R O G R E S S U I F E AT U R E C R E E P ! ! ! ! !
  46. R E FAC TO R I NG C A L

    L B AC K I N T E R FAC E S
  47. R E FAC TO R I NG I N T

    E R FAC E C A L L B AC K S
  48. R E FAC TO R I NG S H A

    R E D V I E W MO D E L I N T E R FAC E C A L L B AC K S
  49. UsernameFragment RegistrationActivity RegistrationViewModel L I V E D ATA UsernameCallback

  50. UsernameFragment RegistrationActivity RegistrationViewModel L I V E D ATA

  51. UsernameFragment ProgressFragment EmailFragment InterestsFragment RegistrationActivity RegistrationViewModel L I V E

    D ATA
  52. L IV E DATA & V I E W M

    O D E L R E F R E S H E R
  53. L IV E DATA & V I E W MO

    D E L R E F R E S H E R
  54. L IV E DATA & V I E W MO

    D E L R E F R E S H E R L IV E DATA
  55. L IV E DATA W O R K S L

    I K E A B E H AV I O R S U B J EC T
  56. L IV E DATA W O R K S L

    I K E A B E H AV I O R S U B J EC T E V E RY T I M E W E S U B S C R I B E TO I T, I T E M I TS T H E L AT E ST VA L U E
  57. L IV E DATA W O R K S L

    I K E A B E H AV I O R S U B J EC T E V E RY T I M E W E S U B S C R I B E TO I T, I T E M I TS T H E L AT E ST VA L U E O B S E R VAT IO N C A N B E AT TAC H E D TO A L I F EC YC L E
  58. V I E W MO D E L

  59. V I E W MO D E L S H

    A R E V I E W MO D E L
  60. V I E W MO D E L S H

    A R E V I E W MO D E L
  61. V I E W MO D E L S H

    A R E V I E W MO D E L
  62. V I E W MO D E L S H

    A R E V I E W MO D E L
  63. V I E W MO D E L S H

    A R E V I E W MO D E L
  64. V I E W MO D E L S H

    A R E V I E W MO D E L
  65. V I E W MO D E L S H

    A R E V I E W MO D E L
  66. V I E W MO D E L S H

    A R E V I E W MO D E L
  67. V I E W MO D E L S H

    A R E V I E W MO D E L
  68. V I E W MO D E L S H

    A R E V I E W MO D E L
  69. V I E W MO D E L S H

    A R E V I E W MO D E L WAIT A MINUTE!!!!
  70. V I E W MO D E L S H

    A R E V I E W MO D E L
  71. V I E W MO D E L

  72. V I E W MO D E L V I

    E W M O D E L I N STA N C E S A R E S TO R E D I N A M A P W H E R E T H E C A NO N I C A L N A M E I S A K E Y A N D T H E C L A Z Z I S T H E VA LU E
  73. V I E W MO D E L V I

    E W M O D E L I N STA N C E S A R E S TO R E D I N A M A P W H E R E T H E C A NO N I C A L N A M E I S A K E Y A N D T H E C L A Z Z I S T H E VA LU E W H E N W E D O N ’ T F I N D A N I N S TA N C E FO R T H AT C L A S S C A N O N I C A L N A M E , W E T H E N M A K E A N E W I N STA NC E A N D A D D TO T H E M A P
  74. V I E W MO D E L I F

    W E H AV E T H E C L A Z Z O F T H E V I E W M O D E L T H AT W E WA N T
  75. V I E W MO D E L I F

    W E H AV E T H E C L A Z Z O F T H E V I E W M O D E L T H AT W E WA N T C A N W E PA S S T H AT I N F O R M AT IO N TO O U R F R AG M E N TS A N D H AV E I T LO O K F O R T H E V I E W M O D E L F O R T H AT C L A Z Z ?
  76. V I E W MO D E L I F

    W E H AV E T H E C L A Z Z O F T H E V I E W M O D E L T H AT W E WA N T C A N W E PA S S T H AT I N F O R M AT IO N TO O U R F R AG M E N TS A N D H AV E I T LO O K F O R T H E V I E W M O D E L F O R T H AT C L A Z Z ? C A N W E ACC E S S T H E PA R E N T V I E W MO D E L W I T HO U T K NOW I NG W H AT T H AT PA R E N T V I E W M O D E L I S ?
  77. V I E W MO D E L I F

    W E H AV E T H E C L A Z Z O F T H E V I E W M O D E L T H AT W E WA N T C A N W E PA S S T H AT I N F O R M AT IO N TO O U R F R AG M E N TS A N D H AV E I T LO O K F O R T H E V I E W M O D E L F O R T H AT C L A Z Z ? C A N W E ACC E S S T H E PA R E N T V I E W MO D E L W I T HO U T K NOW I NG W H AT T H AT PA R E N T V I E W M O D E L I S ?
  78. V I E W MO D E L I F

    W E H AV E T H E C L A Z Z O F T H E V I E W M O D E L T H AT W E WA N T C A N W E PA S S T H AT I N F O R M AT IO N TO O U R F R AG M E N TS A N D H AV E I T LO O K F O R T H E V I E W M O D E L F O R T H AT C L A Z Z ? C A N W E ACC E S S T H E PA R E N T V I E W MO D E L W I T HO U T K NOW I NG W H AT T H AT PA R E N T V I E W M O D E L I S ? Y E S W E C A N
  79. UsernameFragment ProgressFragment EmailFragment InterestsFragment RegistrationActivity RegistrationViewModel L I V E

    D ATA
  80. UsernameFragment ProgressFragment EmailFragment InterestsFragment RegistrationViewModel L I V E D

    ATA
  81. RegistrationViewModel L I V E D ATA UsernameFragment EmailFragment ProgressFragment

    InterestsFragment <ViewModelContract>
  82. RegistrationViewModel L I V E D ATA UsernameFragment EmailFragment ProgressFragment

    InterestsFragment <ViewModelContract> <ViewModelContract>
  83. RegistrationViewModel L I V E D ATA UsernameFragment EmailFragment ProgressFragment

    InterestsFragment <ViewModelContract> <ViewModelContract> <ViewModelContract>
  84. RegistrationViewModel L I V E D ATA UsernameFragment EmailFragment ProgressFragment

    InterestsFragment <ViewModelContract> <ViewModelContract> <ViewModelContract> <ViewModelContract>
  85. RegistrationViewModel L I V E D ATA UsernameFragment EmailFragment ProgressFragment

    InterestsFragment <ViewModelContract> <ViewModelContract> <ViewModelContract> <ViewModelContract> : UsernameFragmentViewModelContract
  86. RegistrationViewModel L I V E D ATA UsernameFragment EmailFragment ProgressFragment

    InterestsFragment <ViewModelContract> <ViewModelContract> <ViewModelContract> <ViewModelContract> : UsernameFragmentViewModelContract : EmailFragmentViewModelContract
  87. RegistrationViewModel L I V E D ATA UsernameFragment EmailFragment ProgressFragment

    InterestsFragment <ViewModelContract> <ViewModelContract> <ViewModelContract> <ViewModelContract> : UsernameFragmentViewModelContract : EmailFragmentViewModelContract : InterestsFragmentViewModelContract
  88. RegistrationViewModel L I V E D ATA UsernameFragment EmailFragment ProgressFragment

    InterestsFragment <ViewModelContract> <ViewModelContract> <ViewModelContract> <ViewModelContract> : UsernameFragmentViewModelContract : EmailFragmentViewModelContract : InterestsFragmentViewModelContract : ProgressFragmentViewModelContract
  89. RegistrationViewModel L I V E D ATA UsernameFragment EmailFragment ProgressFragment

    InterestsFragment <ViewModelContract> <ViewModelContract> <ViewModelContract> <ViewModelContract> : UsernameFragmentViewModelContract : EmailFragmentViewModelContract : InterestsFragmentViewModelContract : ProgressFragmentViewModelContract arguments.put(key, RegistrationViewModel::class.java) instantiate
  90. RegistrationViewModel L I V E D ATA UsernameFragment EmailFragment InterestsFragment

    <ViewModelContract> <ViewModelContract> <ViewModelContract> : UsernameFragmentViewModelContract : EmailFragmentViewModelContract : InterestsFragmentViewModelContract : ProgressFragmentViewModelContract arguments.put(key, RegistrationViewModel::class.java) VMProvider.of(…).get(arguments[key]) as UsernameFragmentViewModelContract) instantiate
  91. RegistrationViewModel L I V E D ATA UsernameFragment EmailFragment InterestsFragment

    <ViewModelContract> <ViewModelContract> <ViewModelContract> : UsernameFragmentViewModelContract : EmailFragmentViewModelContract : InterestsFragmentViewModelContract : ProgressFragmentViewModelContract ProgressFragment <ViewModelContract>
  92. RegistrationViewModel L I V E D ATA UsernameFragment EmailFragment InterestsFragment

    <ViewModelContract> <ViewModelContract> <ViewModelContract> : UsernameFragmentViewModelContract : EmailFragmentViewModelContract : InterestsFragmentViewModelContract : ProgressFragmentViewModelContract ProgressFragment <ViewModelContract>
  93. RegistrationViewModel L I V E D ATA UsernameFragment EmailFragment InterestsFragment

    <ViewModelContract> <ViewModelContract> <ViewModelContract> : UsernameFragmentViewModelContract : EmailFragmentViewModelContract : InterestsFragmentViewModelContract : ProgressFragmentViewModelContract ProgressFragment <ViewModelContract>
  94. RegistrationViewModel L I V E D ATA UsernameFragment EmailFragment InterestsFragment

    <ViewModelContract> <ViewModelContract> <ViewModelContract> : UsernameFragmentViewModelContract : EmailFragmentViewModelContract : InterestsFragmentViewModelContract : ProgressFragmentViewModelContract ProgressFragment <ViewModelContract>
  95. RegistrationViewModel L I V E D ATA UsernameFragment EmailFragment InterestsFragment

    <ViewModelContract> <ViewModelContract> <ViewModelContract> : UsernameFragmentViewModelContract : EmailFragmentViewModelContract : InterestsFragmentViewModelContract : ProgressFragmentViewModelContract ProgressFragment <ViewModelContract>
  96. RegistrationViewModel L I V E D ATA UsernameFragment EmailFragment InterestsFragment

    <ViewModelContract> <ViewModelContract> <ViewModelContract> : UsernameFragmentViewModelContract : EmailFragmentViewModelContract : InterestsFragmentViewModelContract : ProgressFragmentViewModelContract ProgressFragment <ViewModelContract>
  97. RegistrationViewModel L I V E D ATA RegistrationActivity

  98. None
  99. RegistrationMasterViewModel L I V E D ATA RegistrationMasterFragment

  100. RegistrationMasterViewModel L I V E D ATA RegistrationMasterFragment

  101. UsernameFragment EmailFragment InterestsFragment <ViewModelContract> <ViewModelContract> <ViewModelContract> : UsernameFragmentViewModelContract : EmailFragmentViewModelContract

    : InterestsFragmentViewModelContract : ProgressFragmentViewModelContract ProgressFragment <ViewModelContract> RegistrationMasterViewModel L I V E D ATA
  102. UsernameFragment EmailFragment InterestsFragment <ViewModelContract> <ViewModelContract> <ViewModelContract> : UsernameFragmentViewModelContract : EmailFragmentViewModelContract

    : InterestsFragmentViewModelContract : ProgressFragmentViewModelContract ProgressFragment <ViewModelContract> RegistrationMasterViewModel L I V E D ATA arguments.put(key, RegistrationMasterViewModel::class.java) instantiate
  103. A R EG I ST R AT I O N

    F LO W W I T H V I E W M O D E L CO N T R AC T S
  104. ProgressFragment RegistrationActivity RegistrationViewModel L I V E D ATA

  105. ProgressFragment RegistrationActivity RegistrationViewModel L I V E D ATA

  106. ProgressFragment RegistrationActivity RegistrationViewModel L I V E D ATA

  107. ProgressFragment L I V E D ATA RegistrationActivity RegistrationViewModel L

    I V E D ATA 1 ProgressViewModelContract
  108. ProgressFragment L I V E D ATA RegistrationActivity RegistrationViewModel L

    I V E D ATA 1 2 ProgressViewModelContract
  109. ProgressFragment L I V E D ATA RegistrationActivity RegistrationViewModel L

    I V E D ATA 1 2 3 ProgressViewModelContract
  110. ProgressFragment ProgressViewModelContract L I V E D ATA RegistrationActivity RegistrationViewModel

    L I V E D ATA 1 2 3 4
  111. P R OG R E S S V I E

    W MO D E L CO N T R AC T
  112. P R OG R E S S V I E

    W MO D E L CO N T R AC T
  113. P R OG R E S S V I E

    W MO D E L CO N T R AC T
  114. U S I NG T H E V I E

    W M O D E L CO N T R AC T
  115. U S I NG T H E V I E

    W M O D E L CO N T R AC T
  116. U S I NG T H E V I E

    W M O D E L CO N T R AC T
  117. U S I NG T H E V I E

    W M O D E L CO N T R AC T
  118. R E T R I E V I N G

    A V I E W MO D E L CO N T R AC T
  119. R E T R I E V I N G

    A V I E W MO D E L CO N T R AC T
  120. R E T R I E V I N G

    A V I E W MO D E L CO N T R AC T
  121. R E T R I E V I N G

    A V I E W MO D E L CO N T R AC T
  122. E N FO R C I NG V I E

    W MO D E L CO N T R AC T U SAG E
  123. E N FO R C I NG V I E

    W MO D E L CO N T R AC T U SAG E
  124. R EG I ST E R I NG T H

    E V I E W MO D E L K E Y
  125. C R E AT I N G T H E

    F R AG M E N T
  126. C R E AT I N G T H E

    F R AG M E N T
  127. C R E AT I N G T H E

    F R AG M E N T
  128. I M P L E M E N T I

    N G T H E CO N T R AC T
  129. I M P L E M E N T I

    N G T H E CO N T R AC T
  130. I M P L E M E N T I

    N G T H E CO N T R AC T
  131. V I E W MO D E L CO N

    T R AC TS , A DVA NC E D PA R E N T F R AG M E N T S & AC T IV I T I E S M ATC H BY L A Z Y S T Y L E
  132. V I E W MO D E L CO N

    T R AC TS , A DVA NC E D
  133. V I E W MO D E L CO N

    T R AC TS , A DVA NC E D
  134. TA K E AWAYS

  135. TA K E AWAYS Decouples Fragments

  136. TA K E AWAYS Decouples Fragments Reduces Scope

  137. TA K E AWAYS Decouples Fragments Reduces Scope Improves reusability

  138. TA K E AWAYS

  139. MV V M & N E S T E D

    F R AG M E N T/ V I E W S - V I E W M O D E L CO N T R AC TS M A R C O S DA M A S C E N O S R . A N D R O I D E NG I N E E R @marcospaulosd B R E T E R I C K S O N A N D R O I D E NG I N E E R I NG M A N AG E R linkedin.com/in/breterickson/
  140. MV V M & N E S T E D

    F R AG M E N T/ V I E W S - V I E W M O D E L CO N T R AC TS M A R C O S DA M A S C E N O S R . A N D R O I D E NG I N E E R @marcospaulosd B R E T E R I C K S O N A N D R O I D E NG I N E E R I NG M A N AG E R linkedin.com/in/breterickson/ T H A N K YO U
  141. MV V M & N E S T E D

    F R AG M E N T/ V I E W S - V I E W M O D E L CO N T R AC TS M A R C O S DA M A S C E N O S R . A N D R O I D E NG I N E E R @marcospaulosd B R E T E R I C K S O N A N D R O I D E NG I N E E R I NG M A N AG E R linkedin.com/in/breterickson/ T H A N K YO U http://bit.ly/
 ViewModelContracts