use of io.realm.RealmResults in project FastAdapter by mikepenz.
the class RealmActivity method onCreate.
@Override
protected void onCreate(final Bundle savedInstanceState) {
findViewById(android.R.id.content).setSystemUiVisibility(findViewById(android.R.id.content).getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Handle Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(R.string.sample_realm_list);
//style our ui
new MaterializeBuilder().withActivity(this).build();
//create our FastAdapter which will manage everything
mFastItemAdapter = new FastItemAdapter<>();
//configure our fastAdapter
mFastItemAdapter.withOnClickListener(new FastAdapter.OnClickListener<RealmSampleUserItem>() {
@Override
public boolean onClick(View v, IAdapter<RealmSampleUserItem> adapter, RealmSampleUserItem item, int position) {
Toast.makeText(v.getContext(), item.getName(), Toast.LENGTH_SHORT).show();
return false;
}
});
//get our recyclerView and do basic setup
RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setItemAnimator(new AlphaInAnimator());
rv.setAdapter(mFastItemAdapter);
//Get a realm instance for this activity
mRealm = Realm.getDefaultInstance();
//Add a realm on change listener (donĀ“t forget to close this realm instance before adding this listener again)
mRealm.where(RealmSampleUserItem.class).findAllAsync().addChangeListener(new RealmChangeListener<RealmResults<RealmSampleUserItem>>() {
@Override
public void onChange(RealmResults<RealmSampleUserItem> userItems) {
//This will call twice
//1.) from findAllAsync()
//2.) from createData()
mFastItemAdapter.setNewList(userItems);
}
});
//fill with some sample data
createData();
//set the back arrow in the toolbar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(false);
//restore selections (this has to be done after the items were added
mFastItemAdapter.withSavedInstanceState(savedInstanceState);
}
use of io.realm.RealmResults in project realm-java by realm.
the class RetrofitExample method onResume.
@Override
protected void onResume() {
super.onResume();
// Load all persons and merge them with their latest stats from GitHub (if they have any)
subscription = realm.where(Person.class).isNotNull("githubUserName").findAllSortedAsync("name").asObservable().filter(new Func1<RealmResults<Person>, Boolean>() {
@Override
public Boolean call(RealmResults<Person> persons) {
// We only want the list once it is loaded.
return persons.isLoaded();
}
}).flatMap(new Func1<RealmResults<Person>, Observable<Person>>() {
@Override
public Observable<Person> call(RealmResults<Person> persons) {
// Emit each person individually
return Observable.from(persons);
}
}).flatMap(new Func1<Person, Observable<GitHubUser>>() {
@Override
public Observable<GitHubUser> call(Person person) {
// get GitHub statistics. Retrofit automatically does this on a separate thread.
return api.user(person.getGithubUserName());
}
}).map(new Func1<GitHubUser, UserViewModel>() {
@Override
public UserViewModel call(GitHubUser gitHubUser) {
// Map Network model to our View model
return new UserViewModel(gitHubUser.name, gitHubUser.public_repos, gitHubUser.public_gists);
}
}).observeOn(// Retrofit put us on a worker thread. Move back to UI
AndroidSchedulers.mainThread()).subscribe(new Action1<UserViewModel>() {
@Override
public void call(UserViewModel user) {
// Print user info.
TextView userView = new TextView(RetrofitExample.this);
userView.setText(String.format(Locale.US, "%s : %d/%d", user.getUsername(), user.getPublicRepos(), user.getPublicGists()));
container.addView(userView);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
throwable.printStackTrace();
}
});
}
use of io.realm.RealmResults in project Rocket.Chat.Android by RocketChat.
the class RealmMessageRepository method delete.
@Override
public Single<Boolean> delete(Message message) {
return Single.defer(() -> {
final Realm realm = RealmStore.getRealm(hostname);
final Looper looper = Looper.myLooper();
if (realm == null || looper == null) {
return Single.just(false);
}
realm.beginTransaction();
return RxJavaInterop.toV2Flowable(realm.where(RealmMessage.class).equalTo(RealmMessage.ID, message.getId()).findAll().<RealmResults<RealmMessage>>asObservable()).filter(realmObject -> realmObject.isLoaded() && realmObject.isValid()).firstElement().toSingle().flatMap(realmMessages -> Single.just(realmMessages.deleteAllFromRealm())).doOnEvent((success, throwable) -> {
if (success) {
realm.commitTransaction();
} else {
realm.cancelTransaction();
}
close(realm, looper);
});
});
}
use of io.realm.RealmResults in project realm-java by realm.
the class AnimationActivity method onResume.
@Override
protected void onResume() {
super.onResume();
// Load all persons and start inserting them with 1 sec. intervals.
// All RealmObject access has to be done on the same thread `findAllAsync` was called on.
// Warning: This example doesn't handle back pressure well.
subscription = realm.where(Person.class).findAllAsync().asObservable().flatMap(new Func1<RealmResults<Person>, Observable<Person>>() {
@Override
public Observable<Person> call(RealmResults<Person> persons) {
return Observable.from(persons);
}
}).zipWith(Observable.interval(1, TimeUnit.SECONDS), new Func2<Person, Long, Person>() {
@Override
public Person call(Person person, Long tick) {
return person;
}
}).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Person>() {
@Override
public void call(Person person) {
TextView personView = new TextView(AnimationActivity.this);
personView.setText(person.getName());
container.addView(personView);
}
});
}
use of io.realm.RealmResults in project realm-java by realm.
the class ThrottleSearchActivity method onResume.
@Override
protected void onResume() {
super.onResume();
// Listen to key presses and only start search after user paused to avoid excessive redrawing on the screen.
subscription = RxTextView.textChangeEvents(searchInputView).debounce(200, // default Scheduler is Schedulers.computation()
TimeUnit.MILLISECONDS).observeOn(// Needed to access Realm data
AndroidSchedulers.mainThread()).flatMap(new Func1<TextViewTextChangeEvent, Observable<RealmResults<Person>>>() {
@Override
public Observable<RealmResults<Person>> call(TextViewTextChangeEvent event) {
// Realm currently doesn't support the standard Schedulers.
return realm.where(Person.class).beginsWith("name", event.text().toString()).findAllSortedAsync("name").asObservable();
}
}).filter(new Func1<RealmResults<Person>, Boolean>() {
@Override
public Boolean call(RealmResults<Person> persons) {
// RealmObservables will emit the unloaded (empty) list as its first item
return persons.isLoaded();
}
}).subscribe(new Action1<RealmResults<Person>>() {
@Override
public void call(RealmResults<Person> persons) {
searchResultsView.removeAllViews();
for (Person person : persons) {
TextView view = new TextView(ThrottleSearchActivity.this);
view.setText(person.getName());
searchResultsView.addView(view);
}
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
throwable.printStackTrace();
}
});
}
Aggregations