Skip to content

Commit

Permalink
login screen small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
osik2000 committed Jan 13, 2024
1 parent 32169f6 commit 09a40c1
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 291 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,14 @@ package pl.pawelosinski.skatefreak.service

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
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.SignInButton
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.tasks.Task
import com.google.firebase.FirebaseException
Expand All @@ -46,22 +23,11 @@ import com.google.firebase.auth.PhoneAuthOptions
import com.google.firebase.auth.PhoneAuthProvider
import pl.pawelosinski.skatefreak.R
import pl.pawelosinski.skatefreak.auth.PhoneAuthUserData
import pl.pawelosinski.skatefreak.local.isDarkMode
import pl.pawelosinski.skatefreak.local.loggedUser
import pl.pawelosinski.skatefreak.model.User
import pl.pawelosinski.skatefreak.model.User.Companion.ACCOUNT_TYPE_GOOGLE
import pl.pawelosinski.skatefreak.model.User.Companion.ACCOUNT_TYPE_PHONE
import pl.pawelosinski.skatefreak.service.FirebaseAuthService.Companion.PHONE_TAG
import pl.pawelosinski.skatefreak.service.FirebaseAuthService.Companion.isUserDataSet
import pl.pawelosinski.skatefreak.service.FirebaseAuthService.Companion.isUserLoggedIn
import pl.pawelosinski.skatefreak.service.FirebaseAuthService.Companion.phoneAuthUserData
import pl.pawelosinski.skatefreak.service.FirebaseAuthService.Companion.resendToken
import pl.pawelosinski.skatefreak.service.FirebaseAuthService.Companion.storedVerificationId
import pl.pawelosinski.skatefreak.ui.auth.UserSetDataActivity
import pl.pawelosinski.skatefreak.ui.common.myCommonModifier
import pl.pawelosinski.skatefreak.ui.common.myToast
import pl.pawelosinski.skatefreak.ui.menu.MainMenuActivity
import pl.pawelosinski.skatefreak.ui.theme.SkateFreakTheme
import java.util.concurrent.TimeUnit

class FirebaseAuthService(val activity: ComponentActivity) {
Expand Down Expand Up @@ -394,251 +360,6 @@ class FirebaseAuthService(val activity: ComponentActivity) {
}
}

@Composable
fun LoginScreen(firebaseAuthService: FirebaseAuthService) {
val isUserDataSet by remember {
mutableStateOf(isUserDataSet)
}

val isUserLoggedIn by remember {
mutableStateOf(isUserLoggedIn)
}

// val isAuthInProgress by remember {
// mutableStateOf(phoneAuthUserData.value.isAuthInProgress.value)
// }
// var verificationCode by remember { mutableStateOf("") }

SkateFreakTheme(darkTheme = isDarkMode) {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background,

) {
Column(
modifier = Modifier
.padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
if (isUserLoggedIn.value && loggedUser.value.firebaseId.isNotEmpty()) {
databaseService.setLoggedUserById(loggedUser.value.firebaseId)
Log.d("LoginActivity", "Before data check: isUserDataSet: $isUserDataSet")
if (!isUserDataSet.value) {
isUserDataSet.value = loggedUser.value.checkRequiredData()
Log.d("LoginActivity", "After data check: isUserDataSet: $isUserDataSet")
}

if (!isUserDataSet.value) {
Log.d("LoginActivity", "isUserDataSet: ${isUserDataSet.value}")
Text(
text = "Aby kontynuować, proszę uzupełnić dane profilu",
modifier = Modifier.padding(bottom = 16.dp)
)
} else if (loggedUser.value.name.isNotEmpty()) {
Text(
text = "Witaj ${loggedUser.value.name}",
modifier = Modifier.padding(bottom = 16.dp)
)
} else {
Text(
text = "Witaj ${loggedUser.value.phoneNumber}",
modifier = Modifier.padding(bottom = 16.dp)
)
}

if (isUserDataSet.value) {
// Button to go to LoggedUserMenuActivity
MainMenuButton(firebaseAuthService)
} else {
// Button to go to UserDataActivity
Button(
onClick = {
phoneAuthUserData.value = PhoneAuthUserData()
val intent = Intent(
firebaseAuthService.currentActivity.value,
UserSetDataActivity::class.java
)
firebaseAuthService.currentActivity.value.startActivity(intent)
firebaseAuthService.currentActivity.value.finish()
},
modifier = myCommonModifier,
shape = MaterialTheme.shapes.small
) {
Text("Uzupełnij dane użytkownika")
}
}
SignOutButton(signOut = { firebaseAuthService.signOut() })
} else {
Text(
text = "Zaloguj się",
modifier = Modifier.padding(bottom = 16.dp)
)
PhoneLoginForm(firebaseAuthService)
GoogleSignInButton(firebaseAuthService)
}
}
}
}

}


