Skip to content

Commit

Permalink
feat: implemented coroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
PratyushSingh07 authored and therajanmaurya committed Aug 15, 2023
1 parent 1e5fb21 commit bbebdcf
Show file tree
Hide file tree
Showing 17 changed files with 266 additions and 499 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/org/mifos/mobile/api/DataManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ class DataManager @Inject constructor(
return baseApiManager.thirdPartyTransferApi?.makeTransfer(transferPayload)
}

fun registerUser(registerPayload: RegisterPayload?): Observable<ResponseBody?>? {
suspend fun registerUser(registerPayload: RegisterPayload?): Response<ResponseBody?>? {
return baseApiManager.registrationApi?.registerUser(registerPayload)
}

fun verifyUser(userVerify: UserVerify?): Observable<ResponseBody?>? {
suspend fun verifyUser(userVerify: UserVerify?): Response<ResponseBody?>? {
return baseApiManager.registrationApi?.verifyUser(userVerify)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import okhttp3.ResponseBody
import org.mifos.mobile.api.ApiEndPoints
import org.mifos.mobile.models.register.RegisterPayload
import org.mifos.mobile.models.register.UserVerify
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.POST

Expand All @@ -13,8 +14,8 @@ import retrofit2.http.POST
*/
interface RegistrationService {
@POST(ApiEndPoints.REGISTRATION)
fun registerUser(@Body registerPayload: RegisterPayload?): Observable<ResponseBody?>?
suspend fun registerUser(@Body registerPayload: RegisterPayload?): Response<ResponseBody?>?

@POST(ApiEndPoints.REGISTRATION + "/user")
fun verifyUser(@Body userVerify: UserVerify?): Observable<ResponseBody?>?
suspend fun verifyUser(@Body userVerify: UserVerify?): Response<ResponseBody?>?
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package org.mifos.mobile.repositories

import io.reactivex.Observable
import okhttp3.ResponseBody
import org.mifos.mobile.models.User
import retrofit2.Response

interface UserAuthRepository {

fun registerUser(
suspend fun registerUser(
accountNumber: String?,
authenticationMode: String?,
email: String?,
Expand All @@ -16,11 +15,11 @@ interface UserAuthRepository {
mobileNumber: String?,
password: String?,
username: String?
): Observable<ResponseBody?>?
): Response<ResponseBody?>?

suspend fun login(username: String, password: String): Response<User?>?

fun verifyUser(authenticationToken: String?, requestId: String?): Observable<ResponseBody?>?
suspend fun verifyUser(authenticationToken: String?, requestId: String?): Response<ResponseBody?>?

suspend fun updateAccountPassword(
newPassword: String, confirmPassword: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import javax.inject.Inject

class UserAuthRepositoryImp @Inject constructor(private val dataManager: DataManager) : UserAuthRepository {

override fun registerUser(
override suspend fun registerUser(
accountNumber: String?,
authenticationMode: String?,
email: String?,
Expand All @@ -22,7 +22,7 @@ class UserAuthRepositoryImp @Inject constructor(private val dataManager: DataMan
mobileNumber: String?,
password: String?,
username: String?
): Observable<ResponseBody?>? {
): Response<ResponseBody?>? {
val registerPayload = RegisterPayload().apply {
this.accountNumber = accountNumber
this.authenticationMode = authenticationMode
Expand All @@ -45,7 +45,7 @@ class UserAuthRepositoryImp @Inject constructor(private val dataManager: DataMan
}


override fun verifyUser(authenticationToken: String?, requestId: String?): Observable<ResponseBody?>? {
override suspend fun verifyUser(authenticationToken: String?, requestId: String?): Response<ResponseBody?>? {
val userVerify = UserVerify().apply {
this.authenticationToken = authenticationToken
this.requestId = requestId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class RegistrationFragment : BaseFragment() {

is RegistrationUiState.Error -> {
hideProgress()
showError(MFErrorParser.errorMessage(state.exception))
showError(state.exception)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class RegistrationVerificationFragment : BaseFragment() {

is RegistrationUiState.Error -> {
hideProgress()
showError(MFErrorParser.errorMessage(state.exception))
showError(state.exception)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class UpdatePasswordFragment : BaseFragment(), TextWatcher, OnFocusChangeListene

is RegistrationUiState.Error -> {
hideProgress()
showError(MFErrorParser.errorMessage(state.exception))
showError(state.exception)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.mifos.mobile.utils

sealed class RegistrationUiState {
data class Error(val exception: Throwable) : RegistrationUiState()
data class Error(val exception: String) : RegistrationUiState()
object Success : RegistrationUiState()
object Loading : RegistrationUiState()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ import androidx.core.util.PatternsCompat
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.observers.DisposableObserver
import io.reactivex.schedulers.Schedulers
import okhttp3.ResponseBody
import kotlinx.coroutines.launch
import org.mifos.mobile.repositories.UserAuthRepository
import org.mifos.mobile.utils.RegistrationUiState
import javax.inject.Inject

@HiltViewModel
class RegistrationViewModel @Inject constructor(private val userAuthRepositoryImp: UserAuthRepository) :
ViewModel() {
private val compositeDisposables: CompositeDisposable = CompositeDisposable()

private val _registrationUiState = MutableLiveData<RegistrationUiState>()
val registrationUiState: LiveData<RegistrationUiState> get() = _registrationUiState
Expand Down Expand Up @@ -56,49 +52,38 @@ class RegistrationViewModel @Inject constructor(private val userAuthRepositoryIm
password: String,
username: String
) {
_registrationUiState.value = RegistrationUiState.Loading
userAuthRepositoryImp.registerUser(
accountNumber,
authenticationMode,
email,
firstName,
lastName,
mobileNumber,
password,
username
)?.observeOn(AndroidSchedulers.mainThread())?.subscribeOn(Schedulers.io())
?.subscribeWith(object : DisposableObserver<ResponseBody?>() {
override fun onComplete() {}
override fun onError(e: Throwable) {
_registrationUiState.value = RegistrationUiState.Error(e)
}

override fun onNext(responseBody: ResponseBody) {
_registrationUiState.value = RegistrationUiState.Success
}
})?.let { compositeDisposables.add(it) }
viewModelScope.launch {
_registrationUiState.value = RegistrationUiState.Loading
val response = userAuthRepositoryImp.registerUser(
accountNumber,
authenticationMode,
email,
firstName,
lastName,
mobileNumber,
password,
username
)
if (response?.isSuccessful == true) {
_registrationUiState.value = RegistrationUiState.Success
} else {
_registrationUiState.value =
response?.errorBody()?.string()?.let { RegistrationUiState.Error(it) }
}
}
}

fun verifyUser(authenticationToken: String?, requestId: String?) {
_registrationVerificationUiState.value = RegistrationUiState.Loading
userAuthRepositoryImp.verifyUser(authenticationToken, requestId)?.observeOn(AndroidSchedulers.mainThread())
?.subscribeOn(Schedulers.io())
?.subscribeWith(object : DisposableObserver<ResponseBody?>() {
override fun onComplete() {}
override fun onError(e: Throwable) {
_registrationVerificationUiState.value =
RegistrationUiState.Error(e)
}

override fun onNext(responseBody: ResponseBody) {
_registrationVerificationUiState.value =
RegistrationUiState.Success
}
})?.let { compositeDisposables.add(it) }
}

override fun onCleared() {
super.onCleared()
compositeDisposables.clear()
viewModelScope.launch {
_registrationVerificationUiState.value = RegistrationUiState.Loading
val response = userAuthRepositoryImp.verifyUser(authenticationToken, requestId)
if (response?.isSuccessful == true) {
_registrationVerificationUiState.value =
RegistrationUiState.Success
} else {
_registrationVerificationUiState.value =
response?.errorBody()?.string()?.let { RegistrationUiState.Error(it) }
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ class UpdatePasswordViewModel @Inject constructor(
clientRepositoryImp.updateAuthenticationToken(newPassword)
} else {
_updatePasswordUiState.value =
RegistrationUiState.Error(Throwable(response?.body()?.string()))
response?.errorBody()?.string()?.let { RegistrationUiState.Error(it) }
}
} catch (e: Throwable) {
_updatePasswordUiState.value =
RegistrationUiState.Error(e)
response?.errorBody()?.string()?.let { RegistrationUiState.Error(it) }
}

}
}

Expand Down
Loading

0 comments on commit bbebdcf

Please sign in to comment.