Skip to content

Commit

Permalink
login on actions
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed May 11, 2024
1 parent 3aaf365 commit 41cdcf5
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class MainActivity : AppCompatActivity() {
}

val accessToken = uri.getFragmentOrQueryParameter("access_token")
if (accessToken.isNullOrBlank()) {
if (accessToken.isNullOrBlank() || !::root.isInitialized) {
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ interface MediumComponent : ContentHolderComponent {
val initialMedium: Medium
val titleLanguage: Flow<SettingsTitle?>
val charLanguage: Flow<SettingsChar?>
val isLoggedIn: Flow<Boolean>
val loginUri: String

val mediumState: Flow<MediumRepository.State>
val isAdult: Flow<Boolean>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ fun MediumScreen(component: MediumComponent) {
bannerImageFlow = component.bannerImage,
coverImage = coverImage,
titleFlow = component.title,
isLoggedIn = component.isLoggedIn,
loginUri = component.loginUri,
isFavoriteFlow = component.isFavorite,
isFavoriteBlockedFlow = component.isFavoriteBlocked,
siteUrlFlow = component.siteUrl,
Expand Down Expand Up @@ -117,11 +119,19 @@ fun MediumScreen(component: MediumComponent) {
}

if (!notReleased) {
val loggedIn by component.isLoggedIn.collectAsStateWithLifecycle(false)
val status by component.listStatus.collectAsStateWithLifecycle(component.initialMedium.entry?.status ?: MediaListStatus.UNKNOWN__)
val type by component.type.collectAsStateWithLifecycle(component.initialMedium.type)
val uriHandler = LocalUriHandler.current

ExtendedFloatingActionButton(
onClick = { component.edit() },
onClick = {
if (!loggedIn) {
uriHandler.openUri(component.loginUri)
} else {
component.edit()
}
},
expanded = listState.isScrollingUp() && listState.canScrollForward,
icon = {
Icon(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ class MediumScreenComponent(

private val aniListClient by di.instance<ApolloClient>(Constants.AniList.APOLLO_CLIENT)
private val appSettings by di.instance<Settings.PlatformAppSettings>()

private val userHelper by di.instance<UserHelper>()
override val isLoggedIn: Flow<Boolean> = userHelper.isLoggedIn
override val loginUri: String = userHelper.loginUrl

override val titleLanguage: Flow<SettingsTitle?> = appSettings.titleLanguage.flowOn(ioDispatcher())
override val charLanguage: Flow<CharLanguage?> = appSettings.charLanguage.flowOn(ioDispatcher())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import androidx.compose.material.icons.filled.ArrowBackIosNew
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.FavoriteBorder
import androidx.compose.material.icons.filled.Share
import androidx.compose.material.icons.rounded.ArrowBackIosNew
import androidx.compose.material.icons.rounded.Favorite
import androidx.compose.material.icons.rounded.FavoriteBorder
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
Expand Down Expand Up @@ -56,6 +59,8 @@ fun CollapsingToolbar(
bannerImageFlow: Flow<String?>,
coverImage: Medium.CoverImage,
titleFlow: Flow<Medium.Title>,
isLoggedIn: Flow<Boolean>,
loginUri: String,
isFavoriteFlow: Flow<Boolean>,
isFavoriteBlockedFlow: Flow<Boolean>,
siteUrlFlow: Flow<String>,
Expand Down Expand Up @@ -106,7 +111,7 @@ fun CollapsingToolbar(
}
) {
Icon(
imageVector = Icons.Default.ArrowBackIosNew,
imageVector = Icons.Rounded.ArrowBackIosNew,
contentDescription = null
)
}
Expand Down Expand Up @@ -177,22 +182,28 @@ fun CollapsingToolbar(
enter = fadeIn(),
exit = fadeOut()
) {
val loggedIn by isLoggedIn.collectAsStateWithLifecycle(false)
val isFavoriteBlocked by isFavoriteBlockedFlow.collectAsStateWithLifecycle(initialMedium.isFavoriteBlocked)
val isFavorite by isFavoriteFlow.collectAsStateWithLifecycle(initialMedium.isFavorite)
var favoriteChanged by remember(isFavorite) { mutableStateOf<Boolean?>(null) }
val uriHandler = LocalUriHandler.current

IconButton(
onClick = {
favoriteChanged = !(favoriteChanged ?: isFavorite)
onToggleFavorite()
if (!loggedIn) {
uriHandler.openUri(loginUri)
} else {
favoriteChanged = !(favoriteChanged ?: isFavorite)
onToggleFavorite()
}
},
enabled = !isFavoriteBlocked
enabled = !loggedIn || !isFavoriteBlocked
) {
Icon(
imageVector = if (favoriteChanged ?: isFavorite) {
Icons.Default.Favorite
Icons.Rounded.Favorite
} else {
Icons.Default.FavoriteBorder
Icons.Rounded.FavoriteBorder
},
contentDescription = null
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import kotlinx.coroutines.flow.StateFlow

interface CharacterComponent : DialogComponent {
val initialChar: Character
val isLoggedIn: Flow<Boolean>
val loginUri: String

val state: Flow<CharacterRepository.State>
val charLanguage: Flow<CharLanguage?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material.icons.rounded.ArrowBackIosNew
import androidx.compose.material.icons.rounded.Favorite
import androidx.compose.material.icons.rounded.FavoriteBorder
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -112,22 +115,28 @@ fun CharacterDialog(component: CharacterComponent) {
enter = fadeIn(),
exit = fadeOut()
) {
val loggedIn by component.isLoggedIn.collectAsStateWithLifecycle(false)
val isFavoriteBlocked by component.isFavoriteBlocked.collectAsStateWithLifecycle(component.initialChar.isFavoriteBlocked)
val isFavorite by component.isFavorite.collectAsStateWithLifecycle(component.initialChar.isFavorite)
var favoriteChanged by remember(isFavorite) { mutableStateOf<Boolean?>(null) }
val uriHandler = LocalUriHandler.current

IconButton(
onClick = {
favoriteChanged = !(favoriteChanged ?: isFavorite)
component.toggleFavorite()
if (!loggedIn) {
uriHandler.openUri(component.loginUri)
} else {
favoriteChanged = !(favoriteChanged ?: isFavorite)
component.toggleFavorite()
}
},
enabled = !isFavoriteBlocked
enabled = !loggedIn || !isFavoriteBlocked
) {
Icon(
imageVector = if (favoriteChanged ?: isFavorite) {
Icons.Default.Favorite
Icons.Rounded.Favorite
} else {
Icons.Default.FavoriteBorder
Icons.Rounded.FavoriteBorder
},
contentDescription = null,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import dev.datlag.aniflow.common.nullableFirebaseInstance
import dev.datlag.aniflow.common.onRender
import dev.datlag.aniflow.model.safeFirstOrNull
import dev.datlag.aniflow.other.Constants
import dev.datlag.aniflow.other.UserHelper
import dev.datlag.aniflow.settings.Settings
import dev.datlag.aniflow.settings.model.CharLanguage
import dev.datlag.aniflow.settings.model.TitleLanguage
Expand All @@ -28,12 +29,16 @@ class CharacterDialogComponent(
private val onDismiss: () -> Unit
) : CharacterComponent, ComponentContext by componentContext {

private val aniListClient by di.instance<ApolloClient>(Constants.AniList.APOLLO_CLIENT)
private val characterRepository by di.instance<CharacterRepository>()
private val aniListClient by instance<ApolloClient>(Constants.AniList.APOLLO_CLIENT)
private val characterRepository by instance<CharacterRepository>()

private val appSettings by di.instance<Settings.PlatformAppSettings>()
private val appSettings by instance<Settings.PlatformAppSettings>()
override val charLanguage: Flow<CharLanguage?> = appSettings.charLanguage.flowOn(ioDispatcher())

private val userHelper by instance<UserHelper>()
override val isLoggedIn: Flow<Boolean> = userHelper.isLoggedIn
override val loginUri: String = userHelper.loginUrl

override val state = characterRepository.character
private val characterSuccessState = state.mapNotNull {
it.safeCast<CharacterRepository.State.Success>()
Expand Down

0 comments on commit 41cdcf5

Please sign in to comment.