Skip to content

Instantly share code, notes, and snippets.

@supercoffee
Created November 20, 2019 17:22
Show Gist options
  • Save supercoffee/3da8cffc71c269da00a7e33eccff4595 to your computer and use it in GitHub Desktop.
Save supercoffee/3da8cffc71c269da00a7e33eccff4595 to your computer and use it in GitHub Desktop.
Kotlin Data binding nullability
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="weather" type="org.example.kotlin.databinding.WeatherData" />
</data>
<LinearLayout
android:orientation="vertical"
android:layout_margin="4dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="@{weather.location}"
android:textSize="18sp" />
<TextView android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="@{weather.temperature}" />
<TextView android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="@{weather.info}" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@{weather.imageUrl}"
android:contentDescription="@string/image" />
<Button
android:text="@string/change"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="changeTemperatureAndImage" />
<Button
android:text="@string/next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="startActivity" />
</LinearLayout>
</layout>
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.example.kotlin.databinding.databinding;
import android.databinding.DataBindingComponent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.databinding.ViewDataBinding.IncludedLayouts;
import android.databinding.adapters.TextViewBindingAdapter;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.example.kotlin.databinding.ImageBindingAdapter;
import org.example.kotlin.databinding.WeatherData;
public class ActivityMainBinding extends ViewDataBinding {
@Nullable
private static final IncludedLayouts sIncludes = null;
@Nullable
private static final SparseIntArray sViewsWithIds = null;
@NonNull
private final LinearLayout mboundView0;
@NonNull
private final TextView mboundView1;
@NonNull
private final TextView mboundView2;
@NonNull
private final TextView mboundView3;
@NonNull
private final ImageView mboundView4;
@Nullable
private WeatherData mData;
private long mDirtyFlags = -1L;
public ActivityMainBinding(@NonNull DataBindingComponent bindingComponent, @NonNull View root) {
super(bindingComponent, root, 1);
Object[] bindings = mapBindings(bindingComponent, root, 5, sIncludes, sViewsWithIds);
this.mboundView0 = (LinearLayout)bindings[0];
this.mboundView0.setTag((Object)null);
this.mboundView1 = (TextView)bindings[1];
this.mboundView1.setTag((Object)null);
this.mboundView2 = (TextView)bindings[2];
this.mboundView2.setTag((Object)null);
this.mboundView3 = (TextView)bindings[3];
this.mboundView3.setTag((Object)null);
this.mboundView4 = (ImageView)bindings[4];
this.mboundView4.setTag((Object)null);
this.setRootTag(root);
this.invalidateAll();
}
public void invalidateAll() {
synchronized(this) {
this.mDirtyFlags = 2L;
}
this.requestRebind();
}
public boolean hasPendingBindings() {
synchronized(this) {
return this.mDirtyFlags != 0L;
}
}
public boolean setVariable(int variableId, @Nullable Object variable) {
boolean variableSet = true;
if (1 == variableId) {
this.setData((WeatherData)variable);
} else {
variableSet = false;
}
return variableSet;
}
public void setData(@Nullable WeatherData Data) {
this.updateRegistration(0, Data);
this.mData = Data;
synchronized(this) {
this.mDirtyFlags |= 1L;
}
this.notifyPropertyChanged(1);
super.requestRebind();
}
@Nullable
public WeatherData getData() {
return this.mData;
}
protected boolean onFieldChange(int localFieldId, Object object, int fieldId) {
switch(localFieldId) {
case 0:
return this.onChangeData((WeatherData)object, fieldId);
default:
return false;
}
}
private boolean onChangeData(WeatherData Data, int fieldId) {
if (fieldId == 0) {
synchronized(this) {
this.mDirtyFlags |= 1L;
return true;
}
} else {
return false;
}
}
protected void executeBindings() {
long dirtyFlags = 0L;
synchronized(this) {
dirtyFlags = this.mDirtyFlags;
this.mDirtyFlags = 0L;
}
String dataTemperature = null;
String dataImageUrl = null;
WeatherData data = this.mData;
String dataLocation = null;
String dataInfo = null;
if ((dirtyFlags & 3L) != 0L && data != null) {
dataTemperature = data.getTemperature();
dataImageUrl = data.getImageUrl();
dataLocation = data.getLocation();
dataInfo = data.getInfo();
}
if ((dirtyFlags & 3L) != 0L) {
TextViewBindingAdapter.setText(this.mboundView1, dataLocation);
TextViewBindingAdapter.setText(this.mboundView2, dataTemperature);
TextViewBindingAdapter.setText(this.mboundView3, dataInfo);
ImageBindingAdapter.setImageUrl(this.mboundView4, dataImageUrl);
}
}
@NonNull
public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup root, boolean attachToRoot) {
return inflate(inflater, root, attachToRoot, DataBindingUtil.getDefaultComponent());
}
@NonNull
public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup root, boolean attachToRoot, @Nullable DataBindingComponent bindingComponent) {
return (ActivityMainBinding)DataBindingUtil.inflate(inflater, 2131296283, root, attachToRoot, bindingComponent);
}
@NonNull
public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater) {
return inflate(inflater, DataBindingUtil.getDefaultComponent());
}
@NonNull
public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater, @Nullable DataBindingComponent bindingComponent) {
return bind(inflater.inflate(2131296283, (ViewGroup)null, false), bindingComponent);
}
@NonNull
public static ActivityMainBinding bind(@NonNull View view) {
return bind(view, DataBindingUtil.getDefaultComponent());
}
@NonNull
public static ActivityMainBinding bind(@NonNull View view, @Nullable DataBindingComponent bindingComponent) {
if (!"layout/activity_main_0".equals(view.getTag())) {
throw new RuntimeException("view tag isn't correct on view:" + view.getTag());
} else {
return new ActivityMainBinding(bindingComponent, view);
}
}
}
package org.example.kotlin.databinding
import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import org.example.kotlin.databinding.databinding.ActivityMainBinding
import org.jetbrains.anko.startActivity
class MainActivity : AppCompatActivity() {
var counter = 1
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.weather = WeatherData(
"Saint Petersburg",
"10 °C",
"Cloudy with rain and possible thunderstorms",
"http://loremflickr.com/800/600/city")
}
fun changeTemperatureAndImage(view: View) {
++counter
binding.apply {
weather.temperature = "$counter c"; // This is a compile error
// See line 98 in ActivityMainBinding for the declaration of the getWeather method.
}
}
fun startActivity(view: View) = startActivity<OtherActivity>()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment