Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test with databinding #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions adapters/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ android {
targetSdkVersion sdkVersion
project.archivesBaseName = "android-adapters"
}

dataBinding {
enabled = true
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.realm;

import android.app.Activity;
import android.databinding.BaseObservable;

import java.util.ArrayList;
import java.util.List;

public abstract class RealmObjectDataBindingListener<T> implements RealmObjectChangeListener<T> {

private final BaseObservable observable;
private final List<String> fieldNames;
private final List<Integer> fieldIds;

private RealmObjectDataBindingListener(BaseObservable observable,
List<String> fieldNames, List<Integer> fieldIds) {
this.observable = observable;
this.fieldNames = fieldNames;
this.fieldIds = fieldIds;
}

@Override
public void onChange(T t, ObjectChangeSet objectChangeSet) {
if (objectChangeSet.isDeleted()) {
onDeleted();
return;
}

for (ObjectChangeSet.FieldChange fieldChange : objectChangeSet.getFieldChanges()) {
int index = fieldNames.indexOf(fieldChange.fieldName);
if (index != -1) {
observable.notifyPropertyChanged(fieldIds.get(index));
}
}
}

public abstract void onDeleted();


public static class Builder<T> {
private BaseObservable observable;
private List<String> fieldNames = new ArrayList<String>();
private List<Integer> fieldIds = new ArrayList<Integer>();
private Runnable runnable = null;

public Builder(BaseObservable observable) {
this.observable = observable;
}

public Builder observe(String fieldName, int fieldId) {
fieldNames.add(fieldName);
fieldIds.add(fieldId);
return this;
}

public Builder onDeleted(Runnable runnable) {
this.runnable = runnable;
return this;
}

public RealmObjectDataBindingListener<T> build() {
return new RealmObjectDataBindingListener<T>(observable, fieldNames, fieldIds) {
@Override
public void onDeleted() {
if (runnable != null) {
runnable.run();
}
}
};
}
}
}
10 changes: 3 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ buildscript {
ext {
sdkVersion = 25
buildTools = '25.0.0'
realmVersion = '3.0.0'
realmVersion = '3.1.0-SNAPSHOT'
supportLibraryVersion = '25.2.0'
}

repositories {
jcenter()
maven {
url 'http://oss.jfrog.org/artifactory/oss-snapshot-local'
}
mavenLocal()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0' // Android Build tools
Expand All @@ -35,9 +33,7 @@ allprojects {
version = file("${rootDir}/version.txt").text.trim();
repositories {
jcenter()
maven {
url 'http://oss.jfrog.org/artifactory/oss-snapshot-local'
}
mavenLocal()
}
}

Expand Down
5 changes: 5 additions & 0 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ android {
command {
events 2000
}

dataBinding {
enabled = true
}
}

dependencies {
compile project(':adapters')
compile "com.android.support:appcompat-v7:${supportLibraryVersion}"
compile "com.android.support:recyclerview-v7:${supportLibraryVersion}"
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha5'
}
12 changes: 10 additions & 2 deletions example/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">

<activity
android:name="io.realm.examples.adapters.MainActivity"
android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Expand All @@ -23,6 +23,14 @@
<activity
android:name=".ui.recyclerview.RecyclerViewExampleActivity"
android:label="RecyclerView Example"/>
<activity
android:name=".ui.databinding.OwnerListActivity"
android:label="@string/title_activity_owner_list">
</activity>
<activity
android:name=".ui.databinding.OwnerDetailsActivity"
android:label="@string/title_activity_owner_details">
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import io.realm.examples.adapters.ui.databinding.OwnerListActivity;
import io.realm.examples.adapters.ui.listview.ListViewExampleActivity;
import io.realm.examples.adapters.ui.recyclerview.RecyclerViewExampleActivity;

Expand All @@ -35,6 +36,7 @@ protected void onCreate(Bundle savedInstanceState) {

setupButton(R.id.button_listview, ListViewExampleActivity.class);
setupButton(R.id.button_recyclerview, RecyclerViewExampleActivity.class);
setupButton(R.id.button_data_binding, OwnerListActivity.class);
}

void startActivity(Class<? extends Activity> activityClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,15 @@ public void execute(Realm realm) {
}
});
}

public static void addOwnerAsync(Realm realm, final int count) {
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
for (int i = 0; i < count; i++) {
realm.copyToRealm(Owner.create());
}
}
});
}
}
15 changes: 15 additions & 0 deletions example/src/main/java/io/realm/examples/adapters/model/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.realm.examples.adapters.model;

