Commit 56fb3177 authored by Yusuke Iwaki's avatar Yusuke Iwaki

introduce retrolambda also into realm-helpers

parent 9f7e2efa
......@@ -2,4 +2,7 @@
<issue id="InvalidPackage">
<ignore regexp="okio.*jar"/>
</issue>
<issue id="NewApi">
<ignore regexp="Try-with-resources requires API level 19"/>
</issue>
</lint>
\ No newline at end of file
apply plugin: 'com.android.library'
apply plugin: 'realm-android'
apply plugin: 'me.tatarka.retrolambda'
buildscript {
repositories {
......@@ -8,6 +9,8 @@ buildscript {
dependencies {
classpath rootProject.ext.androidPlugin
classpath rootProject.ext.realmPlugin
classpath rootProject.ext.retroLambdaPlugin
classpath rootProject.ext.retroLambdaPatch
}
}
......@@ -15,6 +18,10 @@ android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.compileSdkVersion
......
<lint>
<issue id="NewApi">
<ignore regexp="Try-with-resources requires API level 19"/>
</issue>
</lint>
\ No newline at end of file
......@@ -9,7 +9,9 @@ abstract class AbstractRealmResultsObserver<T extends RealmObject> {
protected Realm realm;
private RealmChangeListener<RealmResults<T>> listener;
protected abstract RealmResults<T> queryItems(Realm realm);
protected abstract RealmChangeListener<RealmResults<T>> getListener();
private RealmResults<T> results;
......
......@@ -104,27 +104,21 @@ public class RealmHelper {
final TaskCompletionSource<Void> task = new TaskCompletionSource<>();
final Realm realm = instance();
realm.executeTransactionAsync(new Realm.Transaction() {
@Override public void execute(Realm realm) {
try {
transaction.execute(realm);
} catch (JSONException exception) {
throw new RuntimeException(exception);
}
}
}, new Realm.Transaction.OnSuccess() {
@Override public void onSuccess() {
realm.close();
task.setResult(null);
realm.executeTransactionAsync(_realm -> {
try {
transaction.execute(_realm);
} catch (JSONException exception) {
throw new RuntimeException(exception);
}
}, new Realm.Transaction.OnError() {
@Override public void onError(Throwable error) {
realm.close();
if (error instanceof Exception) {
task.setError((Exception) error);
} else {
task.setError(new Exception(error));
}
}, () -> {
realm.close();
task.setResult(null);
}, error -> {
realm.close();
if (error instanceof Exception) {
task.setError((Exception) error);
} else {
task.setError(new Exception(error));
}
});
......@@ -143,7 +137,7 @@ public class RealmHelper {
public <T extends RealmObject, VM, VH extends RealmModelViewHolder<VM>>
RecyclerView.Adapter<VH> createListAdapter(Context context, RealmListObserver.Query<T> query,
RealmModelListAdapter.Constructor<T, VM, VH> constructor) {
RealmModelListAdapter.Constructor<T, VM, VH> constructor) {
return constructor.getNewInstance(context).initializeWith(this, query);
}
}
......@@ -32,11 +32,9 @@ public class RealmListObserver<T extends RealmObject> extends AbstractRealmResul
}
@Override public final RealmChangeListener<RealmResults<T>> getListener() {
return new RealmChangeListener<RealmResults<T>>() {
@Override public void onChange(RealmResults<T> element) {
if (onUpdateListener != null) {
onUpdateListener.onUpdateResults(element);
}
return element -> {
if (onUpdateListener != null) {
onUpdateListener.onUpdateResults(element);
}
};
}
......
......@@ -32,11 +32,7 @@ public abstract class RealmModelListAdapter<T extends RealmObject, VM,
/*package*/ RealmModelListAdapter<T, VM, VH> initializeWith(final RealmHelper realmHelper,
RealmListObserver.Query<T> query) {
realmListObserver = new RealmListObserver<>(realmHelper, query)
.setOnUpdateListener(new RealmListObserver.OnUpdateListener<T>() {
@Override public void onUpdateResults(List<T> results) {
updateData(realmHelper.copyFromRealm(results));
}
});
.setOnUpdateListener(results -> updateData(realmHelper.copyFromRealm(results)));
return this;
}
......@@ -71,12 +67,10 @@ public abstract class RealmModelListAdapter<T extends RealmObject, VM,
@Override public void onBindViewHolder(VH holder, int position) {
VM model = getItem(position);
holder.itemView.setTag(model);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
VM model2 = (VM) (view.getTag());
if (model2 != null && onItemClickListener != null) {
onItemClickListener.onItemClick(model2);
}
holder.itemView.setOnClickListener(view -> {
VM model2 = (VM) (view.getTag());
if (model2 != null && onItemClickListener != null) {
onItemClickListener.onItemClick(model2);
}
});
holder.bind(model);
......
......@@ -48,18 +48,16 @@ public class RealmObjectObserver<T extends RealmObject> extends AbstractRealmRes
}
@Override protected final RealmChangeListener<RealmResults<T>> getListener() {
return new RealmChangeListener<RealmResults<T>>() {
@Override public void onChange(RealmResults<T> element) {
T currentResult = impl.extractObjectFromResults(element);
String currentResultString = currentResult != null ? currentResult.toString() : null;
if (previousResultString != null && previousResultString.equals(currentResultString)) {
return;
}
previousResultString = currentResultString;
if (onUpdateListener != null) {
onUpdateListener.onUpdateObject(
currentResult != null ? realm.copyFromRealm(currentResult) : null);
}
return element -> {
T currentResult = impl.extractObjectFromResults(element);
String currentResultString = currentResult != null ? currentResult.toString() : null;
if (previousResultString != null && previousResultString.equals(currentResultString)) {
return;
}
previousResultString = currentResultString;
if (onUpdateListener != null) {
onUpdateListener.onUpdateObject(
currentResult != null ? realm.copyFromRealm(currentResult) : null);
}
};
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment