https://kotlinlang.org logo
#android-studio
Title
# android-studio
s

sammy nduba

03/26/2024, 7:59 AM
Does this code work for my viewmodel RXjava user onboarding:
Copy code
class AuthViewModel @Inject constructor(private val repository: UserRepository) : ViewModel() {


    var oFirstName = ObservableField<String> ()
    var oLastName = ObservableField<String> ()
    var oUid = ObservableField<String> ()


    private val disposables = CompositeDisposable()
    var authListener: AuthListener? = null

    fun registerUser() {
        try {
            if (oFirstName.get().isNullOrEmpty() || oLastName.get().isNullOrEmpty()) {
                authListener?.onFailure("Please enter both first name and last name")
                Log.d("ViewModel", "Registration started")
                return
            }
            val user = User( firstName = oFirstName.get(), lastName = oLastName.get(),
                id = oUid.get()?.toIntOrNull() ?: 0
            )

            disposables.add(
                repository.register(user)
                    .subscribeOn(<http://Schedulers.io|Schedulers.io>())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(
                        {
                            Log.d("ViewModel", "User Registered")
                            authListener?.onSuccess()
                        },
                        {
                            Log.d("ViewModel", "Registration failed")
                            authListener?.onFailure("")?.let { error(it) }
                        }
                    )
            )
        }catch (e: Exception) {
            Log.e("ViewModel", "Unable to complete registration")
        }
    }
    override fun onCleared() {
        super.onCleared()
        disposables.clear()
    }
}
b

Benjamin Szilagyi

03/26/2024, 11:49 AM
Hi , I'm just curious, if you use kotlin , why do you use RX instead of coroutine flows?
s

sammy nduba

03/26/2024, 12:41 PM
Because I am working on old applications - maintaining them and updating features