์ด์ ํฌ์คํ ์ ์ฐ์ฅ์ ์ ๋๋ค.
https://rkdrkd-history.tistory.com/55
[Android, Kotlin] Android clean architecture ํ๋ก์ ํธ์์ api ์๋ฌ ํธ๋ค๋งํ๊ธฐ
clean architecture ํ๋ก์ ํธ๋ฅผ ๊ตฌ์ํ๊ณ ๊ฐ๋ฐํ๋ค ๋ณด๋ฉด api error ์ฒ๋ฆฌ๋ฅผ ์ด๋ป๊ฒ ํ๊ณ ์๋๊ฐ? ์์ ์ ํ์๋ ๊ทธ๋ฅ ๋ฌด์ง์ฑ์ผ๋ก(?) presentation layer์ try catch๋ฅผ ์ด์ฉํด ์ฒ๋ฆฌ๋ฅผ ํ์์๋ค. ํ์ง๋ง ๊ทธ ๋ฐฉ๋ฒ์
rkdrkd-history.tistory.com
์ด์ ํฌ์คํ ์์ clean architecture ๊ด์ ์์ api error handling ๋ฐฉ๋ฒ์ ๊ณต๋ถํด ๋ดค๋๋ฐ,
์ด๋ฒ ๊ธ์์๋ ์ง๋ ๊ธ์ ์ฐ์ฅ์ ์ธ clean architecture ๊ด์ ์์ compose๋ก api ํต์ ๋ฒ์ ์ ์ด๋ณด๊ฒ ๋ค.
์ฌ์ค ํ ํฌ์คํ ์ ๋ชจ๋ ๋ด์ ์๊ฐ์ด์์ง๋ง, ์ฃผ์ ์ฐจ์ด๋ ์๊ณ ๊ธ์ด ๋๋ฌด ๊ธธ์ด์ง ๋ฏํด์ ๋ ๊ฐ๋ก ๋๋ ์ ์ดํดํด ์ฃผ์๊ธธ xD
1. viewModel parameter ์์ฑ
compose์์ viewmodel์ ์ฌ์ฉํ๋ ค๋ฉด screen ํจ์์ viewmodel ํ๋ผ๋ฏธํฐ๋ฅผ ๋ง๋ค์ด์ ์ด๊ธฐํ ํ ์ฌ์ฉํด ์ฃผ๋ฉด ๋๋ค.
@Composable
fun MainScreen(
twitchAuthViewModel: TwitchAuthViewModel = hiltViewModel()
) {
. . .
}
ํ์ฌ clean architecture๋ก ๊ฐ๋ฐ์ ํ๊ณ ์๊ณ , ์์กด์ฑ ์ฃผ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ hilt๋ฅผ ์ฌ์ฉ ์ค์ด๊ธฐ์ hiltViewModel()์ ์ฌ์ฉํด ์ฃผ์๋ค.
hiltViewModel์ navigation compose library๋ฅผ ์ถ๊ฐํด์ ์ฌ์ฉํ ์ ์๋ค.
"androidx.navigation:navigation-compose:2.6.0" // 2023-08-01 ๊ธฐ์ค
์ด์ ์์ฑํ hiltViewModel์ ์ด์ฉํด api ํธ์ถ ์์ ์ ์ํํด ๋ณด๊ฒ ๋ค.
๋ค์ ์ฝ๋๋ mainScreen ์ ์ฒด ์ฝ๋์ด๋ค
@Composable
fun MainScreen(
twitchAuthViewModel: TwitchAuthViewModel = hiltViewModel()
) {
LaunchedEffect(Unit) {
twitchAuthViewModel.getOAuthToken(
clientId = BuildConfig.CLIENT_ID,
clientSecret = BuildConfig.CLIENT_SECRET,
grantType = "client_credentials"
)
}
val oAuthToken by twitchAuthViewModel.oAuth.observeAsState()
Column(modifier = Modifier.fillMaxSize()) {
when (val data = oAuthToken) {
ApiState.Loading -> {}
is ApiState.Success -> {
Text(text = "accessToken: ${data.data?.accessToken}")
Text(text = "tokenType: ${data.data?.tokenType}")
Text(text = "expiresIn: ${data.data?.expiresIn}")
}
ApiState.BadRequest -> Text(text = "bad request")
ApiState.Forbidden -> Text(text = "forbidden")
ApiState.TimeOut -> Text(text = "time out")
ApiState.Server -> Text(text = "server error")
ApiState.Unknown -> Text(text = "unknown")
else -> Text(text = "else")
}
}
}
launchedEffect(Unit)๋ฅผ ์ด์ฉํด api ๋ก์ง์ด ํ ๋ฒ๋ง ์ํ๋๊ฒ ํ์๋ค.
๊ทธ๋ฆฌ๊ณ observeAsState๋ฅผ ์ด์ฉํด twitchAuthViewModel.oAuth์ observing ํ๋ฉด์๋ state๋ฅผ ๊ฐ์ ธ์ ์ฌ์ฉํ ์ ์๊ฒ ํ์๋ค.
when์ ์ด์ฉํด ApiState ์ํ์ ๋ฐ๋ผ ๋ค๋ฅธ ๋ก์ง์ด ํธ์ถ๋๊ฒ ํ์๋ค.
2. ์ํ ๊ฒฐ๊ณผ
api ํธ์ถ์ด ์ ์์ ์ผ๋ก ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
์ ๋ฆฌ
compose๋ก api ํต์ ์ ๊ณต๋ถํด ๋ดค๋๋ฐ observing ํ๋ฉด์ data state๋ฅผ ์ธ ์ ์๋ค๋ ์ ์ด ํธ๋ฆฌํ ๊ฒ ๊ฐ์๋ค.
๊ทธ๋ฌ๋ ๋์ค์ ์ฌ์ฉํ api๊ฐ ๋ง์์ง๋ฉด, when ๋ฌธ์ด ๋ง์์ ธ ์ฝ๋๊ฐ ๋๋ฌ์์ง ๊ฒ ๊ฐ์์ ํด๊ฒฐํ ๋ฐฉ๋ฒ์ ์๊ฐํด๋ด์ผ ํ ๊ฒ ๊ฐ๋ค.
์ฐธ๊ณ
https://arca.live/b/twitchdev/44459710
* ํธ์์น API ๊ฐ์ด๋ * - ํธ์์น๊ฐ๋ฐ ์ฑ๋
0. Introduction (์๋ก )ํธ์์น API๋ฅผ ์ฒ์ ์ ํด๋ณด๊ฑฐ๋ ์ฒ์ ์ด์ฉํ๋ ์ฌ๋๋ค์ ์ํ ๊ฐ์ด๋ ์ ๋๋ค.์ค์ฝํ๊ฐ ํ์ ์๊ณ ์ฑํ ํฐ์ ์ฌ์ฉํ๋ ํธ์์น api ๋ง ๋ค๋ฃจ๊ฒ ์ต๋๋ค.์์ด ๋ฌธ์๋ง ๋ณด๋ฉด ๊ดด๋ฆฌ๊ฐ์ ๋
arca.live
'๐ฑ| Android > ๐ | Jetpack' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android, Kotlin] compose๋ก tablayout ๊ตฌํํ๊ธฐ (2) | 2023.08.15 |
---|---|
[Android, Kotlin] compose navigation์์ data class tpye ๋๊ธฐ๊ธฐ (2) | 2023.08.11 |
[Android, Kotlin] Compose UI ์ ๋ฆฌ(7) (2) | 2023.04.16 |
[Android, Kotlin] Compose UI ์ ๋ฆฌ(6) (0) | 2023.04.07 |
[Android, Kotlin] Android Activity์ lifeCycle ์ ๋ฆฌ (0) | 2023.03.17 |