Android Kotlin SingleLiveEvent With Flow - android

I am using a livedata on viewmodel and flow on repository and data source.
when I tried to connect them to each other and get a data stream as below
that error occurred
Error
java.lang.ClassCastException: androidx.lifecycle.CoroutineLiveData cannot be cast to com.versec.versecko.util.SingleLiveEvent
ViewModel
val singleChat : SingleLiveEvent<ChatRoomEntity> = repository.getChatRooms().asLiveData() as SingleLiveEvent<ChatRoomEntity>
Fragment
viewModel.singleChat.observe(viewLifecycleOwner, Observer {
roomList.add(it)
chatRoomAdapter.changeRooms(roomList)
chatRoomAdapter.notifyDataSetChanged()
})
SingleLiveEvent
class SingleLiveEvent<T> : MutableLiveData<T>() {
companion object {
private val TAG = "SingleLiveEvent"
}
private val pending : AtomicBoolean = AtomicBoolean(false)
#MainThread
override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
if (hasActiveObservers()) { Log.w(TAG, "Multiple Observers ,,,")}
super.observe(owner, Observer { t ->
if (pending.compareAndSet(true, false)) {
observer.onChanged(t)
}
})
}
#MainThread
override fun setValue(value: T?) {
pending.set(true)
super.setValue(value)
}
#MainThread
fun call() {value = null } }
what is the proper way to use both??

From what I understood, the reason you are using SingleLiveData is because you don't want the observer to receive same ChatRoomEntity twice in which case it will be duplicated in the list. A simple solution to this is to maintain the list inside the ViewModel and expose this list to the UI, then you won't have to use workarounds like SingleLiveData.
// ViewModel
private val _chatRoomsLiveData = MutableLiveData(emptyList<ChatRoomEntity>())
val chatRoomsLiveData: LiveData<List<ChatRoomEntity>> = _chatRoomsLiveData
init {
viewModelScope.launch {
repository.getChatRooms().collect {
_chatRoomsLiveData.value = _chatRoomsLiveData.value!! + it
}
}
}
// Fragment
viewModel.chatRoomsLiveData.observe(viewLifecycleOwner, Observer {
chatRoomAdapter.changeRooms(it)
chatRoomAdapter.notifyDataSetChanged()
})

Related

Live data observer triggered twice on fragment created

The issue that I have is not actually bug or big problem. And all works as it should, but nevertheless it annoys me.
In Fragment pbserver:
viewModel.pageNumbersPosition.observe(viewLifecycleOwner) {
if (it!=null) {
SharedPreferenceHelper.pagesNumber = viewModel.pageNumbersArray.value?.get(it)
DLog.d("Set: ${viewModel.pageNumbersArray.value?.get(it)}}")
//Log shows twice as start
}
}
ViewModel:
class MenuViewModel : ViewModel() {
var pageNumbersArray = MutableLiveData(getPageNumbering())
var pageNumbersPosition = MutableLiveData(pageNumbersArray.value?.indexOf(SharedPreferenceHelper.pagesNumber))
private fun getPageNumbering():Array<String> {
val list = mutableListOf<String>()
for (i in 1..25) {
list.add(i.toString())
}
return list.toTypedArray()
}
}
Spinner:
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#{viewModel.pageNumbersArray}"
android:selectedItemPosition="#={viewModel.pageNumbersPosition}"/>
What happes is viewModel.pageNumbersPosition.observe triggered twice on start. Once from the initiation of the fragment and second time when the spinner sets. This is actually suppose to happen, but I don't like it when Shared Preference sets twice.
I came across a handy class SingleLiveEvent that we can use instead of LiveData in ViewModel class to send only new updates after subscription.
class SingleLiveEvent<T> : MutableLiveData<T>() {
private val pending = AtomicBoolean(false)
override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
super.observe(owner, Observer<T> { t ->
if (pending.compareAndSet(true, false)) {
observer.onChanged(t)
}
})
}
override fun setValue(t: T?) {
pending.set(true)
super.setValue(t)
}
fun call() {
postValue(null)
}
}
This LiveData extension only calls the observable if there's an explicit call to setValue() or call().
Update, primary constructor with parameter:
class SingleLiveEvent<T>(value: T) : MutableLiveData<T>(value) {...}
You can check if there is equal value in your shared to avoid the double set
if (it!=null) {
viewModel.pageNumbersArray.value?.get(it).let{ value ->
if (SharedPreferenceHelper.pagesNumber != value)
SharedPreferenceHelper.pagesNumber = value
}
}

Avoid LiveData triggered twice

