Skip to content

Commit

Permalink
0.5.1: Remove createDataSourceFactory(asAsync) + Fix PagedLiveResults…
Browse files Browse the repository at this point in the history
….updateQuery()

* 0.5.0: open/closeManually() and createDataSourceFactory(query, asAsync)

* I need to work on this

* Fix PagedLiveResults.updateQuery()

* Apply hack-fixes for ManagedDogAdapter (?!?!?!)

* Remove `createDataSourceFactory(asAsync)` because it does not work
  • Loading branch information
Zhuinden authored Oct 8, 2018
1 parent d39e73c commit 6720963
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 46 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Monarchy 0.5.1 (2018-10-08)
- Remove `createDataSourceFactory(Query, boolean asAsync)` because it doesn't actually work with Sync. :(

- Fix `PagedLiveResults.updateQuery()`. Now it re-creates the RealmResults as it was originally intended.

# Monarchy 0.5.0 (2018-09-14)
- Add `openManually()` and `closeManually()` methods for people who use Sync and need their session to stay alive in a controlled manner. Please note that this increments the Realm ref count by one, and cannot be called multiple times. (So `openManually` called once, then `closeManually` called once).

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ With that, you can use a singleton Monarchy instance to manage Realm queries, an

To use `Monarchy`, you need to add as a dependency:

implementation 'com.github.Zhuinden:realm-monarchy:0.5.0'
implementation 'com.github.Zhuinden:realm-monarchy:0.5.1'

And it's available on Jitpack, so you need to add

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ public void onBindViewHolder(ManagedDogAdapter.ViewHolder holder, int position)

@Override
public int getItemCount() {
return items == null ? 0 : items.size();
try {
return items == null ? 0 : items.size();
} catch(IllegalStateException e) {
return 0; // I should check `RealmResults.isValid()` here, but I'm using List<T>.
// RecyclerView is running the prefetch worker on the next Handler loop, but by then, Realm is closed.
}
}

