Skip to content
This repository has been archived by the owner on Jan 7, 2025. It is now read-only.

Authentication UI #17

Merged
merged 10 commits into from
Apr 11, 2024
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

<nav-graph android:value="@navigation/my_nav" />
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.fictadvisor.android.ui

import RegistrationViewModel
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import com.fictadvisor.android.data.dto.*
import com.fictadvisor.android.databinding.FragmentContinueRegistrationBinding
import com.fictadvisor.android.repository.AuthRepository
import com.fictadvisor.android.utils.StorageUtil
import com.fictadvisor.android.validator.RegistrationInputValidator
import com.fictadvisor.android.viewmodel.AuthViewModel
import com.fictadvisor.android.viewmodel.AuthViewModelFactory
import kotlinx.coroutines.CoroutineScope
Expand All @@ -25,7 +24,7 @@ class ContinueRegistrationFragment : Fragment() {
private lateinit var binding: FragmentContinueRegistrationBinding
private lateinit var authViewModel: AuthViewModel
private val authRepository = AuthRepository()
private lateinit var inputValidator: RegistrationInputValidator
private lateinit var registrationViewModel: RegistrationViewModel
private lateinit var storageUtil: StorageUtil


Expand All @@ -36,7 +35,7 @@ class ContinueRegistrationFragment : Fragment() {
binding = FragmentContinueRegistrationBinding.inflate(inflater, container, false)
val view = binding.root

inputValidator = RegistrationInputValidator(requireContext())
registrationViewModel = ViewModelProvider(this).get(RegistrationViewModel::class.java)

storageUtil = StorageUtil(requireActivity())

Expand All @@ -47,6 +46,8 @@ class ContinueRegistrationFragment : Fragment() {

setBackButtonListener()
setRegisterButtonListener()
initViewModelObservers()

return view
}

Expand Down Expand Up @@ -91,7 +92,7 @@ class ContinueRegistrationFragment : Fragment() {
val name = arguments.getString("name")
val middleName = arguments.getString("middleName")
val lastname = arguments.getString("lastname")
val isCaptain = binding.checkBoxCaptain.isChecked
val isCaptain = arguments.getBoolean("isCaptain")

if (name != null && lastname != null && middleName != null && group != null) {
return StudentDTO(
Expand All @@ -110,17 +111,14 @@ class ContinueRegistrationFragment : Fragment() {
val email = binding.editTextTextEmail.text.toString()
val password = binding.editTextPassword.text.toString()
val passwordConfirm = binding.editTextTextConfirmPass.text.toString()
if(!inputValidator.isUserDataValid(email, password, passwordConfirm)){
return UserDTO("", "", "")
}
val arguments = arguments
if (arguments != null) {
val username = arguments.getString("username")
if (username != null) {
return UserDTO(username = username, email = email, password = password)
}
val username = binding.editTextTextUsername.text.toString()

return if (!registrationViewModel.validateUserData(email, password, passwordConfirm, username)) {
UserDTO("", "", "")
} else {
UserDTO(username = username, email = email, password = password)
}
return UserDTO("", "", "")

}

private fun handleIsRegisteredResponse(
Expand All @@ -129,20 +127,8 @@ class ContinueRegistrationFragment : Fragment() {
when (response) {
is BaseResponse.Success -> {
if (response.data != true) {
if (studentData.isCaptain) {
CoroutineScope(Dispatchers.IO).launch {
val group = studentData.groupId
authViewModel.checkCaptain(group)
}
authViewModel.authCheckCaptainResponse.observe(viewLifecycleOwner) { captainResponse ->
captainResponse?.let {
handleCaptainCheckResponse(captainResponse)
}
}
} else {
registerUser(studentData, userData, telegramData)
}
} else { // User is already registered
registerUser(studentData, userData, telegramData)
} else {
showErrorLog("Користувач вже зареєстрований")
}
}
Expand All @@ -157,21 +143,6 @@ class ContinueRegistrationFragment : Fragment() {
}
}

private fun handleCaptainCheckResponse(captainResponse: BaseResponse<Boolean>) {
when (captainResponse) {
is BaseResponse.Success -> {
Toast.makeText(requireContext(), "Староста для групи призначений", Toast.LENGTH_SHORT).show()
}

is BaseResponse.Error -> {
showErrorLog("Check captain error: ${captainResponse.error}")
}

is BaseResponse.Loading -> {
// Loading, if needed
}
}
}
private fun registerUser(
studentData: StudentDTO, userData: UserDTO, telegramData: TelegramDTO
) {
Expand Down Expand Up @@ -215,4 +186,22 @@ class ContinueRegistrationFragment : Fragment() {
Log.e("ContinueRegistrationFragment", message)
}


private fun initViewModelObservers() {
registrationViewModel.emailErrorLiveData.observe(viewLifecycleOwner) { emailError ->
binding.editTextTextEmailLayout.error = emailError
}

registrationViewModel.passwordErrorLiveData.observe(viewLifecycleOwner) { passwordError ->
binding.editTextPasswordLayout.error = passwordError
}

registrationViewModel.passwordConfirmErrorLiveData.observe(viewLifecycleOwner) { passwordConfirmError ->
binding.editTextTextConfirmPassLayout.error = passwordConfirmError
}

registrationViewModel.usernameErrorLiveData.observe(viewLifecycleOwner) { usernameError ->
binding.editTextTextUsernameLayout.error = usernameError
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ class ForgotPasswordFragment : Fragment() {
}

private fun setPreviousButtonListener() {
binding.buttonPrevious.setOnClickListener {
binding.textViewBack.setOnClickListener {
view?.let { it1 -> Navigation.findNavController(it1).navigateUp() }
}
}
private fun setSendButtonListener() {
binding.buttonSend.setOnClickListener {
// get email from username
val email = binding.editTextSendEmail.text.toString()
if(inputValidator.isEmailValid(email).isValid){
sendRecoveryPasswordRequest(email)
Expand All @@ -80,7 +81,7 @@ class ForgotPasswordFragment : Fragment() {
is BaseResponse.Success -> {
val message = response.data.toString()
showSuccessLog("Лист надіслано: $message")
val action = ForgotPasswordFragmentDirections.actionForgotPasswordFragmentToLoginFragment()
val action = ForgotPasswordFragmentDirections.actionForgotPasswordFragmentToVerifyEmailFragment()
Navigation.findNavController(requireView()).navigate(action)
}
is BaseResponse.Error -> {
Expand Down
18 changes: 13 additions & 5 deletions app/src/main/java/com/fictadvisor/android/ui/LoginFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import com.fictadvisor.android.data.dto.OrdinaryStudentResponse
import com.fictadvisor.android.databinding.FragmentLoginBinding
import com.fictadvisor.android.repository.AuthRepository
import com.fictadvisor.android.utils.StorageUtil
import com.fictadvisor.android.validator.LoginInputValidator
import com.fictadvisor.android.validator.LoginViewModel
import com.fictadvisor.android.viewmodel.AuthViewModel
import com.fictadvisor.android.viewmodel.AuthViewModelFactory
import kotlinx.coroutines.CoroutineScope
Expand All @@ -26,7 +26,7 @@ class LoginFragment : Fragment() {
private lateinit var binding: FragmentLoginBinding
private lateinit var authViewModel: AuthViewModel
private val authRepository = AuthRepository()
private lateinit var inputValidator: LoginInputValidator
private lateinit var loginViewModel: LoginViewModel
private lateinit var storageUtil: StorageUtil
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -40,7 +40,6 @@ class LoginFragment : Fragment() {
): View? {
binding = FragmentLoginBinding.inflate(inflater, container, false)
val view = binding.root
inputValidator = LoginInputValidator(requireContext())
storageUtil = StorageUtil(requireContext())


Expand All @@ -49,10 +48,11 @@ class LoginFragment : Fragment() {
AuthViewModelFactory(authRepository)
).get(AuthViewModel::class.java)

loginViewModel = ViewModelProvider(this).get(LoginViewModel::class.java)
setLoginButtonListener()
setPreviousButtonListener()
setForgotPasswordTextListener()

initViewModelObservers()
return view
}

Expand All @@ -65,7 +65,7 @@ class LoginFragment : Fragment() {
binding.buttonLogin.setOnClickListener {
val username = binding.editTextLogin.text.toString()
val password = binding.editTextPassword.text.toString()
if (inputValidator.isLoginDataValid(username)) {
if (loginViewModel.validateLoginData(username)) {
loginUser(username, password)
if (storageUtil.getTokens()?.accessToken != null) {
getStudentInfo(storageUtil.getTokens()?.accessToken!!)
Expand Down Expand Up @@ -104,6 +104,8 @@ class LoginFragment : Fragment() {
}

is BaseResponse.Error -> {
binding.editTextPasswordLayout.error = "Невірний логін або пароль"
binding.editTextLoginLayout.error = "Невірний логін або пароль"
showErrorLog("Помилка входу: ${registerResponse.error?.message}")
}

Expand Down Expand Up @@ -164,6 +166,12 @@ class LoginFragment : Fragment() {
Log.e("LoginFragment", message)
}

private fun initViewModelObservers() {
loginViewModel.loginErrorLiveData.observe(viewLifecycleOwner) { errorMessage ->
binding.editTextLoginLayout.error = errorMessage
}
}

companion object {
@JvmStatic
fun newInstance(): LoginFragment {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)


// val viewModelFactory = AuthViewModelFactory(authRepository)
//
// authViewModel = ViewModelProvider(this, viewModelFactory)[(AuthViewModel::class.java)]
Expand Down
Loading
Loading