-
Notifications
You must be signed in to change notification settings - Fork 892
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
20 changed files
with
694 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,79 @@ | ||
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) | ||
|
||
// 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> |
152 changes: 152 additions & 0 deletions
152
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,152 @@ | ||
package com.quran.mobile.feature.sync | ||
|
||
import android.content.Intent | ||
import android.os.Bundle | ||
import androidx.activity.compose.rememberLauncherForActivityResult | ||
import androidx.activity.compose.setContent | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.automirrored.filled.ArrowBack | ||
import androidx.compose.material3.CircularProgressIndicator | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.IconButton | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TextButton | ||
import androidx.compose.material3.TopAppBar | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.dp | ||
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.LoginEvent | ||
import com.quran.mobile.feature.sync.presenter.LoginState | ||
import com.quran.mobile.feature.sync.presenter.QuranLoginPresenter | ||
import net.openid.appauth.AuthorizationException | ||
import net.openid.appauth.AuthorizationResponse | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
class QuranLoginActivity : AppCompatActivity() { | ||
@Inject | ||
lateinit var quranLoginPresenter: QuranLoginPresenter | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
val injector = (application as? QuranApplicationComponentProvider) | ||
?.provideQuranApplicationComponent() as? AuthComponentInterface | ||
injector?.authComponentFactory()?.generate()?.inject(this) | ||
|
||
setContent { | ||
QuranTheme { | ||
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(Modifier.padding(paddingValues)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
@Composable | ||
fun LoginScreen(modifier: Modifier = Modifier) { | ||
val authenticationState = quranLoginPresenter.present() | ||
|
||
Column(modifier.padding(16.dp)) { | ||
Text( | ||
stringResource(R.string.sync_with_quran_com_details), | ||
style = MaterialTheme.typography.bodySmall | ||
) | ||
|
||
when (authenticationState) { | ||
is LoginState.LoggedIn -> LoggedIn(authenticationState) | ||
is LoginState.LoggingIn -> CircularProgressIndicator() | ||
is LoginState.LoggingOut -> LoggingOut(authenticationState) | ||
is LoginState.Authenticating -> AuthenticatingState(authenticationState) | ||
is LoginState.LoggedOut -> { | ||
TextButton(onClick = { authenticationState.eventHandler(LoginEvent.Login) }) { | ||
Text(stringResource(R.string.login)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun LoggedIn(state: LoginState.LoggedIn) { | ||
Column { | ||
if (state.name.isNotEmpty()) { | ||
Text(text = state.name) | ||
} | ||
|
||
if (state.email.isNotEmpty()) { | ||
Text(text = state.email) | ||
} | ||
|
||
TextButton(onClick = { state.eventHandler(LoginEvent.Logout) }) { | ||
Text(stringResource(R.string.logout)) | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun AuthenticatingState(state: LoginState.Authenticating) { | ||
val authorizationLauncher = | ||
rememberLauncherForActivityResult(contract = ActivityResultContracts.StartActivityForResult()) { result -> | ||
val data: Intent? = result.data | ||
if (result.resultCode == RESULT_OK && data != null) { | ||
val response = AuthorizationResponse.fromIntent(data) | ||
val exception = AuthorizationException.fromIntent(data) | ||
state.eventHandler(LoginEvent.OnAuthenticationResult(response, exception)) | ||
} else { | ||
Timber.d("Authorization request canceled") | ||
} | ||
} | ||
|
||
val intent = state.intent | ||
LaunchedEffect(intent) { | ||
if (intent != null) { | ||
authorizationLauncher.launch(intent) | ||
} | ||
} | ||
|
||
CircularProgressIndicator() | ||
} | ||
|
||
@Composable | ||
fun LoggingOut(state: LoginState.LoggingOut) { | ||
val authorizationLauncher = | ||
rememberLauncherForActivityResult(contract = ActivityResultContracts.StartActivityForResult()) { result -> | ||
val data: Intent? = result.data | ||
if (result.resultCode == RESULT_OK && data != null) { | ||
val response = AuthorizationResponse.fromIntent(data) | ||
val exception = AuthorizationException.fromIntent(data) | ||
state.eventHandler(LoginEvent.OnLogoutResult(response, exception)) | ||
} else { | ||
Timber.d("Sign out request canceled") | ||
} | ||
} | ||
|
||
LaunchedEffect(state.intent) { | ||
authorizationLauncher.launch(state.intent) | ||
} | ||
|
||
CircularProgressIndicator() | ||
} | ||
} |
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 | ||
} |
Oops, something went wrong.