Skip to content

Commit

Permalink
GT-2200 Create No Internet Error
Browse files Browse the repository at this point in the history
  • Loading branch information
gyasistory committed Oct 31, 2024
1 parent ca18bd0 commit 708c567
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
19 changes: 17 additions & 2 deletions app/src/main/kotlin/org/cru/godtools/ui/login/LoginLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ private val FACEBOOK_BLUE = Color(red = 0x18, green = 0x77, blue = 0xf2)

@Composable
@OptIn(ExperimentalMaterial3Api::class)
fun LoginLayout(createAccount: Boolean = false, onEvent: (event: LoginLayoutEvent) -> Unit) {
fun LoginLayout(
modifier: Modifier = Modifier,
createAccount: Boolean = false,
onEvent: (event: LoginLayoutEvent) -> Unit = {},
) {
var loginError: LoginResponse.Error? by rememberSaveable { mutableStateOf(null) }
val loginLauncher = rememberLoginLauncher(createAccount) {
when (it) {
Expand All @@ -61,7 +65,7 @@ fun LoginLayout(createAccount: Boolean = false, onEvent: (event: LoginLayoutEven

Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
modifier = modifier
.fillMaxSize()
.background(GodToolsTheme.GT_BLUE)
.verticalScroll(rememberScrollState())
Expand Down Expand Up @@ -147,10 +151,21 @@ fun LoginLayout(createAccount: Boolean = false, onEvent: (event: LoginLayoutEven
private fun LoginError(error: LoginResponse.Error?, onDismiss: () -> Unit) {
if (error != null) {
AlertDialog(
title = {
Text(
stringResource(
when (error) {
LoginResponse.Error.NotConnected -> R.string.account_error_not_connected_title
else -> R.string.account_error_title
}
)
)
},
text = {
Text(
stringResource(
when (error) {
LoginResponse.Error.NotConnected -> R.string.account_error_not_connected
LoginResponse.Error.UserAlreadyExists -> R.string.account_error_user_already_exists
LoginResponse.Error.UserNotFound -> R.string.account_error_user_not_found
else -> R.string.account_error_unknown
Expand Down
2 changes: 2 additions & 0 deletions library/account/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ android {

dependencies {
implementation(project(":library:api"))
implementation(project(":library:base"))

implementation(libs.androidx.activity.compose)

implementation(libs.gtoSupport.androidx.activity)
implementation(libs.gtoSupport.base)
implementation(libs.gtoSupport.kotlin.coroutines)
implementation(libs.gtoSupport.util)

implementation(libs.dagger)
implementation(libs.hilt)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package org.cru.godtools.account

import android.annotation.SuppressLint
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET
import android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED
import android.os.Build
import androidx.activity.result.ActivityResultLauncher
import androidx.annotation.VisibleForTesting
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.core.app.ActivityOptionsCompat
import dagger.Lazy
import java.io.IOException
Expand Down Expand Up @@ -39,6 +46,7 @@ class GodToolsAccountManager @VisibleForTesting internal constructor(
internal constructor(
providers: Set<@JvmSuppressWildcards AccountProvider>,
userApi: Lazy<UserApi>,
coroutineScope: CoroutineScope = CoroutineScope(SupervisorJob()),
) : this(providers.sortedWith(Ordered.COMPARATOR), userApi)

// region Active Provider
Expand Down Expand Up @@ -73,15 +81,21 @@ class GodToolsAccountManager @VisibleForTesting internal constructor(
createAccount: Boolean,
onResponse: (LoginResponse) -> Unit,
): ActivityResultLauncher<AccountType> {
val context = LocalContext.current
val launchers = providers.associate {
it.type to it.rememberLauncherForLogin(createAccount) { result ->
onResponse(
when {
result.isSuccess -> LoginResponse.Success
else -> when (result.exceptionOrNull()) {
AuthenticationException.UserNotFound -> LoginResponse.Error.UserNotFound

AuthenticationException.UserAlreadyExists -> LoginResponse.Error.UserAlreadyExists
else -> LoginResponse.Error()

else -> when {
!context.isConnected() -> LoginResponse.Error.NotConnected
else -> LoginResponse.Error()
}
}
}
)
Expand Down Expand Up @@ -117,3 +131,14 @@ class GodToolsAccountManager @VisibleForTesting internal constructor(
internal suspend fun authenticateWithMobileContentApi() = activeProvider?.authenticateWithMobileContentApi(false)
?: Result.failure(AuthenticationException.NoActiveProvider)
}

// TODO Remove when GTO-Support-Util is updated
@SuppressLint("MissingPermission")
private fun Context.isConnected(): Boolean {
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager ?: return false
return when {
Build.VERSION.SDK_INT < Build.VERSION_CODES.M -> connectivityManager.activeNetworkInfo?.isConnected
else -> connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
?.let { it.hasCapability(NET_CAPABILITY_INTERNET) && it.hasCapability(NET_CAPABILITY_VALIDATED) }
} ?: false
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ sealed interface LoginResponse : Parcelable {
data object UserNotFound : Error()
@Parcelize
data object UserAlreadyExists : Error()
@Parcelize
data object NotConnected : Error()
}
}

0 comments on commit 708c567

Please sign in to comment.