Last active
April 3, 2022 13:59
-
-
Save rajabiy/45c9e0f8c31be8b5b05ccff21d6aa8f6 to your computer and use it in GitHub Desktop.
Android two way data binding for date field with @InverseBindingAdapter and @BindingAdapter decorators for Realm model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package uz.aminiy.inventory.models; | |
import android.databinding.BindingAdapter; | |
import android.databinding.InverseBindingAdapter; | |
import android.widget.TextView; | |
import java.text.DateFormat; | |
import java.text.ParseException; | |
import java.util.Date; | |
import java.util.UUID; | |
import io.realm.RealmObject; | |
import io.realm.RealmResults; | |
import io.realm.annotations.LinkingObjects; | |
import io.realm.annotations.PrimaryKey; | |
/** | |
* Created by amin on 05.03.18. | |
*/ | |
public class Asset extends RealmObject { | |
@PrimaryKey | |
private String id = UUID.randomUUID().toString(); | |
private Date editedAt; | |
private String number; | |
private String serial_number; | |
private String name; | |
private AssetType type; | |
private AssetModel model; | |
private Date arrival_date; | |
private Supplier supplier; | |
private Workspace workspace; | |
private Float summa; | |
private Float remain; | |
private Date inventory_date; | |
@LinkingObjects("asset") | |
private final RealmResults<AssetCharacteristic> characteristics = null; | |
public Asset() { | |
id = UUID.randomUUID().toString(); | |
editedAt = new Date(); | |
} | |
public String getId() { | |
return id; | |
} | |
public void setId(String id) { | |
this.id = id; | |
editedAt = new Date(); | |
} | |
public String getNumber() { | |
return number; | |
} | |
public void setNumber(String number) { | |
this.number = number; | |
editedAt = new Date(); | |
} | |
public String getSerial_number() { | |
return serial_number; | |
} | |
public void setSerial_number(String serial_number) { | |
this.serial_number = serial_number; | |
editedAt = new Date(); | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
editedAt = new Date(); | |
} | |
public AssetType getType() { | |
return type; | |
} | |
public AssetModel getModel() { | |
return model; | |
} | |
public void setModel(AssetModel model) { | |
this.model = model; | |
this.type = model.getAssettype(); | |
editedAt = new Date(); | |
} | |
@BindingAdapter("android:text") | |
public static void setText(TextView view, Date date) { | |
if (date!=null) { | |
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM); | |
String localizedDate = df.format(date); | |
view.setText(localizedDate); | |
} | |
} | |
@BindingAdapter("android:text") | |
public static void setText(TextView view, Float aFloat) { | |
if (aFloat!=null) { | |
view.setText(aFloat.toString()); | |
} | |
} | |
@BindingAdapter("android:date") | |
public static void setDate(TextView view, Date date) { | |
if (date!=null) { | |
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM); | |
String localizedDate = df.format(date); | |
view.setText(localizedDate); | |
} | |
} | |
@InverseBindingAdapter(attribute = "android:text", event = "android:textAttrChanged") | |
public static Float captureTextValue(TextView view) { | |
CharSequence newValue = view.getText(); | |
return Float.parseFloat(newValue.toString()); | |
} | |
@InverseBindingAdapter(attribute = "android:text", event = "android:textAttrChanged") | |
public static Date captureDateValue(TextView view) { | |
CharSequence date = view.getText(); | |
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); | |
Date date1 = new Date(); | |
if (date!=null) { | |
try { | |
date1 = df.parse(date.toString()); | |
} | |
catch (ParseException pe) | |
{ | |
} | |
} | |
return date1; | |
} | |
@InverseBindingAdapter(attribute = "android:date", event = "android:textAttrChanged") | |
public static Date DateValue(TextView view) { | |
CharSequence date = view.getText(); | |
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); | |
Date date1 = new Date(); | |
if (date!=null) { | |
try { | |
date1 = df.parse(date.toString()); | |
} | |
catch (ParseException pe) | |
{ | |
} | |
} | |
return date1; | |
} | |
public Date getArrival_date() { | |
return arrival_date; | |
} | |
public void setArrival_date(Date arrival_date) { | |
this.arrival_date = arrival_date; | |
editedAt = new Date(); | |
} | |
public Supplier getSupplier() { | |
return supplier; | |
} | |
public void setSupplier(Supplier supplier) { | |
this.supplier = supplier; | |
editedAt = new Date(); | |
} | |
public Workspace getWorkspace() { | |
return workspace; | |
} | |
public void setWorkspace(Workspace workspace) { | |
this.workspace = workspace; | |
editedAt = new Date(); | |
} | |
public Float getSumma() { | |
return summa; | |
} | |
public void setSumma(Float summa) { | |
this.summa = summa; | |
editedAt = new Date(); | |
} | |
public Float getRemain() { | |
return remain; | |
} | |
public void setRemain(Float remain) { | |
this.remain = remain; | |
editedAt = new Date(); | |
} | |
public Date getInventory_date() { | |
return inventory_date; | |
} | |
public void setInventory_date(Date inventory_date) { | |
this.inventory_date = inventory_date; | |
editedAt = new Date(); | |
} | |
public Date getEditedAt() { | |
return editedAt; | |
} | |
public void setEditedAt(Date editedAt) { | |
this.editedAt = editedAt; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment