<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jarred Martin on Medium]]></title>
        <description><![CDATA[Stories by Jarred Martin on Medium]]></description>
        <link>https://medium.com/@jarredmartin?source=rss-7e3ef686accb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ObYqvI9d6tFPQRL9.</url>
            <title>Stories by Jarred Martin on Medium</title>
            <link>https://medium.com/@jarredmartin?source=rss-7e3ef686accb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 20:55:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jarredmartin/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A Kotlin-only ViewModel approach — part 1]]></title>
            <link>https://medium.com/@jarredmartin/another-viewmodel-approach-b0578a726ed2?source=rss-7e3ef686accb------2</link>
            <guid isPermaLink="false">https://medium.com/p/b0578a726ed2</guid>
            <category><![CDATA[app-development]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Jarred Martin]]></dc:creator>
            <pubDate>Sat, 14 Oct 2023 09:42:17 GMT</pubDate>
            <atom:updated>2023-10-23T14:45:23.495Z</atom:updated>
            <content:encoded><![CDATA[<h3>A Kotlin-only ViewModel approach - part 1</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bydh37oEFQCni5k2xDBz1w.jpeg" /><figcaption>Lions Head in Cape Town, South Africa</figcaption></figure><p>In this post I will be showing you a ViewModel pattern that will fulfill our clean code principles namely;</p><ol><li>Clear separation between concerns i.e View, Business logic.</li><li>Favouring composability instead of Inheritance.</li><li>Writing testable code.</li><li>Rely on Kotlin only dependencies.</li></ol><h3>You will see the use of</h3><ol><li>Kotlin Flow</li><li>Kotlin Flow operators</li><li>Kotlin Sealed Classes</li></ol><p>You’re probably wondering why not just use the ViewModel classes provided by Androidx? Well there’s nothing wrong with using them, they work perfectly fine.</p><p>The are some cases when developing an application where you do not want to have dependencies to Android components when implementing your business logic. This helps when it comes to writing unit tests as you would not have to mock any behaviour from an Android dependency.</p><h3>The general approach I will be demonstrating is</h3><p>1. Our <em>View</em> (<em>Fragment</em> or <em>Activity</em>) will be observing a state field in our ViewModel by means of a Kotlin <em>Flow</em>, when the state field is updated our <em>View</em> will also be updated accordingly.</p><p>2. Our <em>View</em> will be interacting with the ViewModel by means of different types of Actions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/462/1*v3rS5lmwZDel1_d3U91mfg.png" /><figcaption>Interaction between View and ViewModel</figcaption></figure><h3>Lets see some code</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F9HQRIttS5C4Za%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fshow-money-maguire-9HQRIttS5C4Za&amp;image=https%3A%2F%2Fmedia0.giphy.com%2Fmedia%2Fv1.Y2lkPTc5MGI3NjExMnZwMnVxYW53aW43bWJidzkzenhrY2ppMXRpZnR2dWczZTdvNWJyeiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw%2F9HQRIttS5C4Za%2Fgiphy_s.gif&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="231" frameborder="0" scrolling="no"><a href="https://medium.com/media/da56e280cb70497c5e4abe0f102b098c/href">https://medium.com/media/da56e280cb70497c5e4abe0f102b098c/href</a></iframe><p>In our code example we will have an <em>Activity</em> with two <em>Buttons </em>and a <em>TextView</em>. The two <em>Buttons</em> will be to increment or decrement a counter, the <em>TextView</em> will display the value of the counter.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/216/1*nA3VMkUdLlVrJ4J92qeRnw.gif" /><figcaption>Our demo app</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ff4a43388a428abfd5ab8e33cf0a8cc1/href">https://medium.com/media/ff4a43388a428abfd5ab8e33cf0a8cc1/href</a></iframe><p>Here’s our <em>MainActivity.kt, </em>as you can see it’s pretty straight forward.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c08a474c83e3c5da99614b01f54b4b74/href">https://medium.com/media/c08a474c83e3c5da99614b01f54b4b74/href</a></iframe><ol><li>We have our click listeners attached to our two Buttons. Clicking either Button sends an Increment or Decrement Action to our ViewModel.</li><li>Then we are observing changes to our ViewModel state field, and mapping it to return only changes in the counter variable. This is then all launched in the <em>LifeCycleScope</em>, note that in my example my <em>MainActivity</em> extends from <em>AppCompatActivity</em> which does not have access to the <em>LifeCycleScope. </em>Therefore I needed to include a dependency to.</li></ol><pre>androidx.lifecycle:lifecycle-runtime-ktx:2.6.2</pre><h3>Now to the good stuff</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/da55fe27d7c795caef657a718673eda0/href">https://medium.com/media/da55fe27d7c795caef657a718673eda0/href</a></iframe><p>Here we have our ViewModel interface, it’s also quite simple. We will see some improvements in part 2 of this article series so check that out as well.</p><ol><li><em>currentState </em>is of type <em>MutableStateFlow&lt;S&gt;</em>, where <em>S</em> is a generic type that we can define later in the implementation of the ViewModel for our example app. We use <em>MutableStateFlow</em> because our state needs to be mutable ie. it can change and it has a cool feature built in, values are only emitted if the state actually changes, that means we don’t need to use the <em>.distinctUntilChanged() </em>operation when observing the sate changes in the <em>MainActivity</em></li><li><em>state</em>, This is our read-only value of the <em>currentState, </em>this is useful to have in addtion to <em>currentState </em>because we don’t want any class which has an instance of our ViewModel to be able to externally modify the <em>state</em> of our ViewModel.</li><li><em>onAction(action: A), </em>this function is used whenever there is an action/event that occurs and our ViewModel needs to perform an operation on <em>currentState, </em>be it when a user clicks a Button, enters text into an EditText etc…</li><li>Finally <em>intialState(): S </em>is the function which defines the initial value of <em>currentState </em>when the ViewModel is initialized.</li></ol><p><em>MyViewModel.kt </em>implements our ViewModel interface</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/258fc5dfbe1f461182ad2aee82ba8e1b/href">https://medium.com/media/258fc5dfbe1f461182ad2aee82ba8e1b/href</a></iframe><p><em>MyViewModel.kt </em>implements our ViewModel interface, let’s go through the class from top to bottom.</p><ol><li>Our <em>currentState</em> is initalized with the default state that’s returned by our <em>initialState</em>() function.</li><li><em>state</em> just returns our <em>currentState</em> as a read-only <em>stateFlow.</em></li><li><em>onAction() </em>here we have our logic that handles what happens when different types of <em>Actions</em> are sent to our ViewModel, it’s a simple when statement with each branch modifying our state using the <em>copy</em> function and then returns the <em>newState</em>. In our example app this just increments or decrements the <em>counter</em> value stored in our <em>State</em> class. Finally the <em>newState</em> is emitted to our <em>currentState</em> Flow.</li><li>Lastly we have our internally Classes of our ViewModel, <em>Sealed Class </em>Action and our <em>State </em>Data Class. Our Actions are defined as <em>Sealed Classes</em> as they allow us to have a finite set of Actions our ViewModel can handle similarly to how <em>Enums</em> function. The <em>State Class</em> only holds the value of our <em>counter</em> as that’s the only state our <em>Activity</em> contains.</li></ol><p>And that’s a wrap for part 1 of this article series! Looking forward to you comments and better yet, ways to improve on this approach.</p><p>Coming up next. How does the ViewModel notify the View of an event or error, see you in part 2. 👋</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b0578a726ed2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Secure App-API communication using an HMAC]]></title>
            <link>https://medium.com/@jarredmartin/secure-app-api-communication-using-an-hmac-316314b63445?source=rss-7e3ef686accb------2</link>
            <guid isPermaLink="false">https://medium.com/p/316314b63445</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[backend]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Jarred Martin]]></dc:creator>
            <pubDate>Tue, 21 Apr 2020 11:38:36 GMT</pubDate>
            <atom:updated>2020-04-22T19:53:52.958Z</atom:updated>
            <content:encoded><![CDATA[<h3>Verifying App-API communication using an HMAC</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iM5bBfiTX64Yvi33zu4GqA.jpeg" /><figcaption>tre cime di lavaredo, May 2018</figcaption></figure><p>Nowadays with the huge popularity of apps, more and more of us are using apps that handle sensitive private financial data, like mobile banking and trading digital assets.</p><p>When it comes to security every system can be broken, us as app developers can only make it harder for hackers to break through and cause havoc. We can only secure apps by adding multiple layers of security to protect user data.</p><p>With this article I hope to show you how to add another layer of protection to the communication between your app and your backend api.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/489/1*wVTdiPzzZXdXo09U1A1RDQ.jpeg" /><figcaption>On the left we have the ideal situation, on the right we have a potential man in the middle attack.</figcaption></figure><p>The diagram above illustrates the situation we will try to protect our app from, where a hacker intercepts the requests from our app and modifies the contents to be whatever they want.</p><p>Of course one of the ways we can protect the app from a man-in-the-middle attack is to use secure HTTPS and certificate pinning, here we will just be adding another layer to make it harder for the attacker to compromise our app.</p><h3>How can we be protected from this attack?</h3><p>What we want to achieve is being able to ensure that whatever the api is receiving is what the app actually sent, so how can we do this?</p><p><strong><em>Request body signing</em></strong></p><p>We can follow an approach called <strong>HMAC</strong> (Hash based message authentication code) in order to secure our request body.</p><ol><li>Using the HMAC we can create a hash (SHA-256) of the request body and send the hash along with the request where our api can verify the <strong><em>authenticity</em></strong> and <strong><em>integrity</em></strong> of our request body. Our api would then compute the <strong>HMAC</strong> of the received request body with the secret key and compare it with what was sent in the request, if the two hashes match we can say that the request body has not be altered in flight.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/270/1*32vYUwS9QS8VAtSDXhIfJw.jpeg" /><figcaption>right?</figcaption></figure><p>not so fast, the problem with computing an <strong>HMAC</strong> is that we need a key to be able to perform the hashing. but where can we store this key in the app?</p><ul><li>Source code</li><li>A .txt file</li><li>A binary file</li></ul><p>If we just hard code the key in our source code, a hacker could decompile our .apk and read our source code and extract our key and use it to correctly sign all kinds of request body.</p><p>Same goes for the other two options…</p><p>A way to store the key more securely and make it harder for hackers to retrieve it would be to use native code and the Android NDK. As native code written in C++ would be a lot harder to disassemble and read our secret key.</p><p><em>This is not a fool proof solution to save the secret from disassembly, it is possible but this just adds another layer for a hacker to bypass.</em></p><p><a href="https://developer.android.com/studio/projects/add-native-code">https://developer.android.com/studio/projects/add-native-code</a></p><h3>lets see some code…</h3><p><a href="https://github.com/scudza/RequestBodyHMAcExample">scudza/RequestBodyHMAcExample</a></p><p>First we need to setup an android studio project that has native C++ support</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AXZluMn5-kMOQEMSdx9fEQ.png" /><figcaption>select Native C++</figcaption></figure><p>Android studio will automagically create the necessary files; additions to your build.grade, CMAKE file and a C++ file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*VmUWVPOJuhsDpgcNPqESIw.png" /><figcaption>notice the cpp folder</figcaption></figure><p>The <em>native-lib.cpp</em> is where we will store our secret key, it’s a C++ file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ff8dd917c782e022fde9aac224d75c23/href">https://medium.com/media/ff8dd917c782e022fde9aac224d75c23/href</a></iframe><p>The file is really simple all we have is a method that returns a string which would be our secret key.</p><p>The method name <em>Java_com_example_hmacrequestbody_HttpClient_clientSecret </em>must include the full package name of where this method will be called from. We can split this one up as follows:</p><ul><li><em>java_com_example_hmacrequestbody</em></li></ul><p>The directory path</p><ul><li><em>HttpClient</em></li></ul><p>The class in which the method is called</p><ul><li><em>clientSecret</em></li></ul><p>The method name</p><h4>HMAC and body signing</h4><p>Now that we have our secret key stored in our app, we need to somehow:</p><ol><li>retrieve it</li><li>sign our request body</li><li>append signature to request header</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d135420fae8dc7cdb70580a2da8b94aa/href">https://medium.com/media/d135420fae8dc7cdb70580a2da8b94aa/href</a></iframe><p>We do that by first loading our native library, in the example it’s the first thing done when the class is instantiated in the <strong><em>init</em></strong> block. Then we have our function that returns the secret key as a <strong><em>String </em></strong>as an<strong><em> external fun.</em></strong></p><p>Now that we have our secret key we need to use it in our cryptographic hashing function, for the purpose of this example we will be using <strong><em>HmacSHA256</em></strong>. It’s important to note here, that the string data that we are computing the cryptographic hash of, is the appended strings of;</p><ul><li>JSON body of the request serialized</li><li>The request URL</li></ul><p>This ensures that the hash would be unique in two dimensions of endpoint and body data.</p><p>And finally the the hash is encoded to a String using BASE64</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/70c3bcc3cd8050429d3213ff625e328a/href">https://medium.com/media/70c3bcc3cd8050429d3213ff625e328a/href</a></iframe><h4>Putting it all together</h4><p>To use the function we just call it like any other function, and put it all together here.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ada2501ba9dad8004211bedf9c0b6ad/href">https://medium.com/media/4ada2501ba9dad8004211bedf9c0b6ad/href</a></iframe><p>Here we are using the standard OkHTTP library for making our request, as you can see the <em>hmac</em> goes into the header of the request.</p><p>That would be it for the App side of this example, we would need to adapt our server side code to handle the additional header called “authorisation” and along with the clients secret key then verify that that the <em>hmac</em> sent corresponds to the body data in the request, maybe I’ll write another medium post demonstrating this side.</p><p>If you’ve made it this far, I’d like to thank you for taking the time to read my article and hope you gained some useful knowledge. It was a fun exercise and it’s always great to contribute some knowledge to the Android community.</p><p>✌️✌️</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=316314b63445" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ViewModels and Databinding]]></title>
            <link>https://medium.com/@jarredmartin/viewmodels-and-databinding-bdf19464766b?source=rss-7e3ef686accb------2</link>
            <guid isPermaLink="false">https://medium.com/p/bdf19464766b</guid>
            <category><![CDATA[viewmodel]]></category>
            <category><![CDATA[android-data-binding]]></category>
            <category><![CDATA[data-binding]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Jarred Martin]]></dc:creator>
            <pubDate>Fri, 20 Dec 2019 13:34:30 GMT</pubDate>
            <atom:updated>2019-12-23T08:31:08.947Z</atom:updated>
            <content:encoded><![CDATA[<h4>Why should we even bother?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_112vdiGt0uIKm14pehQXw.jpeg" /></figure><p>In software engineering and more specifically building Android apps we always want to have a clear separation between different concerns within our app. Most, if not all apps have some kind of UI and logic that controls what is shown within that UI. We want to keep those two parts of our code separate to:</p><ol><li>make it easier to be able to test each one separately from the other</li><li>help in finding the source of a UI bug, whether it’s from the model or something in the view</li><li>help with code readability, no one wants to read a 1000 line class</li></ol><h4>So how can we accomplish this?</h4><p>We will build a small app so I can demonstrate how this is done. The app will consist of:</p><ul><li>Activity class</li><li>Layout file</li><li>Viewmodel class</li><li>Viewmodel interface</li></ul><p><a href="https://github.com/scudza/DatabindingViewmodelSample">scudza/DatabindingViewmodelSample</a></p><p>The app will consist of 1 screen that has some TextView’s and Button that, when pressed increments an Integer. With that we can see how UI input can affect the layout.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/270/1*6Z6Qq2sMFzCzzjaY3oozlg.gif" /></figure><h3>Sounds simple enough, so show me the code!</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0371b82806c405f3a9d82270a4f0e7ca/href">https://medium.com/media/0371b82806c405f3a9d82270a4f0e7ca/href</a></iframe><p>Here we can see the Activity layout .xml, what you’ll notice that is new is the &lt;layout&gt; , &lt;data&gt; and &lt;variable&gt; tags.</p><p>They each correspond to different sections in the .xml file, the 2 tags of interest are the &lt;data&gt; and &lt;variable&gt; tags.</p><p>The &lt;variable&gt; tag allows you to specify a variable in the file, for us this is the Interface to the `ViewModel` we specifically use an Interface here so that in future if we would like to use this same layout in another screen all we have to do is have that screens ViewModel implement the Interface and we’re good to go! You don’t have to use an Interface here as the type , you could just use the ViewModel class directly and it would work the same.</p><p>Further down the file you can see where the actual magic happens, android:text=&quot;@{vm.title}&quot; what this line means is that from our variable called vm that we defined above, the text from this TextView is coming from a field inside IViewmodel</p><p>And even further down we have our Button , what’s useful for our Button is to connect its’ click listener to our Viewmodel .We do it like this android:click=&quot;@{vm::onClick}&quot; where the onClick is the method name in our variable vm .It’s import here that the method takes in a parameter of type View? (nullable)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/60e64a0529202e8c42488e4530b3a4ef/href">https://medium.com/media/60e64a0529202e8c42488e4530b3a4ef/href</a></iframe><p>Here’s our Interface , looks pretty simple, 3 String fields and the onClick for our Button</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/62e530532d828c65df46f7b9c8627328/href">https://medium.com/media/62e530532d828c65df46f7b9c8627328/href</a></iframe><p>So in our ViewModel class we can see that it extends the Android ViewModel and implements our interface that connects it to our .xml layout file.</p><p>We have the onClick function for our button, all it does is increment our myInt variable and then sets it as a String to our TextView, really easy…</p><h4>Conclusion</h4><p>In conclusion with an approach like this we can accomplish</p><ul><li>Code reuse ability.</li><li>Clear separation between business logic and Android framework code.</li><li>Having code that is testable by writing unit tests.</li></ul><p>I hope this small demo, provided some insights that you can use for your next Android app ✌️✌️.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bdf19464766b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>