95 lines
3.8 KiB
Kotlin
95 lines
3.8 KiB
Kotlin
package kr.o2o.nalssigak
|
|
|
|
import android.Manifest
|
|
import android.content.pm.PackageManager
|
|
import android.os.Bundle
|
|
import androidx.activity.ComponentActivity
|
|
import androidx.activity.compose.setContent
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
|
import androidx.compose.foundation.layout.Box
|
|
import androidx.compose.foundation.layout.fillMaxSize
|
|
import androidx.compose.runtime.Composable
|
|
import androidx.compose.runtime.LaunchedEffect
|
|
import androidx.compose.runtime.getValue
|
|
import androidx.compose.runtime.mutableStateOf
|
|
import androidx.compose.runtime.remember
|
|
import androidx.compose.runtime.setValue
|
|
import androidx.compose.ui.Modifier
|
|
import androidx.compose.ui.graphics.Color
|
|
import androidx.wear.compose.foundation.pager.VerticalPager
|
|
import androidx.wear.compose.foundation.pager.rememberPagerState
|
|
import androidx.wear.compose.material3.MaterialTheme
|
|
import androidx.wear.compose.material3.VerticalPageIndicator
|
|
import kotlinx.coroutines.delay
|
|
import java.time.Instant
|
|
import kotlin.concurrent.thread
|
|
|
|
/** 컴플리케이션 탭이 실어 보내는 각 코드 */
|
|
const val EXTRA_GAK = "gak"
|
|
|
|
class MainActivity : ComponentActivity() {
|
|
private var summary by mutableStateOf<Summary?>(null)
|
|
private var loading by mutableStateOf(true)
|
|
|
|
private val askLocation = registerForActivityResult(ActivityResultContracts.RequestPermission()) { load() }
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
val opened = intent.getStringExtra(EXTRA_GAK)
|
|
setContent { MaterialTheme { App(summary, loading, opened) { load(force = true) } } }
|
|
if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) load()
|
|
else askLocation.launch(Manifest.permission.ACCESS_COARSE_LOCATION)
|
|
}
|
|
|
|
private fun load(force: Boolean = false) {
|
|
loading = true
|
|
thread {
|
|
val s = SummaryRepo.load(this, force)
|
|
runOnUiThread { summary = s; loading = false }
|
|
}
|
|
}
|
|
}
|
|
|
|
private enum class Page { DETAIL, EVENTS, TOP, PICKS, SUNSET, HOURS }
|
|
|
|
@Composable
|
|
private fun App(s: Summary?, loading: Boolean, opened: String?, onRetry: () -> Unit) {
|
|
var now by remember { mutableStateOf(Instant.now()) }
|
|
LaunchedEffect(Unit) {
|
|
while (true) {
|
|
delay(20_000)
|
|
now = Instant.now()
|
|
}
|
|
}
|
|
if (s == null) {
|
|
StatePage(loading, onRetry)
|
|
return
|
|
}
|
|
// 스킨 링에서 누른 각이 있으면 그 상세부터, 아니면 주변 축제부터 (축제가 없으면 오늘의 각부터)
|
|
val detail = opened?.let { c -> (s.top + s.picks).firstOrNull { it.code == c } }
|
|
val pages = buildList {
|
|
if (detail != null) add(Page.DETAIL)
|
|
if (s.events.isNotEmpty()) add(Page.EVENTS)
|
|
if (s.top.isNotEmpty()) add(Page.TOP)
|
|
if (s.picks.isNotEmpty()) add(Page.PICKS)
|
|
if (s.sunset != null) add(Page.SUNSET)
|
|
if (s.slots.isNotEmpty()) add(Page.HOURS)
|
|
}
|
|
val state = rememberPagerState { pages.size }
|
|
Box(Modifier.fillMaxSize()) {
|
|
VerticalPager(state = state, modifier = Modifier.fillMaxSize()) { i ->
|
|
when (pages[i]) {
|
|
Page.DETAIL -> DetailPage(detail!!, now)
|
|
Page.EVENTS -> EventsPage(s.events, now)
|
|
Page.TOP -> GakListPage("오늘의 각", s.top.take(3), bgButter, medals = true, now)
|
|
Page.PICKS -> GakListPage("내 각", s.picks, bgMint, medals = false, now)
|
|
Page.SUNSET -> SunsetPage(s.sunset!!, now)
|
|
Page.HOURS -> HoursPage(s, now)
|
|
}
|
|
}
|
|
val page = pages[state.currentPage]
|
|
val dot = if (page == Page.SUNSET) Color.White else Tok.ink
|
|
VerticalPageIndicator(pagerState = state, selectedColor = dot, unselectedColor = dot.copy(alpha = 0.35f), backgroundColor = Color.Transparent)
|
|
}
|
|
}
|