@Override public Observable<List<TMWRule>> loadRemoteRules() { return ruleApi .getAllRules(new DbSearch(Storage.loadUserId())) .subscribeOn(Schedulers.io()) .map( new Func1<DbDocuments<DbRule>, List<TMWRule>>() { @Override public List<TMWRule> call(DbDocuments<DbRule> docs) { new Delete().from(TMWRule.class).execute(); ActiveAndroid.beginTransaction(); try { for (DbRule dbRule : docs.getDocuments()) { checkGcmNotification(dbRule); TMWRule rule = DataMapper.toRule(dbRule); rule.save(); } ActiveAndroid.setTransactionSuccessful(); } finally { ActiveAndroid.endTransaction(); } return new Select().from(TMWRule.class).execute(); } }); }
@Override public Observable<Boolean> refreshRule(final String ruleId) { return ruleApi .getAllRules(new DbSearch(Storage.loadUserId())) .subscribeOn(Schedulers.io()) .map( new Func1<DbDocuments<DbRule>, Boolean>() { @Override public Boolean call(DbDocuments<DbRule> docs) { for (DbRule dbRule : docs.getDocuments()) { if (dbRule.getId().equals(ruleId)) { new Delete().from(TMWRule.class).where("dbId = ?", ruleId).execute(); TMWRule rule = DataMapper.toRule(dbRule); rule.save(); Storage.prepareRuleForEdit(rule); return true; } } return false; } }); }