question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement Observable pattern like MutableLiveData in Android

See original GitHub issue

Is your feature request related to a problem? Please describe. This feature suggestion aims to solve the problem of listening for variable value change (normally managed with JavaRX) without the need of importing the whole JavaRX system.

Describe the solution you’d like It would be great to have a decorator that provides Observable features.

Describe alternatives you’ve considered There are currently no elegant solutions available:

  • Observable pattern: verbose and now deprecated in Java
  • JavaRX: a-way-too-wide system to import only for such a small problem
  • PropertyChangeSupport: it works but it’s too verbose and scattered, it would be glad to have a wrapper for it

Additional context Below a potential working solution (without the decorators):

package it.muzzialessandro.utils;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.UUID;

public class Observable<T> {

    private T value;
    private final PropertyChangeSupport pcSupport;
    private final String uuid;

    public Observable(T value) {
        this.value = value;
        this.pcSupport = new PropertyChangeSupport(this);
        this.uuid = UUID.randomUUID().toString();
        this.pcSupport.addPropertyChangeListener(evt -> {
                    synchronized (this) {
                        this.notify();
                    }
                });
    }

    //region Public Methods

    public void addPropertyChangeListener(PropertyChangeListener pcl) {
        this.pcSupport.addPropertyChangeListener(pcl);
    }

    public void removePropertyChangeListener(PropertyChangeListener pcl) {
        this.pcSupport.removePropertyChangeListener(pcl);
    }

    public void listenForChange() {
        synchronized (this) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //endregion

    //region Getter and Setter

    public T getValue() {
        return value;
    }

    public synchronized void setValue(T value) {
        this.pcSupport.firePropertyChange(this.uuid, this.value, value);
        this.value = value;
    }

    public String getUuid() {
        return this.uuid;
    }

    //endregion

}

Usage:

declaration:

Observable<String> strObs;

synchronous:

strObs.listenForChange();

asynchronous:

strObs.addPropertyChangeListener(evt -> doSomething());

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
AleMuzzicommented, Mar 30, 2021

I must have explained myself badly, my intention was to imitate the behavior of that Android class. I would really appreciate the lazy decorator, I’ve used lazy properties a couple of times in Kotlin. Honestly I didn’t understand the concept of “dynamic properties” but I will wait for news from you, count on me for the feedback 💪 If you prefer just send me a standard email instead of writing here, do as you prefer.

0reactions
AleMuzzicommented, Mar 30, 2021

Yeah, great idea, keep me updated

Read more comments on GitHub >

github_iconTop Results From Across the Web

LiveData overview - Android Developers
The Room persistence library supports observable queries, which return LiveData objects. Observable queries are written as part of a Database ...
Read more >
How to implement observer pattern in android studio?
In Android, the observer pattern is implemented by using the class ViewModel and the class LiveData / StateFlow. If you want to have...
Read more >
LiveData vs ObservableField in Android - MindOrks
In this blog, we will talk about LiveData vs ObservableField in Android. We will also talk about the differences and also compare them....
Read more >
ViewModels and LiveData: Patterns + AntiPatterns - Medium
The most common use case for LiveData is using MutableLiveData in ViewModels and exposing them as LiveData to make them immutable from the ......
Read more >
Observer Pattern In Kotlin
Also known as Publish / Subscribe Pattern, Signal-Slot Pattern. Part of Event-Driven Architecture. In this tutorial, we will show you how to implement...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found