Skip to content

Commit

Permalink
feat: login with Google, Facebook, Microsoft
Browse files Browse the repository at this point in the history
  • Loading branch information
k1rill committed Dec 4, 2023
1 parent 14eec0d commit 6f73afc
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 36 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/org/openedx/app/di/ScreenModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,4 @@ val screenModule = module {
}

viewModel { WhatsNewViewModel(get()) }
}
}
2 changes: 1 addition & 1 deletion auth/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ dependencies {
testImplementation "io.mockk:mockk-android:$mockk_version"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version"
testImplementation "androidx.arch.core:core-testing:$android_arch_version"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package org.openedx.auth.presentation.signin
*/
internal data class SignInUIState(
val shouldShowSocialLogin: Boolean = false,
val isLogistrationEnabled: Boolean = false,
val showProgress: Boolean = false,
val loginSuccess: Boolean = false,
)
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import org.openedx.core.utils.Logger
import org.openedx.core.R as CoreRes

class SignInViewModel(
private val config: Config,
private val interactor: AuthInteractor,
private val resourceManager: ResourceManager,
private val preferencesManager: CorePreferences,
Expand All @@ -46,12 +45,13 @@ class SignInViewModel(
config: Config,
) : BaseViewModel() {

val isLogistrationEnabled get() = config.isPreLoginExperienceEnabled()

private val logger = Logger("SignInViewModel")

private val _uiState = MutableStateFlow(
SignInUIState(shouldShowSocialLogin = config.isSocialAuthEnabled())
SignInUIState(
shouldShowSocialLogin = config.isSocialAuthEnabled(),
isLogistrationEnabled = config.isPreLoginExperienceEnabled(),
)
)
internal val uiState: StateFlow<SignInUIState> = _uiState

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openedx.auth.presentation.signin.compose

import android.content.res.Configuration
import android.content.res.Configuration.UI_MODE_NIGHT_NO
import android.content.res.Configuration.UI_MODE_NIGHT_YES
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
Expand All @@ -13,6 +14,7 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.rememberScrollState
Expand Down Expand Up @@ -55,6 +57,7 @@ import org.openedx.auth.presentation.signin.AuthEvent
import org.openedx.auth.presentation.signin.SignInUIState
import org.openedx.auth.presentation.ui.LoginTextField
import org.openedx.core.UIMessage
import org.openedx.core.ui.BackBtn
import org.openedx.core.ui.HandleUIMessage
import org.openedx.core.ui.OpenEdXButton
import org.openedx.core.ui.OpenEdXOutlinedButton
Expand Down Expand Up @@ -121,7 +124,21 @@ internal fun LoginScreen(
uiMessage = uiMessage,
scaffoldState = scaffoldState
)

if (state.isLogistrationEnabled) {
Box(
modifier = Modifier
.statusBarsPadding()
.fillMaxWidth(),
contentAlignment = Alignment.CenterStart
) {
BackBtn(
modifier = Modifier.padding(end = 16.dp),
tint = Color.White
) {
onEvent(AuthEvent.BackClick)
}
}
}
Column(
Modifier.padding(it),
horizontalAlignment = Alignment.CenterHorizontally
Expand Down Expand Up @@ -213,14 +230,16 @@ private fun AuthForm(
.fillMaxWidth()
.padding(top = 20.dp, bottom = 36.dp)
) {
Text(
modifier = Modifier.noRippleClickable {
onEvent(AuthEvent.RegisterClick)
},
text = stringResource(id = R.string.auth_register),
color = MaterialTheme.appColors.primary,
style = MaterialTheme.appTypography.labelLarge
)
if (state.isLogistrationEnabled.not()) {
Text(
modifier = Modifier.noRippleClickable {
onEvent(AuthEvent.RegisterClick)
},
text = stringResource(id = R.string.auth_register),
color = MaterialTheme.appColors.primary,
style = MaterialTheme.appTypography.labelLarge
)
}
Spacer(modifier = Modifier.weight(1f))
Text(
modifier = Modifier.noRippleClickable {
Expand Down Expand Up @@ -371,10 +390,10 @@ private fun PasswordTextField(
)
}

@Preview(uiMode = Configuration.UI_MODE_NIGHT_NO)
@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES)
@Preview(name = "NEXUS_5_Light", device = Devices.NEXUS_5, uiMode = Configuration.UI_MODE_NIGHT_NO)
@Preview(name = "NEXUS_5_Dark", device = Devices.NEXUS_5, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Preview(uiMode = UI_MODE_NIGHT_NO)
@Preview(uiMode = UI_MODE_NIGHT_YES)
@Preview(name = "NEXUS_5_Light", device = Devices.NEXUS_5, uiMode = UI_MODE_NIGHT_NO)
@Preview(name = "NEXUS_5_Dark", device = Devices.NEXUS_5, uiMode = UI_MODE_NIGHT_YES)
@Composable
private fun SignInScreenPreview() {
OpenEdXTheme {
Expand All @@ -388,8 +407,8 @@ private fun SignInScreenPreview() {
}


@Preview(name = "NEXUS_9_Light", device = Devices.NEXUS_9, uiMode = Configuration.UI_MODE_NIGHT_NO)
@Preview(name = "NEXUS_9_Night", device = Devices.NEXUS_9, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Preview(name = "NEXUS_9_Light", device = Devices.NEXUS_9, uiMode = UI_MODE_NIGHT_NO)
@Preview(name = "NEXUS_9_Night", device = Devices.NEXUS_9, uiMode = UI_MODE_NIGHT_YES)
@Composable
private fun SignInScreenTabletPreview() {
OpenEdXTheme {
Expand Down
6 changes: 3 additions & 3 deletions auth/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<string name="auth_example_email" translatable="false">username@domain.com</string>
<string name="auth_enter_password">Enter password</string>
<string name="auth_create_new_account">Create new account.</string>
<string name="auth_google">Sign in with Google</string>
<string name="auth_facebook">Sign in with Facebook</string>
<string name="auth_microsoft">Sign in with Microsoft</string>
<string name="auth_google" tools:ignore="ExtraTranslation">Sign in with Google</string>
<string name="auth_facebook" tools:ignore="ExtraTranslation">Sign in with Facebook</string>
<string name="auth_microsoft" tools:ignore="ExtraTranslation">Sign in with Microsoft</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ class SignInViewModelTest {
appUpgradeNotifier = appUpgradeNotifier,
facebookAuthHelper = facebookAuthHelper,
googleAuthHelper = googleAuthHelper,
microsoftAuthHelper = microsoftAuthHelper
microsoftAuthHelper = microsoftAuthHelper,
config = config,
)
viewModel.login("", "")
coVerify(exactly = 0) { interactor.login(any(), any()) }
Expand All @@ -123,7 +124,8 @@ class SignInViewModelTest {
appUpgradeNotifier = appUpgradeNotifier,
facebookAuthHelper = facebookAuthHelper,
googleAuthHelper = googleAuthHelper,
microsoftAuthHelper = microsoftAuthHelper
microsoftAuthHelper = microsoftAuthHelper,
config = config,
)
viewModel.login("acc@test.o", "")
coVerify(exactly = 0) { interactor.login(any(), any()) }
Expand Down Expand Up @@ -152,7 +154,8 @@ class SignInViewModelTest {
appUpgradeNotifier = appUpgradeNotifier,
facebookAuthHelper = facebookAuthHelper,
googleAuthHelper = googleAuthHelper,
microsoftAuthHelper = microsoftAuthHelper
microsoftAuthHelper = microsoftAuthHelper,
config = config,
)
viewModel.login("acc@test.org", "")

Expand Down Expand Up @@ -180,7 +183,8 @@ class SignInViewModelTest {
appUpgradeNotifier = appUpgradeNotifier,
facebookAuthHelper = facebookAuthHelper,
googleAuthHelper = googleAuthHelper,
microsoftAuthHelper = microsoftAuthHelper
microsoftAuthHelper = microsoftAuthHelper,
config = config,
)
viewModel.login("acc@test.org", "ed")

Expand Down Expand Up @@ -210,7 +214,8 @@ class SignInViewModelTest {
appUpgradeNotifier = appUpgradeNotifier,
facebookAuthHelper = facebookAuthHelper,
googleAuthHelper = googleAuthHelper,
microsoftAuthHelper = microsoftAuthHelper
microsoftAuthHelper = microsoftAuthHelper,
config = config,
)
coEvery { interactor.login("acc@test.org", "edx") } returns Unit
viewModel.login("acc@test.org", "edx")
Expand Down Expand Up @@ -241,7 +246,8 @@ class SignInViewModelTest {
appUpgradeNotifier = appUpgradeNotifier,
facebookAuthHelper = facebookAuthHelper,
googleAuthHelper = googleAuthHelper,
microsoftAuthHelper = microsoftAuthHelper
microsoftAuthHelper = microsoftAuthHelper,
config = config,
)
coEvery { interactor.login("acc@test.org", "edx") } throws UnknownHostException()
viewModel.login("acc@test.org", "edx")
Expand Down Expand Up @@ -273,7 +279,8 @@ class SignInViewModelTest {
appUpgradeNotifier = appUpgradeNotifier,
facebookAuthHelper = facebookAuthHelper,
googleAuthHelper = googleAuthHelper,
microsoftAuthHelper = microsoftAuthHelper
microsoftAuthHelper = microsoftAuthHelper,
config = config,
)
coEvery { interactor.login("acc@test.org", "edx") } throws EdxError.InvalidGrantException()
viewModel.login("acc@test.org", "edx")
Expand Down Expand Up @@ -305,7 +312,8 @@ class SignInViewModelTest {
appUpgradeNotifier = appUpgradeNotifier,
facebookAuthHelper = facebookAuthHelper,
googleAuthHelper = googleAuthHelper,
microsoftAuthHelper = microsoftAuthHelper
microsoftAuthHelper = microsoftAuthHelper,
config = config,
)
coEvery { interactor.login("acc@test.org", "edx") } throws IllegalStateException()
viewModel.login("acc@test.org", "edx")
Expand Down
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,7 @@ def getCurrentFlavor() {
return ""
}
}

task generateMockedRawFile() {
doLast { configHelper.generateMicrosoftConfig() }
}
4 changes: 2 additions & 2 deletions buildSrc/src/main/groovy/org/edx/builder/ConfigHelper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ class ConfigHelper {
jsonWriter.withWriter {
builder.writeTo(it)
}
generateMicrosoftConfig(config)
}

private def generateMicrosoftConfig(config) {
def generateMicrosoftConfig() {
def config = fetchConfig()
def social = config.get("SOCIAL")
def applicationId = config.getOrDefault("APPLICATION_ID", "")
def clientId = social.getOrDefault("MICROSOFT_CLIENT_ID", "")
Expand Down
1 change: 1 addition & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ dependencies {
def insertBuildConfigFields(currentFlavour, buildType) {
if (currentFlavour == buildType.name) {
configHelper.generateConfigJson()
configHelper.generateMicrosoftConfig()
}
def config = configHelper.fetchConfig()
def platformName = config.getOrDefault("PLATFORM_NAME", "")
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@
android:name="com.facebook.sdk.ClientToken"
android:value="@string/facebook_client_token" />
</application>
</manifest>
</manifest>

0 comments on commit 6f73afc

Please sign in to comment.