import io.realm.RealmObject;

public class Dog extends RealmObject {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
46 changes: 46 additions & 0 deletions example/src/main/java/io/realm/examples/adapters/model/Owner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.realm.examples.adapters.model;

import java.util.concurrent.atomic.AtomicInteger;

import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;

public class Owner extends RealmObject {
@PrimaryKey
private int id;
private String name;
private Dog dog;

private static AtomicInteger counter = new AtomicInteger(0);

public int getId() {
return id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Dog getDog() {
return dog;
}

public void setDog(Dog dog) {
this.dog = dog;
}

public static Owner create() {
int id = counter.getAndIncrement();
Dog dog = new Dog();
dog.setName("Dog-" + id);
Owner owner = new Owner();
owner.id = id;
owner.setName("Owner-" + id);
owner.setDog(dog);
return owner;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.realm.examples.adapters.ui.databinding;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import io.realm.Realm;
import io.realm.RealmObjectDataBindingListener;
import io.realm.examples.adapters.R;
import io.realm.examples.adapters.databinding.ActivityOwnerDetailsBinding;
import io.realm.examples.adapters.model.Dog;
import io.realm.examples.adapters.model.Owner;

public class OwnerDetailsActivity extends AppCompatActivity {

public static String EXTRA_DATA_OWNER_ID = "owner_id";

private Realm realm;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

realm = Realm.getDefaultInstance();
int id = getIntent().getIntExtra(EXTRA_DATA_OWNER_ID, -1);
Owner owner = realm.where(Owner.class).equalTo("id", id).findFirst();
Dog dog = owner.getDog();

final ActivityOwnerDetailsBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_owner_details);
binding.setOwner(owner);
binding.setDog(owner.getDog());

owner.addChangeListener(
new RealmObjectDataBindingListener.Builder<Owner>(binding)
.observe("name", R.id.owner_name_text_edit)
.onDeleted(new Runnable() {
@Override
public void run() {
finish();
}
})
.build());
dog.addChangeListener(
new RealmObjectDataBindingListener.Builder<Dog>(binding)
.observe("name", R.id.dog_name_text_edit)
.build());
}

@Override
protected void onDestroy() {
super.onDestroy();
realm.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.realm.examples.adapters.ui.databinding;

import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;

import io.realm.Realm;
import io.realm.RealmResults;
import io.realm.examples.adapters.R;
import io.realm.examples.adapters.model.DataHelper;
import io.realm.examples.adapters.model.Owner;
import io.realm.examples.adapters.ui.DividerItemDecoration;

public class OwnerListActivity extends AppCompatActivity {

private Realm realm;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_owner_list);
realm = Realm.getDefaultInstance();

setUpRecyclerView((RecyclerView) findViewById(R.id.recycler_view));
}

@Override
protected void onDestroy() {
super.onDestroy();
realm.close();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.owner_list_view_options, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_add:
DataHelper.addOwnerAsync(realm, 1);
return true;
default:
break;
}
return false;
}

private void setUpRecyclerView(RecyclerView recyclerView) {
RealmResults<Owner> results = realm.where(Owner.class).findAllAsync();
OwnerListViewAdapter adapter = new OwnerListViewAdapter(results);

recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
}
}
Loading