-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
get rid of token refresh and add user helper
- Loading branch information
Showing
13 changed files
with
213 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
mutation ViewerMutation($adult: Boolean) { | ||
UpdateUser(displayAdultContent: $adult) { | ||
id, | ||
name, | ||
avatar { | ||
medium, | ||
large | ||
}, | ||
bannerImage, | ||
options { | ||
displayAdultContent | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
query ViewerQuery { | ||
Viewer { | ||
id, | ||
name, | ||
avatar { | ||
medium, | ||
large | ||
}, | ||
bannerImage, | ||
options { | ||
displayAdultContent | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
anilist/src/commonMain/kotlin/dev/datlag/aniflow/anilist/model/User.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package dev.datlag.aniflow.anilist.model | ||
|
||
import dev.datlag.aniflow.anilist.ViewerQuery | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class User( | ||
val id: Int, | ||
val name: String, | ||
val avatar: Avatar = Avatar(), | ||
val banner: String? = null, | ||
val displayAdultContent: Boolean = false | ||
) { | ||
constructor(query: ViewerQuery.Viewer) : this( | ||
id = query.id, | ||
name = query.name, | ||
avatar = query.avatar.let(::Avatar), | ||
banner = query.bannerImage?.ifBlank { null }, | ||
displayAdultContent = query.options?.displayAdultContent ?: false | ||
) | ||
|
||
@Serializable | ||
data class Avatar( | ||
val medium: String? = null, | ||
val large: String? = null | ||
) { | ||
constructor(query: ViewerQuery.Avatar?) : this( | ||
medium = query?.medium?.ifBlank { null }, | ||
large = query?.large?.ifBlank { null } | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 0 additions & 67 deletions
67
composeApp/src/commonMain/kotlin/dev/datlag/aniflow/other/TokenRefreshHandler.kt
This file was deleted.
Oops, something went wrong.
78 changes: 78 additions & 0 deletions
78
composeApp/src/commonMain/kotlin/dev/datlag/aniflow/other/UserHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package dev.datlag.aniflow.other | ||
|
||
import com.apollographql.apollo3.ApolloClient | ||
import com.apollographql.apollo3.api.Optional | ||
import dev.datlag.aniflow.anilist.ViewerMutation | ||
import dev.datlag.aniflow.anilist.ViewerQuery | ||
import dev.datlag.aniflow.anilist.model.User | ||
import dev.datlag.aniflow.model.safeFirstOrNull | ||
import dev.datlag.aniflow.settings.Settings | ||
import dev.datlag.tooling.async.suspendCatching | ||
import dev.datlag.tooling.compose.withMainContext | ||
import kotlinx.coroutines.flow.* | ||
import kotlinx.datetime.Clock | ||
import org.publicvalue.multiplatform.oidc.OpenIdConnectClient | ||
import org.publicvalue.multiplatform.oidc.appsupport.CodeAuthFlowFactory | ||
import org.publicvalue.multiplatform.oidc.types.remote.AccessTokenResponse | ||
|
||
class UserHelper( | ||
private val userSettings: Settings.PlatformUserSettings, | ||
private val appSettings: Settings.PlatformAppSettings, | ||
private val client: ApolloClient, | ||
private val authFlowFactory: CodeAuthFlowFactory, | ||
private val oidc: OpenIdConnectClient | ||
) { | ||
|
||
val isLoggedIn: Flow<Boolean> = userSettings.isAniListLoggedIn.distinctUntilChanged() | ||
val user: Flow<User?> = isLoggedIn.transform { loggedIn -> | ||
if (loggedIn) { | ||
emitAll( | ||
client.query(ViewerQuery()).toFlow().map { | ||
it.data?.Viewer?.let(::User)?.also { user -> | ||
appSettings.setAdultContent(user.displayAdultContent) | ||
} | ||
} | ||
) | ||
} else { | ||
emit(null) | ||
} | ||
} | ||
|
||
suspend fun login(): Boolean { | ||
if (isLoggedIn.safeFirstOrNull() == true) { | ||
return true | ||
} | ||
|
||
val flow = withMainContext { | ||
authFlowFactory.createAuthFlow(oidc) | ||
} | ||
|
||
val tokenResult = suspendCatching { | ||
flow.getAccessToken() | ||
} | ||
|
||
tokenResult.getOrNull()?.let { | ||
updateStoredToken(it) | ||
} | ||
|
||
return tokenResult.isSuccess | ||
} | ||
|
||
suspend fun updateAdultSetting(value: Boolean) { | ||
appSettings.setAdultContent(value) | ||
client.mutation( | ||
ViewerMutation( | ||
adult = Optional.present(value) | ||
) | ||
).execute() | ||
} | ||
|
||
private suspend fun updateStoredToken(tokenResponse: AccessTokenResponse) { | ||
userSettings.setAniListTokens( | ||
access = tokenResponse.access_token, | ||
expires = tokenResponse.expires_in?.let { | ||
Clock.System.now().epochSeconds + it | ||
}?.toInt() | ||
) | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...Main/kotlin/dev/datlag/aniflow/ui/navigation/screen/initial/settings/SettingsComponent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.