Play 스토어 심사에 낸 1(0.1) 빌드와 같은 코드 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
46 lines
2.3 KiB
Kotlin
46 lines
2.3 KiB
Kotlin
package kr.o2o.nalssigak
|
|
|
|
import android.app.PendingIntent
|
|
import android.content.Intent
|
|
import androidx.wear.watchface.complications.data.ComplicationData
|
|
import androidx.wear.watchface.complications.data.ComplicationType
|
|
import androidx.wear.watchface.complications.data.PlainComplicationText
|
|
import androidx.wear.watchface.complications.data.RangedValueComplicationData
|
|
import androidx.wear.watchface.complications.datasource.ComplicationRequest
|
|
import androidx.wear.watchface.complications.datasource.SuspendingComplicationDataSourceService
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.withContext
|
|
import kotlin.math.roundToInt
|
|
|
|
/**
|
|
* 워치 스킨 날씨 칸 + 가운데 1위. 스킨 칸이 8개까지라 한 칸에 몰아 준다.
|
|
* 값 = 날씨 코드(0 맑음·1 비·2 천둥·3 눈) × 1000 + 1위 점수, 글자 = 지금 기온, 제목 = 1위 이름(없으면 빈 글자).
|
|
*/
|
|
class RainComplicationService : SuspendingComplicationDataSourceService() {
|
|
|
|
override suspend fun onComplicationRequest(request: ComplicationRequest): ComplicationData? {
|
|
val s = withContext(Dispatchers.IO) { SummaryRepo.load(this@RainComplicationService) } ?: return null
|
|
val code = when (s.rainLabel) {
|
|
"비 와요", "곧 비" -> 1
|
|
"천둥 쳐요" -> 2
|
|
"눈 와요" -> 3
|
|
else -> 0
|
|
}
|
|
val top = s.top.firstOrNull()
|
|
return data(code, top?.score ?: 0, s.tempC?.let { "${it.roundToInt()}°" } ?: "-°", top?.name ?: "")
|
|
}
|
|
|
|
override fun getPreviewData(type: ComplicationType): ComplicationData = data(0, 88, "20°", "산책각")
|
|
|
|
private fun data(code: Int, score: Int, temp: String, name: String): ComplicationData {
|
|
// 가운데 구슬을 누르면 앱 첫 장
|
|
val open = PendingIntent.getActivity(this, 0, Intent(this, MainActivity::class.java)
|
|
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK), PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
|
|
return RangedValueComplicationData.Builder(code * 1000f + score, 0f, 3100f, PlainComplicationText.Builder("$name $score, $temp").build())
|
|
.setText(PlainComplicationText.Builder(temp).build())
|
|
.setTitle(PlainComplicationText.Builder(name).build())
|
|
.setTapAction(open)
|
|
.build()
|
|
}
|
|
}
|