Code Monkey home page Code Monkey logo

realm-databind-experiment's Introduction

realm-databind-experiment

Making Realm work with Databinding.

Look at this beautiful RealmObject

package com.zhuinden.realmdatabind.realm.objects;

import android.databinding.Bindable;
import android.databinding.Observable;
import android.databinding.PropertyChangeRegistry;

import com.zhuinden.realmdatabind.BR;
import com.zhuinden.realmdatabind.realm.databind.RealmDataBinding;

import io.realm.RealmObject;
import io.realm.annotations.Ignore;
import io.realm.annotations.PrimaryKey;

/*
 * Created by Zhuinden on 2016.09.04..
 */
public class Post extends RealmObject implements Observable, RealmDataBinding {
    @PrimaryKey
    private long id;

    private String text;

    @Ignore
    private transient PropertyChangeRegistry mCallbacks;

    @Bindable
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
        if(!isValid()) { // !isManaged() in Realm 2.0
            notifyPropertyChanged(BR.id);
        }
    }

    @Bindable
    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
        if(!isValid()) { // !isManaged() in Realm 2.0
            notifyPropertyChanged(BR.text);
        }
    }

    @Override
    public synchronized void addOnPropertyChangedCallback(OnPropertyChangedCallback callback) {
        if (mCallbacks == null) {
            mCallbacks = new PropertyChangeRegistry();
        }
        mCallbacks.add(callback);
    }

    @Override
    public synchronized void removeOnPropertyChangedCallback(OnPropertyChangedCallback callback) {
        if (mCallbacks != null) {
            mCallbacks.remove(callback);
        }
    }

    /**
     * Notifies listeners that all properties of this instance have changed.
     */
    @Override
    public synchronized void notifyChange() {
        if (mCallbacks != null) {
            mCallbacks.notifyCallbacks(this, 0, null);
        }
    }

    /**
     * Notifies listeners that a specific property has changed. The getter for the property
     * that changes should be marked with {@link Bindable} to generate a field in
     * <code>BR</code> to be used as <code>fieldId</code>.
     *
     * @param fieldId The generated BR id for the Bindable field.
     */
    public void notifyPropertyChanged(int fieldId) {
        if (mCallbacks != null) {
            mCallbacks.notifyCallbacks(this, fieldId, null);
        }
    }
}

realm-databind-experiment's People

Contributors

zhuinden avatar

Stargazers

W-ANGELOS-ΑΓΓΕΛΟΣ avatar Andrew Hughes avatar Ahmed Sabry avatar Matías Dumrauf avatar jinjoochoi avatar Chris Gaddes avatar Roger Paris avatar Wade avatar

Watchers

James Cloos avatar  avatar  avatar

realm-databind-experiment's Issues

How to do this with Java 1.7 ?

How to do this with Java 1.7 ?
I can't do below 1.8

RealmDataBinding.Factory FACTORY = () -> element -> {
    if(element instanceof RealmDataBinding) {
        ((RealmDataBinding)element).notifyChange();
    }
};

Transient has been supported. What about now?!

I read your publication on Medium and I saw you mentioned this:

You might not know, but a class that defines the Realm schema must either implement RealmModel and have @RealmClass annotation, or extend RealmObject.
This also means you can’t extend BaseObservable easily, because it has a transient field, and transient fields are not supported.

On the other hand, I see this closed issue: 4436.
Honestly, I get confused! 😕 What is best practice code to work with Realm and DataBinding simultaneously?

P.S. If the best solution is using transient nowadays, please explain with a snipped code. 🙏

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.