Skip to content

Commit

Permalink
🔀 Merge pull request #10 from snuhcs-course/refactor/auth-frontend
Browse files Browse the repository at this point in the history
Refactor/auth frontend
  • Loading branch information
sukchan-0811 authored Oct 26, 2023
2 parents 05b21b1 + 8174570 commit ed24da1
Show file tree
Hide file tree
Showing 23 changed files with 826 additions and 399 deletions.
3 changes: 3 additions & 0 deletions frontend/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ dependencies {
implementation("androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion")

// LiveData
implementation("androidx.compose.runtime:runtime-livedata:1.5.4")

// Coroutines
val coroutinesVersion = "1.7.1"
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.core.view.WindowCompat
import com.example.speechbuddy.MainApplication.Companion.token_prefs
import com.example.speechbuddy.compose.SpeechBuddyApp
import com.example.speechbuddy.domain.utils.TokenSharedPreferences
import com.example.speechbuddy.ui.SpeechBuddyTheme
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
token_prefs = TokenSharedPreferences(applicationContext)
super.onCreate(savedInstanceState)

// Displaying edge-to-edge
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.example.speechbuddy

import android.app.Application
import com.example.speechbuddy.domain.utils.TokenSharedPreferences
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class MainApplication : Application()
class MainApplication : Application(){
companion object{
lateinit var token_prefs : TokenSharedPreferences
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.speechbuddy.compose.landing.LandingScreen
import com.example.speechbuddy.compose.login.LoginScreen
import com.example.speechbuddy.compose.resetpassword.EmailVerificationScreen
import com.example.speechbuddy.compose.emailverification.EmailVerificationScreen
import com.example.speechbuddy.compose.resetpassword.ResetPasswordScreen
import com.example.speechbuddy.compose.signup.SignupScreen

Expand Down Expand Up @@ -38,32 +38,30 @@ fun SpeechBuddyNavHost(
navController.navigateUp()
},
onResetPasswordClick = {
navController.navigate("email_verification/password")
navController.navigate("email_verification/reset_password")
},
onSignupClick = {
navController.navigate("signup")
navController.navigate("email_verification/signup")
}
)
}
composable("signup") {
SignupScreen(
composable("email_verification/{source}") { backStackEntry ->
val source = backStackEntry.arguments?.getString("source")
EmailVerificationScreen(
source = source,
onBackClick = {
navController.navigateUp()
},
onSignupClick = {},
email = ""
navController = navController,
)
}
composable("email_verification/password") {
EmailVerificationScreen(
composable("signup/{emailInput}") {backStackEntry ->
val emailInput = backStackEntry.arguments?.getString("emailInput")
SignupScreen(
onBackClick = {
navController.navigateUp()
},
onSubmitClick = {
},
onNextClick = {
navController.navigate("reset_password")
}
email = emailInput ?: ""
)
}
composable("reset_password") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.example.speechbuddy.compose.emailverification

import android.annotation.SuppressLint
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.example.speechbuddy.R
import com.example.speechbuddy.compose.utils.ButtonLevel
import com.example.speechbuddy.compose.utils.ButtonUi
import com.example.speechbuddy.compose.utils.TextFieldUi
import com.example.speechbuddy.compose.utils.TitleUi
import com.example.speechbuddy.compose.utils.TopAppBarUi
import com.example.speechbuddy.ui.SpeechBuddyTheme
import com.example.speechbuddy.ui.models.EmailVerificationErrorType
import com.example.speechbuddy.viewmodel.EmailVerificationViewModel

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun EmailVerificationScreen(
source: String?,
modifier: Modifier = Modifier,
onBackClick: () -> Unit,
navController: NavHostController,
viewModel: EmailVerificationViewModel = hiltViewModel()
) {
val uiState by viewModel.uiState.collectAsState()
val isEmailError = uiState.error?.type == EmailVerificationErrorType.EMAIL
val isVerifyCodeError = uiState.error?.type == EmailVerificationErrorType.VERIFY_CODE
val isError = isEmailError || isVerifyCodeError

Surface(
modifier = modifier.fillMaxSize()
) {
Scaffold(
topBar = { TopAppBarUi(onBackClick = onBackClick) }
) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(horizontal = 24.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
TitleUi(
title = when (source) {
"reset_password" -> stringResource(id = R.string.reset_password_title)
"signup" -> stringResource(id = R.string.verify_email_field)
else -> stringResource(id = R.string.verify_email_field)
},
description = when (source) {
"reset_password" -> stringResource(id = R.string.reset_password_subtitle1)
"signup" -> stringResource(id = R.string.verify_email_explain)
else -> stringResource(id = R.string.verify_email_explain_default)
}
)

Spacer(modifier = Modifier.height(15.dp))

// Email Text Field
TextFieldUi(
value = viewModel.emailInput,
onValueChange = { viewModel.setEmail(it) },
label = { Text(text = stringResource(id = R.string.email_field)) },
supportingButton = {
ButtonUi(
text = stringResource(id = R.string.send_validation_code),
onClick = { viewModel.verifySend(source) },
level = ButtonLevel.TERTIARY
)
},
supportingText = {
if (isEmailError) {
Text(stringResource(id = uiState.error!!.messageId))
} else if(uiState.isSuccessfulSend) {
Text(stringResource(id = R.string.verification_code_sent))
}
},
isError = isError,
isValid = uiState.isValidEmail,
// If verification email is sent successfully, a user cannot change one's email input
isEnabled = !uiState.isSuccessfulSend
)

// Verify code Text Field
TextFieldUi(
value = viewModel.verifyCodeInput,
onValueChange = { viewModel.setVerifyCode(it) },
label = { Text(text = stringResource(id = R.string.validation_code)) },
supportingText = {
if (isVerifyCodeError) {
Text(stringResource(id = uiState.error!!.messageId))
}
},
isError = isError,
isValid = uiState.isValidVerifyCode,
isEnabled = uiState.isSuccessfulSend
)

Spacer(modifier = Modifier.height(15.dp))

ButtonUi(
text = stringResource(id = R.string.next),
onClick = { viewModel.verifyAccept(source, navController) },
isEnabled = uiState.isSuccessfulSend
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ fun LoginScreen(
// Login Button
ButtonUi(
text = stringResource(id = R.string.login_text),
onClick = { viewModel.login() },
onClick = {
viewModel.login()
},
isEnabled = !isError,
isError = isError
)
Expand All @@ -105,7 +107,7 @@ fun LoginScreen(

// Forgot Password Button
ButtonUi(
text = stringResource(id = R.string.forgot_passowrd),
text = stringResource(id = R.string.forgot_password),
onClick = onResetPasswordClick,
isError = isError,
level = ButtonLevel.SECONDARY
Expand All @@ -120,4 +122,5 @@ fun LoginScreen(
}
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fun ResetPasswordScreen(
horizontalAlignment = Alignment.CenterHorizontally
) {
TitleUi(
title = stringResource(id = R.string.reset_passoword_title),
title = stringResource(id = R.string.reset_password_title),
description = stringResource(id = R.string.reset_password_subtitle2)
)

Expand Down
Loading

0 comments on commit ed24da1

Please sign in to comment.