Skip to content

Commit

Permalink
Merge branch 'gsoc-2023' into gsoc_2023_profile_screen_migration_compose
Browse files Browse the repository at this point in the history
  • Loading branch information
narendraanjana09 authored Aug 30, 2023
2 parents 813a133 + 42e58ca commit 16ffae4
Show file tree
Hide file tree
Showing 41 changed files with 1,574 additions and 1,147 deletions.
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ dependencies {
testImplementation "junit:junit:$junitVersion"
androidTestImplementation "androidx.test:runner:$testRunnerVersion"
androidTestImplementation "androidx.test.espresso:espresso-core:$espressoCoreVersion"
// Lifecycle components
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycleRuntime"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycleExtensions"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class UserRepositoryImpl(
.deleteAllElections()
}

override suspend fun sendForgotPasswordLink(username: String?): String {
override suspend fun sendForgotPasswordLink(username: String?): List<String> {
return apiRequest { api.sendForgotPassword(username) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ interface Api {
suspend fun resendOTP(@Path("userName") userName: String?): Response<AuthResponse>

@POST("auth/forgotPassword/send/{userName}")
suspend fun sendForgotPassword(@Path("userName") userName: String?): Response<String>
suspend fun sendForgotPassword(@Path("userName") userName: String?): Response<List<String>>

@GET("election")
suspend fun getAllElections(): Response<ElectionListResponse>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface UserRepository {
suspend fun logout()
fun getUser(): Flow<User>
suspend fun deleteUser()
suspend fun sendForgotPasswordLink(username: String?): String
suspend fun sendForgotPasswordLink(username: String?): List<String>
suspend fun updateUser(updateUserData: UpdateUserDto): List<String>
suspend fun changeAvatar(url: String): List<String>
suspend fun changePassword(password: String): List<String>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class SendForgotPasswordLinkUseCase @Inject constructor(
) {
suspend operator fun invoke(
username: String?
): String {
): List<String> {
return repository.sendForgotPasswordLink(username)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ class MainActivity : AppCompatActivity() {
window.addFlags(LayoutParams.FLAG_TRANSLUCENT_STATUS)
supportActionBar?.hide()
}
R.id.settingsFragment
-> {
supportActionBar?.hide()
}
R.id.twoFactorAuthFragment
-> {
supportActionBar?.hide()
}
else -> {
window.clearFlags(LayoutParams.FLAG_TRANSLUCENT_STATUS)
supportActionBar?.show()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ComposeView
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
import org.aossie.agoraandroid.R
import org.aossie.agoraandroid.databinding.FragmentForgotPasswordBinding
import androidx.navigation.fragment.findNavController
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import org.aossie.agoraandroid.ui.fragments.BaseFragment
import org.aossie.agoraandroid.utilities.HideKeyboard
import org.aossie.agoraandroid.utilities.ResponseUI
import org.aossie.agoraandroid.utilities.hide
import org.aossie.agoraandroid.utilities.show
import org.aossie.agoraandroid.ui.screens.auth.forgotPassword.ForgotPasswordScreen
import org.aossie.agoraandroid.ui.screens.auth.forgotPassword.ForgotPasswordScreenEvents.OnBackIconClick
import org.aossie.agoraandroid.ui.theme.AgoraTheme
import javax.inject.Inject

/**
Expand All @@ -31,44 +33,44 @@ constructor(
private val forgotPasswordViewModel: ForgotPasswordViewModel by viewModels {
viewModelFactory
}
private lateinit var binding: FragmentForgotPasswordBinding
private lateinit var composeView: ComposeView

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentForgotPasswordBinding.inflate(inflater)
return binding.root
return ComposeView(requireContext()).also {
composeView = it
}
}

override fun onFragmentInitiated() {

binding.buttonSendLink.setOnClickListener {
val userName = binding.editTextUserName.editText?.text.toString().trim()
if (userName.isEmpty()) {
notify("Please Enter User Name")
} else {
HideKeyboard.hideKeyboardInActivity(activity as AppCompatActivity)
binding.progressBar.show()
forgotPasswordViewModel.sendResetLink(userName)
}
}
composeView.setContent {

lifecycleScope.launch {
forgotPasswordViewModel.getSendResetLinkStateFlow.collect {
if (it != null) {
when (it.status) {
ResponseUI.Status.LOADING -> binding.progressBar.show()
ResponseUI.Status.SUCCESS -> {
binding.progressBar.hide()
notify(getString(R.string.link_sent_please_check_your_email))
val userNameState by forgotPasswordViewModel.userNameState.collectAsState()
val progressErrorState by forgotPasswordViewModel.progressAndErrorState.collectAsState()

val systemUiController = rememberSystemUiController()
val useDarkIcons = !isSystemInDarkTheme()

DisposableEffect(systemUiController, useDarkIcons) {
systemUiController.setStatusBarColor(
color = Color.Transparent,
darkIcons = useDarkIcons
)
onDispose {}
}
AgoraTheme {
ForgotPasswordScreen(progressErrorState,userNameState) { event ->
when(event){
OnBackIconClick -> {
findNavController().navigateUp()
}
ResponseUI.Status.ERROR -> {
binding.progressBar.hide()
notify(it.message)
else -> {
forgotPasswordViewModel.onEvent(event)
}
else -> {}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ package org.aossie.agoraandroid.ui.fragments.auth.forgotpassword

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import org.aossie.agoraandroid.R
import org.aossie.agoraandroid.domain.useCases.authentication.forgotPassword.SendForgotPasswordLinkUseCase
import org.aossie.agoraandroid.ui.screens.auth.forgotPassword.ForgotPasswordScreenEvents
import org.aossie.agoraandroid.ui.screens.auth.forgotPassword.ForgotPasswordScreenEvents.OnSendLinkClick
import org.aossie.agoraandroid.ui.screens.auth.forgotPassword.ForgotPasswordScreenEvents.OnUserNameEntered
import org.aossie.agoraandroid.ui.screens.auth.forgotPassword.ForgotPasswordScreenEvents.SnackBarActionClick
import org.aossie.agoraandroid.ui.screens.common.Util.ScreensState
import org.aossie.agoraandroid.utilities.ApiException
import org.aossie.agoraandroid.utilities.AppConstants
import org.aossie.agoraandroid.utilities.NoInternetException
import org.aossie.agoraandroid.utilities.ResponseUI
import javax.inject.Inject

class ForgotPasswordViewModel
Expand All @@ -18,26 +24,78 @@ constructor(
private val sendForgotPasswordLinkUseCase: SendForgotPasswordLinkUseCase
) : ViewModel() {

private val _getSendResetLinkStateFlow: MutableStateFlow<ResponseUI<Any>?> =
MutableStateFlow(null)
val getSendResetLinkStateFlow: StateFlow<ResponseUI<Any>?> =
_getSendResetLinkStateFlow
private val _userNameState = MutableStateFlow ("")
val userNameState = _userNameState.asStateFlow()

fun sendResetLink(userName: String?) = viewModelScope.launch {
_getSendResetLinkStateFlow.value = ResponseUI.loading()
private val _progressAndErrorState = MutableStateFlow (ScreensState())
val progressAndErrorState = _progressAndErrorState.asStateFlow()


private fun sendResetLink(userName: String?) = viewModelScope.launch {
showLoading("Sending link...")
try {
sendForgotPasswordLinkUseCase(userName)
_getSendResetLinkStateFlow.value = ResponseUI.success()
hideLoading()
showMessage(R.string.link_sent_please_check_your_email)
} catch (e: ApiException) {
if (e.message == "412") {
_getSendResetLinkStateFlow.value = ResponseUI.error(AppConstants.INVALID_USERNAME_MESSAGE)
showMessage(AppConstants.INVALID_USERNAME_MESSAGE)
} else {
_getSendResetLinkStateFlow.value = ResponseUI.error(e.message)
showMessage(e.message!!)
}
} catch (e: NoInternetException) {
_getSendResetLinkStateFlow.value = ResponseUI.error(e.message)
showMessage(e.message!!)
} catch (e: Exception) {
_getSendResetLinkStateFlow.value = ResponseUI.error(e.message)
showMessage(e.message!!)
}
}

fun onEvent(forgotPasswordScreenEvents: ForgotPasswordScreenEvents){
when(forgotPasswordScreenEvents){
OnSendLinkClick -> {
val userName = _userNameState.value.trim()
if (userName.isEmpty()) {
showMessage("Please Enter User Name")
} else {
sendResetLink(userName)
}
}
is OnUserNameEntered -> {
_userNameState.value = forgotPasswordScreenEvents.userName
}
SnackBarActionClick -> {
hideSnackBar()
}
else -> {}
}
}

private fun showLoading(message: Any) {
_progressAndErrorState.value = progressAndErrorState.value.copy(
loading = Pair(message,true)
)
}

fun showMessage(message: Any) {
_progressAndErrorState.value = progressAndErrorState.value.copy(
message = Pair(message,true),
loading = Pair("",false)
)
viewModelScope.launch {
delay(AppConstants.SNACKBAR_DURATION)
hideSnackBar()
}
}

private fun hideSnackBar() {
_progressAndErrorState.value = progressAndErrorState.value.copy(
message = Pair("",false)
)
}

private fun hideLoading() {
_progressAndErrorState.value = progressAndErrorState.value.copy(
loading = Pair("",false)
)
}
}
Loading

0 comments on commit 16ffae4

Please sign in to comment.