post03_kivy_and_android.skr 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. (post
  2. :title "State of data-binding (MVVM) in Kivy and Android"
  3. :date (make-date* 2016 9 23)
  4. :tags '("data" "data-binding" "kivy" "python" "android" "sdk" "mvvm")
  5. (h4 [TLDR: Data-binding support is an essential component in the MVVM
  6. framework. While android has recently adopted data-binding, Kivy has always long had excellent data-binding support])
  7. (h3 [MVVM and data-binding])
  8. (p [(Skip if familiar) Application architectures like MVVM
  9. (Model, View, View-Model) exist to aid in separating view logic from
  10. business logic. What makes
  11. MVVM different, is its focus on binding data between the view and the
  12. view-model. What this means, is that data in the view and in the
  13. view-model are tightly coupled, in that any registerable event in the
  14. view, is immediately recognized by the view-model. Additionally, the
  15. view-model has hooks into the model, so it can format and send any
  16. changed model data directly to the view.])
  17. (h3 [Android and MVVM])
  18. (p [Android was designed, unlike iOS, to be architecture independent.
  19. For many small applications, this is preferable, since setting up
  20. MVVM requires many more files and added complexity.
  21. However, as an application continues
  22. to grow, it's easy for Android code can get messy. In particular,
  23. Activities, which already deal with life-cycles, and broadcast
  24. listeners, also has to manage view events from the view.
  25. Interacting with the view in activities
  26. makes for ugly code too: after typecasting each widget we want to
  27. interact with
  28. after finding it by id, even simple things like setting text or
  29. responding to a click
  30. event are awfully verbose.
  31. (Place example code here)
  32. Worse yet, since we are working directly with activities, nearly every
  33. test requires the full SDK,
  34. which implies we are stick with slow emulation testing.
  35. (Maybe a link here?)])
  36. (p [Clearly there was room for improvement, and a number of packages were since
  37. developed to address these issues. DI frameworks like ButterKnife (link)
  38. handle automatic view binding, and RoboBindings (link) allow view logic
  39. to be handled in a separate ViewModel class.
  40. ])
  41. (p [Since DI is separate software concept, let's instead focus on RoboBindings.
  42. RoboBindings is a great library because
  43. it allows for strong separation of concerns.
  44. Now, all the view logic originally in the Activity is implemented in a
  45. separate ViewModel class, freeing the Activity to focus on its main
  46. goals: controlling lifecycles, and listening for app/system-wide broadcasts.
  47. And unlike before, the viewmodel and view directly share the same data,
  48. and agree on an API to handle events like button clicks. Overall, this makes
  49. for much cleaner view logic code. Event listeners are gone, since the
  50. view chooses how to delegate events to the view-model, and in the view-model
  51. we don't have to manually update any view widget since any data change
  52. in the view-model automatically updates the view!])
  53. (p [Yeah, data-binding is pretty damn genius. Just one last point: the viewmodel
  54. can be any standard POJO (plain old java object) which allows native
  55. unit testing.])
  56. (p [Robobinding is of course a 3rd party library, which is somewhat risky for a
  57. large app developer that needs reliability. Fortunately, Android
  58. has since Marshmallow, provided its own databinding library: (link
  59. here)
  60. While I still personally use robobinding, it looks solid, and
  61. follows a similar approach])
  62. (h3 [Okay so what is/why Kivy?])
  63. (p [I have a feeling y'all are less likely to have heard of Kivy. Essentially
  64. it's a well-established cross platform application framework written
  65. in Python. It was originally designed to run on large multi-touch
  66. screens, but has since been ported to run on Android and iOS. One of
  67. it's coolest features is kv-lang, an alternative to XML seen in Android,
  68. that makes it really easy to build a widget tree, with access to
  69. the full power of Python. In addition it has amazing data-binding
  70. support.])
  71. (p [Since we are familiar with what data-binding looks like in Android, let's
  72. get right to some code.])
  73. (ul
  74. (li [calling a proc in kv:])
  75. (li [binding an item in kv to field in python:])
  76. (li [hooks for receiving event from view and updating view:])
  77. (li [managing simple view logic within the view:])))