@Composable
fun MainMenuButton(firebaseAuthService: FirebaseAuthService) {
val context = LocalContext.current
Button(
onClick = {
val intent = Intent(context, MainMenuActivity::class.java)
firebaseAuthService.currentActivity.value.startActivity(intent)
firebaseAuthService.currentActivity.value.finish()
},
modifier = myCommonModifier,
shape = MaterialTheme.shapes.small
) {
Text("Przejdź do menu głównego")
}
}


@Composable
fun SignOutButton(
signOut: () -> Unit = {
// Configure Google Sign In
}
) {
Button(
modifier = myCommonModifier,
onClick = {
signOut()
},
shape = MaterialTheme.shapes.small
) {
Text(text = "Wyloguj się")
}
}


@Composable
fun GoogleSignInButton(firebaseAuthService: FirebaseAuthService) {
// Reference to the GoogleSignInClient from the activity

AndroidView(
modifier = myCommonModifier
.border(1.dp, MaterialTheme.colorScheme.onSurface, MaterialTheme.shapes.small),
factory = { context ->
SignInButton(context).apply {
setSize(SignInButton.SIZE_WIDE)
}
},
update = { signInButton ->
signInButton.setOnClickListener {
// Start the sign-in process when the Google sign-in button is clicked
firebaseAuthService.googleSignIn()
}
}
)
}


@Composable
fun PhoneLoginForm(firebaseAuthService: FirebaseAuthService) {
// val loginService = LoginService()
val isAuthInProgress by remember {
mutableStateOf(phoneAuthUserData.value.isAuthInProgress.value)
}
val isVerificationCompleted by remember {
mutableStateOf(phoneAuthUserData.value.isVerificationCompleted)
}
val isUserLoggedIn by remember {
mutableStateOf(phoneAuthUserData.value.isUserLoggedIn)
}
val storedVerificationId by remember {
mutableStateOf(storedVerificationId.value)
}
var userPhoneNumber by remember {
if (phoneAuthUserData.value.userPhoneNumber.value.isEmpty()) {
mutableStateOf("+48")
} else {
mutableStateOf(phoneAuthUserData.value.userPhoneNumber.value)
}
}
val resendToken by remember {
mutableStateOf(resendToken)
}
var verificationCode by remember { mutableStateOf("") }
Column(
modifier = Modifier
.padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
// val pattern = remember { Regex("^\\+48\\d\\d\\d\\d\\d\\d\\d\\d\\d$") }
OutlinedTextField(
value = userPhoneNumber,
onValueChange = {
userPhoneNumber = it
},
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
label = { Text("Phone (+48XXXXXXXXX)") },
singleLine = true
)
Log.d(PHONE_TAG, "isAuthInProgress: $isAuthInProgress")
if (isAuthInProgress) {
val pattern = remember { Regex("^\\d?\\d?\\d?\\d?\\d?\\d?$") }
OutlinedTextField(
value = verificationCode,
onValueChange = {
if (it.isEmpty() || it.matches(pattern)) {
verificationCode = it
}
},
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
label = { Text("Kod Weryfikacyjny SMS") },
singleLine = true
)
Button(
onClick = {
firebaseAuthService.resendVerificationCode(userPhoneNumber, resendToken.value)
},
modifier = myCommonModifier,
shape = MaterialTheme.shapes.small
) {
Text("Wyślij ponownie kod weryfikacyjny")
}
}

Button(
onClick = {
if (
!isAuthInProgress &&
!isVerificationCompleted.value &&
!isUserLoggedIn.value
) {
firebaseAuthService.startPhoneNumberVerification(userPhoneNumber)
} else if (!isVerificationCompleted.value) {
if (verificationCode.matches(Regex("^\\d\\d\\d\\d\\d\\d$"))) {
firebaseAuthService.verifyPhoneNumberWithCode(
storedVerificationId,
verificationCode
)
} else {
Toast.makeText(
firebaseAuthService.currentActivity.value,
"Niepoprawny kod weryfikacyjny",
Toast.LENGTH_SHORT
).show()
}
}
},
modifier = myCommonModifier,
shape = MaterialTheme.shapes.small
) {
Text("Login")
}
}
}



Expand Down
Loading

0 comments on commit 09a40c1

Please sign in to comment.