I'm new to android and I'm developing a few applications for studying.
I've been trying to improve a code that I have but I got stuck in the following problem:
I'm creating a new user, validating it with Google Firebase. I managed to create a user normally but I'm not able to handle with one exception from the register moment which is the "FirebaseAuthUserCollisionException".
I created a class to handle a most of exceptions from email/password mistakes:
class AddUser(private val repository: UserRepository) {
#Throws(InvalidUserException::class)
suspend operator fun invoke(user: UserModel) {
if(user.email.isEmpty()) {
throw InvalidUserException("Email cannot be empty")
}
if(!Patterns.EMAIL_ADDRESS.matcher(user.email).matches()) {
throw InvalidUserException("Email is not valid")
}
if(user.password.length <= 5) {
throw InvalidUserException("Password should contain at least 6 characters")
}
if(user.password.isEmpty()) {
throw InvalidUserException("Password cannot be empty")
}
if(user.confirmPassword.isEmpty()) {
throw InvalidUserException("Confirm password cannot be empty")
}
if(user.password != user.confirmPassword) {
throw InvalidUserException("Passwords does not match")
}
repository.insert(user)
}
}
My repository:
class UserRepositoryImpl: UserRepository {
private var auth: FirebaseAuth = Firebase.auth
private var database: DatabaseReference = FirebaseDatabase.getInstance().getReference("users")
override suspend fun insert(user: UserModel) {
auth = FirebaseAuth.getInstance()
auth.createUserWithEmailAndPassword(user.email, user.password).addOnCompleteListener {
if(it.isSuccessful) {
database.child(user.id.toString()).setValue(user)
} else {
//exception here
}
}
}
}
When this function is triggered, it navigates to another fragment and toasts the successful message, which is incorrect because the exception happens.
Fragment:
private fun configEventFlow() = lifecycleScope.launch {
viewModel.eventFlow.collectLatest { event ->
when(event) {
is RegisterViewModel.UiEvent.ShowToast -> {
toast(event.message)
}
is RegisterViewModel.UiEvent.SaveUser -> {
val action = RegisterFragmentDirections.actionRegisterFragmentToMainFragment()
findNavController().navigate(action)
toast(getString(R.string.register_successfully))
}
}
}
}
private fun configUserRegistration() = with(binding) {
fabRegister.setOnClickListener {
val email = editRegisterEmail.text.toString()
viewModel.onEvent(RegisterUserEvents.EnteredEmail(email))
val password = editRegisterPassword.text.toString()
viewModel.onEvent(RegisterUserEvents.EnteredPassword(password))
val confirmPassword = editRegisterPasswordConfirm.text.toString()
viewModel.onEvent(RegisterUserEvents.EnteredConfirmPassword(confirmPassword))
viewModel.onEvent(RegisterUserEvents.SaveUser)
}
}
ViewModel:
#HiltViewModel
class RegisterViewModel #Inject constructor(private val useCases: UserUseCases): ViewModel() {
private val _email = MutableStateFlow<ResourceState<String>>(ResourceState.Empty())
private val email: StateFlow<ResourceState<String>> = _email
private val _password = MutableStateFlow<ResourceState<String>>(ResourceState.Empty())
private val password: StateFlow<ResourceState<String>> = _password
private val _confirmPassword = MutableStateFlow<ResourceState<String>>(ResourceState.Empty())
private val confirmPassword: StateFlow<ResourceState<String>> = _confirmPassword
private val _eventFlow = MutableSharedFlow<UiEvent>()
val eventFlow = _eventFlow.asSharedFlow()
fun onEvent(event: RegisterUserEvents) {
when(event) {
is RegisterUserEvents.EnteredEmail -> {
_email.value = ResourceState.Success(event.value)
}
is RegisterUserEvents.EnteredPassword -> {
_password.value = ResourceState.Success(event.value)
}
is RegisterUserEvents.EnteredConfirmPassword -> {
_confirmPassword.value = ResourceState.Success(event.value)
}
is RegisterUserEvents.SaveUser -> {
viewModelScope.launch {
try {
useCases.addUser(
UserModel(
id = System.currentTimeMillis().toInt(),
email = email.value.data!!,
password = password.value.data!!,
confirmPassword = confirmPassword.value.data!!
)
)
_eventFlow.emit(UiEvent.SaveUser)
} catch(e: InvalidUserException) {
_eventFlow.emit(UiEvent.ShowToast(message = e.message!!))
}
}
}
}
}
sealed class UiEvent {
data class ShowToast(val message: String): UiEvent()
object SaveUser: UiEvent()
}
}
Is there a way that I can manage this specific exception in this pattern? Even if I catch the exception there, the action is completed and my application follows at it was registered but in the database it does not occur because of the exception. Im sure that I'll have to face it again when login to handle specific exceptions from Firebase, which I cannot create this way but I have to receive them and display to the user.
Any suggestions??
Sorry if it's missing any content, tell me and I update asap.
Thanks in advance.
Related
I want to write a simple test for my viewModel to check if it gets data from repository. The app itself working without problem but in test, i have the following test failed.
It looks like the viewModel init block not running, because it suppose to call getUpcomingMovies() method in init blocks and post value to upcomingMovies live data object. When i test it gets null value.
Looks like i am missing a minor thing, need help to solve this.
Here is the test:
#ExperimentalCoroutinesApi
class MoviesViewModelShould: BaseUnitTest() {
private val repository: MoviesRepository = mock()
private val upcomingMovies = mock<Response<UpcomingResponse>>()
private val upcomingMoviesExpected = Result.success(upcomingMovies)
#Test
fun emitsUpcomingMoviesFromRepository() = runBlocking {
val viewModel = mockSuccessfulCaseUpcomingMovies()
assertEquals(upcomingMoviesExpected, viewModel.upcomingMovies.getValueForTest())
}
private fun mockSuccessfulCaseUpcomingMovies(): MoviesViewModel {
runBlocking {
whenever(repository.getUpcomingMovies(1)).thenReturn(
flow {
emit(upcomingMoviesExpected)
}
)
}
return MoviesViewModel(repository)
}
}
And viewModel:
class MoviesViewModel(
private val repository: MoviesRepository
): ViewModel() {
val upcomingMovies: MutableLiveData<UpcomingResponse> = MutableLiveData()
var upcomingMoviesPage = 0
private var upcomingMoviesResponse: UpcomingResponse? = null
init {
getUpcomingMovies()
}
fun getUpcomingMovies() = viewModelScope.launch {
upcomingMoviesPage++
repository.getUpcomingMovies(upcomingMoviesPage).collect { result ->
if (result.isSuccess) {
result.getOrNull()!!.body()?.let {
if (upcomingMoviesResponse == null) {
upcomingMoviesResponse = it
} else {
val oldMovies = upcomingMoviesResponse?.results
val newMovies = it.results
oldMovies?.addAll(newMovies)
}
upcomingMovies.postValue(upcomingMoviesResponse ?: it)
}
}
}
}
}
And the result is:
expected:<Success(Mock for Response, hashCode: 1625939772)> but was:<null>
Expected :Success(Mock for Response, hashCode: 1625939772)
Actual :null
I am creating an android application following the MVVM patron with the goal of retrieving data from a Firebase collection.
Before applying this pattern, I did proof of concept and I was able to retrieve data from the Firebase collection. But once I apply MVVM, I am not able to get the data from that collection, my screen does not show anything. I am not able to return the data from the repository to be painted on the screen.
This is my code:
Model:
data class PotatoesData(
val modifiedDate: String,
var potatoes: List<Potato>
)
data class Potato(
val type: String,
val site: String
)
State:
data class PotatoesState(
val isLoading: Boolean = false,
val potatoes: List<Potato> = emptyList(),
val error: String = ""
)
ModelView:
#HiltViewModel
class PotatoesViewModel #Inject constructor(
private val getPotatoesDataUseCase: GetPotatoesData
) : ViewModel() {
private val _state = mutableStateOf(PotatoesState())
val state: State<PotatoesState> = _state
init {
getPotatoes()
}
private fun getPotatoes() {
getPotatoesDataUseCase().onEach { result ->
when (result) {
is Resource.Success -> {
_state.value = PotatoesState(potatoes = result.data?.potatoes ?: emptyList())
}
is Resource.Error -> {
_state.value = PotatoesState(
error = result.message ?: "An unexpected error occurred"
)
}
is Resource.Loading -> {
_state.value = PotatoesState(isLoading = true)
}
}
}.launchIn(viewModelScope)
}
}
UseCase:
class GetPotatoesData #Inject constructor(
private val repository: PotatoRepository
) {
operator fun invoke(): Flow<Resource<PotatoesData>> = flow {
try {
emit(Resource.Loading())
val potatoes = repository.getPotatoesData()
emit(Resource.Success(potatoes))
} catch (e: IOException) {
emit(Resource.Error("Couldn't reach server. Check your internet connection."))
}
}
}
Repository implementation:
class PotatoRepositoryImpl : PotatoRepository {
override suspend fun getPotatoesData(): PotatoesData {
var potatoes = PotatoesData("TEST", emptyList())
FirestoreProvider.getLastPotatoes(
{ potatoesData ->
if (potatoesData != null) {
potatoes = potatoesData
}
},
{
potatoes
}
)
return potatoes
}
}
Firestore provider:
object FirestoreProvider {
private val incidentsRef = FirebaseFirestore.getInstance().collection(FirestoreCollection.POTATOES.key)
fun getLastPotatoes(
success: (potatoesData: PotatoesData?) -> Unit,
failure: () -> Unit
) {
val query: Query = orderBy(FirestoreField.CREATED_DATE, Query.Direction.DESCENDING).limit(1)
val querySnapshot: Task<QuerySnapshot> = query.get()
querySnapshot
.addOnSuccessListener {
if (!querySnapshot.result.isEmpty) {
val document = querySnapshot.result.documents[0]
val potatoesDataDB: PotatoesDataDto? = document.toObject(PotatoesDataDto::class.java)
potatoesDataDB?.let {
success(potatoesDataDB.toPotatoesData())
} ?: run {
success(null)
}
} else {
success(null)
}
}
.addOnFailureListener {
failure()
}
}
private fun orderBy(field: FirestoreField, direction: Query.Direction): Query {
return incidentsRef.orderBy(field.key, direction)
}
}
I am thankful for any kind of help! Thanks in advance!
I think the error is in the way of how you are handling Firestore callbacks. in FirestoreProvider: the callback will fire later than the function getLastPotatoes returns. Try to make that function suspend and use suspendCoroutine to wait for the callback and return it's result. It will look something like:
suspend fun getLastPotatoes() = suspendCoroutine <PotatoesData?> { continuation ->
val query: Query = orderBy(FirestoreField.CREATED_DATE, Query.Direction.DESCENDING).limit(1)
val querySnapshot: Task<QuerySnapshot> = query.get()
querySnapshot
.addOnSuccessListener {
if (!querySnapshot.result.isEmpty) {
val document = querySnapshot.result.documents[0]
val potatoesDataDB: PotatoesDataDto? = document.toObject(PotatoesDataDto::class.java)
potatoesDataDB?.let {
continuation.resume(potatoesDataDB.toPotatoesData())
} ?: run {
continuation.resume(null)
}
} else {
continuation.resume(null)
}
}
.addOnFailureListener {
continuation.resumeWithException(...)
}
}
suspendCoroutine suspends coroutine in which it executed until we decide to continue by calling appropriate methods - Continuation.resume....
In your PotatoRepositoryImpl:
override suspend fun getPotatoesData(): PotatoesData {
var potatoes = PotatoesData("TEST", emptyList())
try {
val potatoesData = FirestoreProvider.getLastPotatoes()
if (potatoesData != null) {
potatoes = potatoesData
}
} catch (e: Exception) {
// handle Exception
}
return potatoes
}
I am implementing an "Add to favourites" functionality in a detail screen. If the user taps the FAB, I want to set the fab as selected and update my database. How could I use the same value that I am sending to the database to be used in my fragment (to be consistent, in case there is some issue while updating the DB)
Fragment
class BeerDetailsFragment : Fragment(R.layout.fragment_beer_details) {
private val viewModel by viewModels<BeerDetailsViewModel>()
private val args by navArgs<BeerDetailsFragmentArgs>()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
subscribeToObservers()
viewModel.getBeer(args.beerId)
}
private fun subscribeToObservers() {
viewModel.beer.observe(viewLifecycleOwner, { resource ->
when(resource.status) {
Status.SUCCESS -> {
loadData(resource.data)
}
Status.ERROR -> {
showError(resource.message)
}
Status.LOADING -> {}
}
})
}
private fun loadData(beerDetails: BeerDomainModel?) {
if (beerDetails != null) {
Glide.with(requireContext())
.load(beerDetails.imageMedium)
.placeholder(R.drawable.ic_beer)
.error(R.drawable.ic_beer)
.fallback(R.drawable.ic_beer)
.into(beerDetailsImage)
beerDetailsName.text = beerDetails.name
beerDetailsDescription.text = beerDetails.description
fab.isSelected = beerDetails.isFavourite
fab.setOnClickListener {
viewModel.updateBeer(beerDetails)
// I shouldn't do it like this in case there is an issue while updating the DB
fab.isSelected = !beerDetails.isFavourite
}
}
}
...
View Model class
class BeerDetailsViewModel #ViewModelInject constructor(private val repository: BreweryRepository) : ViewModel() {
private val beerId = MutableLiveData<String>()
fun getBeer(id: String) {
beerId.value = id
}
var beer = beerId.switchMap { id ->
liveData {
emit(Resource.loading(null))
emit(repository.getBeer(id))
}
}
fun updateBeer(beer: BeerDomainModel) {
viewModelScope.launch {
repository.updateBeer(beer)
}
}
}
Repository
class BreweryRepository #Inject constructor(private val breweryApi: BreweryApi, private val beerDao: BeerDao, private val responseHandler: ResponseHandler) {
...
suspend fun getBeer(id: String): Resource<BeerDomainModel> {
return try {
withContext(IO) {
val isInDB = beerDao.isInDB(id)
if (!isInDB) {
val response = breweryApi.getBeer(id).beer.toDomainModel()
beerDao.insert(response.toBeerEntity())
responseHandler.handleSuccess(response)
} else {
val beer = beerDao.get(id).toDomainModel()
responseHandler.handleSuccess(beer)
}
}
} catch (e: Exception) {
responseHandler.handleException(e)
}
}
suspend fun updateBeer(beer: BeerDomainModel) {
withContext(IO) {
val dbBeer = with(beer) {
copy(isFavourite = !isFavourite)
toBeerEntity()
}
beerDao.update(dbBeer)
}
}
}
I would prefer to use a unidirectional flow with the following implementation:
Not sure how is your DAO implemented, but if you are using Room you could update your get method to return a Flow instead. That way whenever your data is updated, you will get back the updated data.
Then in your VM you just get that Flow or stream of data and subscribe to the updates. Flow has a very convenient method: asLiveData() so your code will look much cleaner.
If you are not using Room, then what I'd do is either construct a Flow or some type of stream and on updates successful updates send out the new data.
I have an issue with my code which is throwing NetworkOnMainThreadException. I am trying to connect to an Android app to Odoo using Android XML-RPC library.
Here is what I am doing.
class OdooServiceImpl : OdooService {
/* This is the only function doing network operation*/
override fun userAuthenticate(
host: String,
login: String,
password: String,
database: String
): Single<Int> {
val client = XMLRPCClient("$host/xmlrpc/2/common")
val result =
client.call("login", database, login, password)
return Single.just(result as Int)
}}
This class is called from a repository class.
The repository if called by the viewmodel class using rxandroid
class OdooViewModel(private val mainRepository: OdooRepository, private val context: Context) :
ViewModel() {
val host = "https://myodoo-domain.com"
private val user = MutableLiveData<OdooResource<Int>>()
private val compositeDisposable = CompositeDisposable()
init {
authUser()
}
private fun authUser(){
user.postValue(OdooResource.authConnecting(null))
compositeDisposable.add(
mainRepository.userAuthenticate(host,"mylogin","mypassword","mdb")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
if (it != null) {
user.postValue(OdooResource.authSuccess(it))
} else {
user.postValue(
OdooResource.authError(
null,
msg = "Something went wring while authenticating to $host"
)
)
}
}, {
server.postValue(
OdooResource.conError(
null,
msg = "Something went wring while authenticating to $host"
)
)
})
)
}
override fun onCleared() {
super.onCleared()
compositeDisposable.dispose()
}
fun getUser(): LiveData<OdooResource<Int>> {
return user
}
}
I have called this class from my activity as follow
class OdooActivity : AppCompatActivity() {
private lateinit var odooViewModel: OdooViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_odoo)
setupViewModel()
setupObserver()
}
private fun setupObserver() {
odooViewModel.getUser().observe(this, Observer {
Log.i("TAGGG", "Tests")
when (it.status) {
OdooStatus.AUTHENTICATION_SUCCESS -> {
progressBar.visibility = View.GONE
it.data?.let { server -> textView.setText(server.toString()) }
textView.visibility = View.VISIBLE
}
OdooStatus.AUTHENTICATION -> {
progressBar.visibility = View.VISIBLE
textView.visibility = View.GONE
}
OdooStatus.AUTHENTICATION_ERROR -> {
//Handle Error
progressBar.visibility = View.GONE
Toast.makeText(this, it.message, Toast.LENGTH_LONG).show()
}
else -> {
}
}
})
}
private fun setupViewModel() {
val viewModelFactory = OdooViewModelFactory(OdooApiHelper(OdooServiceImpl()), this)
odooViewModel = ViewModelProviders.of(this, viewModelFactory).get(OdooViewModel::class.java)
}
}
When I run the app this is a the line which is throwing the error
odooViewModel = ViewModelProviders.of(this, viewModelFactory).get(OdooViewModel::class.java)
What am I missing here??
The culprit is here:
val result = client.call("login", database, login, password)
return Single.just(result as Int)
The call to generate the result is executed, when setting up the Rx chain, which happens on the main thread. You have to make sure that the network-call is done when actually subscribing (on io()). One solution could be to return a Single.fromCallable:
return Single.fromCallable { client.call("login", database, login, password) as Int }
I have an activity to perform rest API everytime it opened and i use MVVM pattern for this project. But with this snippet code i failed to get updated everytime i open activity. So i debug all my parameters in every line, they all fine the suspect problem might when apiService.readNewsAsync(param1,param2) execute, my postValue did not update my resulRead parameter. There were no crash here, but i got result which not updated from result (postValue). Can someone explain to me why this happened?
Here what activity looks like
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
DataBindingUtil.setContentView<ActivityReadBinding>(this,
R.layout.activity_read).apply {
this.viewModel = readViewModel
this.lifecycleOwner = this#ReadActivity
}
readViewModel.observerRead.observe(this, Observer {
val sukses = it.isSuccess
when{
sukses -> {
val data = it.data as Read
val article = data.article
//Log.d("-->", "${article.toString()}")
}
else -> {
toast("ada error ${it.msg}")
Timber.d("ERROR : ${it.msg}")
}
}
})
readViewModel.getReadNews()
}
Viewmodel
var observerRead = MutableLiveData<AppResponse>()
init {
observerRead = readRepository.observerReadNews()
}
fun getReadNews() {
// kanal and guid i fetch from intent and these value are valid
loadingVisibility = View.VISIBLE
val ok = readRepository.getReadNews(kanal!!, guid!!)
if(ok){
loadingVisibility = View.GONE
}
}
REPOSITORY
class ReadRepositoryImpl private constructor(private val newsdataDao: NewsdataDao) : ReadRepository{
override fun observerReadNews(): MutableLiveData<AppResponse> {
return newsdataDao.resultRead
}
override fun getReadNews(channel: String, guid: Int) = newsdataDao.readNews(channel, guid)
companion object{
#Volatile private var instance: ReadRepositoryImpl? = null
fun getInstance(newsdataDao: NewsdataDao) = instance ?: synchronized(this){
instance ?: ReadRepositoryImpl(newsdataDao).also {
instance = it
}
}
}
}
MODEL / DATA SOURCE
class NewsdataDao {
private val apiService = ApiClient.getClient().create(ApiService::class.java)
var resultRead = MutableLiveData<AppResponse>()
fun readNews(channel: String, guid: Int): Boolean{
GlobalScope.launch {
val response = apiService.readNewsAsync(Constants.API_TOKEN, channel, guid.toString()).await()
when{
response.isSuccessful -> {
val res = response.body()
val appRes = AppResponse(true, "ok", res!!)
resultRead.postValue(appRes)
}
else -> {
val appRes = AppResponse(false, "Error: ${response.message()}", null)
resultRead.postValue(appRes)
}
}
}
return true
}
}
Perhaps this activity is not getting stopped.
Check this out:
When you call readViewModel.getReadNews() in onCreate() your activity is created once, only if onStop is called will it be created again.