I have such issue:
Two fragments: A and B, which inject viewModel, but for some reason I have result of LiveData in both of my fragments.
How can I avoid triggering live data pushing me old value? How to reset liveData somehow or ignore old values? Thanks.
In both of them I am listening to LiveData changes like this:
#AndroidEntryPoint
class LoginFragment : MyBaseDebugFragment(R.layout.spinner_layout) {
#Inject
private val viewModel: AuthorizationViewModel by activityViewModels()
{
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
viewModel.loginActionLD.observe(viewLifecycleOwner) { loginStatus ->
//Do something regarding the value of login status.
}
viewModel.DoLogin()
}
#HiltViewModel
class AuthorizationViewModel #Inject constructor(
private val login: LoginUseCase,
private val logout: LogoutUseCase,
#Dispatcher.IO private val ioDispatcher: CoroutineDispatcher,
#Scope.Application private val externalScope: CoroutineScope,
) : ViewModel() {
private val _loginActionLD = MutableLiveData<LoginAction>()
val loginActionLD: LiveData<LoginAction> = _loginActionLD
fun DoLogin(from: AuthRequest) {
launchOnMyExternalScope {
_loginActionLD.postValue(login().toLoginAction(from))
}
}
private fun launchOnMyExternalScope(block: suspend CoroutineScope.() -> Unit) =
externalScope.launch(ioDispatcher, block = block)
}
}
#Module
#InstallIn(SingletonComponent::class)
object CoroutineScopeModule {
#Singleton
#Scope.Application
#Provides
fun provideApplicationScope(#Dispatcher.IO ioDispatcher: CoroutineDispatcher): CoroutineScope =
CoroutineScope(SupervisorJob() + ioDispatcher)
}
#Qualifier
#Retention(AnnotationRetention.BINARY)
annotation class Scope {
#Qualifier
#Retention(AnnotationRetention.BINARY)
annotation class Application
}
There is a handy class SingleLiveEvent that you can use instead of LiveData in your ViewModel class to send only new updates after subscription.
class SingleLiveEvent<T> : MutableLiveData<T>() {
private val pending = AtomicBoolean(false)
override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
super.observe(owner, Observer<T> { t ->
if (pending.compareAndSet(true, false)) {
observer.onChanged(t)
}
})
}
override fun setValue(t: T?) {
pending.set(true)
super.setValue(t)
}
fun call() {
postValue(null)
}
}
This LiveData extension only calls the observable if there's an explicit call to setValue() or call().
Here is what helped me to avoid LiveData to trigger twice it's handler. This code is tested carefully:
open class LiveEvent<T> : MediatorLiveData<T>() {
private val observers = ArraySet<OneTimeObserver<in T>>()
#MainThread
override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
val wrapper = OneTimeObserver(observer)
observers.add(wrapper)
super.observe(owner, wrapper)
}
#MainThread
override fun observeForever(observer: Observer<in T>) {
val wrapper = OneTimeObserver(observer)
observers.add(wrapper)
super.observeForever(wrapper)
}
#MainThread
override fun removeObserver(observer: Observer<in T>) {
if ((observer is OneTimeObserver && observers.remove(observer)) || observers.removeIf { it.observer == observer }) {
super.removeObserver(observer)
}
}
#MainThread
override fun setValue(t: T?) {
observers.forEach { it.newValue() }
super.setValue(t)
}
private class OneTimeObserver<T>(val observer: Observer<T>) : Observer<T> {
private var handled = AtomicBoolean(true)
override fun onChanged(t: T?) {
if (handled.compareAndSet(false, true)) observer.onChanged(t)
}
fun newValue() {
handled.set(false)
}
}
}
And then instead of such code:
private val _loginAction = MutableLiveData<LoginAction>()
val loginActionLD: LiveData<LoginAction> = _loginAction
I have used this code:
private val _loginAction = LiveEvent<LoginAction>()
val loginActionLD: LiveData<LoginAction> = _loginAction

livedata observer called each time when back on fragment

I have fragment1, from which I go to fragment2.
The problem is that I can't go back to fragment1 from fragment2
This is how I handle a button click
val onClickLiveData = MutableLiveData<OnClick>()
fun onClick() {
onClickLiveData.value = OnClick.CLICK
}
This is how I handle transition to fragment2
private fun subscribeToClickCallbacks() {
viewModel.onClickLiveData.observe(viewLifecycleOwner, Observer {
findNavController().navigate(R.id.action_home_fragment_to_repositories_fragment)
})
}
I process the transition back like this
navController.popBackStack()
With the help of debug, I found out that with each transition to fragment1, livedata is called and instantly opens fragment2.
How can the problem be solved? I would be grateful for every answer.
If live data is observer multiple times use SingleEvent for handle this case.
Create one global class for SingleLiveData like this.
class SingleLiveEvent<T> : MutableLiveData<T>() {
private val mPending = AtomicBoolean(false)
#MainThread
override fun observe(owner: LifecycleOwner, observer: Observer<T>) {
if (hasActiveObservers()) {
Log.w(TAG, "Multiple observers registered but only one will be notified of changes.")
}
// Observe the internal MutableLiveData
super.observe(owner, object : Observer<T> {
override fun onChanged(t: T?) {
if (mPending.compareAndSet(true, false)) {
observer.onChanged(t)
}
}
})
}
#MainThread
override fun setValue(t: T?) {
mPending.set(true)
super.setValue(t)
}
/**
* Used for cases where T is Void, to make calls cleaner.
*/
#MainThread
fun call() {
setValue(null)
}
companion object {
private val TAG = "SingleLiveEvent"
}
}
Now use this class like this,
val onClickLiveData = SingleLiveEvent<OnClick>()

