Search in sources :

Example 1 with RealmLabel

use of com.kboyarshinov.realmrxjavaexample.model.RealmLabel 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 2 with RealmLabel

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

the class RealmDataService method issueFromRealm.

private static Issue issueFromRealm(RealmIssue realmIssue) {
    final String title = realmIssue.getTitle();
    final String body = realmIssue.getBody();
    final User user = userFromRealm(realmIssue.getUser());
    final RealmList<RealmLabel> realmLabels = realmIssue.getLabels();
    final List<Label> labels = new ArrayList<>(realmLabels.size());
    for (RealmLabel realmLabel : realmLabels) {
        labels.add(labelFromRealm(realmLabel));
    }
    return new Issue(title, body, user, labels);
}
Also used : RealmLabel(com.kboyarshinov.realmrxjavaexample.model.RealmLabel) RealmUser(com.kboyarshinov.realmrxjavaexample.model.RealmUser) User(com.kboyarshinov.realmrxjavaexample.model.User) Issue(com.kboyarshinov.realmrxjavaexample.model.Issue) RealmIssue(com.kboyarshinov.realmrxjavaexample.model.RealmIssue) ArrayList(java.util.ArrayList) RealmLabel(com.kboyarshinov.realmrxjavaexample.model.RealmLabel) Label(com.kboyarshinov.realmrxjavaexample.model.Label)

Aggregations

Issue (com.kboyarshinov.realmrxjavaexample.model.Issue)2 Label (com.kboyarshinov.realmrxjavaexample.model.Label)2 RealmIssue (com.kboyarshinov.realmrxjavaexample.model.RealmIssue)2 RealmLabel (com.kboyarshinov.realmrxjavaexample.model.RealmLabel)2 RealmUser (com.kboyarshinov.realmrxjavaexample.model.RealmUser)2 User (com.kboyarshinov.realmrxjavaexample.model.User)1 Realm (io.realm.Realm)1 RealmList (io.realm.RealmList)1 ArrayList (java.util.ArrayList)1 Func1 (rx.functions.Func1)1