Search in sources :

Example 1 with RealmResults

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);
}
Also used : LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) MaterializeBuilder(com.mikepenz.materialize.MaterializeBuilder) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) AlphaInAnimator(com.mikepenz.itemanimators.AlphaInAnimator) RealmSampleUserItem(com.mikepenz.fastadapter.app.items.RealmSampleUserItem) RecyclerView(android.support.v7.widget.RecyclerView) FastAdapter(com.mikepenz.fastadapter.FastAdapter) Toolbar(android.support.v7.widget.Toolbar) RealmResults(io.realm.RealmResults)

Example 2 with RealmResults

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();
        }
    });
}
Also used : Action1(rx.functions.Action1) Observable(rx.Observable) TextView(android.widget.TextView) Func1(rx.functions.Func1) Person(io.realm.examples.rxjava.model.Person) RealmResults(io.realm.RealmResults)

Example 3 with RealmResults

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);
        });
    });
}
Also used : User(chat.rocket.core.models.User) MessageRepository(chat.rocket.core.repositories.MessageRepository) Realm(io.realm.Realm) RealmUser(chat.rocket.persistence.realm.models.ddp.RealmUser) Optional(com.fernandocejas.arrow.optional.Optional) RealmResults(io.realm.RealmResults) RxJavaInterop(hu.akarnokd.rxjava.interop.RxJavaInterop) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) RealmMessage(chat.rocket.persistence.realm.models.ddp.RealmMessage) ArrayList(java.util.ArrayList) Message(chat.rocket.core.models.Message) List(java.util.List) Flowable(io.reactivex.Flowable) Pair(android.support.v4.util.Pair) Sort(io.realm.Sort) Room(chat.rocket.core.models.Room) Looper(android.os.Looper) RealmStore(chat.rocket.persistence.realm.RealmStore) Looper(android.os.Looper) Realm(io.realm.Realm) RealmMessage(chat.rocket.persistence.realm.models.ddp.RealmMessage) RealmResults(io.realm.RealmResults)

Example 4 with RealmResults

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);
        }
    });
}
Also used : Action1(rx.functions.Action1) TextView(android.widget.TextView) Person(io.realm.examples.rxjava.model.Person) Observable(rx.Observable) RealmResults(io.realm.RealmResults) Func2(rx.functions.Func2)

Example 5 with RealmResults

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();
        }
    });
}
Also used : Action1(rx.functions.Action1) TextViewTextChangeEvent(com.jakewharton.rxbinding.widget.TextViewTextChangeEvent) TextView(android.widget.TextView) RxTextView(com.jakewharton.rxbinding.widget.RxTextView) Func1(rx.functions.Func1) Person(io.realm.examples.rxjava.model.Person) RealmResults(io.realm.RealmResults)

Aggregations

RealmResults (io.realm.RealmResults)10 Realm (io.realm.Realm)6 TextView (android.widget.TextView)3 ClientResetHandler (io.realm.ClientResetHandler)3 ObjectServerError (io.realm.ObjectServerError)3 RealmChangeListener (io.realm.RealmChangeListener)3 SyncConfiguration (io.realm.SyncConfiguration)3 SyncSession (io.realm.SyncSession)3 SyncUser (io.realm.SyncUser)3 Person (io.realm.examples.rxjava.model.Person)3 Ignore (org.junit.Ignore)3 Test (org.junit.Test)3 Action1 (rx.functions.Action1)3 Context (android.content.Context)2 Intent (android.content.Intent)2 RealmSampleUserItem (com.mikepenz.fastadapter.app.items.RealmSampleUserItem)2 Date (java.util.Date)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutorService (java.util.concurrent.ExecutorService)2 Observable (rx.Observable)2