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

Fixed bug when refreshing RealmResults #26

Merged
merged 2 commits into from
May 13, 2016
Merged
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.2.1

### Bug fixes

* Fixed crash when auto-updating RealmResults (#25).


## 1.2.0

### Enhancements
Expand Down
13 changes: 10 additions & 3 deletions adapters/src/main/java/io/realm/RealmRecyclerViewAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public abstract class RealmRecyclerViewAdapter<T extends RealmObject, VH extends
protected final LayoutInflater inflater;
protected final Context context;
private final boolean hasAutoUpdates;
private final RealmChangeListener<BaseRealm> listener;
private final RealmChangeListener listener;
private OrderedRealmCollection<T> adapterData;

public RealmRecyclerViewAdapter(Context context, OrderedRealmCollection<T> data, boolean autoUpdate) {
Expand All @@ -51,9 +51,13 @@ public RealmRecyclerViewAdapter(Context context, OrderedRealmCollection<T> data,
this.adapterData = data;
this.inflater = LayoutInflater.from(context);
this.hasAutoUpdates = autoUpdate;
this.listener = hasAutoUpdates ? new RealmChangeListener<BaseRealm>() {

// Right now don't use generics, since we need maintain two different
// types of listeners until RealmList is properly supported.
// See https://github.com/realm/realm-java/issues/989
this.listener = hasAutoUpdates ? new RealmChangeListener() {
@Override
public void onChange(BaseRealm results) {
public void onChange(Object results) {
notifyDataSetChanged();
}
} : null;
Expand Down Expand Up @@ -135,9 +139,11 @@ public void updateData(OrderedRealmCollection<T> data) {
private void addListener(OrderedRealmCollection<T> data) {
if (data instanceof RealmResults) {
RealmResults realmResults = (RealmResults) data;
//noinspection unchecked
realmResults.addChangeListener(listener);
} else if (data instanceof RealmList) {
RealmList realmList = (RealmList) data;
//noinspection unchecked
realmList.realm.handlerController.addChangeListenerAsWeakReference(listener);
} else {
throw new IllegalArgumentException("RealmCollection not supported: " + data.getClass());
Expand All @@ -150,6 +156,7 @@ private void removeListener(OrderedRealmCollection<T> data) {
realmResults.removeChangeListener(listener);
} else if (data instanceof RealmList) {
RealmList realmList = (RealmList) data;
//noinspection unchecked
realmList.realm.handlerController.removeWeakChangeListener(listener);
} else {
throw new IllegalArgumentException("RealmCollection not supported: " + data.getClass());
Expand Down
8 changes: 7 additions & 1 deletion example/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:name=".MyApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
Expand All @@ -16,7 +17,12 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<activity
android:name=".ui.listview.ListViewExampleActivity"
android:label="ListView Example"/>
<activity
android:name=".ui.recyclerview.RecyclerViewExampleActivity"
android:label="RecyclerView Example"/>
</application>

</manifest>
88 changes: 29 additions & 59 deletions example/src/main/java/io/realm/examples/adapters/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,80 +17,50 @@
package io.realm.examples.adapters;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.view.ViewGroup;
import android.widget.Button;

import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmResults;
import io.realm.examples.adapters.adapter.MyListAdapter;
import io.realm.examples.adapters.model.TimeStamp;
import java.util.Map;
import java.util.TreeMap;

public class MainActivity extends Activity {
import io.realm.examples.adapters.ui.listview.ListViewExampleActivity;
import io.realm.examples.adapters.ui.recyclerview.RecyclerViewExampleActivity;

private Realm realm;
public class MainActivity extends AppCompatActivity {

private ViewGroup container;
private final TreeMap<String, Class<? extends Activity>> buttons = new TreeMap<String, Class<? extends Activity>>() {{
put("ListView", ListViewExampleActivity.class);
put("RecyclerView", RecyclerViewExampleActivity.class);
}};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

RealmConfiguration realmConfig = new RealmConfiguration.Builder(this).build();
Realm.deleteRealm(realmConfig); // Resets the Realm between app starts
realm = Realm.getInstance(realmConfig);

// RealmResults are "live" views, that are automatically kept up to date, even when changes happen
// on a background thread. The RealmBaseAdapter will automatically keep track of changes and will
// automatically refresh when a change is detected.
RealmResults<TimeStamp> timeStamps = realm.where(TimeStamp.class).findAll();
final MyListAdapter adapter = new MyListAdapter(this, timeStamps);

ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
final String timestamp = adapter.getItem(i).getTimeStamp();
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.where(TimeStamp.class).equalTo("timeStamp", timestamp).findAll().deleteAllFromRealm();
}
});
return true;
}
});
}

@Override
protected void onDestroy() {
super.onDestroy();
realm.close();
container = (ViewGroup) findViewById(R.id.list);
setupButtons();
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_add) {
final String timestamp = Long.toString(System.currentTimeMillis());
realm.executeTransactionAsync(new Realm.Transaction() {
private void setupButtons() {
for (final Map.Entry<String, Class<? extends Activity>> entry : buttons.entrySet()) {
Button button = new Button(this);
button.setText(entry.getKey());
button.setOnClickListener(new View.OnClickListener() {
@Override
public void execute(Realm realm) {
realm.createObject(TimeStamp.class).setTimeStamp(timestamp);
public void onClick(View v) {
startActivity(entry.getValue());
}
});
return true;
container.addView(button);
}
return super.onOptionsItemSelected(item);
}

private void startActivity(Class<? extends Activity> activityClass) {
startActivity(new Intent(this, activityClass));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2016 Realm Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.realm.examples.adapters;

import android.app.Application;

import io.realm.Realm;
import io.realm.RealmConfiguration;

public class MyApplication extends Application {

@Override
public void onCreate() {
super.onCreate();
RealmConfiguration realmConfig = new RealmConfiguration.Builder(this).build();
Realm.deleteRealm(realmConfig); // Delete Realm between app restarts.
Realm.setDefaultConfiguration(realmConfig);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import io.realm.RealmObject;

public class TimeStamp extends RealmObject {

public static final String TIMESTAMP = "timeStamp";

private String timeStamp;

public String getTimeStamp() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (C) 2014 The Android Open Source Project
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2016?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is straight copy from AOSP, so I'll rather keep the original.

*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

up up up ⬆️ 🆙

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


package io.realm.examples.adapters.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

private static final int[] ATTRS = new int[]{
android.R.attr.listDivider
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Less indentation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's is what the IntelliJ auto-formatter puts it at, so I'll rather keep it.

};

public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;

public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

private Drawable mDivider;

private int mOrientation;

public DividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}

public void setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}

@Override
public void onDraw(Canvas c, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}

public void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();

final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}

public void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();

final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}

@Override
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}
Loading