Search in sources :

Example 1 with Issue

use of com.kboyarshinov.realmrxjavaexample.model.Issue in project realm-rxjava-example by kboyarshinov.

the class MainActivity method requestWithZip.

private void requestWithZip() {
    if (compositeSubscription == null) {
        return;
    }
    Observable<Issue> issues = dataService.issues();
    Subscription subscription = Observable.zip(issues.take(10), issues.takeLast(10), new Func2<Issue, Issue, Pair<Issue, Issue>>() {

        @Override
        public Pair<Issue, Issue> call(Issue issue, Issue issue2) {
            return new Pair<>(issue, issue2);
        }
    }).toList().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<List<Pair<Issue, Issue>>>() {

        @Override
        public void call(List<Pair<Issue, Issue>> pairs) {
            Log.d(TAG, "List of issue pairs " + pairs.size());
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            Log.e(TAG, "Error requesting issue pairs", throwable);
        }
    });
    compositeSubscription.add(subscription);
}
Also used : Issue(com.kboyarshinov.realmrxjavaexample.model.Issue) ArrayList(java.util.ArrayList) List(java.util.List) CompositeSubscription(rx.subscriptions.CompositeSubscription) Subscription(rx.Subscription) Pair(android.support.v4.util.Pair)

Example 2 with Issue

use of com.kboyarshinov.realmrxjavaexample.model.Issue in project realm-rxjava-example by kboyarshinov.

the class MainActivity method requestWithFlatMap.

private void requestWithFlatMap() {
    if (compositeSubscription == null) {
        return;
    }
    Subscription subscription = dataService.findUser("kboyarshinov").flatMap(new Func1<User, Observable<List<Issue>>>() {

        @Override
        public Observable<List<Issue>> call(User user) {
            return dataService.issuesListByUser(user);
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<List<Issue>>() {

        @Override
        public void call(List<Issue> issues) {
            Log.d(TAG, "Issues by user received with size " + issues.size());
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            Log.e(TAG, "Request issues by user error", throwable);
        }
    });
    compositeSubscription.add(subscription);
}
Also used : User(com.kboyarshinov.realmrxjavaexample.model.User) Issue(com.kboyarshinov.realmrxjavaexample.model.Issue) ArrayList(java.util.ArrayList) List(java.util.List) CompositeSubscription(rx.subscriptions.CompositeSubscription) Subscription(rx.Subscription) Func1(rx.functions.Func1)

Example 3 with Issue

use of com.kboyarshinov.realmrxjavaexample.model.Issue in project realm-rxjava-example by kboyarshinov.

the class RealmDataService method newIssue.

@Override
public Observable<Issue> newIssue(final String title, final String body, final User user, List<Label> labels) {
    // map internal UI objects to Realm objects
    final RealmUser realmUser = new RealmUser();
    realmUser.setLogin(user.getLogin());
    final RealmList<RealmLabel> realmLabels = new RealmList<>();
    for (Label label : labels) {
        RealmLabel realmLabel = new RealmLabel();
        realmLabel.setName(label.getName());
        realmLabel.setColor(label.getColor());
        realmLabels.add(realmLabel);
    }
    return RealmObservable.object(context, new Func1<Realm, RealmIssue>() {

        @Override
        public RealmIssue call(Realm realm) {
            // internal object instances are not created by realm
            // saving them using copyToRealm returning instance associated with realm
            RealmUser user = realm.copyToRealm(realmUser);
            RealmList<RealmLabel> labels = new RealmList<RealmLabel>();
            for (RealmLabel realmLabel : realmLabels) {
                labels.add(realm.copyToRealm(realmLabel));
            }
            // create RealmIssue instance and save it
            RealmIssue issue = new RealmIssue();
            issue.setTitle(title);
            issue.setBody(body);
            issue.setUser(user);
            issue.setLabels(labels);
            return realm.copyToRealm(issue);
        }
    }).map(new Func1<RealmIssue, Issue>() {

        @Override
        public Issue call(RealmIssue realmIssue) {
            // map to UI object
            return issueFromRealm(realmIssue);
        }
    });
}
Also used : RealmLabel(com.kboyarshinov.realmrxjavaexample.model.RealmLabel) RealmList(io.realm.RealmList) Issue(com.kboyarshinov.realmrxjavaexample.model.Issue) RealmIssue(com.kboyarshinov.realmrxjavaexample.model.RealmIssue) RealmUser(com.kboyarshinov.realmrxjavaexample.model.RealmUser) RealmLabel(com.kboyarshinov.realmrxjavaexample.model.RealmLabel) Label(com.kboyarshinov.realmrxjavaexample.model.Label) RealmIssue(com.kboyarshinov.realmrxjavaexample.model.RealmIssue) Func1(rx.functions.Func1) Realm(io.realm.Realm)

Example 4 with Issue

use of com.kboyarshinov.realmrxjavaexample.model.Issue in project realm-rxjava-example by kboyarshinov.

the class MainActivity method addNewIssue.

private void addNewIssue() {
    if (compositeSubscription == null) {
        return;
    }
    String title = "Feature request: removing issues";
    String body = "Add function to remove issues";
    User user = new User("kboyarshinov");
    List<Label> labels = new ArrayList<>();
    labels.add(new Label("feature", "FF5722"));
    Subscription subscription = dataService.newIssue(title, body, user, labels).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Action1<Issue>() {

        @Override
        public void call(Issue issue) {
            Log.d(TAG, "Issue with title " + issue.getTitle() + " successfully saved");
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            Log.e(TAG, "Add new issue error", throwable);
        }
    });
    compositeSubscription.add(subscription);
}
Also used : User(com.kboyarshinov.realmrxjavaexample.model.User) Issue(com.kboyarshinov.realmrxjavaexample.model.Issue) ArrayList(java.util.ArrayList) Label(com.kboyarshinov.realmrxjavaexample.model.Label) CompositeSubscription(rx.subscriptions.CompositeSubscription) Subscription(rx.Subscription)

Example 5 with Issue

use of com.kboyarshinov.realmrxjavaexample.model.Issue in project realm-rxjava-example by kboyarshinov.

the class MainActivity method requestAllIssues.

private void requestAllIssues() {
    if (compositeSubscription == null) {
        return;
    }
    Subscription subscription = dataService.issuesList().observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Action1<List<Issue>>() {

        @Override
        public void call(List<Issue> issues) {
            Log.d(TAG, "Issues received with size " + issues.size());
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            Log.e(TAG, "Request all issues error", throwable);
        }
    });
    compositeSubscription.add(subscription);
}
Also used : Issue(com.kboyarshinov.realmrxjavaexample.model.Issue) ArrayList(java.util.ArrayList) List(java.util.List) CompositeSubscription(rx.subscriptions.CompositeSubscription) Subscription(rx.Subscription)

Aggregations

Issue (com.kboyarshinov.realmrxjavaexample.model.Issue)6 ArrayList (java.util.ArrayList)5 Subscription (rx.Subscription)4 CompositeSubscription (rx.subscriptions.CompositeSubscription)4 Label (com.kboyarshinov.realmrxjavaexample.model.Label)3 User (com.kboyarshinov.realmrxjavaexample.model.User)3 List (java.util.List)3 RealmIssue (com.kboyarshinov.realmrxjavaexample.model.RealmIssue)2 RealmLabel (com.kboyarshinov.realmrxjavaexample.model.RealmLabel)2 RealmUser (com.kboyarshinov.realmrxjavaexample.model.RealmUser)2 Func1 (rx.functions.Func1)2 Pair (android.support.v4.util.Pair)1 Realm (io.realm.Realm)1 RealmList (io.realm.RealmList)1