Skip to content

Commit

Permalink
fix: google login
Browse files Browse the repository at this point in the history
  • Loading branch information
EndikaCo committed Feb 25, 2024
1 parent 46ab51b commit 022387a
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 36 deletions.
6 changes: 4 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ dependencies {
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
implementation("androidx.compose.material3:material3-android:1.2.0")
implementation("com.google.firebase:firebase-auth:22.3.1")
implementation("com.google.android.gms:play-services-auth:21.0.0")

//Testing

Expand Down Expand Up @@ -113,7 +115,7 @@ dependencies {
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.7.0")

//observeAsState (liveData)
implementation("androidx.compose.runtime:runtime-livedata:1.6.1")
implementation("androidx.compose.runtime:runtime-livedata:1.6.2")

//dagger hilt
implementation("com.google.dagger:hilt-android:2.50")
Expand All @@ -135,5 +137,5 @@ dependencies {
implementation ("com.google.code.gson:gson:2.10.1")

//extended icons
implementation("androidx.compose.material:material-icons-extended:1.6.1")
implementation("androidx.compose.material:material-icons-extended:1.6.2")
}
1 change: 0 additions & 1 deletion app/src/main/java/com/endcodev/myinvoice/MyInvoiceApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.endcodev.myinvoice
import android.app.Application
import dagger.hilt.android.HiltAndroidApp


@HiltAndroidApp
class MyInvoiceApp : Application(){
override fun onCreate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.endcodev.myinvoice.data.database.entities.CustomersEntity
import com.endcodev.myinvoice.data.database.entities.ProductEntity
import com.endcodev.myinvoice.data.database.entities.SaleEntity
import com.endcodev.myinvoice.domain.models.invoice.SaleItem
import com.endcodev.myinvoice.domain.models.product.Product
import com.google.gson.Gson

@ProvidedTypeConverter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.endcodev.myinvoice.data.network
package com.endcodev.myinvoice.data.network.auth

import android.util.Log
import com.endcodev.myinvoice.domain.utils.App
Expand Down Expand Up @@ -59,4 +59,8 @@ class AuthenticationService @Inject constructor(
}
}
}

fun disconnectUser() {
Firebase.auth.signOut()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.endcodev.myinvoice.data.network
package com.endcodev.myinvoice.data.network.auth

import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.database.ktx.database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.endcodev.myinvoice.presentation
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
Expand All @@ -13,8 +14,12 @@ import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

val viewModel by viewModels<MainActivityViewModel>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContent {
MyInvoiceTheme {
Surface(
Expand All @@ -26,4 +31,9 @@ class MainActivity : ComponentActivity() {
}
}
}

override fun onStop() {
super.onStop()
viewModel.disconnectUser()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.endcodev.myinvoice.presentation

import android.util.Patterns
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.endcodev.myinvoice.R
import com.endcodev.myinvoice.data.network.auth.AuthenticationService
import com.endcodev.myinvoice.data.network.auth.FirebaseClient
import com.endcodev.myinvoice.presentation.compose.components.UiText
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class MainActivityViewModel @Inject constructor(
private val auth: AuthenticationService,
firebaseClient: FirebaseClient
) : ViewModel() {

fun disconnectUser() {
auth.disconnectUser()
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package com.endcodev.myinvoice.presentation.compose.screens.auth

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.util.Log
import android.widget.Toast
import androidx.activity.compose.ManagedActivityResultLauncher
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
Expand All @@ -22,8 +29,8 @@ import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Divider
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
Expand All @@ -42,6 +49,7 @@ import androidx.compose.ui.Alignment.Companion.CenterHorizontally
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
Expand All @@ -55,10 +63,17 @@ import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.endcodev.myinvoice.R
import com.endcodev.myinvoice.domain.utils.App
import com.endcodev.myinvoice.presentation.navigation.AuthScreen
import com.endcodev.myinvoice.presentation.navigation.Graph
import com.endcodev.myinvoice.presentation.theme.MyInvoiceTheme
import com.endcodev.myinvoice.presentation.viewmodels.LoginViewModel
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.GoogleAuthProvider

@Composable
fun LoginActions(navController: NavHostController) {
Expand All @@ -67,8 +82,21 @@ fun LoginActions(navController: NavHostController) {
val email by viewModel.email.observeAsState(initial = "")
val password by viewModel.password.observeAsState(initial = "")
val isLoginEnabled by viewModel.isLoginEnabled.observeAsState(initial = false)
val userLogged by viewModel.userLogged.observeAsState(initial = false)

val launcher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartActivityForResult(),
onResult = { gLoginInit(it) }
)
val context = LocalContext.current

LaunchedEffect(key1 = userLogged) {
if (userLogged) {
navController.popBackStack() // clear nav history
navController.navigate(Graph.HOME)
}
}

LaunchedEffect(key1 = viewModel) {
viewModel.errors.collect { error ->
error.asString(context)
Expand All @@ -83,8 +111,6 @@ fun LoginActions(navController: NavHostController) {
isLoginEnables = isLoginEnabled,
onLoginClick = {
viewModel.login()
navController.popBackStack() // clear nav history
navController.navigate(Graph.HOME)
},
onSignUpClick = {
navController.navigate(AuthScreen.SignUp.route)
Expand All @@ -98,10 +124,60 @@ fun LoginActions(navController: NavHostController) {
onPassChanged = {
viewModel.onLoginChanged(password = it, email = email)
},
onExitClick = { activity.finish() }
onExitClick = { activity.finish() },
onGoogleLoginClick = {
googleLogin(context, launcher)
}
)
}

private fun gLoginInit(result: ActivityResult?) {

if (result != null) {

if (result.resultCode == Activity.RESULT_OK) {

val task = GoogleSignIn.getSignedInAccountFromIntent(result.data)

try {
val account = task.getResult(ApiException::class.java)
if (account != null)
gLogin(account)
} catch (e: ApiException) {
Log.e(App.tag, "gLoginInit: error", )
}
}
}
//binding.progress.visibility = View.GONE
}

private fun gLogin(account: GoogleSignInAccount) {
val credential = GoogleAuthProvider.getCredential(account.idToken, null)
val auth = FirebaseAuth.getInstance()
auth.signInWithCredential(credential)
.addOnCompleteListener {
if (it.isSuccessful) {
Log.v(App.tag, "gLogin: Login success", )
} else {
Log.v(App.tag, "gLogin: Login fail", )
}
}
}

private fun googleLogin(
context: Context,
launcher: ManagedActivityResultLauncher<Intent, ActivityResult>
) {
val googleConf = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(context.getString(R.string.default_web_client_id))
.requestEmail()
.build()

val googleClient = GoogleSignIn.getClient(context, googleConf)
val signInIntent = Intent(googleClient.signInIntent)
launcher.launch(signInIntent)
}

@Composable
fun LoginScreen(
email: String,
Expand All @@ -112,7 +188,8 @@ fun LoginScreen(
onForgotClick: () -> Unit,
onEmailChanged: (String) -> Unit,
onPassChanged: (String) -> Unit,
onExitClick: () -> Unit
onExitClick: () -> Unit,
onGoogleLoginClick: () -> Unit
) {
Scaffold(
topBar = { LoginHeader(onExitClick) },
Expand All @@ -124,7 +201,8 @@ fun LoginScreen(
onLoginClick,
onForgotClick,
onEmailChanged,
onPassChanged
onPassChanged,
onGoogleLoginClick
)
},
bottomBar = { LoginFooter(onSignUpClick) }
Expand Down Expand Up @@ -153,7 +231,8 @@ fun LoginBody(
onLoginClick: () -> Unit,
onForgotClick: () -> Unit,
onLoginChanged: (String) -> Unit,
onPassChanged: (String) -> Unit
onPassChanged: (String) -> Unit,
onGoogleLoginClick: () -> Unit
) {
Column(
modifier = Modifier
Expand All @@ -174,16 +253,16 @@ fun LoginBody(
Spacer(modifier = Modifier.size(16.dp))
OrDivider(Modifier.align(CenterHorizontally))
Spacer(modifier = Modifier.size(16.dp))
SocialLogin(Modifier.align(CenterHorizontally))
SocialLogin(Modifier.align(CenterHorizontally), onGoogleLoginClick)
}
}

@Composable
fun LoginFooter(onSignUpClick: () -> Unit) {
Column(modifier = Modifier.fillMaxWidth()) {
Divider(
HorizontalDivider(
modifier = Modifier
.background(Color(R.color.transparent))
.background(colorResource(id = R.color.transparent))
.height(1.dp)
.fillMaxWidth()
)
Expand Down Expand Up @@ -293,10 +372,11 @@ fun OrDivider(modifier: Modifier) {
}

@Composable
fun SocialLogin(modifier: Modifier) {
fun SocialLogin(modifier: Modifier, onGoogleLoginClick: () -> Unit) {
Row(
modifier = modifier
.height(36.dp)
.clickable { onGoogleLoginClick() }
.border(
BorderStroke(width = 1.dp, color = MaterialTheme.colorScheme.onBackground),
shape = RoundedCornerShape(16.dp)
Expand Down Expand Up @@ -355,7 +435,8 @@ fun LoginScreenPreview() {
onForgotClick = {},
onEmailChanged = {},
onPassChanged = {},
onExitClick = {}
onExitClick = {},
onGoogleLoginClick = {}
)
}
}
Expand All @@ -374,7 +455,8 @@ fun LoginScreenPreview2() {
onForgotClick = {},
onEmailChanged = {},
onPassChanged = {},
onExitClick = {}
onExitClick = {},
onGoogleLoginClick = {}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ fun SignUpBody(
Spacer(modifier = Modifier.size(16.dp))
OrDivider(Modifier.align(CenterHorizontally))
Spacer(modifier = Modifier.size(16.dp))
SocialLogin(Modifier.align(CenterHorizontally))
SocialLogin(Modifier.align(CenterHorizontally), onGoogleLoginClick = {})
}
}

Expand Down Expand Up @@ -232,4 +232,28 @@ fun SignUpPreview() {
onVerifyChanged = {},
)
}
}

@Preview(name = "Light Mode")
@Preview(name = "Dark Mode", uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
fun SignUpPreview2() {
MyInvoiceTheme {

val success = false
val isLoading = false

SignUpScreen(
onSignUpClick = {},
success,
isLoading,
"Email",
"Password",
"Repeat password",
false,
onEmailChanged = {},
onPassChanged = {},
onVerifyChanged = {},
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ fun NavigationBar(navController: NavHostController) {
* @param index The index of the NavBarItem in the list of navigation bar items.
* @param selectedItemIndex The index of the currently selected NavBarItem in the navigation bar.
*/
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ItemBadge(item: NavBarItem, index: Int, selectedItemIndex: Int) {
BadgedBox(
Expand Down
Loading

0 comments on commit 022387a

Please sign in to comment.