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

Commit

Permalink
Merge pull request #11 from fictadvisor/feature-viewmodels
Browse files Browse the repository at this point in the history
Feature viewmodels (needs checks on a real email)
  • Loading branch information
vita133 authored Mar 7, 2024
2 parents ca38257 + a1ee773 commit f6d5645
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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
Expand All @@ -25,6 +26,7 @@ class ContinueRegistrationFragment : Fragment() {
private lateinit var authViewModel: AuthViewModel
private val authRepository = AuthRepository()
private lateinit var inputValidator: RegistrationInputValidator
private lateinit var storageUtil: StorageUtil


override fun onCreateView(
Expand All @@ -36,6 +38,8 @@ class ContinueRegistrationFragment : Fragment() {

inputValidator = RegistrationInputValidator(requireContext())

storageUtil = StorageUtil(requireActivity())

authViewModel = ViewModelProvider(
this,
AuthViewModelFactory(authRepository)
Expand Down Expand Up @@ -188,6 +192,7 @@ class ContinueRegistrationFragment : Fragment() {
when (registerResponse) {
is BaseResponse.Success -> {
showSuccessLog("Реєстрація успішна")
storageUtil.setEmail(binding.editTextTextEmail.text.toString())
val action = ContinueRegistrationFragmentDirections.actionContinueRegistrationFragmentToVerifyEmailFragment()
Navigation.findNavController(requireView()).navigate(action)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class VerifyEmailFragment : Fragment() {
binding = FragmentVerifyEmailBinding.inflate(inflater, container, false)
val view = binding.root

storageUtil = StorageUtil(requireContext())
storageUtil = StorageUtil(requireActivity())

authViewModel = ViewModelProvider(
this,
Expand All @@ -53,9 +53,18 @@ class VerifyEmailFragment : Fragment() {
sendEmailVerificationRequest(token)
}

setResendButtonOnClickListener()

return view
}

private fun setResendButtonOnClickListener() {
binding.resendEmailButton.setOnClickListener {
val email = storageUtil.getEmail()!!
resendEmailVerificationRequest(email)
}
}

private fun sendEmailVerificationRequest(token: String) {
CoroutineScope(Dispatchers.IO).launch {
authViewModel.verifyEmailToken(token)
Expand All @@ -68,6 +77,28 @@ class VerifyEmailFragment : Fragment() {
}
}

private fun resendEmailVerificationRequest(email: String) {
CoroutineScope(Dispatchers.IO).launch {
authViewModel.verifyEmail(email)
}

authViewModel.authVerifyEmailResponse.observe(viewLifecycleOwner) { response ->
response?.let {
when (response) {
is BaseResponse.Success -> {
binding.messageTV.text = "Повторний запит на підтвердження email відправлено на адресу $email"
}

is BaseResponse.Error -> {
binding.messageTV.text = "Помилка при повторному відправленні email: ${response.error?.message}"
}

is BaseResponse.Loading -> {}
}
}
}
}

private fun handleVerificationEmailResponse(response: BaseResponse<AuthLoginResponse>) {
when (response) {
is BaseResponse.Success -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ enum class StorageKeys {
ACCESS_TOKEN,
REFRESH_TOKEN,
TELEGRAM_INFO,
EMAIL
}
12 changes: 12 additions & 0 deletions app/src/main/java/com/fictadvisor/android/utils/StorageUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,16 @@ class StorageUtil(private val context: Context) {
null
}
}

fun setEmail(email: String) {
context.getSharedPreferences(prefName, Context.MODE_PRIVATE).edit().apply {
putString(StorageKeys.EMAIL.name, email)
apply()
}
}

fun getEmail(): String? {
val prefs = context.getSharedPreferences(prefName, Context.MODE_PRIVATE)
return prefs.getString(StorageKeys.EMAIL.name, null)
}
}
10 changes: 10 additions & 0 deletions app/src/main/res/layout/fragment_verify_email.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,14 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/resendEmailButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Відправити знову"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/messageTV" />

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit f6d5645

Please sign in to comment.