<?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 Tomaz Wang on Medium]]></title>
        <description><![CDATA[Stories by Tomaz Wang on Medium]]></description>
        <link>https://medium.com/@tomazwang?source=rss-be1015e7ea6a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*XsJFSs-snNhS3CcCp6FFfA.png</url>
            <title>Stories by Tomaz Wang on Medium</title>
            <link>https://medium.com/@tomazwang?source=rss-be1015e7ea6a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 20:25:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tomazwang/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[認真學 Compose 1  — 從 0 到畫面]]></title>
            <link>https://medium.com/@tomazwang/learn-compose-the-hard-way-1-68c400399c21?source=rss-be1015e7ea6a------2</link>
            <guid isPermaLink="false">https://medium.com/p/68c400399c21</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <dc:creator><![CDATA[Tomaz Wang]]></dc:creator>
            <pubDate>Sat, 21 Aug 2021 09:06:25 GMT</pubDate>
            <atom:updated>2021-08-21T13:39:16.782Z</atom:updated>
            <content:encoded><![CDATA[<h3>認真學 Compose 1 — 從 0 到畫面</h3><p>這篇文章是我試圖認真研究 Compose 的筆記</p><ul><li><a href="https://medium.com/@tomazwang/learn-compose-the-hard-way-0-4f068edc7a5b">認真學 Compose 0 — 前言</a></li><li>認真學 Compose 1 — 從 0 到畫面 (本篇)</li><li>&lt; 後面努力中 &gt; …</li></ul><p>本次的結果放在這個專案中，tag 0.1.0-from_0_to_screen <br>有興趣的人可以直接看 code</p><p><a href="https://github.com/TomazWang/Learn-Compose-The-Hard-Way_lcthw/tree/0.1.0-from_0_to_screen">GitHub - TomazWang/Learn-Compose-The-Hard-Way_lcthw at 0.1.0-from_0_to_screen</a></p><h3>我想要做什麼</h3><p>簡單做完官方提供的 Codelab, 了解基礎運用後<br>我想要來自己做點東西探索一下</p><p>學習像 Jetpack Compose 這樣面向前端 UI 的 framework<br>最直覺的方法就是從臨摹開始</p><p>這次我想嘗試的 UI 是 Spotify <br>的搜尋頁只有 UI、沒有行為, 並且撇去過於複雜的部分<br>目標是透過這個模仿可以探索 Jetpack Compose 提供的 API</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*wZqM0pm2UZ5F7hBKS5Pjig.png" /><figcaption>右圖是 Spotify 的搜尋頁畫面; 左圖是我這次模仿的成品</figcaption></figure><blockquote>這次的學習有很大部分參考了<a href="https://github.com/Bogomil-Stoyanov/SpotifyUiJetpackCompose">這個專案</a>。程式部分相同的部分絕對不是巧合</blockquote><h3>第一個畫面</h3><p>在開始寫畫面之前，我先將 MainActivity 中 setContent 裡的內容，用一個 Composable function 取代，並將這個 function 放置於適合管理的 package</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f8aa6893a99fb2fcbdf71c3f8f01cb4c/href">https://medium.com/media/f8aa6893a99fb2fcbdf71c3f8f01cb4c/href</a></iframe><p>如此一來，就可以更專注於編輯這一個畫面了</p><p>接著，來拆解一下這個畫面</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*QhA7p1zdOLkIP3F4GZ5IGg.png" /></figure><p>大標題 “Search” 以及下面幾個小標題，是單純的文字<br>白色區塊有一個看起來像是 search bar 的功能，裡面有一張圖片及一些文字<br>再下面有各種音樂曲風的方塊，兩兩一列<br>每個方塊中，有曲風的名稱、對應的圖案，以及對應的背景色</p><h4>Layout — Column &amp; Rows</h4><p>這是一個直立排列畫面，所以我使用 Column 這個 compose function</p><p>Column，可以想像成 vertical 的 LinearLayout。是內容物縱向排列的佈局方式；相對應的，Row 是橫向排列的佈局方式，可以想像成 horizontal 的 LinearLayout</p><blockquote>這邊都用想像的，因為實際上 Compose 在畫面上不是使用 LinearLayout 繪製</blockquote><p>另外還有一種，稱為 Box。有點類似 FrameLayout 的設計，在這個 layout 中的 item 會一個一個往上疊</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*prFzEaZr9IP2P2u52dukVQ.png" /><figcaption>更詳細可以參考 <a href="https://developer.android.com/jetpack/compose/layouts">Layouts in Compose | Jetpack Compose | Android Developers</a></figcaption></figure><p>回到畫面，我在 Column 中擺上對應的文字，並且利用 Box 來代替尚未完成的元件</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f86c3a95f2829c584e7b513d6a02182/href">https://medium.com/media/4f86c3a95f2829c584e7b513d6a02182/href</a></iframe><p>如此，就可以得到以下畫面</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*bcbmOWBCe3IayidogOk8og.png" /></figure><h4>Composable Function 的設計</h4><p>官方內建的 Composable function 都有著類似的設計</p><p>首先，這些 function 都是利用 Kotlin 中 trailing lambda ( function 的最後一個參數是 lamda) 可以獨立寫在 () 之後的特性，讓 compose 能用 DSL 的方式表示</p><p>另外，大部分參數都有 default 值。利用 Kotlin function 呼叫時可以直接指定參數的特性讓使用上更直覺</p><p>以 Text 為例，Text 這個 function 實際上有 16 個參數</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eeeb09e2cd830e5f8edfa2b2aa12ac9f/href">https://medium.com/media/eeeb09e2cd830e5f8edfa2b2aa12ac9f/href</a></iframe><p>但在什麼設定都沒有要調整的情況下，只需要提供 text 這個參數就可以了</p><p>即便我今天要調整文字顏色，我也只需要特別指定 color = xxx 就可以完成調整，用起來相當直覺</p><h4>Modifier</h4><p>在最外層 Column 的地方，可以看到一個寫法</p><pre>Column(modifier = Modifier.....)</pre><p>Modifier 是 compose 這套 framework 中，用來 “調整” 畫面的方式</p><p>大致上可以分成幾大類</p><ul><li>Layout 相關：長、寬、padding ( 沒有 margin, 通通用 padding 解決）</li><li>畫面相關：背景顏色、元件造型</li><li>行為相關：點擊、滑動（這篇文章不會提到)</li></ul><p>在使用上，可以用 chain 的方式串連多個 Modifier</p><pre>Modifier.clip(...).fillMaxHeight().fillMaxWidth().background(...)</pre><p>要注意的是，Modifier 會有執行先後順序，由左至右執行</p><blockquote>更精確地來說，由左至右設定規則，由右至左計算大小。<a href="https://stackoverflow.com/a/65698101">詳見</a></blockquote><p>Modifier 也可以被當作參數傳入 function ( 這個也是我們將 Modifier 傳給 Column 的方法 )</p><p>當你想要把兩個 Modifier 直接做串連時，可以使用<em> </em><em>.then()</em>串連</p><pre>Modifier.then(modifier1).then(modifier2)</pre><h3>第一個元件</h3><p>光一個黑色畫面就囉唆那麼多，什麼時候才能刻出整個畫面<br>趕緊來刻白色的那條搜尋 bar 吧</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/1*Rt3nv-7cQhg3wojZwDwdcw.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b9e1bc96b9fc8b4fedfc47543f441837/href">https://medium.com/media/b9e1bc96b9fc8b4fedfc47543f441837/href</a></iframe><h4>Icon</h4><p>在畫放大鏡時，我原本以為要使用 ImageView 之類的東西<br>後來找到了 Icon 這個可以畫向量圖的元件，還可以透過 Icons 直接取得內建的圖示</p><h4>更多 Modifier</h4><p>padding 除了給一個數字之外，其實還有多種指定方式</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1603ad2647f65b1a251c108505a7af63/href">https://medium.com/media/1603ad2647f65b1a251c108505a7af63/href</a></iframe><p>另外，Row 和 Column 有 alignment 的選項</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ffb467941c0f57ee50722b2002a0319/href">https://medium.com/media/4ffb467941c0f57ee50722b2002a0319/href</a></iframe><p>有點類似 xml 中的 <em>gravity</em> , 可以調整 child element 的排列方式</p><h4>Modifier.clip()</h4><p>因為希望 SearchBox 是有帶點圓角的，所以用了 clip 這個 modifier</p><p>clip 可以將目前的元件切成某個形狀</p><ul><li>矩形：shape = RectangleShape</li><li>圓形：shape = CircleShape</li><li>圓角：shape = RoundedCornerShape</li><li>切角：shape = CutCornerShape</li></ul><p>值得一提的是，我在寫這段的時候踩到 Modifier 順序的雷<br>因為先 padding 再切，和先切再 padding 會得到不一樣的形狀</p><h3>可滑動列表 — LazyColumn</h3><p>會畫 SearchBox 後，我就依樣畫葫蘆的畫了幾個方塊<br>先當作 place holder 的把它填上了畫面</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9d12fbc9b34eb2bbcc5ee09dac4e511/href">https://medium.com/media/f9d12fbc9b34eb2bbcc5ee09dac4e511/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/371/1*hVy356Vs4Lhu515kOwshUA.png" /></figure><p>格子是有了，但畫面無法如預期的滑動</p><p>是這樣的，Column 和 Row 預設是無法 Scroll 的<br>要達成 Scrolling 也不是不行，一樣是透過 Modifier</p><pre>Modifier.verticalScroll(scrollState) // state 在這篇文章不會提到</pre><p>但這樣的 Scroll 沒有類似 RecyclerView 那樣的回收機制<br>這個頁面我期望學會使用替代 RecyclerView 的方法</p><p>於是，就要介紹到 LazyColumn 和 LazyRow</p><h4>Lazy Composables</h4><p>LazyColumn 和 LazyRow 是 Jetpack compose 中用來顯示大量資料的元件</p><p>和 Column 和 Row 間的差異相同，LazyColumn 和 LazyRow 的差異在於排列方向。</p><p>在使用上，Lazy composable 的 content 區塊可以放置 item 或 items 來標明內容</p><pre>LazyColumn {</pre><pre>    // 可以直接放單個 item<br>    item { Text(...) }</pre><pre>    // 可以放 n 個 item, 提供數量<br>    items(5) { Text(...) }<br>    <br>    // 可以放某個資料 (List&lt;T&gt;)<br>    items(dataList) {  data -&gt; Text(data) }</pre><pre>}</pre><p>利用 LazyColumn 改寫後畫面會變這樣</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f4a5b1395f55ce90e7c32a6cee79ad1/href">https://medium.com/media/5f4a5b1395f55ce90e7c32a6cee79ad1/href</a></iframe><p>每一個元件都用 item 包裝，需要重複的元件就用 items 做重複</p><p>在套上 LazyColumn 時，同時也加上了 verticleArrangement <br>這個參數可以設定 LazyColumn 裡面的每個 item 之間的空間</p><h3>獨立元件</h3><p>整個畫面上大致上完成了，只剩下應該要出現在白色框框內的音樂區塊</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/986/1*fzpi8-E89AbRfVGRundEQw.png" /></figure><p>讓我們來分析一下這個區塊</p><p>左上角有一個文字<br>右下角有一張圖片，並且圖片有旋轉<br>最後背景被某個顏色填滿並切圓角</p><p>關於這個元件，因為內容獨立且可能會到處重複<br>於是我在另一個 package 中，開了一個新的 kt file 收整這個元件</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/69c55aa07c6447a5884a88eaa7a9cdd5/href">https://medium.com/media/69c55aa07c6447a5884a88eaa7a9cdd5/href</a></iframe><h4>Function 設計</h4><p>在設計這個元件時，我將所有元件內可能變動的參數都提出來作為 funciton 的參數。這樣的做法可以讓這個元件未來能更有彈性的顯示不同的資料，在 Preview 時，也可以在 preview function 中將這些參數調整成自己想要測試的樣貌來預覽。</p><p>同時，也特別留了一個參數，讓外部可以將 modifier 傳進來</p><blockquote>哪些資料該用傳進來的，哪些資料該直接由此元件取得其實會依狀況 &amp; 設計不同。<br>若這個元件從畫面到功能都非常獨立，也許資料是適合由它自己取得的</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/506/1*s3NpdKw-9B02ZnBYZdBgSA.png" /></figure><p>依照上面的程式，我們可以做出目前的樣板<br>文字的部分看起來沒什麼問題了，接下來就來加上圖片</p><h4>Image</h4><p>在 Compose 中要顯示一個簡單的圖片（從 drawable 中取得圖片)，可以使用 Image 這個 compose fucntion</p><pre>@Composable<br>fun ImageResourceDemo() {<br>  val image: Painter = painterResource(id = R.drawable.composelogo)<br>  Image(painter = image,contentDescription = “”)<br>}</pre><pre>// <a href="https://foso.github.io/Jetpack-Compose-Playground/foundation/image/">foso.github.io/Jetpack-Compose-Playground/foundation/image/</a></pre><p>在使用 Image 時，需要提供一個 Painter。Painter 的建立可以透過內建的 function painterResource 產生</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dcbbb7ddcc2d7a6d71334a4d3ec1f993/href">https://medium.com/media/dcbbb7ddcc2d7a6d71334a4d3ec1f993/href</a></iframe><p>和其他元件一樣，Image 一樣可以接收 Modifier 並作調整。這裡，我就用了 Modifer 將圖片進行旋轉</p><h4>ConstraintLayout</h4><p>但是目前的圖和文字並沒有擺在正確的地方</p><p>這邊又要介紹到另一個 Layout<br>也就是以前 Android 中可能最常用的 ConstrantLayout</p><p>使用 ConstraintLayout 和 Column / Row 這種固定排列的 Layout 不太一樣</p><p>會需要先指定每個元件的 “名稱” (ref, 就像是以前 xml 的 @+id/xxx 一樣)，之後透過 Modifier 將元件與元件之間關聯再一起(建立 constraint)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2ed375d2ac1362be5e05cace89c06d9/href">https://medium.com/media/d2ed375d2ac1362be5e05cace89c06d9/href</a></iframe><p>如此我們就可以得到下面這樣的畫面</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*ynPMjfUd09tsF_-HYaotew.png" /></figure><h4>組裝</h4><p>備料都被好了，接下來就剩組裝啦</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/185ee5861b1c21f33b3d8365cbe8d2a8/href">https://medium.com/media/185ee5861b1c21f33b3d8365cbe8d2a8/href</a></iframe><p>首先我先建立了一個 data class 來裝這些音樂類型可能會需要的參數<br>再來建立一個假的 repo 隨機寫死一些假資料</p><p>最後，將 LazyColum 中的 items(20) 改成取得對應 repo 中的列表<br>因為一排要顯示兩個，所以用 chunk 將資料分為兩兩一組</p><p>最後就產生了這個畫面</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BDVYXQBIF8G2pB0KzRUJIg.jpeg" /></figure><p>最後組裝這段，就沒有很詳細地把步驟記錄下來了<br>有興趣的可以直接到這個練習的 GitHub repo 看看</p><h3>後記</h3><p>這邊應該只有碰到很初淺的 Compose 作法，甚至還沒寫到 State</p><p>希望接下來有辦法繼續往下探索，慢慢累積對 Compose 這套工具的認知</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=68c400399c21" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[認真學 Compose 0 — 前言]]></title>
            <link>https://medium.com/@tomazwang/learn-compose-the-hard-way-0-4f068edc7a5b?source=rss-be1015e7ea6a------2</link>
            <guid isPermaLink="false">https://medium.com/p/4f068edc7a5b</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <dc:creator><![CDATA[Tomaz Wang]]></dc:creator>
            <pubDate>Sat, 21 Aug 2021 09:05:15 GMT</pubDate>
            <atom:updated>2021-08-21T09:13:04.027Z</atom:updated>
            <content:encoded><![CDATA[<h3>認真學 Compose 0 — 前言</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*OT4J6X5y_vrm5_5xpCUDKw.png" /></figure><p>Jetpack Compose 出了這麼久，從 API 還不穩定到現在已經正式 Release<br>一直都沒有機會好好的，系統化的學習這個新的 UI 工具</p><p>最近決定從 0 開始，跟著各種資源一點一滴學習 Compose<br>並且將過程一一記錄下來</p><p>這個文章應該不會是最好的 step by step 教學<br>但希望可以和大家一起發現 / 學到 step by step 學不到的東西<br>一起解答一些我看到 Compose 時的疑惑</p><h3>提出問題</h3><p>在學習一套新的工具時，我喜歡先了解這個工具的設計原理和設計理念<br>知道設計團隊 / 設計者希望我們怎麼用，再去用它總會感覺比較順手</p><p>接觸 Compose 之前，除了基礎的 Android UI 之外，還有接觸過 React 以及 Flutter 這類 Component 結構。但是看了 Jetpack Compose 之後，卻產生了許多問題</p><ul><li>為什麼會有 Jetpack Compose? 現在的 layout 方法不好嗎？</li><li>@Composable 到底做了什麼？</li><li>Jetpack Compose 實際對畫面做了什麼？</li><li>State 怎麼控制最好？</li><li>架構如何安排？有辦法配合原先 Android 的 MVVM 嗎？</li><li>Modifier 裡面裝了多少東西？ 為什麼有時候能多 function？又為什麼可以 chaining？</li><li>Jake Wharton 說過 Jetpack Compose 和 Compose UI 好像不太相同，到底有什麼差？</li></ul><p>… blah blah blah</p><p>帶著這些問題開始研究，希望接下來可以陸續找到答案<br>好了別說這麼多了，趕快開始吧！</p><p><a href="https://medium.com/@tomazwang/learn-compose-the-hard-way-1-68c400399c21">認真學 Compose 1  — 從 0 到畫面</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4f068edc7a5b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[那些 Kotlin 中的靜態事]]></title>
            <link>https://medium.com/@tomazwang/%E9%82%A3%E4%BA%9B-kotlin-%E4%B8%AD%E7%9A%84%E9%9D%9C%E6%85%8B%E4%BA%8B-407139a17e5b?source=rss-be1015e7ea6a------2</link>
            <guid isPermaLink="false">https://medium.com/p/407139a17e5b</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[companion-object]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Tomaz Wang]]></dc:creator>
            <pubDate>Thu, 26 Jul 2018 06:54:45 GMT</pubDate>
            <atom:updated>2018-07-26T10:32:32.106Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><em>文章有點亂，tl;dr 在最後面可以直接跳過去</em></blockquote><h4>前情提要</h4><p>前幾天在和同事討論把 model class 從 java 轉成 Kotlin</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/573937e26439fe6c824f38089b7fe184/href">https://medium.com/media/573937e26439fe6c824f38089b7fe184/href</a></iframe><p>要轉成 Kotlin 當然是使用 data class 啦～</p><p>這個新手教學都會寫，這也是最常拿來做 Kotlin 宣傳的東西，可以大幅縮減 code 讓大家眼睛為之一亮</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc93fd619b2709ff58ebcab2d2fa5f02/href">https://medium.com/media/bc93fd619b2709ff58ebcab2d2fa5f02/href</a></iframe><p>簡單，但是事情如果真的那麼簡單，就沒必要寫這篇了</p><p>事實是，一開始的 java class 就沒那麼單純。他是一個 Parcelable 的實作！！</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6ce9e8d1c702536d516627f5b735c54/href">https://medium.com/media/d6ce9e8d1c702536d516627f5b735c54/href</a></iframe><p>這些東西轉過去也不是太複雜的，但是有一個地方影起了討論…</p><p>public static final Creator&lt;User&gt; CREATOR = new Creator&lt;User&gt;() { 這個東西該怎麼轉呢？</p><h3>來聊聊 Kotlin 中的靜態變數</h3><blockquote><em>Kotlin 是一個新設計的語言<br>是為了“實務”、為了解決目前語言可能遇到的問題所設計的。<br>有些 Kotlin 中缺少的功能、特別麻煩的設計，都可能是“故意的”</em></blockquote><p>靜態變數這件事情常常被拿出來討論，最為人詬病的地方是你沒辦法測試、沒辦法知道誰去修改他。</p><p>例如，你有一個全域的靜態變數存取著某一個狀態。你在你的 class 內要使用，這個變數卻有可能被其他 class 修改，讓你的 code 不符合預期。</p><p>另外，測試時想要 mock 一個靜態的成員也是很麻煩的，這也增加了使用靜態成員對整體開發上的影響。</p><p>所以，Kotlin 中沒有靜態的概念，甚至沒有 static 關鍵字</p><h4>沒有靜態？那常數怎麼辦？</h4><p>不過靜態成員也有靜態成員的用處，有些東西真的需要放在靜態裡。</p><p>像是常數、像是工廠方法（factory method)。有時（雖然有點爭議）Singleton 模式也需要靜態觀念。</p><p>那怎麼辦呢？在 Kotlin 中怎麼實現？</p><h4>首先，你知道 class 外也可以寫東西嗎？</h4><p>Kotlin 中有一個寫法是 <strong>top level property/function</strong> ，也就是在.kt 檔中、class 之外，你可以寫變數、函數。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b1ee3cbdba3c22519e93089cb530096/href">https://medium.com/media/3b1ee3cbdba3c22519e93089cb530096/href</a></iframe><p>什麼！！？ 原來這些東西可以寫在 class 之外？</p><p>對的，但是其實也沒什麼大不了。因為 Kotlin 最終還是 run 在 JVM 上，</p><p>這些 top level 的成員將會放在一個以檔名為命名的 class 中，以 <strong>靜態</strong> 成員的身份存在</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae60af94218e089d2674dd88123724ea/href">https://medium.com/media/ae60af94218e089d2674dd88123724ea/href</a></iframe><p>恭喜，我們學會第一個在 Kotlin 中增加靜態成員的方法了！ 而且大多時候，都可以用這個方法解決。</p><p>不過這個方法有些缺點。首先在 java 端使用時，class 都需要加 Kt 實在有夠煩的。</p><p>加在 Kotlin 有提供給我們一拖拉庫的 interop annotation 讓我們配合 java</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/08d51d6fd59f303e4fa8641f9cec2277/href">https://medium.com/media/08d51d6fd59f303e4fa8641f9cec2277/href</a></iframe><p>加上了 @file:JvmName(&quot;TopLevelUtils&quot;) 後，java 端的使用就會變成我們比較熟悉的</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/13c80b17923fb6a17b2f191cf423031f/href">https://medium.com/media/13c80b17923fb6a17b2f191cf423031f/href</a></iframe><h4>咦不對啊？Kotlin 中寫靜態不是要用 companion object 嗎？</h4><p>Top level 的成員很好用，而且直接對應了 java 的 static。看起來是很好的解決辦法，但他仍有一些缺點。</p><p>最大的缺點是它並不包含在 class 內，所以無法使用 class 的 private 成員。</p><p>這樣的話，若想要使用 factory method 這種建置實體的方法的話，就可能會需要對 private 的成員有存取權</p><p>這時候就到了教科書上會寫的 companion object 了</p><h4>Kotlin object 有三種</h4><blockquote><em>object 這個關鍵字，在我一開始學習 Kotlin 的時候是個一直搞混的概念。<br>因為它在很多看似不相同的情境下都可以用，而且在 java 又沒有完全對應的使用方法存在。</em></blockquote><p>Kotlin 中有三個地方都可以使用 object 關鍵字：</p><ul><li>class 宣告</li><li>companion object</li><li>變數宣告</li></ul><p>儘管看起來他的用法很雜，但當你在 Kotlin 中使用 object，都只代表一件事：<strong>你寫了一個 class, 並且立即產生一個實體。</strong></p><h4>Singleton 變簡單</h4><p>Singleton 模式在很多時候還是很好用的，在 Kotlin 中，你再也不用手動把 constructor 鎖成 private, 也不用手動建立 getInstance method, 不用手動處理靜態 instance 的生成與取用。</p><p>如果希望在 Kotlin 中產生一個 Singleton, 只要在宣告 class 時使用 object 關鍵字就好了。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/836d4a8ffe3a2c2e64015975f4183ba2/href">https://medium.com/media/836d4a8ffe3a2c2e64015975f4183ba2/href</a></iframe><p>就如同上面說的，object 關鍵字產生了這個 class 也同時產生了一個、唯一一個實體。因為object不能包含 constructor, 所以也不能被其他人建立（new)。</p><p>在 java 端，使用 object 時會發現一個小問題。你必須得先指向那個自動產出來的實體，才能取用內容（別忘了，Kotlin 沒有原生的 static)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a4e0801c4597ac3318edc163d01d4437/href">https://medium.com/media/a4e0801c4597ac3318edc163d01d4437/href</a></iframe><p>這時候可以使用剛剛說明的 interop annotation, 標記這個 method 是靜態的</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d49356d2eafc0f896f1020727a5bb99f/href">https://medium.com/media/d49356d2eafc0f896f1020727a5bb99f/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af15d8adf2dc1dd5816dea035f1af8ef/href">https://medium.com/media/af15d8adf2dc1dd5816dea035f1af8ef/href</a></iframe><p>這樣在使用上會變很方便，但是INSTANCE 還是會存在喔～ 這樣並不會省下記憶體空間</p><h4>Companion Object （伴生物件）</h4><p>有時候，我們不希望我們的 class 是一個 Singleton, 但是我們又需要有一個靜態區塊，這時我們就需要 companion object</p><p>companion object 和一般的 object 不同，他是隸屬於某個 class 底下的單一實體。在 Kotlin 中，基本上取代了 java class 對應的 static 區塊</p><p>所以，如果你需要一個常數，可以這樣做:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a190225f3945682e0dea28b31d20702/href">https://medium.com/media/0a190225f3945682e0dea28b31d20702/href</a></iframe><p>const 關鍵字代表著常數，基本上取代了 public static final 的 java 寫法。可惜，const 只能使用在基本型態上。所以當你需要有一個靜態的物件，或是一個靜態的 function，則可以：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/20ea36d27cc5d0e6689ba960f576921f/href">https://medium.com/media/20ea36d27cc5d0e6689ba960f576921f/href</a></iframe><p>如同上面說到 object 一樣，沒有 const 加持的 staticFunc 和 STATIC_OBJ 在 java 端取用時，都需要先指到 companion object 之後才能使用</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dfc30946cfd73639de25f2f1cda1e4bc/href">https://medium.com/media/dfc30946cfd73639de25f2f1cda1e4bc/href</a></iframe><p>也如同上面所說，這個問題可以由 interop annotaion 解決。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/53c5318bcd99d4b6d190dff22e81af02/href">https://medium.com/media/53c5318bcd99d4b6d190dff22e81af02/href</a></iframe><p>不過這樣的寫法，並不會讓 companion object 實體消失喔，只是把 staticFunc, 和 STATIC_OBJ 開放出來讓 java 端的人可以直接使用而已。</p><p><a href="https://gist.github.com/TomazWang/8c7b2a1b741183173b4898c49baa7d78">https://gist.github.com/TomazWang/8c7b2a1b741183173b4898c49baa7d78</a></p><h4>最後，object 也可以用來宣告變數</h4><p>這個做法，通常是拿來取代 java 的匿名物件。還記得 object 的定義是你產了一個 class，並同時產生他的實體。這也是匿名 class 在做的事情。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/020407d4b7f5f241428a72c535583d1d/href">https://medium.com/media/020407d4b7f5f241428a72c535583d1d/href</a></iframe><h3>OK, 回到 Parcelable 那一題</h3><p>講了那麼多，一開始的 CREATOR 應該怎麼放呢？</p><p>依照剛剛的邏輯：</p><ol><li>因為他是靜態，而且他又不是基本型態，所以他應該放在 companion object 中。</li><li>因為他又需要馬上實作 Creator&lt;User&gt;，所以要使用 object 變數宣告</li><li>我們希望 Java 端的人可以更方便用到這個 CREATOR, 所以加 @JavaFeild</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0342b183684320eea6e4d29a93330aed/href">https://medium.com/media/0342b183684320eea6e4d29a93330aed/href</a></iframe><p>太好了！問題解決了….. 嗎？</p><p>如果這個 class 沒有別的 static 成員需求，那讓 companion object 存在是多餘的（因為外部只會使用到 CREATOR) 何不讓 companion object 就是 CREATOR呢？</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/adf3c049d6504577a795df3731fd651f/href">https://medium.com/media/adf3c049d6504577a795df3731fd651f/href</a></iframe><h3>tl;dr 長話短說</h3><blockquote><em>文章有點落落長＋流水帳，所以整理一下重點</em></blockquote><ul><li>Kotlin 沒有 static, 而是透過各種不同方法實現相同概念</li><li>需要 static 時，首選 top level 定義</li><li>可以加上 @file:JvmName() 讓 java 端更好用</li><li>如果不適合使用 top level, 使用 companion object + const</li><li>如果不適合使用 const, 使用 val + @JvmField</li><li>@JvmField, @JvmStatic 不會讓 object 所產生的實體消失。（不過如果都沒有使用，這個部分會被 proguard 之類的工具移除掉，所以也不用太擔心效能）</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=407139a17e5b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android#io18筆記 — Android App Bundle 與 Dynamic Delivery]]></title>
            <link>https://medium.com/@tomazwang/android-io18%E7%AD%86%E8%A8%98-android-app-bundle-%E8%88%87-dynamic-delivery-1a062576ed06?source=rss-be1015e7ea6a------2</link>
            <guid isPermaLink="false">https://medium.com/p/1a062576ed06</guid>
            <category><![CDATA[dynamic-delivery]]></category>
            <category><![CDATA[google-io-2017]]></category>
            <category><![CDATA[android-bundle]]></category>
            <category><![CDATA[io18]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Tomaz Wang]]></dc:creator>
            <pubDate>Wed, 16 May 2018 10:05:00 GMT</pubDate>
            <atom:updated>2018-05-16T10:05:14.511Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><a href="https://medium.com/@tomazwang/android-in-io18-android-app-bundle-and-dynamic-delivery-13cae07070f2">English Version</a></blockquote><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F0raqVydJmNE%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D0raqVydJmNE&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F0raqVydJmNE%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/75e93e3c8aa810bb4139e74b6d1d5025/href">https://medium.com/media/75e93e3c8aa810bb4139e74b6d1d5025/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FbViNOUeFuiQ%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DbViNOUeFuiQ&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FbViNOUeFuiQ%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/77eb5da64acab56252b4c4da81e3e03f/href">https://medium.com/media/77eb5da64acab56252b4c4da81e3e03f/href</a></iframe><p>The docs:</p><ul><li><a href="https://developer.android.com/platform/technology/app-bundle/">Android App Bundle</a></li></ul><h3>總覽</h3><p>Android App Bundle 是今年 Google 今年在 Google IO 上推出的新格式，與 apk 非常相同，它包含所有 app 的程式、資源。不同的是，使用者不能直接將 App Bundle 檔安裝至自己的裝置。App bundle 可以被分解成多個小區塊，Google Play Store 會將這些區塊重新組合為 apk 檔並提供使用者下載。</p><p>為什麼要這樣做呢？首先，這樣開發者將不需要為了不同版本、不同語系的裝置管理多個 apk 檔，未來只需要一個 app bundle 檔，Google Play 將會自動在使用者下載 app 時，依照使用者的裝置與需求，挑選出最適合的組合給使用者。</p><p>例如：假設我的 app 目前支援繁體中文、簡體中文與英文，包裝成 app bundle 上送給 Google Play 時，Google Play 將會把這個 app bundle 拆解成 4 個 apk 檔。一個主要功能 apk (Base Apk)，與三個各自包含一種語言的組態 apk （Configuration Apk)。</p><p>當一個使用者，使用設定為英語語系的裝置下載這個 app 時，Google Play 將會判斷這個使用者的語系，將 Base Apk 與英語的 Configuration Apk 組合，產出 apk 檔給這位使用者。</p><h3>Dynamic Delivery 動態發佈</h3><p>上面說的這個流程，Google 稱其為 Dynamic Delivery。透過 Dynamic Delivery Google Play 可以更精準地替裝置準備專屬的 apk ，只包含他會使用到的資源。同時，Dynamic Delivery 也讓 app 有模組化的能力。開發者可以將某個獨立的功能與主要功能分開，透過 Dynamic Delivery 的幫助，讓使用者只有在使用到該功能時再做下載。</p><p>不過，因為 apk 檔是由 Google Play 產生，apk 的簽名將必須在 Google Play 上處理，因此，如果要使用 Dynamic Delivery 這個功能，必須也同時使用 signing by Google Play。</p><h3>建置 App Bundle</h3><p>要建置 App Bundle 只需要透過 Android Studio 3.2+ Build 選單中 Build Bundles / APKs 的功能，就樣 Apk 檔ㄧ樣的流程。當然，你也看以透過 gradle 建置。</p><pre><em>// build apk</em><br>gradlew assembleFlavorType</pre><pre><em>// build app bundle</em><br>gradlew bundleFlavorType</pre><p>就這樣！建置 App Bundle 不需要修改任何程式與配置。只是建出來的東西不同而已。</p><h3>我的想法</h3><p>App Bindle 鼓勵我們透過新的方式建置與部屬我們的 app。我最期待的是 Dynamic Feature：透過 App Bundle 的能力，將功能區分開，等到使用者需要下載時才下載，這樣的功能。不過，App Bundle 也有一些缺點，使用者安裝使用的 apk 檔與我們測試時的 apk 檔不同，這點令開發者緊張。另外使用 App Bundle 代表簽名流程必須得交給 Google Play，且動態下載的功能也必須透過 Google Play 才能測試。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1a062576ed06" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android in #io18 — Android App Bundle and Dynamic Delivery: The Overview]]></title>
            <link>https://medium.com/@tomazwang/android-in-io18-android-app-bundle-and-dynamic-delivery-13cae07070f2?source=rss-be1015e7ea6a------2</link>
            <guid isPermaLink="false">https://medium.com/p/13cae07070f2</guid>
            <category><![CDATA[io18]]></category>
            <category><![CDATA[google-io-2018]]></category>
            <category><![CDATA[app-bundle]]></category>
            <category><![CDATA[dynamic-delivery]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Tomaz Wang]]></dc:creator>
            <pubDate>Wed, 16 May 2018 07:32:30 GMT</pubDate>
            <atom:updated>2018-05-16T09:54:34.012Z</atom:updated>
            <content:encoded><![CDATA[<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F0raqVydJmNE%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D0raqVydJmNE&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F0raqVydJmNE%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/75e93e3c8aa810bb4139e74b6d1d5025/href">https://medium.com/media/75e93e3c8aa810bb4139e74b6d1d5025/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FbViNOUeFuiQ%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DbViNOUeFuiQ&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FbViNOUeFuiQ%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/77eb5da64acab56252b4c4da81e3e03f/href">https://medium.com/media/77eb5da64acab56252b4c4da81e3e03f/href</a></iframe><h3>The docs:</h3><ul><li><a href="https://developer.android.com/platform/technology/app-bundle/">Android App Bundle</a></li></ul><h3>Overview</h3><p>Android App Bundle is a new format that contains all the resources and codes of an app, just like an apk file, but not for users to install on their devices. An app bundle can be separated into small chunks by Google Play and re-construct it whenever a user downloads the app.</p><p>Developers will no longer need to manage multiple apks for different devices and different versions. With app bundle, Google Play will pick whatever small pieces that the user’s device need automatically.</p><p>For example, let’s say my app supports traditional Chinese, simplified Chinese, and English. The app bundle will be separated into 4 parts: one base apk and three configuration apks that each contains one language.</p><p>When a user downloads my app with a device in English, Google Play will recognize that and combines the base apk and the configuration apk with English language resources, create an apk for the user.</p><h3>Dynamic Delivery</h3><p>This workflow is called Dynamic Delivery. With Dynamic Delivery, Google Play will take the app bundle and generate an apk specific to the device with only the resources needed by that devices. It also create the abilities to have modular features. Developers can separates a feature from the base apk to another module, and deploy it only when the user uses this feature.</p><p>However, because the apk is built by Google Play, the signing process has to be moved to Google Play. Therefore, if you’re trying to use Dynamic Delivery, you need to use app signing by Google Play.</p><h3>Building the App Bundle</h3><p>To build an app bundle, you can find the Build Bundles / APKs option in the Build menu.(Only works on Androis Studio 3.2+). You can also build app bundles by gradle task.</p><pre><em>// build apk</em><br>gradlew assembleFlavorType</pre><pre><em>// build app bundle</em><br>gradlew bundleFlavorType</pre><p>That’s it! It’s all you need to change from apk to app bundle.</p><h3>Final Thought</h3><p>The App Bundle brings us a new way to construct our app. The main thing I am looking forward to is Dynamic Feature, a way to leverage app bundle that splits features into different feature apks and install them on demand. There&#39;s also some pullbacks thought, such as the apk file installing in customers&#39; device will not be the same as the file your using for debugging. The &quot;on demand&quot; part of this workflow cannot be tested until the bundle is uploaded to Google Play.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=13cae07070f2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android in #io18 — Modern Android development (現代 Android 開發方法)]]></title>
            <link>https://medium.com/@tomazwang/android-in-io18-modern-android-development-f7d6dbce4a32?source=rss-be1015e7ea6a------2</link>
            <guid isPermaLink="false">https://medium.com/p/f7d6dbce4a32</guid>
            <category><![CDATA[google-io-2018]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Tomaz Wang]]></dc:creator>
            <pubDate>Thu, 10 May 2018 09:44:29 GMT</pubDate>
            <atom:updated>2018-05-11T10:24:29.809Z</atom:updated>
            <content:encoded><![CDATA[<p>Android 今年 10 歲了，開發社群在這 10 年來也發展出了各種寫法 就連 Google 自己也很常對同一個議題提出了很多次不同的作法</p><p>今天 Google 的人站出來，一次解決所有問題 一些常見、有分歧的 Android 開發問題，Google 統一給予他們認為最 “現代” 的做法</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FIrMw7MEgADk%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DIrMw7MEgADk&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FIrMw7MEgADk%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/d1d3465d09ac93386bd823e271128a59/href">https://medium.com/media/d1d3465d09ac93386bd823e271128a59/href</a></iframe><h3>簡單統整</h3><h3>Tools</h3><ul><li>用 Android Studio (廢話)</li><li>用 Profiler 觀察 CPU, memory, network</li></ul><h3>ART （Android Runtime)</h3><ul><li>可以，而且歡迎使用 enum （以前都說要用 @IntDef)</li><li>歡迎使用 Integer, Boolean 這些基本型態的物件型態</li><li>但是還是要注意寫法，雖然 runtime 進步了，仍然要注意記憶體和 CPU 使用</li></ul><h3>Kotlin</h3><ul><li>因為可以互通，寫 Java 的時候可以考量到 Kotlin 方呼叫時的方便性，反之亦然</li><li>別忘了看 Kotlin style guild.</li></ul><h3>Layout</h3><ul><li>AbsoluteLayout …. 別用了！！</li><li>LinearLayout ….. 可以用，但是簡單的狀況，不要有巢狀結構</li><li>FrameLayout …. 簡單情況可以用</li><li>GridLayout …. 除非你有 GUI 工具，不然不要用（可是Google 忘記開發那個工具了）</li><li>RelativeLayout …. 用 ConstraintLayout 取代</li><li>ConstraintLayout …. 就是你了！</li></ul><h3>Adaptive View (列表、表格、畫面會重複的)</h3><ul><li>ListView, GridView, Gallery … 都別用了</li><li>改用 RecyclerView …. 要直、要橫、要格子狀都能做到</li></ul><h3>Fragment / Activity</h3><ul><li>統一都用 support.v4 的 Fragment</li><li>未來會有更好的導覽工具(Navigation, 隸屬 Android JetPack)，會密切與 Fragment 互動</li><li>盡量使用 single Activity 的設計。</li></ul><h3>Architecture</h3><ul><li>歡迎參考 Android Architecture Components</li><li>歡迎使用 Repository 模式</li></ul><h3>圖像處理</h3><ul><li>別用 9-patch 了，改用 VectorDrawable</li><li>歡迎使用 Glide, Picasso, Lottie 來處理圖片</li></ul><h3>其他</h3><blockquote><em>因為這幾項都是要配合 Android Architecture Components 才能達成，所以列在其他</em></blockquote><ul><li>Activity, Fragment 的生命週期，改用 LifecycleOwner 處理、觀察</li><li>使用 support.v4.Fragment / AppCompactActivty</li><li>Sqlite 歡迎改用 Room</li><li>View 獨立於流程，透過 LiveData 通知資料</li><li>Paging, 可以取代 CursorLoader</li></ul><h3>Final Thought</h3><p>大部分的技術都持續有在關注，尤其是 Android Architecture Components。Android Arch 除了奠定了官方期望的結構外，同時也提供了很多工具，讓資料處理、流程處理脫離 View。以往 Android 的 Unit Test 往往會受限 Android framework. 透過這些工具的協助，可以將元件拆散，更方便做測試。</p><p>在畫面上，以前每個畫面可能都需要有不同的作法，如今幾乎全部都能靠 ConstraintLayout + RecyclerView 完成。</p><p>一直被形容為 Android framework 中的異型的 Fragment 又再度被拉回來。這也呼應了比較新式的 App 設計，幾個大牌的 App 都開始使用單頁設計，在切換功能時只切換部分畫面元件，並且有較平順的畫面切換。這些都是 Fragment 最能發揮功能的地方。</p><p>但是 Fragment 複雜的生命週期設計與 corea API 與 support library 重複的 Fragment 老是讓開發者搞混，認為 Fragment 是複雜的東西。生命週期的部分 Google 打算用 LifecycleOwner 解決，而 core API 中的 Fragment 也算是正式被 deprecate 了，未來一切以 support lib 中的 Fragment 為主。</p><p>流程處理上，雖然有 Android Runtime 的協助，但手機的記憶體資源、CPU 資源以及電池都是開發者應該負責好好管理的。思考如何更好的管理記憶體、運算時間，這是以前到現在，唯一一個不變的準則。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f7d6dbce4a32" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SOLID Principle in Android]]></title>
            <link>https://medium.com/@tomazwang/solid-principle-in-android-dab56363ae8?source=rss-be1015e7ea6a------2</link>
            <guid isPermaLink="false">https://medium.com/p/dab56363ae8</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[android-development]]></category>
            <dc:creator><![CDATA[Tomaz Wang]]></dc:creator>
            <pubDate>Sun, 01 Oct 2017 07:24:23 GMT</pubDate>
            <atom:updated>2017-10-01T07:24:23.207Z</atom:updated>
            <content:encoded><![CDATA[<h4><em>References:</em></h4><ul><li><a href="https://android.jlelse.eu/android-development-the-solid-principles-3b5779b105d2">Android Development: the SOLID Principles</a></li><li><a href="https://academy.realm.io/posts/learning-path-solid-principles-for-android/">Learning Path: SOLID Principles for Android</a></li><li><a href="https://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29">SOLID (object-oriented design) wiki page</a></li><li><a href="https://skyyen999.gitbooks.io/-study-design-pattern-in-java/content/oodPrinciple.html">物件導向程式設計基本原則 — SOLID</a></li></ul><p>SOLID 是五個物件導向的五個大原則，可以協助設計師建立好維護、易擴充的程式</p><ul><li><strong><em>S</em></strong>ingle Responsibility Principle (SRP) 單一職責</li><li><strong><em>O</em></strong>pen-Closed Principle (OCP) 開放/封閉原則</li><li><strong><em>L</em></strong>iskov Substitution Principle (LSP) Liskov替換</li><li><strong><em>I</em></strong>nterface Segregation Principle (ISP) 介面隔離</li><li><strong><em>D</em></strong>ependency Inversion Principle (DIP) 依賴反轉</li></ul><h3>Single Responsibility Principle (SRP) 單一職責</h3><pre><em>A class should have one, and only one, reason to change.</em></pre><pre><em>一個 Class 只做一件事。每個 Class 都該有一個（也只該有一個）理由修改</em></pre><p>以 RecyclerView 的 Adapter 為例，下面是一個普通的 Adpater 範例</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e01bf4c3f42691a9f46c5d7054984822/href">https://medium.com/media/e01bf4c3f42691a9f46c5d7054984822/href</a></iframe><p>要理解為什麼這個 Adapter 違反了 SRP，要先理解一個 Adapter 應該做什麼？</p><p>一個 RecyclerView 的 Adapter 應該負責“將資料物件，以清單的方式擺在畫面上”</p><p>而這支 Adpater，注意 onBindView 的實作，同時“將資料擺在畫面上”，又做了“組合要顯示的字串”的動作</p><p>這樣這支 Adapter 就同時擔負了“處理資料”以及“組合畫面”兩個責任，這是 SRP 所不樂見的</p><blockquote>在Adapter 中組合資料，那又怎樣？</blockquote><p>這個資料字串的組合將會與這個Adpater 綁再一起</p><p>未來要在別的頁面（例如電影的資訊內頁）顯示電影類型，又得在組一次</p><p>未來如果組合的方式需要修改，就必須得修改多個地方，少改一個就會有不一致的狀況</p><p>如此 onBindViewHolder 做的不只是把資料和 ViewHolder binding ，而多做了其他的邏輯</p><blockquote>那該怎麼修改呢？</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1e72666908eb113fda4bd98705172478/href">https://medium.com/media/1e72666908eb113fda4bd98705172478/href</a></iframe><p>將“組合資料”這個動作轉交給其他人，例如一個 Util&lt;這樣這段組合的 Code 將可以在其他地方重複利用</p><p>同樣的，也可以進一步將 onBindViewHolder 中的 code 寫進 ViewHolder 本身</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a638643ec09e7198d298923f1bcb447/href">https://medium.com/media/7a638643ec09e7198d298923f1bcb447/href</a></iframe><p>如此，Adapter Class 的責任就只剩“安排資料在畫面上的順序“</p><p>將”每個資料在畫面上的顯示“ 這個責任交給 ViewHolder 進行</p><h3>Open-Closed Principle (OCP) 開放/封閉原則</h3><pre><em>Software entities (classes, modules, functions, etc…) should be open for extension, but closed for modification.</em></pre><pre><em>軟體應該對擴充開放、對修改封閉</em></pre><p>講白話一點，OCP 原則是在有新的需求時，以不更動原本程式的前提下，進行擴充。來看看以下範例：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/addb27178fa3ec4b25706ba1ba31aac2/href">https://medium.com/media/addb27178fa3ec4b25706ba1ba31aac2/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae9831e328cac8cb3cd1a15a6fb21911/href">https://medium.com/media/ae9831e328cac8cb3cd1a15a6fb21911/href</a></iframe><p>AreaManager 可以順利的計算 Rectangle 的面積，直到有一個新的圖形出現…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41e9def57c580f49dcf156cd9ff7a9cf/href">https://medium.com/media/41e9def57c580f49dcf156cd9ff7a9cf/href</a></iframe><p>為了計算 Circle 的面積，我們必須把 AreaManager 改成這樣</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99ea34a589c41bab96b0801a3f6a6d89/href">https://medium.com/media/99ea34a589c41bab96b0801a3f6a6d89/href</a></iframe><p>很明顯這就不是一個長久的作法，未來若有更多的形狀加入，AreaManager 就必須得一直做更新，增加原先使用了calculateArea() 這個 method 的其他元件產生錯誤的風險</p><p>比較適合的作法是使用抽象層，透過繼承關係來整合 Rectangle , Circle 這些類似的 Class。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc3587ed3623b8e7daf502b86be1dc5c/href">https://medium.com/media/cc3587ed3623b8e7daf502b86be1dc5c/href</a></iframe><p>如此，若有需要新增形狀，只需要繼承 Shape，不需要去修改既有的 AreaManager</p><h3>Liskov Substitution Principle (LSP) Liskov替換</h3><pre><em>Child classes should never break the parent class’ type definitions.</em></pre><pre><em>子類別應該能替換掉父類別，卻不影響程式運行</em></pre><p>Liskov替換原則非常直覺，就是子類別在複寫任何元件時，對外（Class 的使用者）應該要能達到預期效果</p><p>依照慣例，先來看個錯誤示範：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a05eb9d24f94ecf01dac7f8e6f32deb/href">https://medium.com/media/3a05eb9d24f94ecf01dac7f8e6f32deb/href</a></iframe><p>letStartEngine 在使用 Car 這個 interface 的時候，預期 startEngine 這個 method 可以發動引擎 <br>殊不知特斯拉就是比較潮，在電池沒啟動的情況不能發動引擎 <br>如此，letStartEngine 就不能每次都成功發動車子的引擎。</p><p>這樣的設計就像如果有一個 TextView 的子類別，在 setText() 的時候字沒有顯示出來。 <br>爬了文件才發現他需要 “setText()” 然後再 “display()” <br>遇到這種元件，一定會 debug 到火大。因為他不符合父類別給我們的預期。</p><p>回到車子，其實解決方法很簡單，就是讓 startEngine() 照預期運作</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/06c74a9862fc66a2ea139f139820bd2a/href">https://medium.com/media/06c74a9862fc66a2ea139f139820bd2a/href</a></iframe><p>這樣 letStartEngine 在呼叫 car.startEngine() 時才能得到預期的效果</p><h3>Interface Segregation Principle (ISP) 介面隔離</h3><pre><em>No client should be forced to depend on methods it does not use.</em></pre><pre><em>使用介面的元件，不應該被迫依賴他不需要的 method</em></pre><p>用 Android 來說明 ISP 最好的例子就是 OnClickListener</p><p>點選畫面元件時，需要建立一個OnClickListener的實作、長按時又要實作一個 OnLongClickListener，若要處理按下、放開的動作，又要時做OnTouchListener。</p><p>明明都是類似的功能，為什麼不能寫成這樣呢？</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3bcbd57d85a0b547172148809d6bf7a/href">https://medium.com/media/f3bcbd57d85a0b547172148809d6bf7a/href</a></iframe><p>若寫成這樣，這其實會造成只需要onClick的元件必須得實作onLongClick和onTouch <br>這樣的空實作不僅是一種浪費，同時也增加了實作者未來需要更新的機率。</p><p>假設今天onTouch修改成會多傳入一個值，或是需要回傳值，所有實作OnClickListener的地方都被迫修改，即便他們只是空實作。</p><p>因此，Android 才有各種不同單一功能 的元件需要個別實作。</p><h3>Dependency Inversion Principle (DIP) 依賴反轉</h3><pre><em>1. High-level modules should not depend on low-level modules. Both should depend on abstractions.</em></pre><pre><em>2. Abstractions should not depend upon details. Details should depend upon abstractions.</em></pre><pre><em>1. 高階元件不應該依賴低階元件，應該藉由抽象層溝通<br>2. 抽象層不該依賴實作層，實作層“應該”依賴抽象層</em></pre><p>DIP 大概 SOLID 中最抽象的一個原則，但抽象也是他的重點。簡單來說就是要寫”抽象的程式” <br>什麼叫做抽象呢？</p><p>想像你要寫一個“拿杯子喝水的程式”，如果寫成 伸手拿杯子，靠近嘴巴，把水倒進嘴巴 <br>這樣就是話說得太死，不夠抽象的例子。如果未來要喝飲料呢？如果未來要拿瓶子呢？</p><p>想像現在程式改成 伸手拿那個容器，靠近嘴巴，把那種液體倒進嘴巴 <br>這樣未來要拿什麼裝，要喝什麼都可以。因為現在這行程式依賴的是抽象層，而不是實作層</p><p>OK 來看程式範例：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c7d8a3855f37772f3a5e4ac1e8791a9/href">https://medium.com/media/4c7d8a3855f37772f3a5e4ac1e8791a9/href</a></iframe><p>假設現在Engineer這支 Class 非常的複雜，然後我們又必須得修改讓這個Engineer去執行另一種 SuperProgram，我們就必須得做這些事情：</p><ul><li>必須修改已經很複雜的 Engineer ，找到所有使用 Program 的地方替換掉</li><li>有可能會因此毀掉一些原先可以運作的 method</li><li>UnitTest 有可能直接不能使用，必須得重寫</li></ul><p>用剛剛喝水的例子來想，我們把程式改成這樣：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/24c07c086106a79b27a7eda6538e21ed/href">https://medium.com/media/24c07c086106a79b27a7eda6538e21ed/href</a></iframe><p>如此我們要把 Program 抽換成 SuperProgram時，只需要換掉傳入的資料，其他所有功能都能正常運行（如果你的SuperProgram 有符合 LSP 的話啦～)</p><h4>回到 Android</h4><p>在 Android 的世界，我們常為了顯示一個 List，去 db 中抓資料，將資料放進 Adapter 中，讓 Adapter 來處理顯示邏輯（參考 SRP ）</p><p>但這樣的設計，在沒有抽象層的輔助下，會失去抽換資料來源的能力</p><p>如果今天突然說，資料不抓 db 了改成抓 API，整組程式會需要重寫</p><p>但如果今天我們寫的資料來源是一個抽象層，我們只需要把取得資料的來源</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6901d8db2b92b28873aa788683a2fe19/href">https://medium.com/media/6901d8db2b92b28873aa788683a2fe19/href</a></iframe><p>如此，無論資料來源是 DB、API，或是 ContentProvider。其他顯示、處理資料的功能將都能正常運作。</p><h3>總結</h3><p>SOLID 其實都是一種以不變應萬變的模式</p><p>事先規劃好未來能擴充的能力，未來擴充、維護時將會更輕鬆。</p><p>程式上，每個元件功能也會更單純，更好理解。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dab56363ae8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[I Got My Google Associate Android Developer Certification]]></title>
            <link>https://medium.com/@tomazwang/i-got-my-google-associate-android-developer-certification-6c6ac4e7c95e?source=rss-be1015e7ea6a------2</link>
            <guid isPermaLink="false">https://medium.com/p/6c6ac4e7c95e</guid>
            <dc:creator><![CDATA[Tomaz Wang]]></dc:creator>
            <pubDate>Mon, 21 Aug 2017 06:22:45 GMT</pubDate>
            <atom:updated>2017-08-21T06:22:45.747Z</atom:updated>
            <content:encoded><![CDATA[<p>Google 在 2016 年推出了 <a href="https://developers.google.com/training/certification/associate-android-developer/">Associate Android Developer</a> 這個認證<br>我也一直很想來挑戰自己</p><p>終於，依循著今年的新年規劃，我終於準備好參加考試<br>( 對！我有依照新年新希望走了半年多，有點開心 😎)</p><h4>And I passed the exam!!</h4><p>So… what is Associate Android Developer Certification.</p><p>比較詳細的介紹上面的連結都有，我就分享一下我個人參加認證的經驗：</p><h3>The Exam</h3><p>考試總共分成兩個階段：</p><ul><li>專案階段 ( Building an App )</li><li>面試階段 ( exit interview )</li></ul><p>專案階段算是整個考試的重點。<br>報考當下會拿到一個（幾乎什麼都沒有做，錯誤還一堆的）半成品專案，以及一份完整且清楚的規格。受試者要在 48 小時內（報考就開始計時）將專案完成並交回</p><h4>The App</h4><p>我認為專案本身的難度滿剛好的符合 Associate Developer 這個階級：<br>過程中會用到大多數 Android 基本的元件、觀念<br>同時還會用到幾個比較高級的功能，以及一些需要細心處理的狀況<br>並且<strong>一定會</strong>需要找資料</p><p>從畫面佈局、資料處理到 Unit/UI Test 通通都需要經歷<br>就像真正做了一個專案一樣</p><p>這個階段我大概實際用了 25 個小時<br>雖然我認為可以再更快，不過反正在 deadline 前，東西已做好為主</p><h4>The Exit Interview</h4><p>專案被審核通過後（會由電腦、人工綜合審核），會收到一封信和你約時間<br>在約定時間使用 ZOOM 和一位 Google 的員工作線上面試</p><p>這個面試除了要再次審核你的身份外<br>也會多次的試探是否這個 App 是你本身寫的<br>同時會詢問許多關於 Android 的基礎觀念</p><p>規則上是說大概花 30 分鐘<br>但是我的經驗是實際面試時間只花大概 10分鐘</p><h3>The Result</h3><p>面試後隔兩天，我就收到了 Google 來信，通知我通過了 Associate Android Developer 的認證</p><p>認證本身沒有紙本的證明，只有一個<a href="http://bcert.me/sjirzuck">連結</a>和一個徽章的圖案給你放上 LinkedIn、Twitter 這些網路平台</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*sqX8khQWmKFnz6Ln.png" /></figure><p>不確定在台灣有多少公司了解這個認證的存在<br>但對我自己來說，<br>這個認證不只是平常那種選擇題猜一猜的考試<br>而是真正驗證是否有能力完成一個專案</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6c6ac4e7c95e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android Architecture Components — 初探]]></title>
            <link>https://medium.com/@tomazwang/android-architecture-components-%E5%88%9D%E6%8E%A2-cd236f1a1054?source=rss-be1015e7ea6a------2</link>
            <guid isPermaLink="false">https://medium.com/p/cd236f1a1054</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[google-io-2017]]></category>
            <category><![CDATA[androiddev]]></category>
            <dc:creator><![CDATA[Tomaz Wang]]></dc:creator>
            <pubDate>Thu, 25 May 2017 09:37:32 GMT</pubDate>
            <atom:updated>2017-05-25T09:37:32.788Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://android-developers.googleblog.com/2017/05/android-and-architecture.html">Android Architecture Components</a> 是一組 Google 推出的 Library,</p><p>主要目標在於解決 Android 開發中遇到的幾個問題：</p><ul><li>Activity, Fragment 的生命週期處理導致的 Memory Leak</li><li>使用 SQLite Database 時 SQLite Query 錯誤無法被發現問題</li></ul><p>同時也能將 Android App 的架構更貼近 MVVM。</p><h3>內容</h3><p>Android Architecture Components 主要包含四組 Library：</p><ul><li>Lifecycle：幫你處理 Lifecycle</li><li>ViewModel：綁定畫面與資料</li><li>LiveData：即時且自動更新取得的資料</li><li>Room：更乾淨的 SQLite database 使用方式</li></ul><h3>架構</h3><p>Android Architecture Components 主要將 Android App 分成四個層次</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BfY1xspYZAZYqW-FkrTAvA.png" /></figure><ul><li>View（UI Controller ）</li><li>ViewModel</li><li>Repository</li><li>Data Source</li></ul><h4>View（UI Controller ）</h4><p>View 為畫面上該顯示的東西，主要就是Activity、Fragment、List、Dialog</p><p>Android Architecture 為這層推出了 Lifecycle Components，解決了各個View 在不同的 Lifecycle 需要做不同的動作</p><h4>ViewModel</h4><p>ViewModel 為畫面上的資料，Android Architecture 這邊以 LiveData 、ViewModel 來配合</p><h4>Repository</h4><p>Repository 為分辨該與哪種資料來源溝通的一層結構，同時將來源資料處理為ViewModel 能處理的資料</p><h4>Data Source</h4><p>Data Source 指的是資料的來源，Android App 主要三種資料來源：SQLite database、API、Content Provider。在 SQLite database 這邊 Android Architecture Components 推出了 Room，提供了 annotation base 的資料庫處理</p><p>See Also:</p><p><a href="https://developer.android.com/topic/libraries/architecture/index.html">Android Architecture Components | Android Developers</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FFrteWKKVyzI&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DFrteWKKVyzI&amp;image=http%3A%2F%2Fi.ytimg.com%2Fvi%2FFrteWKKVyzI%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/c73d38907cce5f5032c4a325a59491a0/href">https://medium.com/media/c73d38907cce5f5032c4a325a59491a0/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FvOJCrbr144o%3Fstart%3D277%26feature%3Doembed%26start%3D277&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DvOJCrbr144o&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FvOJCrbr144o%2Fhqdefault.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/5aaa77b275cf1459a0b677bfaa899ace/href">https://medium.com/media/5aaa77b275cf1459a0b677bfaa899ace/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cd236f1a1054" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>