Skip to content

Commit

Permalink
add: trakt login error handling implemented #33
Browse files Browse the repository at this point in the history
  • Loading branch information
Hadi Norouzi authored and hadi-norouzi committed Jan 21, 2024
1 parent e0f097f commit 8e0876b
Showing 1 changed file with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import android.view.ViewGroup
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
Expand All @@ -20,9 +24,13 @@ import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import io.filmtime.feature.trakt.login.LoginState.Failed

private const val TRAKT_LOGIN_URL =
"https://api.trakt.tv/oauth/authorize?response_type=code&client_id=" +
Expand All @@ -36,10 +44,14 @@ fun TraktLoginWebView(
onSuccess: () -> Unit,
) {
val state by viewModel.loginState.collectAsStateWithLifecycle()
val context = LocalContext.current

LaunchedEffect(key1 = state) {
when (state) {
is LoginState.Failed -> TODO() // #33
is Failed -> {
Toast.makeText(context, (state as Failed).error.toString(), Toast.LENGTH_LONG).show()
}

LoginState.Success -> onSuccess()
else -> {}
}
Expand All @@ -61,7 +73,7 @@ fun TraktLoginWebView(
modifier = Modifier.padding(it),
) {
when (state) {
LoginState.Initial -> {
LoginState.Initial, is Failed -> {
AndroidView(
factory = {
WebView(it).apply {
Expand Down Expand Up @@ -89,10 +101,15 @@ fun TraktLoginWebView(
}

LoginState.Loading ->
Column {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
modifier = Modifier.fillMaxSize()
) {
CircularProgressIndicator(
modifier = Modifier.wrapContentSize(),
)
Box(modifier = Modifier.size(8.dp))
Text("Getting tokens")
}

Expand Down

0 comments on commit 8e0876b

Please sign in to comment.