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

MVVM & Nested Fragment: Views - ViewModel Contr...

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.

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. 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
  3. 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
  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 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
  5. 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
  6. 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
  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 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
  8. 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
  9. 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
  10. 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
  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 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
  12. P O P U L A R A R C

    H I T EC T U R E S
  13. 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
  14. 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
  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 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
  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 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
  17. 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
  18. 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. U I

  20. 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 ?
  21. 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 ?
  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 ? 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 ?
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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 ?
  31. 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 ?
  32. 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
  33. F E AT U R E C R E E

    P ! ! ! ! !
  34. 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 ! ! ! ! !
  35. 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 ! ! ! ! !
  36. 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 ! ! ! ! !
  37. R E FAC TO R I NG C A L

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

    E R FAC E C A L L B AC K S
  39. 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
  40. L IV E DATA & V I E W M

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

    D E L R E F R E S H E R
  42. 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
  43. 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
  44. 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
  45. 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
  46. V I E W MO D E L S H

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

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

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

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

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

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

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

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

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

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

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

    A R E V I E W MO D E L
  58. 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
  59. 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
  60. 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
  61. 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 ?
  62. 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 ?
  63. 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 ?
  64. 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
  65. RegistrationViewModel L I V E D ATA UsernameFragment EmailFragment ProgressFragment

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

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

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

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

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

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

    InterestsFragment <ViewModelContract> <ViewModelContract> <ViewModelContract> <ViewModelContract> : UsernameFragmentViewModelContract : EmailFragmentViewModelContract : InterestsFragmentViewModelContract : ProgressFragmentViewModelContract
  72. 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
  73. 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
  74. RegistrationViewModel L I V E D ATA UsernameFragment EmailFragment InterestsFragment

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

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

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

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

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

    <ViewModelContract> <ViewModelContract> <ViewModelContract> : UsernameFragmentViewModelContract : EmailFragmentViewModelContract : InterestsFragmentViewModelContract : ProgressFragmentViewModelContract ProgressFragment <ViewModelContract>
  80. UsernameFragment EmailFragment InterestsFragment <ViewModelContract> <ViewModelContract> <ViewModelContract> : UsernameFragmentViewModelContract : EmailFragmentViewModelContract

    : InterestsFragmentViewModelContract : ProgressFragmentViewModelContract ProgressFragment <ViewModelContract> RegistrationMasterViewModel L I V E D ATA
  81. 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
  82. 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
  83. P R OG R E S S V I E

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

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

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

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

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

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

    W M O D E L CO N T R AC T
  90. 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
  91. 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
  92. 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
  93. 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
  94. E N FO R C I NG V I E

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

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

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

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

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

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

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

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

    N G T H E CO N T R AC T
  103. 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
  104. V I E W MO D E L CO N

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

    T R AC TS , A DVA NC E D
  106. 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/
  107. 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
  108. 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