public void updateData(List<RealmDog> items, @Nullable CustomDiffResult diffResult) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
@Override
public void onDestroyView() {
changes.removeObserver(observer);
managedDogAdapter.notifyDataSetChanged(); // if Realm is closed, then the item count becomes 0. Adapter does not know this while scrolling.
super.onDestroyView();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,28 @@
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.zhuinden.monarchy.Monarchy;
import com.zhuinden.monarchyexample.Dog;
import com.zhuinden.monarchyexample.R;
import com.zhuinden.monarchyexample.RealmDog;
import com.zhuinden.monarchyexample.RealmDogFields;
import com.zhuinden.monarchyexample.application.CustomApplication;
import com.zhuinden.monarchyexample.utils.BaseFragment;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnTextChanged;
import io.realm.Case;
import io.realm.Realm;
import io.realm.RealmQuery;

/**
* Created by Zhuinden on 2017.12.21..
Expand All @@ -35,6 +42,9 @@ public class PagedFragment
@BindView(R.id.recycler_view)
RecyclerView recyclerView;

@BindView(R.id.text_paged_search)
EditText searchText;

@Inject
Monarchy monarchy;

Expand All @@ -45,6 +55,21 @@ public class PagedFragment
pagedDogAdapter.submitList(dogs);
};

Monarchy.RealmDataSourceFactory<RealmDog> realmDataSourceFactory;

@OnTextChanged(R.id.text_paged_search)
public void onSearchTextChanged(Editable editable) {
String text = editable.toString();
realmDataSourceFactory.updateQuery(realm -> {
RealmQuery<RealmDog> query = realm.where(RealmDog.class);
if(text.isEmpty()) {
return query;
} else {
return query.contains(RealmDogFields.NAME, text.trim(), Case.INSENSITIVE);
}
});
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
Expand All @@ -65,8 +90,8 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
recyclerView.setAdapter(pagedDogAdapter);
DataSource.Factory<Integer, RealmDog> realmDataSourceFactory = monarchy.createDataSourceFactory(
realm -> realm.where(RealmDog.class), true);
realmDataSourceFactory = monarchy.createDataSourceFactory(
realm -> realm.where(RealmDog.class));
dataSourceFactory = realmDataSourceFactory.map(input -> Dog.create(input.getName()));
dogs = monarchy.findAllPagedWithChanges(realmDataSourceFactory,
new LivePagedListBuilder<>(dataSourceFactory, 20));
Expand Down
17 changes: 12 additions & 5 deletions monarchy-example/src/main/res/layout/fragment_paged.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
<LinearLayout android:id="@+id/container"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ECECEC"
android:gravity="center"
android:paddingBottom="12dp"
android:paddingTop="12dp"
android:text="Paged"
android:textStyle="bold"
android:textSize="22sp"
android:textColor="@android:color/black"
android:background="#ECECEC"/>
android:textSize="22sp"
android:textStyle="bold"/>

<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@android:color/black"/>

<EditText
android:id="@+id/text_paged_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter search filter here"
android:inputType="text"/>

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
Expand Down
87 changes: 51 additions & 36 deletions monarchy/src/main/java/com/zhuinden/monarchy/Monarchy.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,36 @@ protected Map<LiveResults<? extends RealmModel>, RealmResults<? extends RealmMod
}
};

// CALL THIS SYNC ON MONARCHY THREAD
<T extends RealmModel> void createAndObserveRealmQuery(final LiveResults<T> liveResults) {
Realm realm = realmThreadLocal.get();
checkRealmValid(realm);
if(liveResults == null) {
return;
}
RealmResults<T> results = liveResults.createQuery(realm);
resultsRefs.get().put(liveResults, results);
results.addChangeListener(new RealmChangeListener<RealmResults<T>>() {
@Override
public void onChange(@NonNull RealmResults<T> realmResults) {
liveResults.updateResults(realmResults.createSnapshot());
}
});
}

// CALL THIS SYNC ON MONARCHY THREAD
<T extends RealmModel> void destroyRealmQuery(final LiveResults<T> liveResults) {
Realm realm = realmThreadLocal.get();
checkRealmValid(realm);
if(liveResults == null) {
return;
}
RealmResults<? extends RealmModel> realmResults = resultsRefs.get().remove(liveResults);
if(realmResults != null) {
realmResults.removeAllChangeListeners();
}
}

<T extends RealmModel> void startListening(@Nullable final LiveResults<T> liveResults) {
// build Realm instance
if(refCount.getAndIncrement() == 0) {
Expand All @@ -210,19 +240,7 @@ public void run() {
handler.get().post(new Runnable() {
@Override
public void run() {
Realm realm = realmThreadLocal.get();
checkRealmValid(realm);
if(liveResults == null) {
return;
}
RealmResults<T> results = liveResults.createQuery(realm);
resultsRefs.get().put(liveResults, results);
results.addChangeListener(new RealmChangeListener<RealmResults<T>>() {
@Override
public void onChange(@NonNull RealmResults<T> realmResults) {
liveResults.updateResults(realmResults.createSnapshot());
}
});
createAndObserveRealmQuery(liveResults);
}
});
}
Expand All @@ -236,15 +254,7 @@ <T extends RealmModel> void stopListening(@Nullable final LiveResults<T> liveRes
handler.post(new Runnable() {
@Override
public void run() {
Realm realm = realmThreadLocal.get();
checkRealmValid(realm);
if(liveResults == null) {
return;
}
RealmResults<? extends RealmModel> realmResults = resultsRefs.get().remove(liveResults);
if(realmResults != null) {
realmResults.removeAllChangeListeners();
}
destroyRealmQuery(liveResults);
}
});
// destroy Realm instance
Expand Down Expand Up @@ -521,18 +531,8 @@ public void run() {
* @param query the query
*/
public <T extends RealmModel> RealmDataSourceFactory<T> createDataSourceFactory(Query<T> query) {
return createDataSourceFactory(query, false);
}

/**
* Creates a DataSource.Factory of (Integer, T) that can be used for creating a paged result set.
*
* @param query the query
* @param asAsync determines whether the created query uses the Async API.
*/
public <T extends RealmModel> RealmDataSourceFactory<T> createDataSourceFactory(Query<T> query, boolean asAsync) {
assertMainThread();
PagedLiveResults<T> liveResults = new PagedLiveResults<T>(this, query, asAsync);
PagedLiveResults<T> liveResults = new PagedLiveResults<T>(this, query, false);
return new RealmDataSourceFactory<>(this, liveResults);
}

Expand Down Expand Up @@ -700,7 +700,7 @@ public void doWithRealm(Realm realm) {
*/
public static final class RealmDataSourceFactory<T extends RealmModel>
extends DataSource.Factory<Integer, T> {
Monarchy monarchy;
final Monarchy monarchy;
final PagedLiveResults<T> pagedLiveResults;

RealmDataSourceFactory(Monarchy monarchy, PagedLiveResults<T> pagedLiveResults) {
Expand All @@ -718,10 +718,25 @@ public final DataSource<Integer, T> create() {
/**
* Updates the query that the datasource is evaluated by.
*
* Please note that this method runs asynchronously.
*
* @param query the query
*/
public final void updateQuery(Query<T> query) {
pagedLiveResults.updateQuery(query);
public final void updateQuery(final Query<T> query) {
Handler handler = monarchy.handler.get();
if(handler == null) {
return;
}
handler.post(new Runnable() {
@Override
public void run() {
monarchy.destroyRealmQuery(pagedLiveResults);
pagedLiveResults.updateQuery(query);
monarchy.createAndObserveRealmQuery(pagedLiveResults);
pagedLiveResults.invalidateDatasource();
}
});

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ void setDataSource(Monarchy.RealmTiledDataSource<T> dataSource) {
this.dataSource = dataSource;
}

public void updateQuery(Monarchy.Query<T> query) {
// CALL THIS FROM MONARCHY THREAD
void updateQuery(Monarchy.Query<T> query) {
this.query.set(query);
}

void invalidateDatasource() {
if(dataSource != null) {
dataSource.invalidate();
}
Expand Down

0 comments on commit 6720963

Please sign in to comment.