<?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 AnnChar on Medium]]></title>
        <description><![CDATA[Stories by AnnChar on Medium]]></description>
        <link>https://medium.com/@annchar?source=rss-c07bb99c7f06------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*91CT47GME1qNfPS1e-7EuQ.jpeg</url>
            <title>Stories by AnnChar on Medium</title>
            <link>https://medium.com/@annchar?source=rss-c07bb99c7f06------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 03:01:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@annchar/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[Before becoming a CPR Recorder]]></title>
            <link>https://annchar.medium.com/before-becoming-a-cpr-recorder-721b387a32dd?source=rss-c07bb99c7f06------2</link>
            <guid isPermaLink="false">https://medium.com/p/721b387a32dd</guid>
            <category><![CDATA[cpr]]></category>
            <category><![CDATA[medicine]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[free-app-development]]></category>
            <dc:creator><![CDATA[AnnChar]]></dc:creator>
            <pubDate>Fri, 10 Mar 2023 09:54:18 GMT</pubDate>
            <atom:updated>2023-03-10T10:00:05.088Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lm0JgxgqsV2lhpXPBvQ7Vg.jpeg" /><figcaption>Staff members at Maimonides Medical Center in Brooklyn in 2010 trying to revive a patient who suffered a cardiac arrest. Credit: Ashley Gilbertson for The New York Times</figcaption></figure><h3>Inspiration</h3><p>The app was created because the developer wanted to make something that could help others during a time when they had some free time. They spoke to a doctor friend about whether there was anything they could create to help in case of an emergency and found out that normally in the emergency room, if someone has a heart attack and needs defibrillation, the medical team has to follow a set procedure that starts with timing how long the patient has been in cardiac arrest. Usually, 1–2 people will control the time and record the information on paper while a doctor assesses the patient’s condition and administers medication and CPR until their heartbeat returns to normal. It turns out that one position where an app could be helpful is the person who times and records the data.</p><h3>Team</h3><p>We’re announcing a call for volunteers through Facebook. Who would have thought? Anyone interested in volunteering with us, please let us know.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/887/1*cLbefDaF3oSGIr4tp8Nzyg.png" /></figure><p>And here are the faces of all the participants.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yynuJZvSdRhzeRSkGqhxrw.png" /></figure><p>Everyone has joined, let’s get to work.</p><h3>Timeline</h3><p>For this project, we won’t rush anything, we’ll just keep working on it steadily. But for other projects, we should plan everything from now on.</p><blockquote>Before we start the development process, we want everyone to have a clear understanding. In this context, the word “User” refers to doctors or medical personnel who will be using this application.</blockquote><h3>How to develop</h3><ol><li><strong>Gathering Requirements</strong></li></ol><p>This involves gathering requirements from users about the steps they want to take and breaking them down into features. We’ll prioritize the core features of the application and start organizing them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i2yIMy4wMAmdk7sA3XYWzQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r4J4IzLXolF6rcyTJfbRTA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Oi49vYZAs18muIlLj-WmSw.jpeg" /></figure><p>From gathering requirements, we summarized the information we will implement in the first version.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/192/1*QbJFrAGaNBlBKNSDfS3fUQ.png" /><figcaption>CPR Recorder Icon</figcaption></figure><blockquote><strong>CPR Recorder</strong> is an application for healthcare professionals and paramedics to maintain medical records during the real-time CPR Algorithm. This app also allows you to convert records to image files to be stored on device.</blockquote><blockquote><strong>Features</strong><br>-Aiding the timing of chest accurate compressions during CPR.<br>-Setting notifications for assessment and medication administration.<br>-Rendering medical records into image files.<br>-Providing the flow chart of CPR algorithm as in-app features.</blockquote><p>Once we have the main core of the application, let’s start designing together.</p><p><strong>2. Design Wireframes</strong></p><p>It’s the designer’s job to create wireframes from the requirements we’ve gathered to ensure everyone has a clear picture. After that, we’ll test it with users and make any necessary changes. Once everyone on the team agrees, then we can move on to the next step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p5pDt99pdTgBPBXTsQOqbA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fb9fTHZvlAwCpcraPTFBpA.jpeg" /></figure><p><strong>3. Designing UX/UI</strong></p><p>The designer will start adding details to each page’s UI and create a realistic demo.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e0Q_OVSV7WAoIT57tCDMDA.png" /><figcaption>CPR Recorder on Zeplin</figcaption></figure><p><strong>4. Testing with Users</strong></p><p>Test the demo created with real users. If everything passes or is fixed, move on to the next step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DVtKLD2nl84capYXKx75sg.png" /><figcaption>Demo on Figma</figcaption></figure><p><strong>5. Develop</strong></p><p>This is where the developer takes over building the application and makes it functional. It’s like being a wizard in a magical world.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6T5m1d96GKY_Li1AY_sWIQ.png" /><figcaption>CPR Rocorder on Android Studio</figcaption></figure><p><strong>6. Testing</strong></p><p>Normally, if there is a QA (Quality Assurance) in the team, they will take care of this task. However, since we don’t have one for this project, everyone needs to help with testing. Once all the bugs are fixed, we can prepare for release.</p><p><strong>7. Launch</strong></p><p>Launching doesn’t mean having a meal. It’s just a word that sounds similar, but it means to release the app. Easy peasy. When the app is finished, we can upload it to the Google Play Store.</p><h3>What are you waiting for? Go download it here!</h3><p><a href="https://play.google.com/store/apps/details?id=com.annchar.cprrecord">CPR Recorder - Apps on Google Play</a></p><p><strong>8. Maintenance</strong></p><p>This is the developer’s job to check for bugs through Firebase Crashlytics and keep updating the libraries to the latest version. They may also need to refactor the code to make it easier to develop new features in the future.</p><blockquote>We’re done with the CPR Recorder app. Thanks a lot to all the volunteer team members who sacrificed their time to help develop it.</blockquote><p><strong>Things we learned from this project:</strong></p><ul><li>We used Project Management skills in developing the app from scratch, gathering requirements ourselves, and launching the product.</li><li>Communication skills were essential as we had to communicate with various parties, including users and team members with diverse knowledge.</li><li>We did things we’ve never done before on the Developer side. Normally, when we work for a company, we may not be the ones uploading the app to the store. However, in this project, we had to do it ourselves, or we had to build the entire project framework ourselves. This was a good opportunity for us to try it out.</li></ul><blockquote>If you have an idea, just go ahead and do it. Don’t overthink it…</blockquote><blockquote>Finally, take care and stay safe with love and concern. ❤️❤️❤️</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/1*bAIKNbn3kzIEOlSPQ1HCzw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=721b387a32dd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android Paging 3 library with offset and limit parameters, MVVM, LiveData and Coroutine]]></title>
            <link>https://annchar.medium.com/android-paging-3-library-with-offset-and-limit-parameters-mvvm-livedata-and-coroutine-part1-5f85aa4fd29a?source=rss-c07bb99c7f06------2</link>
            <guid isPermaLink="false">https://medium.com/p/5f85aa4fd29a</guid>
            <category><![CDATA[retrofit]]></category>
            <category><![CDATA[paging3]]></category>
            <category><![CDATA[coroutine]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[koin]]></category>
            <dc:creator><![CDATA[AnnChar]]></dc:creator>
            <pubDate>Sun, 06 Jun 2021 10:38:59 GMT</pubDate>
            <atom:updated>2022-02-25T07:00:15.985Z</atom:updated>
            <content:encoded><![CDATA[<h3>Android Paging 3 library with Offset and Limit parameters, MVVM, LiveData, and Coroutine</h3><h3>What is Paging library?</h3><p>The Paging library helps you load and display pages of data from a larger dataset from local storage or over network. This approach allows your app to use both network bandwidth and system resources more efficiently.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F1cwqGOku2a4&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D1cwqGOku2a4&amp;image=http%3A%2F%2Fi.ytimg.com%2Fvi%2F1cwqGOku2a4%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/60f65b79788afee2f048208334519595/href">https://medium.com/media/60f65b79788afee2f048208334519595/href</a></iframe><h3>Library Architecture</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*b5xb8xSMqlhI8Bus3sY0mw.png" /></figure><p><strong>Repository layer</strong></p><ul><li>A PagingSource object can load data from any single source, including network sources and local databases.</li><li>A RemoteMediator object handles paging from a layered data source, such as a network data source with a local database cache.</li></ul><p><strong>ViewModel layer</strong></p><ul><li>The <a href="https://developer.android.com/reference/kotlin/androidx/paging/Pager">Pager</a> component provides a public API for constructing instances of PagingData that are exposed in reactive streams, based on a PagingSource object and a <a href="https://developer.android.com/reference/kotlin/androidx/paging/PagingConfig">PagingConfig</a> configuration object.</li><li>A PagingData object is a container for a snapshot of paginated data. It queries a <a href="https://developer.android.com/reference/kotlin/androidx/paging/PagingSource">PagingSource</a> object and stores the result.</li></ul><p><strong>UI layer</strong></p><ul><li>The primary Paging library component in the UI layer is <a href="https://developer.android.com/reference/kotlin/androidx/paging/PagingDataAdapter">PagingDataAdapter</a>, a <a href="https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView">RecyclerView</a> adapter that handles paginated data.</li></ul><h3>What is the CoinRanking application?</h3><p><strong>CoinRanking</strong> is a small demo application based on modern Android application tech-stacks and MVVM architecture. Fetching data from the network and render data via Paging 3, showing crypto ranking.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LPIIIEGKqIDdnpgDUaPXdA.gif" /></figure><h3><strong>Prepare to get data from API</strong></h3><p><strong>Get API key</strong></p><ul><li>Register <a href="https://coinmarketcap.com/api">Coinmarketcap API</a></li><li>Open ~local.propertiesfile</li><li>Add api_key the line also add your API key like</li></ul><pre>api_key = 10ce47bf-92fb-42a5-bc4c-93de83123Drf</pre><h3>Step for development</h3><p>After you setup fetch data from network success</p><ol><li><strong>Install Paging3</strong></li></ol><pre>implementation &#39;androidx.paging:paging-runtime-ktx:3.0.0&#39;</pre><p><strong>2. Create API service</strong></p><p>I used the endpoint for getting crypto ranking</p><pre>GET /v1/cryptocurrency/listings/latest?start=offset&amp;limit=limit</pre><p>Document -&gt; <a href="https://coinmarketcap.com/api/documentation/v1/#operation/getV1CryptocurrencyListingsLatest">Coin market cap API</a></p><p>and setup like this</p><pre>interface APIService {<br>    @GET(&quot;/v1/cryptocurrency/listings/latest&quot;)<br>    suspend fun getCryptoList(<br>        @Query(&quot;start&quot;) start: Int,<br>        @Query(&quot;limit&quot;) limit: Int<br>    ): CryptoListJson<br>}</pre><p><strong>3. Create PagingSource</strong></p><p>PagingSource&lt;Key, Value&gt; has two type parameters: Key and Value. The key defines the identifier used to load the data, and the value is the type of the data itself. For example, if you load pages of CryptoItemResponse objects from the network by passing Int page numbers to <a href="https://square.github.io/retrofit/">Retrofit</a>, you would select Int as the Key type and CryptoItemResponse as the Value type.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/0*h3Z75dlb7j6f_gum.png" /><figcaption>How load() function uses and updates the key. Source: <a href="https://developer.android.com/topic/libraries/architecture/paging/v3-paged-data#pagingsource">Official Docs of Paging 3</a></figcaption></figure><p>The <a href="https://developer.android.com/reference/kotlin/androidx/paging/PagingSource#load">load()</a> function should be implemented to retrieve data from the data source (network in our case). <a href="https://developer.android.com/reference/kotlin/androidx/paging/PagingSource#load">load()</a> is a suspend function, so you can call other suspend functions here like a network call. For more information about suspend functions, check the <a href="https://kotlinlang.org/docs/reference/coroutines/composing-suspending-functions.html">Kotlin Coroutines docs</a>.</p><blockquote>By default, the initial load size is 3 * page size</blockquote><p><strong>For example,</strong> You set NETWORK_PAGE_SIZE = 500</p><ul><li>First time, PagingSource load 500 x 3 = 1500 items</li><li>Second time, the next load is500 items.</li><li>Third time, the next load is500 items. etc.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/91df8e124932e746f74ebf58874a987e/href">https://medium.com/media/91df8e124932e746f74ebf58874a987e/href</a></iframe><p>In code, I sent offset and limit params to API. I calculated offset from the page like</p><pre>val position = params.key ?: INITIAL_LOAD_SIZE <br>val offset = <br>if (params.key != null) <br>   ((position — 1) * NETWORK_PAGE_SIZE) + 1 <br>else <br>   INITIAL_LOAD_SIZE<br></pre><p>and sent to API like</p><pre>val jsonResponse = service.getCryptoList(start = offset, limit = params.loadSize).data</pre><blockquote>If the endpoint use page, you can send the page to API directly.</blockquote><p>The <a href="https://developer.android.com/reference/kotlin/androidx/paging/PagingSource.LoadResult">LoadResult</a> object contains the result of the load operation. LoadResult is a sealed class that takes one of two forms, depending on whether the load() call succeeded:</p><ul><li>If the load is successful, return the crypto list wrapped in a LoadResult.Page object.</li><li>If the load is not successful, return the exception wrapped in a LoadResult.Error object.</li></ul><p>The <a href="https://developer.android.com/reference/kotlin/androidx/paging/PagingSource#getrefreshkey">getRefreshKey()</a>is method which takes an <a href="https://developer.android.com/reference/kotlin/androidx/paging/PagingState">PagingState</a> object as a parameter and returns the key to pass into the load() method when the data is refreshed or invalidated after the initial load. The Paging Library calls this method automatically on subsequent refreshes of the data.</p><p><strong>4. Create a repository and pager</strong></p><p><strong>Pager</strong></p><p>The container for the data returned from <a href="https://developer.android.com/reference/kotlin/androidx/paging/PagingSource">PagingSource</a> is called <a href="https://developer.android.com/reference/kotlin/androidx/paging/PagingData">PagingData</a>. A <a href="https://developer.android.com/reference/kotlin/androidx/paging/Pager">Pager</a> instance has to be created to build a <a href="https://developer.android.com/reference/kotlin/androidx/paging/PagingData">PagingData</a> stream. Three parameters are needed to create an instance. These are:</p><ul><li><a href="https://developer.android.com/reference/kotlin/androidx/paging/PagingSource">PagingSource</a> is our data source created in the name CryptoListPagingSource.</li><li><a href="https://developer.android.com/reference/kotlin/androidx/paging/PagingConfig">PagingConfig</a> defines how to get data from the <a href="https://developer.android.com/reference/kotlin/androidx/paging/PagingSource">PagingSource</a> like page size, prefetch distance, etc. Check <a href="https://developer.android.com/reference/kotlin/androidx/paging/PagingConfig">official docs</a> for the parameters you can set for <a href="https://developer.android.com/reference/kotlin/androidx/paging/PagingSource">PagingSource</a>.</li><li>An optional initialKey to start loading with a default key.</li><li>When the Pager object is created, it calls the <a href="https://developer.android.com/reference/kotlin/androidx/paging/PagingSource#load">load()</a>function of the <a href="https://developer.android.com/reference/kotlin/androidx/paging/Pager#init">pagingSourceFactory</a> it has.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/07d10a3bd2454f91e12f40302c4ea67a/href">https://medium.com/media/07d10a3bd2454f91e12f40302c4ea67a/href</a></iframe><ul><li>I used LiveData to wrap the result. Other ways you can use Flow, Observable like this</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/79af2e882d660bcf3e0e57066ec26fc9/href">https://medium.com/media/79af2e882d660bcf3e0e57066ec26fc9/href</a></iframe><p><strong>5. Create ViewModel</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9373b753b3a6e8ed2b4c222fe97d714a/href">https://medium.com/media/9373b753b3a6e8ed2b4c222fe97d714a/href</a></iframe><ul><li><a href="https://developer.android.com/reference/kotlin/androidx/paging/package-summary#cachedin">cachedIn()</a> is used to persist the data beyond configuration changes. The best place to do this in a <a href="https://developer.android.com/reference/android/arch/lifecycle/ViewModel">ViewModel</a>, using the <a href="https://developer.android.com/topic/libraries/architecture/coroutines#viewmodelscope">viewModelScope</a>.</li></ul><p><strong>6. Create Adapter</strong></p><p>Normally, RecyclerView use RecyclerView.Adapter or ListAdapter but for Paging 3 we use PagingDataAdapter but behavior like normal adapter nothing special</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ebc6fb842eb08c048d662c28f8b586b4/href">https://medium.com/media/ebc6fb842eb08c048d662c28f8b586b4/href</a></iframe><p><strong>7. Binding data to </strong>RecyclerView</p><p>The last step is collecting the <a href="https://developer.android.com/reference/kotlin/androidx/paging/PagingData">PagingData</a> object in our fragment or activity and set into the CryptoListAdapter</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/757e83ae9cebd27d74f1a55ad407017a/href">https://medium.com/media/757e83ae9cebd27d74f1a55ad407017a/href</a></iframe><p>You can download the <strong>demo</strong> at the link below</p><p><a href="https://github.com/annchar/CoinRanking">annchar/CoinRanking</a></p><h3><strong>References:</strong></h3><p><a href="https://developer.android.com/topic/libraries/architecture/paging/v3-overview">https://developer.android.com/topic/libraries/architecture/paging/v3-overview</a></p><p><a href="https://developer.android.com/codelabs/android-paging#0">https://developer.android.com/codelabs/android-paging#0</a></p><p><a href="https://android-developers.googleblog.com/2020/07/getting-on-same-page-with-paging-3.html">https://android-developers.googleblog.com/2020/07/getting-on-same-page-with-paging-3.html</a></p><p><a href="https://developer.android.com/topic/libraries/architecture/paging/v3-paged-data">https://developer.android.com/topic/libraries/architecture/paging/v3-paged-data</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5f85aa4fd29a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[กว่าจะมาเป็น CPR Recorder]]></title>
            <link>https://annchar.medium.com/%E0%B8%81%E0%B8%A7%E0%B9%88%E0%B8%B2%E0%B8%88%E0%B8%B0%E0%B8%A1%E0%B8%B2%E0%B9%80%E0%B8%9B%E0%B9%87%E0%B8%99-cpr-recorder-84c4ad5b16cf?source=rss-c07bb99c7f06------2</link>
            <guid isPermaLink="false">https://medium.com/p/84c4ad5b16cf</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[medicine]]></category>
            <category><![CDATA[android-development]]></category>
            <dc:creator><![CDATA[AnnChar]]></dc:creator>
            <pubDate>Thu, 01 Oct 2020 11:22:42 GMT</pubDate>
            <atom:updated>2023-03-10T10:00:49.188Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lm0JgxgqsV2lhpXPBvQ7Vg.jpeg" /><figcaption>Staff members at Maimonides Medical Center in Brooklyn in 2010 trying to revive a patient who suffered a cardiac arrest. Credit: Ashley Gilbertson for The New York Times</figcaption></figure><h3><strong>Inspiration</strong></h3><p>แอปพลิเคชั่นเกิดจากการที่ตัวแอนเองอยากลองทำแอปเพื่อช่วยเหลือคนอื่นบ้างประกอบกับช่วงนั้นมีเวลาเลยได้คุยกับเพื่อนที่เป็นหมอว่าพอจะมีปัญหาอะไรที่เราพอจะสร้างแอปพลิเคชั่นไปช่วยได้ไหม คุยไปคุยมาปรากฎว่ามี ท้าวความก่อน ปกติในห้องฉุกเฉินหากมีคนไข้เกิดอาการโคม่าเข้ามาจนต้องใช้เครื่องกระตุกหัวใจ ทางการแพทย์เขาก็จะมีขั้นตอนที่ต้องทำตั้งแต่เริ่มจับเวลาที่คนไข้เข้ามาจะมีพยาบาลหรือผู้ช่วย 1–2 คนเป็นคนควบคุมเวลาและจดบันทึกลงกระดาษ มีแพทย์ประเมินอาการให้ยากระตุ้นหัวใจ ปั้มหัวใจ ทำจนกว่าคนไข้ชีพจรกลับมาเป็นปกติ จะเห็นได้ว่าทุกขั้นตอนที่กล่าวมานั้นมีหนึ่งตำแหน่งที่เราสามารถใช้แอปพลิเคชั่นช่วยได้นั่นคือตำแหน่งคนจับเวลาและบันทึกข้อมูลนั่นเอง</p><h3><strong>Team</strong></h3><p>ประกาศหากันอย่างง่ายดายผ่านช่องทาง Facebook นี่แหละจ้า ละใครจะไปรู้ มีคนสนใจอยากทำงานจิตอาสาแบบเราด้วย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/887/1*cLbefDaF3oSGIr4tp8Nzyg.png" /></figure><p>และนี่คือโฉมหน้าผู้ร่วมชะตากรรมทั้งหมด</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yynuJZvSdRhzeRSkGqhxrw.png" /></figure><p>ได้มาครบทุกฝ่ายแล้ว ลงมือทำกันเลย</p><h3><strong>Timeline</strong></h3><p>สำหรับโปรเจ็คนี้ไม่ได้เร่งอะไรมากว่างก็ทำไปเรื่อยๆจ้า แต่ถ้าเป็นโปรเจ็คอื่นต้องวางแผนทุกอย่างตั้งแต่ตอนนี้เลย</p><blockquote>ก่อนเริ่มขั้นตอนการพัฒนาอยากให้ทุกคนเข้าใจตรงกันก่อน คำว่า Userในที่นี้หมายถึงคุณหมอหรือบุคลากรทางการแพทย์ที่เป็นคนใช้แอปพลิเคชั่นตัวนี้นั่นเอง</blockquote><h3><strong>How to develop</strong></h3><ol><li><strong>เก็บ Requirement</strong></li></ol><p>เป็นการเก็บ requirement ที่ user(ผู้ใช้งานอยากได้) ถามขั้นตอนการทำงานต่างๆ เพื่อนำมาแตกเป็นฟีเจอร์ อันไหน core ของแอปพลิเคชั่น เริ่มเรียงลำดับความสำคัญในการทำ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i2yIMy4wMAmdk7sA3XYWzQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r4J4IzLXolF6rcyTJfbRTA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Oi49vYZAs18muIlLj-WmSw.jpeg" /></figure><p>จากการเก็บ requirements พวกเราสรุปข้อมูลที่จะทำในเวอร์ชั่นแรกได้ว่า</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/192/1*QbJFrAGaNBlBKNSDfS3fUQ.png" /><figcaption>CPR Recorder Icon</figcaption></figure><blockquote><strong>CPR Recorder</strong> คือ แอปพลิเคชั่นสำหรับบุคลากรทางการแพทย์ สำหรับการบันทึกข้อมูลการให้ยา การทำหัตถการต่างๆ ระหว่างการทำ CPR Algorithm ด้วยเวลา real-time อีกทั้งยังสามารถส่งไฟล์รูปภาพออกมาเพื่อทำการบันทึกในเครื่องได้อีกด้วย</blockquote><blockquote><strong>Features<br></strong>- การส่งสัญญาณให้จังหวะระหว่างการทำ CPR เพื่อให้ จังหวะการทำ CPR ที่ตรงมาตรฐาน<br>- การตั้งการแจ้งเตือน เพื่อการทำการประเมิน และการให้ยา<br>- สามารถ export ผลการบันทึกออกมาเป็นไฟล์รูปภาพได้ <br>- สามารถเปิดดู Flow chart algorithm CPR ได้ภายในแอปพลิเคชั่น</blockquote><p>พอได้ core หลักของแอปพลิเคชั่นแล้วก็ไปออกแบบกันเลย</p><p><strong>2. ออกแบบ Wireframes</strong></p><p>เป็นหน้าที่ของ Designer ในการนำ requirement ที่เก็บมานำมาสร้างเป็น Wireframes เพื่อให้ทุกคนเห็นภาพตรงกัน จากนั้นนำไปทดสอบกับ user และทำการแก้ไข ถ้าหากทุกคนในทีมเห็นพ้องต้องกันแล้วจึงเริ่ม step ถัดไป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p5pDt99pdTgBPBXTsQOqbA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fb9fTHZvlAwCpcraPTFBpA.jpeg" /></figure><p><strong>3. ออกแบบ UX/UI</strong></p><p>Designer จะเริ่มใส่ details ของตัว UI ลงไปแต่ละหน้า และสร้าง demo เสมือนจริงขึ้นมา</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e0Q_OVSV7WAoIT57tCDMDA.png" /><figcaption>CPR Recorder on Zeplin</figcaption></figure><p><strong>4. ทดสอบกับ User</strong></p><p>นำ demo ที่สร้างไว้มาทดสอบกับ user หากผ่านหรือแก้ไขหมดแล้วก็ไป step ถัดไปเลยจ้า</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DVtKLD2nl84capYXKx75sg.png" /><figcaption>Demo on Figma</figcaption></figure><p><strong>5. Develop</strong></p><p>คราวนี้เป็นหน้าที่ Developer ที่จะรับไม้ต่อในการสร้างแอปพลิเคชั่นขึ้นมาเพื่อให้สามารถใช้งานได้จริง เปรียบเสมือนพ่อมดแม่มดในโลกเวทมนต์นั่นเอง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6T5m1d96GKY_Li1AY_sWIQ.png" /><figcaption>CPR Rocorder on Android Studio</figcaption></figure><p><strong>6. Testing</strong></p><p>โดยปกติถ้าในทีมมี QA เราก็จะยกหน้าที่นี้ให้ QA ทำ แต่พอเป็นโปรเจ็คนี้เราไม่มี ทุกคนเลยต้องช่วยกันเทสจ้า พอแก้บัคครบแล้วก็เตรียม release ได้เลย</p><p><strong>7. Launch</strong></p><p>Launch ที่ไม่ได้แปลว่ากินข้าว แค่ออกเสียงคล้ายกันเฉยๆ อ้าวไปไกล กลับมาๆ 555 ง่ายๆเลยเมื่อทำแอปพลิเคชั่นเสร็จแล้วก็ upload application ไปที่ Google Play Store สิรออะไรอยู่เล่า</p><h3><strong>เข้าไปโหลดกันได้ที่นี่นะ</strong></h3><p><a href="https://play.google.com/store/apps/details?id=com.annchar.cprrecord">CPR Recorder - Apps on Google Play</a></p><p><strong>8. Maintenance</strong></p><p>เป็นงานของฝั่ง Developer ที่ต้องคอยเช็ค bug ผ่านตัว Firebase Crashlytics และต้องคอย update libraries ให้เป็นตัวใหม่อยู่เสมอ ทั้งนี้อาจทำการ refactor code เพื่อให้ง่ายต่อการพัฒนาฟีเจอร์ใหม่ๆในอนาคต</p><blockquote>จบไปแล้วกับแอปพลิเคชั่น CPR Recorder ขอบคุณทีมงานจิตอาสาทุกคนมากๆค่าที่เสียสละเวลามาช่วยกันพัฒนา</blockquote><p>สิ่งที่ได้เรียนรู้จากการทำ project นี้</p><ol><li>ได้ใช้สกิล Project Management กับการพัฒนาแอปพลิเคชั่นตั้งแต่ต้น เก็บ requirement เองไปพัฒนาจนไปถึง launch product</li><li>ทักษะ Communication เพราะเราต้องสื่อสารหลายๆฝ่ายตั้งแต่ user ยันคนในทีมที่มีความรู้หลากหลาย</li><li>ได้ทำในสิ่งที่ไม่เคยได้ทำ ฝั่ง Developer เช่นปกติเวลาเราทำงานบริษัทเราอาจจะไม่ได้เป็นคน upload แอปพลิเคชั่นเข้า store เอง แต่เป็น project นี้เราต้องเป็นคนทำเอง หรือ เราต้องขึ้นโครงโปรเจ็คเองทั้งหมด นี่จึงเป็นโอกาสที่ดีที่เราจะได้ลอง</li></ol><blockquote>ถ้ามีไอเดียก็ลงมือทำเลยอย่าไปคิดมาก…</blockquote><blockquote>สุดท้ายอย่าเป็นคนไข้มาใช้แอปพลิเคชั่นนี้เลยเน้อ ด้วยรักและเป็นห่วง ❤️❤️❤️</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/1*bAIKNbn3kzIEOlSPQ1HCzw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=84c4ad5b16cf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What are Sealed Classes? [EN]]]></title>
            <link>https://annchar.medium.com/what-are-sealed-classes-en-66ae1c34303a?source=rss-c07bb99c7f06------2</link>
            <guid isPermaLink="false">https://medium.com/p/66ae1c34303a</guid>
            <category><![CDATA[sealed]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[sealed-classes]]></category>
            <dc:creator><![CDATA[AnnChar]]></dc:creator>
            <pubDate>Fri, 28 Feb 2020 03:17:58 GMT</pubDate>
            <atom:updated>2023-03-10T09:57:09.335Z</atom:updated>
            <content:encoded><![CDATA[<h3>What are Sealed Classes?</h3><blockquote><em>Do you know the organization in the picture? 🙊🙊🙊</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*e5pRJSe3YvmHo4MC2VgB9A.jpeg" /><figcaption>Credit: <a href="https://lifestyle.campus-star.com/knowledge/120197.html">https://lifestyle.campus-star.com/knowledge/120197.html</a></figcaption></figure><p>They are a seal team in Thailand. However, in the world of Kotlin, there is another term of class which is written and read in the same way as a seal team. It is known as <strong>Sealed Class</strong>.</p><h3>Sealed Classes?</h3><p><strong>Sealed Class</strong> is one type of class that extends from an enum class, yet it has additional capabilities. Sealed class can have multiple instances and subclasses whereas an enum class can have only one instance and no subclasses.</p><p><strong>What you should know about Sealed Class?</strong></p><pre>sealed class A{<br>    class B : A()     <br>    class C : A() <br>}</pre><pre>fun main(args: Array&lt;String&gt;){     <br>    var a = A() //compiler error. Class A cannot be instantiated. <br>}</pre><ol><li>Sealed class is unable to be instantiated as it is an abstract class.</li><li>Constructors of a sealed class are private by default.</li><li>All subclasses of a sealed class must be declared within the same file.</li></ol><h3>Demo</h3><p>An example that will be shown here is according to the payment. Suppose that I wanted to show payment details of a user in various forms which have been collected from the user and I applied the normal enum class, the code could be shown below. This means when I create a class, I have to create each one separately before using it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a31984c5931ff542a721e4504a6a1599/href">https://medium.com/media/a31984c5931ff542a721e4504a6a1599/href</a></iframe><p>However, in Sealed Class, you can create multiple instances as well as subclasses. The code is demonstrated below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f12f26ba38e8a7a2223bb1709e25e0f5/href">https://medium.com/media/f12f26ba38e8a7a2223bb1709e25e0f5/href</a></iframe><p>According to the code shown, I use Sealed Class as an intermediary to collect all types of payments. Now let’s try it out!</p><p>In MainViewModel.kt, I have a LiveData which holds Sealed Class known as Payment.</p><pre>private val _paymentData = MutableLiveData&lt;Payment&gt;()    <br>val paymentData = _paymentData</pre><p>Then, it is mocked the value assuming that it was the data received from the user.</p><pre>private val paymentByCreditCardData: Payment = Payment.CreditCard(<br>    name = &quot;AnnChar&quot;,<br>    number = &quot;123456789123&quot;,<br>    expiredDate = &quot;02/23&quot;,<br>    transferAmount = 100.50<br>)<br><br>private val paymentByCashBankData: Payment = Payment.CashBank(<br>    bankCode = &quot;101&quot;,<br>    bankName = &quot;bank&quot;,<br>    transferAmount = 202.20<br>)<br><br>private val paymentByPromptpayData: Payment = Payment.Promptpay(<br>    name = &quot;AnnChar&quot;,<br>    citizenId = &quot;1234567891234&quot;,<br>    mobileNumber = &quot;0891234567&quot;,<br>    transferAmount = 8002.50<br>)<br><br>private val nonePaymentData: Payment = Payment.NonePayment</pre><pre>init {<br>    // Choose data to set payment data<br>    setPaymentData(paymentByCreditCardData)<br>}</pre><p>When data changes, Observer which is waiting in MainActivity.kt, will start working.</p><pre>viewModel = ViewModelProviders.of(this).get(MainViewModel::class.<em>java</em>)<br>viewModel.paymentData.observe(this, <em>Observer</em>&lt;Payment&gt; <strong>{<br>    </strong>bindDataToView(<strong>it</strong>)<br><strong>}</strong>)</pre><p>From the code appeared, you can see that the function of bindDataToView will be called. Within this function, the function of getPaymentDetailToString will be called, too.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cdbd0f1de899addbcf736aa6c56c274b/href">https://medium.com/media/cdbd0f1de899addbcf736aa6c56c274b/href</a></iframe><p>From the code shown above is an advantage of using <strong>Sealed Class</strong> as I can apply it with when expression. After the code is run, it will appear as below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QzbgVO8uFymv8Ceq.png" /></figure><p><strong>Finished!</strong> According to the example given, you can find the full code at the link below. Try to use it as appropriate.</p><p><a href="https://github.com/annchar/SealedClassesDemo">annchar/SealedClassesDemo</a></p><p><strong>Article in Thai version:</strong></p><p><a href="https://medium.com/@annchar/what-are-sealed-classes-9a58f5ed4de8">What are Sealed Classes? [TH]</a></p><p><strong>References:</strong></p><ul><li><a href="https://kotlinlang.org/docs/reference/sealed-classes.html">Sealed Classes</a></li><li><a href="https://www.journaldev.com/18719/kotlin-sealed-class">Kotlin Sealed Class - JournalDev</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=66ae1c34303a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What are Sealed Classes? [TH]]]></title>
            <link>https://annchar.medium.com/what-are-sealed-classes-9a58f5ed4de8?source=rss-c07bb99c7f06------2</link>
            <guid isPermaLink="false">https://medium.com/p/9a58f5ed4de8</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[sealed]]></category>
            <category><![CDATA[sealed-classes]]></category>
            <dc:creator><![CDATA[AnnChar]]></dc:creator>
            <pubDate>Thu, 27 Feb 2020 04:38:43 GMT</pubDate>
            <atom:updated>2023-03-10T09:57:33.967Z</atom:updated>
            <content:encoded><![CDATA[<h3>Sealed Classes คือ?</h3><blockquote>คุณรู้จักหน่วยงานในภาพไหม? 🙊🙊🙊</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*e5pRJSe3YvmHo4MC2VgB9A.jpeg" /><figcaption>Credit: <a href="https://lifestyle.campus-star.com/knowledge/120197.html">https://lifestyle.campus-star.com/knowledge/120197.html</a></figcaption></figure><p>พวกเขาคือหน่วย SEAL ของไทยเรานี่เอง แต่ในโลกของ Kotlin มี class ประเภทหนึ่งที่เขียนและอ่านออกเสียงเหมือนกันซึ่งวันนี้เราจะพาทุกคนไปทำความรู้จักกับสิ่งที่เรียกว่า <strong>Sealed Class</strong> นั่นเอง</p><h3><strong>Sealed Classes?</strong></h3><p><strong>Sealed Class</strong> คือ class ประเภทหนึ่งที่ extension มาจาก enum class จึงทำให้มีคุณสมบัติต่างๆคล้าย enum class แต่จะมีความสามารถเพิ่มเข้ามาคือ สามารถมีได้หลาย instance มี subclasses ได้ ซึ่งต่างจาก enum class ที่มี instance ได้เพียงตัวเดียวเท่านั้นและไม่มี subclasses</p><p><strong>สิ่งที่ควรรู้สำหรับ Sealed Class</strong></p><pre>sealed class A{<br>    class B : A()     <br>    class C : A() <br>}</pre><pre>fun main(args: Array&lt;String&gt;){     <br>    var a = A() //compiler error. Class A cannot be instantiated. <br>}</pre><ol><li>Sealed Class ไม่สามารถ instantiated ได้เพราะมันคือ abstract class</li><li>Constructors ของ Sealed Class เป็น private โดย default</li><li>ทุก subclasses ของ Sealed Class จะต้องถูกประกาศภายใต้ file เดียวกันเท่านั้น</li></ol><h3><strong>Demo</strong></h3><p>โดยตัวอย่างที่เราจะเอามาประยุกต์ใช้จะเกี่ยวกับเรื่อง payment โดยสมมติว่า เราต้องการจะเรียก show ข้อมูล payment detail ของ user ในรูปแบบต่างๆที่เราเก็บค่ามาแล้วจาก user ถ้าเราใช้ enum class แบบปกติจะได้ตาม code ด้านล่าง ซึ่งเวลาเราสร้าง class เราต้องไปสร้างแยกเอาเองแต่ละตัวอีกทีก่อนนำไปใช้งาน</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a31984c5931ff542a721e4504a6a1599/href">https://medium.com/media/a31984c5931ff542a721e4504a6a1599/href</a></iframe><p>แต่ใน Sealed Class นั้นเราสามารถสร้างได้หลาย instance และสามารถมี subclasses ได้ code ที่ได้จึงออกมาในรูปแบบนี้</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f12f26ba38e8a7a2223bb1709e25e0f5/href">https://medium.com/media/f12f26ba38e8a7a2223bb1709e25e0f5/href</a></iframe><p>จาก code เราใช้ Sealed Class เป็นตัวกลางในการรวบรวมข้อมูลทุก type ของ payment ถึงเวลานำไปลองใช้งานกันแล้ว</p><p>ใน MainViewModel.kt เรามี LiveData ตัวหนึ่งซึ่งทำหน้าที่เก็บ Sealed Class ที่ชื่อว่า Payment</p><pre>private val _paymentData = MutableLiveData&lt;Payment&gt;()    <br>val paymentData = _paymentData</pre><p>จากนั้นทำการ mock ค่าให้กับมัน โดยสมมติว่าเป็น data ที่ได้รับจาก user</p><pre>private val paymentByCreditCardData: Payment = Payment.CreditCard(<br>    name = &quot;AnnChar&quot;,<br>    number = &quot;123456789123&quot;,<br>    expiredDate = &quot;02/23&quot;,<br>    transferAmount = 100.50<br>)<br><br>private val paymentByCashBankData: Payment = Payment.CashBank(<br>    bankCode = &quot;101&quot;,<br>    bankName = &quot;bank&quot;,<br>    transferAmount = 202.20<br>)<br><br>private val paymentByPromptpayData: Payment = Payment.Promptpay(<br>    name = &quot;AnnChar&quot;,<br>    citizenId = &quot;1234567891234&quot;,<br>    mobileNumber = &quot;0891234567&quot;,<br>    transferAmount = 8002.50<br>)<br><br>private val nonePaymentData: Payment = Payment.NonePayment</pre><pre>init {<br>    // Choose data to set payment data<br>    setPaymentData(paymentByCreditCardData)<br>}</pre><p>เมื่อข้อมูลมีการเปลี่ยนแปลง ตัว Observer ที่รออยู่ใน MainActivity.kt ก็จะเริ่มทำงาน</p><pre>viewModel = ViewModelProviders.of(this).get(MainViewModel::class.<em>java</em>)<br>viewModel.paymentData.observe(this, <em>Observer</em>&lt;Payment&gt; <strong>{<br>    </strong>bindDataToView(<strong>it</strong>)<br><strong>}</strong>)</pre><p>จาก code จะเห็นว่า function ของ bindDataToView ถูกเรียกใช้งาน ซึ่งภายใน fucntion นี้มีการเรียกใช้ function ของ getPaymentDetailToString ต่ออีกทอดหนึ่ง</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cdbd0f1de899addbcf736aa6c56c274b/href">https://medium.com/media/cdbd0f1de899addbcf736aa6c56c274b/href</a></iframe><p>จาก code ด้านบนนี่คือข้อดีของการใช้ Sealed Class เลยเพราะเราสามารถนำมาใช้ร่วมกับ when expression ได้ เมื่อ run ออกมาจะได้แบบนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nSSwc6XOtEi-D3ZekqcIBw.png" /></figure><p>จบไปแล้วจ้า จากตัวอย่างข้างต้นสามารถเข้าไปดู code เต็มๆได้ที่ลิ้งค์ด้านล่าง ยังไงตอนใช้งานก็ลองเลือกใช้ตามความเหมาะสมน้า</p><p><a href="https://github.com/annchar/SealedClassesDemo">annchar/SealedClassesDemo</a></p><p><strong>Article in English version:</strong></p><p><a href="https://medium.com/@annchar/what-are-sealed-classes-en-66ae1c34303a">What are Sealed Classes? [EN]</a></p><p><strong>References:</strong></p><ul><li><a href="https://kotlinlang.org/docs/reference/sealed-classes.html">Sealed Classes</a></li><li><a href="https://www.journaldev.com/18719/kotlin-sealed-class">Kotlin Sealed Class - JournalDev</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9a58f5ed4de8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ว่าด้วยเรื่อง Data Binding + LiveData]]></title>
            <link>https://annchar.medium.com/mvvm-databinding-livedata-2c53a8825187?source=rss-c07bb99c7f06------2</link>
            <guid isPermaLink="false">https://medium.com/p/2c53a8825187</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[livedata]]></category>
            <category><![CDATA[data-binding]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[AnnChar]]></dc:creator>
            <pubDate>Tue, 26 Jun 2018 08:23:08 GMT</pubDate>
            <atom:updated>2018-06-26T08:23:08.568Z</atom:updated>
            <content:encoded><![CDATA[<p>เนื่องจากงาน Google IO 2018 ที่ผ่านมา ประจวบเหมาะกับเป็นช่วง time gap เพิ่งปิดโปรเจ็คที่ออฟฟิศเสร็จเลยมีเวลาได้ลองเล่น ก่อนจะไปดูตัวอย่างเรามาลองทำความรู้จักกับทั้งสองตัวนี้ก่อน</p><h3><strong>Data Binding</strong></h3><p><strong>Data Binding</strong> เป็น library ที่ทาง Google ออกมานานพอสมควร เป็นตัวช่วยที่ทำให้เราสามารถ bind ข้อมูลเข้ากับ Layout XMLได้ โดยที่มี Data Type ที่รองรับ ดังนี้</p><ul><li>Data object</li><li>Property Reference</li><li>Collections</li><li>String literals</li><li>Resources</li><li>Event handling</li><li>Method references</li><li>Listener bindings</li><li>Imports, variables, and includes</li></ul><p>สามารถดูตัวอย่างและอ่านเพิ่มเติมได้ตามนี้</p><ul><li><a href="https://developer.android.com/topic/libraries/data-binding/">Data Binding Library | Android Developers</a></li><li><a href="https://medium.com/20scoops-cnx/data-binding-android-fd1ce6f63e46">Data Binding Android</a></li></ul><h3>LiveData</h3><p><strong>LiveData </strong>คือ คลาสสำหรับเก็บข้อมูลประเภทที่สามารถตั้งให้เรียกทำงานเมื่อข้อมูลมีการเปลี่ยนแปลงได้หรือที่หลาย ๆ คนคงคุ้นเคยกับคำว่า Observable นั่นเอง แต่สำหรับ LiveData มีความพิเศษอีกอย่างนึง คือ มันเป็น <strong><em>Observable ที่ทำงานร่วมกับ Lifecycle</em></strong> ด้วย</p><p>— ที่มา <a href="https://nuuneoi.com/blog/blog.php?read_id=940">https://nuuneoi.com/blog/blog.php?read_id=940</a></p><ul><li><a href="https://developer.android.com/topic/libraries/architecture/livedata">LiveData overview | App architecture | Android Developers</a></li><li><a href="https://nuuneoi.com/blog/blog.php?read_id=940">NuuNeoI - รู้จัก Android Jetpack แบบจัดเต็ม เครื่องมือเร่งการพัฒนาแอป ฯ แอนดรอยด์ให้เร็วและโค้ดสวยงาม</a></li><li><a href="http://www.akexorcist.com/2017/05/new-things-in-google-io-2017-for-android-developer.html">[Android Dev Tips] สรุปสาระสำคัญจากงาน Google I/O 2017 ที่นักพัฒนาแอนดรอยด์ไม่ควรพลาด</a></li></ul><h3>Let’s Start</h3><p>โจทย์ คือ เราจะทำแอปพลิเคชั่นแสดงผลเกี่ยวกับฟุตบอลโลก 2018 ซึ่งแอปพลิเคชั่นจะมีหน้าตาแบบนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/342/1*ivpJEhUN5oyvoTZM6M5Y2w.gif" /></figure><p>โครงสร้างโปรเจ็คที่วางไว้จะอยู่ในรูปแบบของ <strong>MVVM (Model-View-ViewModel)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/469/1*VmpdCcr70mrEkAy6UjoFLA.png" /></figure><ul><li><strong>Model</strong> : เป็น POJO class เช่นเดียวกันกับ MVC และ MVP</li><li><strong>View</strong> : รวมไฟล์ทั้งหมดที่เกี่ยวกับ View ไม่ว่าจะเป็น Activity, Fragment, Adapter</li><li><strong>ViewModel</strong> : เป็นตัวกลางระหว่างเจ้า View และ Model ซึ่งหน้าที่หลักของมันคือการจัดการ State (ในที่นี้ คือ <em>LiveData</em>) ที่ผูกอยู่กับ UI และเมื่อเราใช้ความสามารถของ Data Binding เมื่อ State ใน ViewModel เปลี่ยน UI ของเรานั้นก็จะเปลี่ยนไปด้วย และในทางกลับกันเมื่อ UI เปลี่ยน State ของเราก็จะเปลี่ยนตามไปด้วยเช่นกัน</li></ul><h3>การ Update UI ผ่าน LiveData</h3><p>เมื่อข้อมูลใน LiveData เกิดการเปลี่ยนแปลง เราสามารถ Update UI ได้ 2 วิธี คือ</p><ol><li><strong>ผ่าน method observe() ของ LiveData</strong></li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5de9c36f8715ccb7e40537cbd6ef83f4/href">https://medium.com/media/5de9c36f8715ccb7e40537cbd6ef83f4/href</a></iframe><p>จะสังเกตุได้ว่าการ observe คือการเฝ้ารอการเปลี่ยนแปลงของข้อมูล ซึ่งเราสามารถนำข้อมูลไปใช้ต่อได้ ดังตัวอย่าง</p><pre>adapter.value = it</pre><p>เมื่อข้อมูลที่เปลี่ยนแปลงและถูก set เข้าไปใน value ของ adapter และมีการสั่ง notifyDataSetChanged() แล้ว onBindViewHolder() ก็จะถูกเรียก ดังโค้ดข้างล่าง</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f38382c99717d994b99b9fa41d64b735/href">https://medium.com/media/f38382c99717d994b99b9fa41d64b735/href</a></iframe><p>เมื่อ set ข้อมูลออกมาเรียบร้อยเราจะได้หน้าตาแอปพลิเคชั่นประมาณนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*joYSGdmkRCymRXrtlEhmQA.png" /></figure><p><strong>2. ใช้ Data Binding observe LiveData และทำการ Update UI ให้อัตโนมัติ</strong></p><p>ในการจะให้ Data Binding ผูกข้อมูลเข้ากับ UI ต้องมีการประกาศตัวแปร &lt;data&gt; และ Map View Model เข้ากับ Layout ที่ต้องการผ่าน @{viewModel.xxx} ดังนี้</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd2d0e8d7b0d66e8e730277b12aa2240/href">https://medium.com/media/dd2d0e8d7b0d66e8e730277b12aa2240/href</a></iframe><p>จากนั้นเราให้ View Model กับ Data Binding ทำความรู้จักกันผ่านคำสั่ง</p><pre>viewDataBinding.<em>viewModel </em>= teamDetailViewModel</pre><p>เมื่อ View Model กับ Data Binding รู้จักกันแล้ว เราก็สามารถสั่งดึงข้อมูลจาก API ผ่าน method loadPlayerList() ใน View Model เพื่อ Update UI ได้</p><pre>teamDetailViewModel.loadPlayerList()</pre><p>โค้ดก็จะเป็นหน้าตาประมาณนี้</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5beecd6138c768ac7c3a3ffc49d9e642/href">https://medium.com/media/5beecd6138c768ac7c3a3ffc49d9e642/href</a></iframe><p>เมื่อ API ตอบกลับมา ก็ทำการ Update ข้อมูลให้กับ LiveData หลังจากนั้น Data Binding จะทำการ Bind ข้อมูลที่เปลี่ยนแปลงเข้ากับ UI ให้เอง</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/489cb3ff956ca7a9e345363cd78264c2/href">https://medium.com/media/489cb3ff956ca7a9e345363cd78264c2/href</a></iframe><p>ซึ่งเวลา run จะได้หน้าตาแอปพลิเคชั่นออกมาประมาณนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*alzMhtnW1BjZBOD3SMv_Zw.png" /></figure><p>ก็คงจะเห็นภาพคร่าวๆแล้วว่า เราสามารถเอา Data Binding และ LiveData มาทำงานในแบบต่างๆ หากใครเคยลองเล่น มีข้อสงสัย หรือมีข้อเสนอแนะก็เข้ามาพูดคุยกันได้ตลอด</p><p>สุดท้ายขอบคุณ <a href="https://medium.com/u/af05d0cb80e1">Teeranai.P</a> ที่ช่วยขัดเกลาและให้คำแนะนำ บทความนี้เป็นบทความ Tech ที่เขียนครั้งแรกในชีวิต ฝากไว้ในอ้อมอกอ้อมใจทุกคนด้วยนะคะ</p><p><strong>Source code</strong> ตัวอย่างลองเอาไป run ได้ตาม link นี้จ้า</p><p><a href="https://github.com/annchar/CheerBall">annchar/CheerBall</a></p><p><strong>เอกสารอ้างอิง</strong></p><p>https://developer.android.com/topic/libraries/architecture/adding-components</p><p>http://www.artit-k.com/android-data-binding/</p><p>https://blog.untitledkingdom.com/refactoring-recyclerview-adapter-to-data-binding-5631f239095f</p><p>https://medium.com/google-developers/android-data-binding-recyclerview-db7c40d9f0e4</p><p>https://medium.com/ta-tonthongkam/%E0%B8%A1%E0%B8%B2%E0%B9%83%E0%B8%8A%E0%B9%89-android-architecture-components-%E0%B8%81%E0%B8%B1%E0%B8%99%E0%B9%80%E0%B8%96%E0%B8%AD%E0%B8%B0-23811a9b4c31</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2c53a8825187" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>