Viewmodel preserving data after finishing activity

I have a MainActivity form that I am opening CreatePassword Activity in that, I am saving password and finish CreatePasswordActivity with sending Intent back to MainActivity.
Like MainActivity -----> CreatePassword(Finish) ---Intent----> MainActivity
private fun observeIntentResult() {
generatePasswordViewModel.getIntentResult().observe(this#CreatePasswordActivity, androidx.lifecycle.Observer { intent ->
Toast.makeText(this, "Got Same Data", Toast.LENGTH_SHORT).show()
setResult(Activity.RESULT_OK, intent)
finish()
})
}
But Now when I open CreatePasswordActivity again from MainActivity, it's LiveData automatically sending previous data (intent) and CreatePasswordActivity is suddenly finished.
Do I have any mistake in implementing code? Anybody have idea how to resolve this?
Finally I have used below class, which helped me to resolve current issue, will see and update answer in future if I will be able to find any better solution.
open class VolatileLiveData<T> : MutableLiveData<T>() {
private val lastValueSeq = AtomicInteger(0)
private val wrappers = HashMap<Observer<in T>, Observer<T>>()
#MainThread
public override fun setValue(value: T) {
lastValueSeq.incrementAndGet()
super.setValue(value)
}
#MainThread
public override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
val observerWrapper = ObserverWrapper(lastValueSeq, observer)
wrappers[observer] = observerWrapper
super.observe(owner, observerWrapper)
}
#MainThread
public override fun observeForever(observer: Observer<in T>) {
val observerWrapper = ObserverWrapper(lastValueSeq, observer)
wrappers[observer] = observerWrapper
super.observeForever(observerWrapper)
}
#MainThread
public override fun removeObserver(observer: Observer<in T>) {
val observerWrapper = wrappers[observer]
observerWrapper?.let {
wrappers.remove(observerWrapper)
super.removeObserver(observerWrapper)
}
}
}
private class ObserverWrapper<T>(private var currentSeq: AtomicInteger, private val observer: Observer<in T>) : Observer<T> {
private val initialSeq = currentSeq.get()
private var _observer: Observer<in T> = Observer {
if (currentSeq.get() != initialSeq) {
// Optimization: this wrapper implementation is only needed in the beginning.
// Once a valid call is made (i.e. with a different concurrent sequence), we
// get rid of it any apply the real implementation as a direct callthrough.
_observer = observer
_observer.onChanged(it)
}
}
override fun onChanged(value: T) {
_observer.onChanged(value)
}
}

Navigation Component - Problem with Livedata lifecycle

I'm beginning with navigation components and I'm facing some problem with a livedata observer.
For example:
I have this livedata, who manage auth response from server.
viewModel.authenticate.observe(this, Observer {
manageAuthResponse(it)
})
Everything works fine, and I go to Fragment B.
But when I'm in Fragment B, and I try to go back to Fragment A (who contains that livedata), the Observer fires again with the previous result (SUCCESS).
How can I prevent this?
When I go back, I want to refresh this result and prevent livedata observer to be fired.
Wrap your LiveData object in a ConsumableValue like this
class ConsumableValue<T>(private val data: T) {
private var consumed = false
fun consume(block: ConsumableValue<T>.(T) -> Unit) {
if (!consumed) {
consumed = true
block(data)
}
}
}
then in viewmodel
val authenticate = MutableLiveData<Consumable<AuthenticationObject>>()
and in your fragment
viewModel.authenticate.observe(this, Observer { consumable ->
consumable.consume {
manageAuthResponse(it)
}
})
Wrap the LiveDate Like this
open class LiveEvent<T> : MediatorLiveData<T>() {
private val observers = ArraySet<ObserverWrapper<in T>>()
#MainThread
override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
val wrapper = ObserverWrapper(observer)
observers.add(wrapper)
super.observe(owner, wrapper)
}
#MainThread
override fun observeForever(observer: Observer<in T>) {
val wrapper = ObserverWrapper(observer)
observers.add(wrapper)
super.observeForever(wrapper)
}
#MainThread
override fun removeObserver(observer: Observer<in T>) {
if (observers.remove(observer)) {
super.removeObserver(observer)
return
}
val iterator = observers.iterator()
while (iterator.hasNext()) {
val wrapper = iterator.next()
if (wrapper.observer == observer) {
iterator.remove()
super.removeObserver(wrapper)
break
}
}
}
#MainThread
override fun setValue(t: T?) {
observers.forEach { it.newValue() }
super.setValue(t)
}
private class ObserverWrapper<T>(val observer: Observer<T>) : Observer<T> {
private var pending = false
override fun onChanged(t: T?) {
if (pending) {
pending = false
observer.onChanged(t)
}
}
fun newValue() {
pending = true
}
}
}
then in ViewModel
val viewModel = LiveEvent<Resource<String>>()
This solution is work for me
You can check out the code in this github

Categories

Resources