-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from imashnake0/auth
Auth
- Loading branch information
Showing
30 changed files
with
403 additions
and
44 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
query UserQuery( | ||
$id: Int, | ||
$name: String, | ||
$isModerator: Boolean, | ||
$search: String, | ||
$sort: [UserSort], | ||
) { | ||
user: User( | ||
id: $id, | ||
name: $name | ||
isModerator: $isModerator | ||
search: $search | ||
sort: $sort | ||
) { | ||
id | ||
name | ||
about | ||
avatar { | ||
large | ||
} | ||
bannerImage | ||
} | ||
} | ||
|
||
query Viewer { | ||
viewer: Viewer { | ||
id | ||
name | ||
about | ||
avatar { | ||
large | ||
} | ||
bannerImage | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
api/anilist/src/main/kotlin/com/imashnake/animite/api/anilist/AnilistUserRepository.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,24 @@ | ||
package com.imashnake.animite.api.anilist | ||
|
||
import com.apollographql.apollo3.ApolloClient | ||
import com.apollographql.apollo3.cache.normalized.FetchPolicy | ||
import com.apollographql.apollo3.cache.normalized.fetchPolicy | ||
import kotlinx.coroutines.flow.Flow | ||
import javax.inject.Inject | ||
|
||
/** | ||
* Repository for anything user related. Including the [ViewerQuery.Viewer]. | ||
* | ||
* @param apolloClient Client with the [`Authorization` header](https://anilist.gitbook.io/anilist-apiv2-docs/overview/oauth/implicit-grant#making-authenticated-requests). | ||
* @property fetchViewer Fetches the current user with an authorized [apolloClient]. | ||
*/ | ||
class AnilistUserRepository @Inject constructor( | ||
@AuthorizedClient private val apolloClient: ApolloClient | ||
) { | ||
fun fetchViewer(): Flow<Result<ViewerQuery.Viewer>> { | ||
return apolloClient | ||
.query(ViewerQuery()) | ||
.fetchPolicy(FetchPolicy.CacheAndNetwork).toFlow() | ||
.asResult { it.viewer!! } | ||
} | ||
} |
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,34 @@ | ||
plugins { | ||
alias(libs.plugins.android.library) | ||
alias(libs.plugins.kotlin) | ||
alias(libs.plugins.hilt) | ||
alias(libs.plugins.ksp) | ||
} | ||
|
||
android { | ||
defaultConfig { | ||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
isMinifyEnabled = false | ||
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro") | ||
} | ||
} | ||
|
||
namespace = "com.imashnake.animite.api.preferences" | ||
} | ||
|
||
kotlin { | ||
jvmToolchain(17) | ||
} | ||
|
||
dependencies { | ||
// Hilt | ||
implementation(libs.hilt.android) | ||
ksp(libs.hilt.android.compiler) | ||
|
||
// DataStore | ||
implementation(libs.datastore) | ||
} |
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 @@ | ||
<manifest /> |
25 changes: 25 additions & 0 deletions
25
api/preferences/src/main/kotlin/com/imashnake/animite/api/preferences/DataStoreModule.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,25 @@ | ||
package com.imashnake.animite.api.preferences | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.PreferenceDataStoreFactory | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.preferencesDataStoreFile | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object DataStoreModule { | ||
@Singleton | ||
@Provides | ||
fun providePreferencesDataStore(@ApplicationContext appContext: Context): DataStore<Preferences> { | ||
return PreferenceDataStoreFactory.create( | ||
produceFile = { appContext.preferencesDataStoreFile("default") } | ||
) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...references/src/main/kotlin/com/imashnake/animite/api/preferences/PreferencesRepository.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,19 @@ | ||
package com.imashnake.animite.api.preferences | ||
|
||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.stringPreferencesKey | ||
import com.imashnake.animite.api.preferences.ext.getValue | ||
import com.imashnake.animite.api.preferences.ext.setValue | ||
import javax.inject.Inject | ||
|
||
class PreferencesRepository @Inject constructor( | ||
private val dataStore: DataStore<Preferences> | ||
) { | ||
private val accessTokenKey = stringPreferencesKey("access_token") | ||
val accessToken = dataStore.getValue(accessTokenKey, null) | ||
|
||
suspend fun setAccessToken(accessToken: String?) { | ||
dataStore.setValue(accessTokenKey, accessToken) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
api/preferences/src/main/kotlin/com/imashnake/animite/api/preferences/ext/DataStoreExt.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,21 @@ | ||
package com.imashnake.animite.api.preferences.ext | ||
|
||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import kotlinx.coroutines.flow.map | ||
|
||
fun <T> DataStore<Preferences>.getValue( | ||
key: Preferences.Key<T>, | ||
default: T? = null | ||
) = data.map { | ||
it[key] ?: default | ||
} | ||
|
||
suspend fun <T> DataStore<Preferences>.setValue( | ||
key: Preferences.Key<T>, | ||
value: T? | ||
) = edit { | ||
if (value != null) it[key] = value | ||
else it.remove(key) | ||
} |
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
@file:Suppress("UnstableApiUsage") | ||
|
||
plugins { | ||
alias(libs.plugins.android.application) | ||
alias(libs.plugins.kotlin) | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
@file:Suppress("UnstableApiUsage") | ||
|
||
plugins { | ||
alias(libs.plugins.android.library) | ||
alias(libs.plugins.kotlin) | ||
|
2 changes: 1 addition & 1 deletion
2
...va/com/imashnake/animite/data/Resource.kt → ...m/imashnake/animite/core/data/Resource.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
Oops, something went wrong.