-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support for login with Quran.com
- Loading branch information
Showing
24 changed files
with
810 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
oauth.properties |
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,82 @@ | ||
import java.io.FileInputStream | ||
import java.util.Properties | ||
|
||
plugins { | ||
id("quran.android.library.compose") | ||
alias(libs.plugins.anvil) | ||
} | ||
|
||
android { | ||
namespace = "com.quran.mobile.feature.sync" | ||
buildFeatures.buildConfig = true | ||
|
||
val properties = Properties() | ||
val propertiesFile = project.projectDir.resolve("oauth.properties") | ||
|
||
if (propertiesFile.exists()) { | ||
properties.load(FileInputStream(propertiesFile)) | ||
} | ||
|
||
defaultConfig { | ||
buildConfigField( | ||
"String", | ||
"CLIENT_ID", | ||
"\"${properties.getProperty("client_id", "")}\"" | ||
) | ||
buildConfigField( | ||
"String", | ||
"DISCOVERY_URI", | ||
"\"${properties.getProperty("discovery_uri", "")}\"" | ||
) | ||
buildConfigField( | ||
"String", | ||
"SCOPES", | ||
"\"${properties.getProperty("scopes", "")}\"" | ||
) | ||
buildConfigField( | ||
"String", | ||
"REDIRECT_URI", | ||
"\"${properties.getProperty("redirect_uri", "")}\"" | ||
) | ||
} | ||
} | ||
|
||
anvil { | ||
useKsp(contributesAndFactoryGeneration = true, componentMerging = true) | ||
generateDaggerFactories.set(true) | ||
} | ||
|
||
dependencies { | ||
implementation(project(":common:di")) | ||
implementation(project(":common:data")) | ||
implementation(project(":common:ui:core")) | ||
|
||
// androidx | ||
implementation(libs.androidx.appcompat) | ||
implementation(libs.androidx.activity.compose) | ||
api(libs.androidx.datastore.prefs) | ||
|
||
// compose | ||
implementation(libs.compose.animation) | ||
implementation(libs.compose.foundation) | ||
implementation(libs.compose.material3) | ||
implementation(libs.compose.ui) | ||
implementation(libs.compose.ui.tooling.preview) | ||
debugImplementation(libs.compose.ui.tooling) | ||
|
||
// coroutines | ||
implementation(libs.kotlinx.coroutines.core) | ||
implementation(libs.kotlinx.coroutines.android) | ||
|
||
// molecule | ||
implementation(libs.molecule) | ||
|
||
// app auth library | ||
implementation(libs.appauth) | ||
|
||
// dagger | ||
implementation(libs.dagger.runtime) | ||
|
||
// timber | ||
implementation(libs.timber) | ||
} |
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,23 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools"> | ||
|
||
<application> | ||
<activity | ||
android:name=".QuranLoginActivity" | ||
android:theme="@style/Theme.AppCompat.NoActionBar" /> | ||
|
||
<activity | ||
android:name="net.openid.appauth.RedirectUriReceiverActivity" | ||
android:exported="true" | ||
tools:node="replace"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.VIEW" /> | ||
|
||
<category android:name="android.intent.category.DEFAULT" /> | ||
<category android:name="android.intent.category.BROWSABLE" /> | ||
|
||
<data android:scheme="com.quran.labs.androidquran" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
</manifest> |
67 changes: 67 additions & 0 deletions
67
feature/sync/src/main/kotlin/com/quran/mobile/feature/sync/QuranLoginActivity.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,67 @@ | ||
package com.quran.mobile.feature.sync | ||
|
||
import android.os.Bundle | ||
import androidx.activity.compose.setContent | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.automirrored.filled.ArrowBack | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.IconButton | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TopAppBar | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import app.cash.molecule.AndroidUiDispatcher | ||
import app.cash.molecule.RecompositionMode | ||
import app.cash.molecule.launchMolecule | ||
import com.quran.labs.androidquran.common.ui.core.QuranTheme | ||
import com.quran.mobile.di.QuranApplicationComponentProvider | ||
import com.quran.mobile.feature.sync.di.AuthComponentInterface | ||
import com.quran.mobile.feature.sync.presenter.QuranLoginPresenter | ||
import com.quran.mobile.feature.sync.ui.LoginScreen | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.SupervisorJob | ||
import javax.inject.Inject | ||
|
||
class QuranLoginActivity : AppCompatActivity() { | ||
@Inject | ||
lateinit var quranLoginPresenter: QuranLoginPresenter | ||
|
||
private val scope = CoroutineScope(SupervisorJob() + AndroidUiDispatcher.Main) | ||
|
||
private val authFlow by lazy(LazyThreadSafetyMode.NONE) { | ||
scope.launchMolecule(mode = RecompositionMode.ContextClock) { | ||
quranLoginPresenter.present() | ||
} | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
val injector = (application as? QuranApplicationComponentProvider) | ||
?.provideQuranApplicationComponent() as? AuthComponentInterface | ||
injector?.authComponentFactory()?.generate()?.inject(this) | ||
|
||
setContent { | ||
QuranTheme { | ||
val authenticationState = authFlow.collectAsState() | ||
|
||
Scaffold(topBar = { | ||
TopAppBar( | ||
title = { Text(stringResource(R.string.sync_with_quran_com)) }, | ||
navigationIcon = { | ||
IconButton(onClick = { finish() }) { | ||
Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = null) | ||
} | ||
} | ||
) | ||
}) { paddingValues -> | ||
LoginScreen(authenticationState.value, Modifier.padding(paddingValues)) | ||
} | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
feature/sync/src/main/kotlin/com/quran/mobile/feature/sync/auth/AuthConstants.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,11 @@ | ||
package com.quran.mobile.feature.sync.auth | ||
|
||
import androidx.datastore.preferences.core.stringPreferencesKey | ||
|
||
object AuthConstants { | ||
val authPreference = stringPreferencesKey(Keys.AUTH_STATE_PREFERENCE_KEY) | ||
|
||
private object Keys { | ||
const val AUTH_STATE_PREFERENCE_KEY = "authState" | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
feature/sync/src/main/kotlin/com/quran/mobile/feature/sync/auth/AuthStateManager.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.quran.mobile.feature.sync.auth | ||
|
||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import com.quran.mobile.feature.sync.di.AuthModule | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.map | ||
import javax.inject.Inject | ||
import javax.inject.Named | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class AuthStateManager @Inject constructor( | ||
@Named(AuthModule.AUTH_DATASTORE) private val dataStore: DataStore<Preferences> | ||
) { | ||
|
||
val authState = dataStore.data | ||
.map { preferences -> preferences[AuthConstants.authPreference] } | ||
.distinctUntilChanged() | ||
|
||
suspend fun setAuthState(authState: String) { | ||
dataStore.edit { preferences -> preferences[AuthConstants.authPreference] = authState } | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
feature/sync/src/main/kotlin/com/quran/mobile/feature/sync/di/AuthComponent.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,18 @@ | ||
package com.quran.mobile.feature.sync.di | ||
|
||
import com.quran.data.di.ActivityLevelScope | ||
import com.quran.data.di.ActivityScope | ||
import com.quran.mobile.feature.sync.QuranLoginActivity | ||
import com.squareup.anvil.annotations.MergeSubcomponent | ||
|
||
@ActivityScope | ||
@MergeSubcomponent(ActivityLevelScope::class) | ||
interface AuthComponent { | ||
fun inject(loginActivity: QuranLoginActivity) | ||
|
||
@MergeSubcomponent.Factory | ||
interface Factory { | ||
fun generate(): AuthComponent | ||
} | ||
} | ||
|
9 changes: 9 additions & 0 deletions
9
feature/sync/src/main/kotlin/com/quran/mobile/feature/sync/di/AuthComponentInterface.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,9 @@ | ||
package com.quran.mobile.feature.sync.di | ||
|
||
import com.quran.data.di.AppScope | ||
import com.squareup.anvil.annotations.ContributesTo | ||
|
||
@ContributesTo(AppScope::class) | ||
interface AuthComponentInterface { | ||
fun authComponentFactory(): AuthComponent.Factory | ||
} |
25 changes: 25 additions & 0 deletions
25
feature/sync/src/main/kotlin/com/quran/mobile/feature/sync/di/AuthModule.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.quran.mobile.feature.sync.di | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.preferencesDataStore | ||
import com.quran.data.di.AppScope | ||
import com.quran.mobile.di.qualifier.ApplicationContext | ||
import com.squareup.anvil.annotations.ContributesTo | ||
import dagger.Module | ||
import dagger.Provides | ||
import javax.inject.Named | ||
|
||
@Module | ||
@ContributesTo(AppScope::class) | ||
object AuthModule { | ||
const val AUTH_DATASTORE = "auth_datastore" | ||
private const val PREFERENCES_STORE = "auth_prefs" | ||
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = PREFERENCES_STORE) | ||
|
||
@Named(AUTH_DATASTORE) | ||
@Provides | ||
fun provideAuthDataStore(@ApplicationContext appContext: Context): DataStore<Preferences> = | ||
appContext.dataStore | ||
} |
45 changes: 45 additions & 0 deletions
45
feature/sync/src/main/kotlin/com/quran/mobile/feature/sync/presenter/LoginState.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,45 @@ | ||
package com.quran.mobile.feature.sync.presenter | ||
|
||
import android.content.Intent | ||
import net.openid.appauth.AuthorizationResponse | ||
|
||
sealed class LoginState { | ||
data class LoggedIn( | ||
val name: String, | ||
val email: String, | ||
val eventHandler: (LoginEvent) -> Unit | ||
) : LoginState() | ||
|
||
data class LoggedOut( | ||
val isAuthenticating: Boolean, | ||
val eventHandler: (LoginEvent) -> Unit | ||
) : LoginState() | ||
|
||
data object LoggingIn : LoginState() | ||
|
||
data class Authenticating( | ||
val intent: Intent?, | ||
val eventHandler: (LoginEvent) -> Unit | ||
) : LoginState() | ||
|
||
data class LoggingOut( | ||
val intent: Intent, | ||
val eventHandler: (LoginEvent) -> Unit | ||
) : LoginState() | ||
} | ||
|
||
sealed class LoginEvent { | ||
data object Login : LoginEvent() | ||
data object Logout : LoginEvent() | ||
data object CancelLogin : LoginEvent() | ||
data object CancelLogout : LoginEvent() | ||
data class OnAuthenticationResult( | ||
val response: AuthorizationResponse?, | ||
val exception: Exception? | ||
) : LoginEvent() | ||
|
||
data class OnLogoutResult( | ||
val response: AuthorizationResponse?, | ||
val exception: Exception? | ||
) : LoginEvent() | ||
} |
Oops, something went wrong.