Slide 1

Slide 1 text

炎炎夏⽇日學 Android Johnny Sung Part3: Android app 實作

Slide 2

Slide 2 text

Mobile device developer Johnny Sung https://fb.com/j796160836 https://blog.jks.coffee/ https://www.slideshare.net/j796160836 https://github.com/j796160836

Slide 3

Slide 3 text

⼩小試⾝身⼿手

Slide 4

Slide 4 text

震動 。

Slide 5

Slide 5 text

震動 val vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator vibrator.vibrate(10) AndroidManifest.xml

Slide 6

Slide 6 text

震動 vibrator.vibrate(longArrayOf(80, 150, 80, 150, 80, 150), -1) vibrator.cancel()

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

休息⼀一下 ☕

Slide 9

Slide 9 text

軟體設計 • 企劃 • UI/UX 介⾯面設計(美術) • 程式 • 測試

Slide 10

Slide 10 text

所需材料 • 畫⾯面線框稿 (Wireframe) • API 接⼝口⽂文件 • 美術圖片

Slide 11

Slide 11 text

成功畫⾯面 載入中畫⾯面 錯誤畫⾯面

Slide 12

Slide 12 text

成功畫⾯面 載入中畫⾯面 錯誤畫⾯面 夕陽⼩小幫⼿手

Slide 13

Slide 13 text

成功畫⾯面 載入中畫⾯面 錯誤畫⾯面 夕陽⼩小幫⼿手

Slide 14

Slide 14 text

所需⼯工具 • Method Draw 線上編輯圖片⼯工具
 https://editor.method.ac/ • App Icon Generator 線上 App icon 產⽣生⼯工具
 https://appicon.co/ • JSON Editor Online 線上 JSON 編輯⼯工具
 https://jsoneditoronline.org/

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

https://sunrise-sunset.org/api

Slide 18

Slide 18 text

22.604098, 120.3001811 K Square 經度 (Latitude) 緯度 (Longitude) 地址:806⾼高雄市前鎮區復興四路路 20 號 座標

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

https://api.sunrise-sunset.org/json?lat=22.604098&lng=120.3001811&date=today&formatted=0 今⽇日 K Square 夕陽時間

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

夕陽⼩小幫⼿手 使⽤用套件 • Gson 解析 JSON 資料 • OkHttp 網路路函式庫 • jDeferred 架構函式庫

Slide 26

Slide 26 text

dependencies { // ... implementation 'org.immutables:gson:2.7.1' implementation 'com.squareup.okhttp3:okhttp:3.12.0' implementation "org.jdeferred.v2:jdeferred-core:2.0.0-beta1" implementation "org.jdeferred.v2:jdeferred-android:2.0.0-beta1" // ... } 加入套件參參考

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

class SunsetResponse { var results: SunsetResult? = null var status: String? = null } class SunsetResult { var sunset: String? = null }

Slide 32

Slide 32 text

單元測試原則 • Arrange – 準備,準備輸入資料與期待值 • Act – 執⾏行行,執⾏行行測試對象 • Assert – 驗證,驗證結果 3A 原則

Slide 33

Slide 33 text

class ExampleUnitTest { @Test fun addition_isCorrect() { // Arrange val expected = 4 // Act val actual = 2 + 2 // Assert assertEquals(expected, actual) } } Arrange 準備 Act 執⾏行行 Aessrt 驗證

Slide 34

Slide 34 text

object SunsetDateUtil { val dateFormater = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX", Locale.US) .apply { timeZone = TimeZone.getTimeZone("UTC") } }

Slide 35

Slide 35 text

Arrange 準備 Act 執⾏行行 Aessrt 驗證 @Test fun testJSON() { // Arrange val json = "{\"results\":{\"sunrise\":\"2019-06-28T21:17:03+00:00\",\"sunset\":\"2019-06-29T10:47:28+00:00\",\"solar_noon\": \"2019-06-29T04:02:16+00:00\",\"day_length\":48625,\"civil_twilight_begin\":\"2019-06-28T20:52:00+00:00\",\"civil_twilight_end\": \"2019-06-29T11:12:31+00:00\",\"nautical_twilight_begin\":\"2019-06-28T20:22:00+00:00\",\"nautical_twilight_end\": \"2019-06-29T11:42:31+00:00\",\"astronomical_twilight_begin\":\"2019-06-28T19:50:46+00:00\",\"astronomical_twilight_end\": \"2019-06-29T12:13:46+00:00\"},\"status\":\"OK\"}" val excepted = "2019-06-29T10:47:28+00:00" // Act val gson = Gson() val obj = gson.fromJson(json, SunsetResponse::class.java) // Assert Assert.assertEquals(excepted, obj.results?.sunset) } }

Slide 36

Slide 36 text

Arrange 準備 Act 執⾏行行 Aessrt 驗證 輸入的資料 期待成功的資料 class SunsetUnitTest { @Test fun testFormatter() { val sunsetTime = "2019-06-24T10:46:49+00:00" val expected = Date(1561373209000) val result = SunsetDateUtil.dateFormater.parse(sunsetTime) assertEquals(expected.time, result.time) } }

Slide 37

Slide 37 text

class NetworkManager { companion object { val instance: NetworkManager by lazy { NetworkManager() } } val mainHandler = Handler(Looper.getMainLooper()) inline fun requestAsync(request: Request, typeToken: Type): Promise { val okHttpClient = OkHttpClient() val gson = GsonBuilder().create() val deferred = DeferredObject() okHttpClient.newCall(request).promise().then { okhttpResponse -> val strResult = okhttpResponse.body()?.string() try { val result = gson.fromJson(strResult, typeToken) if (okhttpResponse.isSuccessful) { mainHandler.post { deferred.resolve(result) } } else { mainHandler.post { deferred.reject(Exception(strResult)) } } } catch (e: Exception) { mainHandler.post { deferred.reject(e) } } }.fail { deferred.reject(it) } return deferred.promise() } } fun Call.promise(): Promise { val deferred = DeferredObject() this.enqueue(object : Callback { override fun onResponse(call: Call, response: Response) { deferred.resolve(response) } override fun onFailure(call: Call, e: IOException) { deferred.reject(e) } }) return deferred.promise() } 補檔案 NetworkManager.kt

Slide 38

Slide 38 text

https://github.com/jdeferred/jdeferred

Slide 39

Slide 39 text

https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Slide 40

Slide 40 text

object NetworkAPI { fun getSunsetDataAsync(): Promise { val okhttpRequest = Request.Builder() .url("https://api.sunrise-sunset.org/json?lat=22.604098&lng=120.3001811&date=today&formatted=0") .method("GET", null) .build() val type = object : TypeToken() {}.type return NetworkManager.instance.requestAsync(okhttpRequest, type) } }

Slide 41

Slide 41 text

https://square.github.io/okhttp/

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

完成了了!

Slide 44

Slide 44 text

Q & A

Slide 45

Slide 45 text

趕快去追夕陽吧

Slide 46

Slide 46 text

No content