index.html 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <!DOCTYPE html>
  2. <html lang="en-us">
  3. <head>
  4. <title>
  5. Simplifying MVI Architecture | codeskraps
  6. </title>
  7. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  8. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  9. <meta name="description" content="Your website description">
  10. <meta name="generator" content="Hugo 0.134.3">
  11. <link rel="canonical" href="https://codeskraps.com/posts/2024/mvi_architecture/" >
  12. <link href="/css/style.min.ee0d47e4d4346c71a65a9e873108c81ffae54d60a2fc2338f6df394eb4b25a82.css" rel="stylesheet">
  13. </head>
  14. <body>
  15. <div class="flexWrapper">
  16. <header class="headerWrapper">
  17. <div class="header">
  18. <div>
  19. <a class="terminal" href="https://codeskraps.com/">
  20. <span>me@codeskraps.com ~ $</span>
  21. </a>
  22. </div>
  23. <input class="side-menu" type="checkbox" id="side-menu">
  24. <label class="hamb" for="side-menu"><span class="hamb-line"></span></label>
  25. <nav class="headerLinks">
  26. <ul>
  27. <li>
  28. <a href="https://codeskraps.com/projects/" title="" >
  29. ~/projects</a>
  30. </li>
  31. <li>
  32. <a href="https://codeskraps.com/about/" title="" >
  33. ~/about</a>
  34. </li>
  35. <li>
  36. <a href="https://codeskraps.com/posts/" title="" >
  37. ~/posts</a>
  38. </li>
  39. </ul>
  40. </nav>
  41. </divi>
  42. <script async src="https://www.googletagmanager.com/gtag/js?id=G-YP8WK3KZF1"></script>
  43. <script>
  44. var doNotTrack = false;
  45. if ( false ) {
  46. var dnt = (navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack);
  47. var doNotTrack = (dnt == "1" || dnt == "yes");
  48. }
  49. if (!doNotTrack) {
  50. window.dataLayer = window.dataLayer || [];
  51. function gtag(){dataLayer.push(arguments);}
  52. gtag('js', new Date());
  53. gtag('config', 'G-YP8WK3KZF1');
  54. }
  55. </script>
  56. </header>
  57. <div class="content">
  58. <main class="main">
  59. <div class="postWrapper">
  60. <h1>Simplifying MVI Architecture</h1>
  61. <section class="postMetadata">
  62. <dl>
  63. <dt>tags</dt>
  64. <dd><span></span>
  65. <a href="/tags/kotlin/">#Kotlin</a><span></span>
  66. <a href="/tags/kmp/">#Kmp</a><span></span>
  67. <a href="/tags/android/">#Android</a><span></span>
  68. <a href="/tags/mvi/">#Mvi</a><span></span>
  69. <a href="/tags/architecture/">#Architecture</a><span></span>
  70. <a href="/tags/mvvm/">#Mvvm</a><span></span>
  71. <a href="/tags/viewmodel/">#Viewmodel</a></dd>
  72. <dt>published</dt>
  73. <dd><time datetime="2024-09-27">September 27, 2024</time></dd>
  74. <dt>reading time</dt>
  75. <dd>4 minutes</dd>
  76. </dl>
  77. </section>
  78. <div>
  79. <p>Model-View-Intent (MVI) is a powerful architectural pattern for building user interfaces, especially in Android development. In this post, we&rsquo;ll explore a helper class that simplifies the implementation of MVI, making it easier to manage state, handle user intents, and emit actions in your application.</p>
  80. <h2 id="the-mvi-helper-class">The MVI Helper Class</h2>
  81. <p>First, let&rsquo;s look at the complete helper class:</p>
  82. <div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-kotlin" data-lang="kotlin"><span style="display:flex;"><span><span style="color:#66d9ef">interface</span> <span style="color:#a6e22e">StateReceiver</span>&lt;STATE&gt; {
  83. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">suspend</span> <span style="color:#66d9ef">fun</span> <span style="color:#a6e22e">updateState</span>(transform: <span style="color:#66d9ef">suspend</span> (STATE) <span style="color:#f92672">-&gt;</span> STATE)
  84. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">suspend</span> <span style="color:#66d9ef">fun</span> <span style="color:#a6e22e">withState</span>(block: <span style="color:#66d9ef">suspend</span> (STATE) <span style="color:#f92672">-&gt;</span> Unit)
  85. </span></span><span style="display:flex;"><span>}
  86. </span></span><span style="display:flex;"><span>
  87. </span></span><span style="display:flex;"><span><span style="color:#66d9ef">suspend</span> <span style="color:#66d9ef">inline</span> <span style="color:#66d9ef">fun</span> &lt;<span style="color:#66d9ef">reified</span> <span style="color:#a6e22e">TYPE</span> : <span style="color:#a6e22e">STATE</span>, <span style="color:#a6e22e">STATE</span>&gt; <span style="color:#a6e22e">StateReceiver</span>&lt;STATE&gt;.withType(
  88. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">crossinline</span> block: <span style="color:#66d9ef">suspend</span> (TYPE) <span style="color:#f92672">-&gt;</span> Unit
  89. </span></span><span style="display:flex;"><span>) {
  90. </span></span><span style="display:flex;"><span> withState { state <span style="color:#f92672">-&gt;</span>
  91. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">if</span> (state <span style="color:#66d9ef">is</span> TYPE) {
  92. </span></span><span style="display:flex;"><span> block(state)
  93. </span></span><span style="display:flex;"><span> }
  94. </span></span><span style="display:flex;"><span> }
  95. </span></span><span style="display:flex;"><span>}
  96. </span></span><span style="display:flex;"><span>
  97. </span></span><span style="display:flex;"><span><span style="color:#66d9ef">suspend</span> <span style="color:#66d9ef">inline</span> <span style="color:#66d9ef">fun</span> &lt;<span style="color:#66d9ef">reified</span> <span style="color:#a6e22e">TYPE</span> : <span style="color:#a6e22e">STATE</span>, <span style="color:#a6e22e">STATE</span>&gt; <span style="color:#a6e22e">StateReceiver</span>&lt;STATE&gt;.updateWithType(
  98. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">crossinline</span> transform: <span style="color:#66d9ef">suspend</span> (TYPE) <span style="color:#f92672">-&gt;</span> TYPE
  99. </span></span><span style="display:flex;"><span>) {
  100. </span></span><span style="display:flex;"><span> withType&lt;TYPE, STATE&gt; { state <span style="color:#f92672">-&gt;</span>
  101. </span></span><span style="display:flex;"><span> updateState { transform(state) }
  102. </span></span><span style="display:flex;"><span> }
  103. </span></span><span style="display:flex;"><span>}
  104. </span></span><span style="display:flex;"><span>
  105. </span></span><span style="display:flex;"><span><span style="color:#66d9ef">interface</span> <span style="color:#a6e22e">StateProvider</span>&lt;STATE&gt; {
  106. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">val</span> state: StateFlow&lt;STATE&gt;
  107. </span></span><span style="display:flex;"><span>}
  108. </span></span><span style="display:flex;"><span>
  109. </span></span><span style="display:flex;"><span><span style="color:#66d9ef">interface</span> <span style="color:#a6e22e">IntentReceiver</span>&lt;INTENT&gt; {
  110. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">fun</span> <span style="color:#a6e22e">handleIntent</span>(intent: INTENT)
  111. </span></span><span style="display:flex;"><span>}
  112. </span></span><span style="display:flex;"><span>
  113. </span></span><span style="display:flex;"><span><span style="color:#66d9ef">interface</span> <span style="color:#a6e22e">ActionProvider</span>&lt;ACTION&gt; {
  114. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">val</span> action: Flow&lt;ACTION&gt;
  115. </span></span><span style="display:flex;"><span>}
  116. </span></span><span style="display:flex;"><span>
  117. </span></span><span style="display:flex;"><span><span style="color:#66d9ef">interface</span> <span style="color:#a6e22e">ActionReceiver</span>&lt;ACTION&gt; {
  118. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">suspend</span> <span style="color:#66d9ef">fun</span> <span style="color:#a6e22e">sendAction</span>(block: <span style="color:#66d9ef">suspend</span> () <span style="color:#f92672">-&gt;</span> ACTION)
  119. </span></span><span style="display:flex;"><span>}
  120. </span></span><span style="display:flex;"><span>
  121. </span></span><span style="display:flex;"><span><span style="color:#66d9ef">interface</span> <span style="color:#a6e22e">StateModule</span>&lt;STATE&gt; : StateReceiver&lt;STATE&gt;, StateProvider&lt;STATE&gt;
  122. </span></span><span style="display:flex;"><span><span style="color:#66d9ef">interface</span> <span style="color:#a6e22e">IntentModule</span>&lt;INTENT&gt; : IntentReceiver&lt;INTENT&gt;
  123. </span></span><span style="display:flex;"><span><span style="color:#66d9ef">interface</span> <span style="color:#a6e22e">ActionModule</span>&lt;ACTION&gt; : ActionReceiver&lt;ACTION&gt;, ActionProvider&lt;ACTION&gt;
  124. </span></span><span style="display:flex;"><span>
  125. </span></span><span style="display:flex;"><span><span style="color:#66d9ef">interface</span> <span style="color:#a6e22e">MVIViewModel</span>&lt;STATE, INTENT, ACTION&gt; : StateModule&lt;STATE&gt;, IntentModule&lt;INTENT&gt;, ActionModule&lt;ACTION&gt;
  126. </span></span><span style="display:flex;"><span>
  127. </span></span><span style="display:flex;"><span><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">MVIViewModelDelegate</span>&lt;STATE, INTENT, ACTION&gt;(
  128. </span></span><span style="display:flex;"><span> initial: STATE
  129. </span></span><span style="display:flex;"><span>) : MVIViewModel&lt;STATE, INTENT, ACTION&gt; {
  130. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">private</span> <span style="color:#66d9ef">val</span> _state = MutableStateFlow(initial)
  131. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">override</span> <span style="color:#66d9ef">val</span> state: StateFlow&lt;STATE&gt; = _state.asStateFlow()
  132. </span></span><span style="display:flex;"><span>
  133. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">private</span> <span style="color:#66d9ef">val</span> _action = Channel&lt;ACTION&gt;()
  134. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">override</span> <span style="color:#66d9ef">val</span> action: Flow&lt;ACTION&gt; = _action.receiveAsFlow()
  135. </span></span><span style="display:flex;"><span>
  136. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">override</span> <span style="color:#66d9ef">suspend</span> <span style="color:#66d9ef">fun</span> <span style="color:#a6e22e">updateState</span>(transform: <span style="color:#66d9ef">suspend</span> (STATE) <span style="color:#f92672">-&gt;</span> STATE) {
  137. </span></span><span style="display:flex;"><span> _state.update { transform(<span style="color:#66d9ef">it</span>) }
  138. </span></span><span style="display:flex;"><span> }
  139. </span></span><span style="display:flex;"><span>
  140. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">override</span> <span style="color:#66d9ef">suspend</span> <span style="color:#66d9ef">fun</span> <span style="color:#a6e22e">withState</span>(block: <span style="color:#66d9ef">suspend</span> (STATE) <span style="color:#f92672">-&gt;</span> Unit) {
  141. </span></span><span style="display:flex;"><span> block(_state.<span style="color:#66d9ef">value</span>)
  142. </span></span><span style="display:flex;"><span> }
  143. </span></span><span style="display:flex;"><span>
  144. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">override</span> <span style="color:#66d9ef">suspend</span> <span style="color:#66d9ef">fun</span> <span style="color:#a6e22e">sendAction</span>(block: <span style="color:#66d9ef">suspend</span> () <span style="color:#f92672">-&gt;</span> ACTION) {
  145. </span></span><span style="display:flex;"><span> _action.trySend(block())
  146. </span></span><span style="display:flex;"><span> }
  147. </span></span><span style="display:flex;"><span>
  148. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">override</span> <span style="color:#66d9ef">fun</span> <span style="color:#a6e22e">handleIntent</span>(intent: INTENT) {
  149. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">throw</span> NotImplementedError()
  150. </span></span><span style="display:flex;"><span> }
  151. </span></span><span style="display:flex;"><span>}</span></span></code></pre></div>
  152. <h2 id="understanding-the-mvi-helper-class">Understanding the MVI Helper Class</h2>
  153. <p>Let&rsquo;s break down the key components of our MVI helper class:</p>
  154. <h3 id="core-interfaces">Core Interfaces</h3>
  155. <ol>
  156. <li><code>StateReceiver&lt;STATE&gt;</code>: Allows updating and accessing the current state.</li>
  157. <li><code>StateProvider&lt;STATE&gt;</code>: Provides access to the state as a <code>StateFlow</code>.</li>
  158. <li><code>IntentReceiver&lt;INTENT&gt;</code>: Handles user intents.</li>
  159. <li><code>ActionProvider&lt;ACTION&gt;</code>: Provides a flow of actions.</li>
  160. <li><code>ActionReceiver&lt;ACTION&gt;</code>: Allows sending actions.</li>
  161. </ol>
  162. <h3 id="composite-interfaces">Composite Interfaces</h3>
  163. <ol>
  164. <li><code>StateModule&lt;STATE&gt;</code>: Combines state receiving and providing.</li>
  165. <li><code>IntentModule&lt;INTENT&gt;</code>: Wraps intent receiving.</li>
  166. <li><code>ActionModule&lt;ACTION&gt;</code>: Combines action receiving and providing.</li>
  167. <li><code>MVIViewModel&lt;STATE, INTENT, ACTION&gt;</code>: The main interface combining all MVI components.</li>
  168. </ol>
  169. <h3 id="helper-functions">Helper Functions</h3>
  170. <ol>
  171. <li><code>withType</code>: Allows type-safe state access.</li>
  172. <li><code>updateWithType</code>: Enables type-safe state updates.</li>
  173. </ol>
  174. <h3 id="the-mviviewmodeldelegate">The MVIViewModelDelegate</h3>
  175. <p>This class implements the <code>MVIViewModel</code> interface, providing a concrete implementation of the MVI pattern.</p>
  176. <h2 id="example-implementation">Example Implementation</h2>
  177. <p>Let&rsquo;s implement a simple counter application using our MVI helper class. Note that we can use either data classes or sealed interfaces for our State, Intent, and Action definitions:</p>
  178. <div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-kotlin" data-lang="kotlin"><span style="display:flex;"><span><span style="color:#75715e">// Define our State, Intent, and Action
  179. </span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">data</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">CounterState</span>(<span style="color:#66d9ef">val</span> count: Int = <span style="color:#ae81ff">0</span>)
  180. </span></span><span style="display:flex;"><span>
  181. </span></span><span style="display:flex;"><span><span style="color:#66d9ef">sealed</span> <span style="color:#66d9ef">interface</span> <span style="color:#a6e22e">CounterIntent</span> {
  182. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">object</span> <span style="color:#a6e22e">Increment</span> : CounterIntent
  183. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">object</span> <span style="color:#a6e22e">Decrement</span> : CounterIntent
  184. </span></span><span style="display:flex;"><span>}
  185. </span></span><span style="display:flex;"><span>
  186. </span></span><span style="display:flex;"><span><span style="color:#66d9ef">sealed</span> <span style="color:#66d9ef">interface</span> <span style="color:#a6e22e">CounterAction</span> {
  187. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">data</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">ShowToast</span>(<span style="color:#66d9ef">val</span> message: String) : CounterAction
  188. </span></span><span style="display:flex;"><span>}
  189. </span></span><span style="display:flex;"><span>
  190. </span></span><span style="display:flex;"><span><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">CounterViewModel</span> : MVIViewModel&lt;CounterState, CounterIntent, CounterAction&gt; <span style="color:#66d9ef">by</span> MVIViewModelDelegate(CounterState()) {
  191. </span></span><span style="display:flex;"><span>
  192. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">init</span> {
  193. </span></span><span style="display:flex;"><span> handleIntent()
  194. </span></span><span style="display:flex;"><span> }
  195. </span></span><span style="display:flex;"><span>
  196. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">private</span> <span style="color:#66d9ef">fun</span> <span style="color:#a6e22e">handleIntent</span>() = viewModelScope.launch {
  197. </span></span><span style="display:flex;"><span> withState { state <span style="color:#f92672">-&gt;</span>
  198. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">when</span> (<span style="color:#66d9ef">val</span> intent = receiveIntent()) {
  199. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">is</span> <span style="color:#a6e22e">CounterIntent</span>.Increment <span style="color:#f92672">-&gt;</span> updateState { <span style="color:#66d9ef">it</span>.copy(count = <span style="color:#66d9ef">it</span>.count + <span style="color:#ae81ff">1</span>) }
  200. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">is</span> <span style="color:#a6e22e">CounterIntent</span>.Decrement <span style="color:#f92672">-&gt;</span> updateState { <span style="color:#66d9ef">it</span>.copy(count = <span style="color:#66d9ef">it</span>.count - <span style="color:#ae81ff">1</span>) }
  201. </span></span><span style="display:flex;"><span> }
  202. </span></span><span style="display:flex;"><span> checkCounterValue(state.count)
  203. </span></span><span style="display:flex;"><span> }
  204. </span></span><span style="display:flex;"><span> }
  205. </span></span><span style="display:flex;"><span>
  206. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">private</span> <span style="color:#66d9ef">suspend</span> <span style="color:#66d9ef">fun</span> <span style="color:#a6e22e">checkCounterValue</span>(count: Int) {
  207. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">if</span> (count % <span style="color:#ae81ff">10</span> <span style="color:#f92672">==</span> <span style="color:#ae81ff">0</span> <span style="color:#f92672">&amp;&amp;</span> count <span style="color:#f92672">!=</span> <span style="color:#ae81ff">0</span>) {
  208. </span></span><span style="display:flex;"><span> sendAction { <span style="color:#a6e22e">CounterAction</span>.ShowToast(<span style="color:#e6db74">&#34;Counter is now </span><span style="color:#e6db74">$count</span><span style="color:#e6db74">!&#34;</span>) }
  209. </span></span><span style="display:flex;"><span> }
  210. </span></span><span style="display:flex;"><span> }
  211. </span></span><span style="display:flex;"><span>}</span></span></code></pre></div>
  212. <p>In this example:</p>
  213. <ol>
  214. <li>We define our <code>State</code> as a data class, and our <code>Intent</code> and <code>Action</code> as sealed interfaces.</li>
  215. <li>The <code>CounterViewModel</code> uses the <code>MVIViewModelDelegate</code> to implement the <code>MVIViewModel</code> interface.</li>
  216. <li>We handle intents in the <code>handleIntent</code> function, updating the state based on the received intent.</li>
  217. <li>The <code>checkCounterValue</code> function demonstrates how to send actions when certain conditions are met.</li>
  218. </ol>
  219. <h2 id="using-the-viewmodel-in-the-ui">Using the ViewModel in the UI</h2>
  220. <p>Here&rsquo;s how you might use this ViewModel in an Android Activity or Fragment:</p>
  221. <div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-kotlin" data-lang="kotlin"><span style="display:flex;"><span><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">CounterActivity</span> : AppCompatActivity() {
  222. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">private</span> <span style="color:#66d9ef">val</span> viewModel: CounterViewModel <span style="color:#66d9ef">by</span> viewModels()
  223. </span></span><span style="display:flex;"><span>
  224. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">override</span> <span style="color:#66d9ef">fun</span> <span style="color:#a6e22e">onCreate</span>(savedInstanceState: Bundle?) {
  225. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">super</span>.onCreate(savedInstanceState)
  226. </span></span><span style="display:flex;"><span> setContentView(<span style="color:#a6e22e">R</span>.layout.activity_counter)
  227. </span></span><span style="display:flex;"><span>
  228. </span></span><span style="display:flex;"><span> <span style="color:#75715e">// Collect state
  229. </span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span> lifecycleScope.launch {
  230. </span></span><span style="display:flex;"><span> viewModel.state.collect { state <span style="color:#f92672">-&gt;</span>
  231. </span></span><span style="display:flex;"><span> updateUI(state)
  232. </span></span><span style="display:flex;"><span> }
  233. </span></span><span style="display:flex;"><span> }
  234. </span></span><span style="display:flex;"><span>
  235. </span></span><span style="display:flex;"><span> <span style="color:#75715e">// Collect actions
  236. </span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span> lifecycleScope.launch {
  237. </span></span><span style="display:flex;"><span> viewModel.action.collect { action <span style="color:#f92672">-&gt;</span>
  238. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">when</span> (action) {
  239. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">is</span> <span style="color:#a6e22e">CounterAction</span>.ShowToast <span style="color:#f92672">-&gt;</span> <span style="color:#a6e22e">Toast</span>.makeText(<span style="color:#66d9ef">this</span><span style="color:#a6e22e">@CounterActivity</span>, action.message, <span style="color:#a6e22e">Toast</span>.LENGTH_SHORT).show()
  240. </span></span><span style="display:flex;"><span> }
  241. </span></span><span style="display:flex;"><span> }
  242. </span></span><span style="display:flex;"><span> }
  243. </span></span><span style="display:flex;"><span>
  244. </span></span><span style="display:flex;"><span> <span style="color:#75715e">// Send intents
  245. </span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span> incrementButton.setOnClickListener {
  246. </span></span><span style="display:flex;"><span> viewModel.handleIntent(<span style="color:#a6e22e">CounterIntent</span>.Increment)
  247. </span></span><span style="display:flex;"><span> }
  248. </span></span><span style="display:flex;"><span> decrementButton.setOnClickListener {
  249. </span></span><span style="display:flex;"><span> viewModel.handleIntent(<span style="color:#a6e22e">CounterIntent</span>.Decrement)
  250. </span></span><span style="display:flex;"><span> }
  251. </span></span><span style="display:flex;"><span> }
  252. </span></span><span style="display:flex;"><span>
  253. </span></span><span style="display:flex;"><span> <span style="color:#66d9ef">private</span> <span style="color:#66d9ef">fun</span> <span style="color:#a6e22e">updateUI</span>(state: CounterState) {
  254. </span></span><span style="display:flex;"><span> counterTextView.text = state.count.toString()
  255. </span></span><span style="display:flex;"><span> }
  256. </span></span><span style="display:flex;"><span>}</span></span></code></pre></div>
  257. <h2 id="conclusion">Conclusion</h2>
  258. <p>The MVI helper class we&rsquo;ve explored simplifies the implementation of the MVI pattern, providing a clean and type-safe way to manage state, handle user intents, and emit actions. By using this helper class, you can create more maintainable and testable view models, leading to more robust applications.</p>
  259. <p>Remember that you can use either data classes or sealed interfaces for your State, Intent, and Action definitions, depending on your specific needs. This flexibility allows you to choose the most appropriate structure for each component of your MVI architecture.</p>
  260. <p>While this helper class provides a solid foundation, you may need to adapt it to fit the specific needs of your project.</p>
  261. </div>
  262. </div>
  263. </main>
  264. </div>
  265. <footer class="footer">
  266. <span>CC 2024, Built with <a href="https://gohugo.io" class="footerLink">Hugo</a> and <a href="https://github.com/LordMathis/hugo-theme-nightfall" class="footerLink">Nightfall</a> theme</span>
  267. </footer>
  268. </div>
  269. </body>
  270. </html>