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

introduce retrolambda also into realm-helpers

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