Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Support for Data Binding #28

Closed
Dev-iL opened this issue Feb 2, 2016 · 4 comments
Closed

Feature request: Support for Data Binding #28

Dev-iL opened this issue Feb 2, 2016 · 4 comments

Comments

@Dev-iL
Copy link
Contributor

Dev-iL commented Feb 2, 2016

I'm building an app that relies heavily on data binding. I was trying to include an instance of RelativeTimeTextView with XML bindings for app:relative_time_prefix and app:reference_time - however this results in a compilation-time error:

Error:java.lang.RuntimeException: Found data binding errors.
        ****/ data binding error ****msg:Cannot find the setter for attribute 'app:relative_time_prefix' with parameter type java.lang.String.

I'm not sure if what's causing this is the fact attributes/fields aren't set as @Bindable or Observable.., or that the property setters aren't named like the binding expects (if I understand the Data Binding documentation correctly).

Needless to say that when I remove the bindings of these fields from my XML, the compilation succeeds.

It would be awesome if somebody could add this support - and help make the library more future-proof :)


I'm using the following XML (irrelevant portions removed):

<data>
    <variable name="message" type="..."/>
</data>
....
<com.github.curioustechizen.ago.RelativeTimeTextView
    android:id="@+id/time_n_sender"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="14sp"
    app:relative_time_prefix='@{"by " + message.senderName + ", "}'
    app:reference_time='@{message.timestamp}'
    tools:textColor="@color/gray"
    tools:text="by Mike, 30/02/2016 22:22:10"/>

P.S.
Does the format dd/MM/yyyy HH:mm:ss constitute a valid for app:reference_time?

@curioustechizen
Copy link
Owner

The problem as you rightly pointed out is the way the custom attribute and its setter method have been named. The naming is incompatible with data binding.

I am considering deprecating the prefix and suffix attributes in favor of a more general mechanism (like a format specifier with positional parameters). See #15

Hence I don't see myself updating the library to include this functionality. However, if you take the time to implement it (simply adding a setRelativeTimePrefix method that just calls setPrefix) then I would be glad to accept pull requests.

For your question about the date format - see #19

@Dev-iL
Copy link
Contributor Author

Dev-iL commented Feb 5, 2016

@curioustechizen I think the right course of action consists of including the following class in the codebase, the presence of which should be sufficient for the binding to work correctly.

However, the version of gradle has to be updated (is that a bad thing...?) from 1.0.0 to at least 1.5.0-alpha1 as mentioned at the top of the Data Binding guide. The latest version of the android-gradle plugin at the time of writing is 2.0.0-beta2. I was trying to do this locally but ran into some difficulties. (Possibly because one can't have data-binding inside a apply plugin: 'com.android.library' -type module.)

BTW, not only the gradle plugin, but also the build tools and the target sdk should be updated (to API-23).

As per the example class in this link, a class like this should appear in the project:

package com.your.package;

import android.databinding.BindingAdapter;
import android.databinding.BindingMethod;
import android.databinding.BindingMethods;
import com.github.curioustechizen.ago.RelativeTimeTextView;

@BindingMethods({
        @BindingMethod(type = RelativeTimeTextView.class, attribute = "rttv:relative_time_prefix", method = "setPrefix"),
        @BindingMethod(type = RelativeTimeTextView.class, attribute = "rttv:relative_time_suffix", method = "setSuffix"),
})
public class RelativeTimeTextViewBindingAdapter {
    @BindingAdapter("rttv:reference_time")
    public static void setReferenceTime(RelativeTimeTextView view, long time) {
        view.setReferenceTime(time);
    }
}

(Note the custom namespace rttv:, which is an optional modification that makes sense to include). The XML is then:

<com.github.curioustechizen.ago.RelativeTimeTextView
    android:id="@+id/time_n_sender"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="14sp"
    rttv:relative_time_prefix='@{"by " + message.senderName + ", "}'
    rttv:reference_time='@{message.timestamp}'
    tools:textColor="@color/gray"
    tools:text="by Mike, 30/02/2016 22:22:10"/>

Note to users:
Adding the above anywhere in a project which uses this library should make it work with data-binding.

@Dev-iL Dev-iL closed this as completed Feb 5, 2016
@Dev-iL
Copy link
Contributor Author

Dev-iL commented Feb 6, 2016

A complete example of RelativeTimeTextView with data-binding can be found here:
Dev-iL@37ab447

@curioustechizen
Copy link
Owner

@Dev-iL This is very helpful. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants