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,27 +104,21 @@ public class RealmHelper { ...@@ -104,27 +104,21 @@ 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();
task.setResult(null);
} }
}, new Realm.Transaction.OnError() { }, () -> {
@Override public void onError(Throwable error) { realm.close();
realm.close(); task.setResult(null);
if (error instanceof Exception) { }, error -> {
task.setError((Exception) error); realm.close();
} else { if (error instanceof Exception) {
task.setError(new Exception(error)); task.setError((Exception) error);
} } else {
task.setError(new Exception(error));
} }
}); });
...@@ -143,7 +137,7 @@ public class RealmHelper { ...@@ -143,7 +137,7 @@ public class RealmHelper {
public <T extends RealmObject, VM, VH extends RealmModelViewHolder<VM>> public <T extends RealmObject, VM, VH extends RealmModelViewHolder<VM>>
RecyclerView.Adapter<VH> createListAdapter(Context context, RealmListObserver.Query<T> query, 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); return constructor.getNewInstance(context).initializeWith(this, query);
} }
} }
...@@ -32,11 +32,9 @@ public class RealmListObserver<T extends RealmObject> extends AbstractRealmResul ...@@ -32,11 +32,9 @@ 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);
}
} }
}; };
} }
......
...@@ -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,12 +67,10 @@ public abstract class RealmModelListAdapter<T extends RealmObject, VM, ...@@ -71,12 +67,10 @@ 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,18 +48,16 @@ public class RealmObjectObserver<T extends RealmObject> extends AbstractRealmRes ...@@ -48,18 +48,16 @@ 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)) { return;
return; }
} previousResultString = currentResultString;
previousResultString = currentResultString; if (onUpdateListener != null) {
if (onUpdateListener != null) { 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