Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(562): migrate auth screen to compose #564

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package co.anitrend.core.android.extensions

import android.graphics.drawable.AdaptiveIconDrawable
import android.os.Build
import androidx.annotation.DrawableRes
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.drawable.toBitmap

/**
* [Source](https://gist.github.com/tkuenneth/ddf598663f041dc79960cda503d14448?permalink_comment_id=4660486#gistcomment-4660486)
*/
@Composable
fun adaptiveIconPainterResource(@DrawableRes id: Int): Painter {
val res = LocalContext.current.resources
val theme = LocalContext.current.theme

return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Android O supports adaptive icons, try loading this first (even though this is least likely to be the format).
val adaptiveIcon = ResourcesCompat.getDrawable(res, id, theme) as? AdaptiveIconDrawable
if (adaptiveIcon != null) {
BitmapPainter(adaptiveIcon.toBitmap().asImageBitmap())
} else {
// We couldn't load the drawable as an Adaptive Icon, just use painterResource
painterResource(id)
}
} else {
// We're not on Android O or later, just use painterResource
painterResource(id)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
package co.anitrend.navigation.model.common

import android.os.Parcelable
import kotlinx.parcelize.Parcelize

/**
* Router parameter contract
*/
interface IParam : Parcelable

@Parcelize
data class None(override val idKey: String = "") : IParam
1 change: 0 additions & 1 deletion feature-auth/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
<!--suppress AndroidElementNotAllowed -->
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
package co.anitrend.auth.component.compose

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.rounded.ArrowBack
import androidx.compose.material.icons.rounded.AccountCircle
import androidx.compose.material.icons.twotone.Info
import androidx.compose.material.icons.twotone.NoAccounts
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Snackbar
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.SuggestionChip
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import co.anitrend.auth.component.viewmodel.state.AuthState
import co.anitrend.core.android.ui.AniTrendPreview
import co.anitrend.core.android.ui.theme.AniTrendTheme3
import co.anitrend.core.android.ui.typography.AniTrendTypography
import kotlinx.coroutines.launch

@Composable
private fun AuthBrandNameComponent(modifier: Modifier = Modifier) {
Row(modifier = modifier) {
Text(
text = stringResource(co.anitrend.auth.R.string.auth_label_segment_first),
style = AniTrendTypography.displayMedium,
)
Text(
text = stringResource(co.anitrend.auth.R.string.auth_label_segment_second),
style = AniTrendTypography.displayMedium.copy(
color = colorResource(co.anitrend.arch.theme.R.color.colorStateBlue)
),

)
}
}

@Composable
private fun AuthHeaderSection(modifier: Modifier = Modifier) {
Column(
modifier = modifier.padding(top = 16.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
AuthBrandNameComponent()
Spacer(modifier = Modifier.padding(top = 8.dp))
Text(text = stringResource(co.anitrend.auth.R.string.label_allow_authorization))
}
}

@Composable
private fun AuthAuthorizationSection(
onAuthorizeClick: () -> Unit,
onAuthorizationHelpClick: () -> Unit,
modifier: Modifier = Modifier
) {
Column(
modifier = modifier.padding(top = 16.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
FilledTonalButton(
onClick = onAuthorizeClick,
) {
Icon(imageVector = Icons.Rounded.AccountCircle, contentDescription = null)
Spacer(modifier = Modifier.padding(start = 6.dp))
Text(text = stringResource(co.anitrend.auth.R.string.auth_label_authorize))
}
Spacer(modifier = Modifier.padding(top = 8.dp))
SuggestionChip(
onClick = onAuthorizationHelpClick,
label = {
Text(text = stringResource(co.anitrend.auth.R.string.auth_label_having_trouble_logging_in))
},
icon = { Icon(imageVector = Icons.TwoTone.Info, contentDescription = null) }
)
}
}

@Composable
private fun AuthAnonymousSection(modifier: Modifier = Modifier) {
Column(
modifier = modifier.padding(top = 16.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = stringResource(co.anitrend.auth.R.string.auth_label_alternative_account),
textAlign = TextAlign.Center,
style = AniTrendTypography.labelMedium,
)
Spacer(modifier = Modifier.padding(top = 8.dp))
SuggestionChip(
onClick = {},
label = {
Text(text = stringResource(co.anitrend.auth.R.string.auth_label_action_start_anonymous_account))
},
icon = { Icon(imageVector = Icons.TwoTone.NoAccounts, contentDescription = null) }
)
}
}

@Composable
private fun AuthContent(
onAuthorizeClick: () -> Unit,
onAuthorizationHelpClick: () -> Unit,
modifier: Modifier = Modifier,
) {
Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
modifier = modifier.then(Modifier.padding(start = 48.dp, end = 48.dp)),
) {
AuthHeaderSection()
Spacer(modifier = Modifier.padding(top = 24.dp))
AuthAuthorizationSection(onAuthorizeClick, onAuthorizationHelpClick)
Spacer(modifier = Modifier.padding(top = 24.dp))
AuthAnonymousSection()
}
}

@Composable
private fun AuthBottomAppBar(onBackPress: () -> Unit) {
BottomAppBar(
actions = {
IconButton(onClick = onBackPress) {
Icon(
imageVector = Icons.AutoMirrored.Rounded.ArrowBack,
contentDescription = "Back"
)
}
}
)
}

@Composable
fun AuthScreenContent(
authState: AuthState,
onAuthorizeClick: () -> Unit,
onAuthorizationHelpClick: () -> Unit,
onBackPress: () -> Unit
) {
val snackbarHostState = remember { SnackbarHostState() }
val state = authState.model.observeAsState()

Scaffold(
bottomBar = {
AuthBottomAppBar(onBackPress = onBackPress)
},
snackbarHost = {
SnackbarHost(hostState = snackbarHostState)
},
modifier = Modifier
) { innerPadding ->
AuthContent(
onAuthorizeClick,
onAuthorizationHelpClick,
modifier = Modifier.padding(innerPadding)
.fillMaxSize()
.verticalScroll(rememberScrollState())
)
}
}


@AniTrendPreview.Mobile
@AniTrendPreview.Light
@AniTrendPreview.Dark
@Composable
private fun MediaDetailComponentPreview() {
AniTrendTheme3 {
AuthContent(
onAuthorizeClick = {},
onAuthorizationHelpClick = {}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class AuthContent(
viewModelState().model.observeOnce(viewLifecycleOwner) { user ->
runCatching {
presenter.runSignInWorker(user.id)
closeScreen()
}.onFailure {
Snackbar.make(
requireView(),
Expand Down
Loading
Loading