com.pushtorefresh.storio.sqlite.queries.Query Java Examples

The following examples show how to use com.pushtorefresh.storio.sqlite.queries.Query. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PrayerCacheManager.java    From android with Apache License 2.0 6 votes vote down vote up
private Observable<PrayerData> getPrayerData(int year, int month, String code) {
    Query query = Query.builder()
            .table(PrayerCacheMeta.TABLE)
            .where(PrayerCacheMeta.Columns.YEAR + " = ? AND " +
                    PrayerCacheMeta.Columns.MONTH + " = ? AND " +
                    PrayerCacheMeta.Columns.CODE + " = ?"
            )
            .whereArgs(year, month, code)
            .build();

    return mSqlite.get()
            .listOfObjects(PrayerCache.class)
            .withQuery(query)
            .prepare()
            .asRxObservable()
            .take(1)
            .flatMapIterable(new Func1<List<PrayerCache>, Iterable<PrayerCache>>() {
                @Override
                public Iterable<PrayerCache> call(List<PrayerCache> caches) {
                    return caches;
                }
            })
            .map(mPrayerDataFactory);
}
 
Example #2
Source File: Example2.java    From AnDevCon-RxPatterns with Apache License 2.0 6 votes vote down vote up
@Override
protected void onResume() {
    super.onResume();

    // QUERY
    sq.get().listOfObjects(Item.class)
            .withQuery(Query.builder().table("Items").build()).prepare().asRxObservable()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(items -> {
                Log.d("Example2", "Queried items");
                adapter.setItems(items);
            });

    // DELETE
    mListView.setOnItemClickListener((adapterView, view, i, l) ->
            sq.delete().object(adapter.getItem(i)).prepare().asRxCompletable()
            .observeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(() -> {
                Log.d("Example2", "Deleted row");
            }));
}
 
Example #3
Source File: StorIOFactory.java    From Reactive-Android-Programming with MIT License 5 votes vote down vote up
public static Observable<StockUpdate> createLocalDbStockUpdateRetrievalObservable(Context context) {
    return v2(StorIOFactory.get(context)
            .get()
            .listOfObjects(StockUpdate.class)
            .withQuery(Query.builder()
                    .table(StockUpdateTable.TABLE)
                    .orderBy("date DESC")
                    .limit(50)
                    .build())
            .prepare()
            .asRxObservable())
            .take(1)
            .flatMap(Observable::fromIterable);
}
 
Example #4
Source File: StorIOFactory.java    From Reactive-Android-Programming with MIT License 5 votes vote down vote up
public static Observable<StockUpdate> createLocalDbStockUpdateRetrievalObservable(Context context) {
    return v2(StorIOFactory.get(context)
            .get()
            .listOfObjects(StockUpdate.class)
            .withQuery(Query.builder()
                    .table(StockUpdateTable.TABLE)
                    .orderBy("date DESC")
                    .limit(50)
                    .build())
            .prepare()
            .asRxObservable())
            .take(1)
            .flatMap(Observable::fromIterable);
}
 
Example #5
Source File: StorIOFactory.java    From Reactive-Android-Programming with MIT License 5 votes vote down vote up
public static Observable<StockUpdate> createLocalDbStockUpdateRetrievalObservable(Context context) {
    return v2(StorIOFactory.get(context)
            .get()
            .listOfObjects(StockUpdate.class)
            .withQuery(Query.builder()
                    .table(StockUpdateTable.TABLE)
                    .orderBy("date DESC")
                    .limit(50)
                    .build())
            .prepare()
            .asRxObservable())
            .take(1)
            .flatMap(Observable::fromIterable);
}
 
Example #6
Source File: PrayerCacheManager.java    From android with Apache License 2.0 5 votes vote down vote up
private Observable<LocationCache> getClosestLocation(final Location location) {
    Query query = Query.builder()
            .table(LocationCacheMeta.TABLE)
            .build();

    return mSqlite.get()
            .listOfObjects(LocationCache.class)
            .withQuery(query)
            .prepare()
            .asRxObservable()
            .take(1)
            .flatMapIterable(new Func1<List<LocationCache>, Iterable<LocationCache>>() {
                @Override
                public Iterable<LocationCache> call(List<LocationCache> models) {
                    return models;
                }
            })
            .scan(new CloserModel(location))
            .filter(new Func1<LocationCache, Boolean>() {
                @Override
                public Boolean call(LocationCache model) {
                    Location l = getLocation(model);
                    return location.distanceTo(l) < mHiddenPreferences.getLocationDistanceLimit();
                }
            })
            .take(1);
}
 
Example #7
Source File: LocalDataManager.java    From Hews with MIT License 5 votes vote down vote up
@Override
public Observable<List<Post>> getPostFromDb(long postId) {
    return StorIOHelper.getStorIOSQLite(mContext)
        .get()
        .listOfObjects(Post.class)
        .withQuery(Query.builder()
            .table(PostTable.TABLE)
            .where(PostTable.COLUMN_ID + " = " + postId)
            .build())
        .prepare()
        .asRxObservable();
}
 
Example #8
Source File: LocalDataManager.java    From Hews with MIT License 5 votes vote down vote up
@Override
public Observable<List<Post>> getAllPostsFromDb() {
    return StorIOHelper.getStorIOSQLite(mContext)
        .get()
        .listOfObjects(Post.class)
        .withQuery(Query.builder().table(PostTable.TABLE).build())
        .prepare()
        .asRxObservable().onBackpressureBuffer();
}
 
Example #9
Source File: LocalDataManager.java    From Hews with MIT License 5 votes vote down vote up
@Override
public Observable<List<Comment>> getStoryCommentsFromDb(long postId) {
    return StorIOHelper.getStorIOSQLite(mContext)
        .get()
        .listOfObjects(Comment.class)
        .withQuery(Query.builder()
            .table(CommentTable.TABLE)
            .where(CommentTable.COLUMN_PARENT + " = " + postId)
            .orderBy(CommentTable.COLUMN_INDEX + " ASC")
            .build())
        .prepare()
        .asRxObservable().onBackpressureBuffer();
}
 
Example #10
Source File: MainActivity.java    From Reactive-Android-Programming with MIT License 4 votes vote down vote up
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RxJavaPlugins.setErrorHandler(ErrorHandler.get());

        ButterKnife.bind(this);

        recyclerView.setHasFixedSize(true);

        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        stockDataAdapter = new StockDataAdapter();
        recyclerView.setAdapter(stockDataAdapter);

        Observable.just("Please use this app responsibly!")
                .subscribe(s -> helloText.setText(s));

        YahooService yahooService = new RetrofitYahooServiceFactory().create();

        String query = "select * from yahoo.finance.quote where symbol in ('YHOO','AAPL','GOOG','MSFT')";
        String env = "store://datatables.org/alltableswithkeys";

        Observable.interval(0, 5, TimeUnit.SECONDS)
                .flatMap(
                        i -> Observable.<YahooStockResult>error(new RuntimeException("Crash"))
                )
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnError(error -> {
                    log("doOnError", "error");
                    Toast.makeText(this, "We couldn't reach internet - falling back to local data",
                            Toast.LENGTH_SHORT)
                            .show();
                })
                .observeOn(Schedulers.io())
                .map(r -> r.getQuery().getResults().getQuote())
                .flatMap(Observable::fromIterable)
                .map(StockUpdate::create)
                .doOnNext(this::saveStockUpdate)
                .onExceptionResumeNext(
                        v2(StorIOFactory.get(this)
                                .get()
                                .listOfObjects(StockUpdate.class)
                                .withQuery(Query.builder()
                                        .table(StockUpdateTable.TABLE)
                                        .orderBy("date DESC")
                                        .limit(50)
                                        .build())
                                .prepare()
                                .asRxObservable())
                                .take(1)
                                .flatMap(Observable::fromIterable)
                )
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(stockUpdate -> {
                    Log.d("APP", "New update " + stockUpdate.getStockSymbol());
                    noDataAvailableView.setVisibility(View.GONE);
                    stockDataAdapter.add(stockUpdate);
                }, error -> {
                    if (stockDataAdapter.getItemCount() == 0) {
                        noDataAvailableView.setVisibility(View.VISIBLE);
                    }
                });

//        demo10();
    }
 
Example #11
Source File: MainActivity.java    From Reactive-Android-Programming with MIT License 4 votes vote down vote up
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        log(this.toString());

        RxJavaPlugins.setErrorHandler(ErrorHandler.get());

        ButterKnife.bind(this);

        recyclerView.setHasFixedSize(true);

        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        stockDataAdapter = new StockDataAdapter();
        recyclerView.setAdapter(stockDataAdapter);

        Observable.just("Please use this app responsibly!")
                .subscribe(s -> helloText.setText(s));

        YahooService yahooService = new RetrofitYahooServiceFactory().create();

        String query = "select * from yahoo.finance.quote where symbol in ('YHOO','AAPL','GOOG','MSFT')";
        String env = "store://datatables.org/alltableswithkeys";

        final Configuration configuration = new ConfigurationBuilder()
                .setDebugEnabled(BuildConfig.DEBUG)
                .setOAuthConsumerKey("tTlvwBfqduVadKKEwMXDCmzA4")
                .setOAuthConsumerSecret("FiIOveHm9jLAtf0YSopWROeOFo3OA9VBM2CAuKwZ8AoL1gl4AK")
                .setOAuthAccessToken("195655474-QY8neLxXxqOsF8PGM8MYLsYGyQxQZA73S4qp0Sc2")
                .setOAuthAccessTokenSecret("lIiock0OTkR4TflFPb9pSMjLL8pN9JKIYKBhWMWwtxyMa")
                .build();

        final FilterQuery filterQuery = new FilterQuery()
                .track("Yahoo", "Google", "Microsoft")
                .language("en");


        Observable.merge(
                Observable.interval(0, 5, TimeUnit.SECONDS)
                        .flatMap(
                                i -> yahooService.yqlQuery(query, env)
                                        .toObservable()
                        )
                        .map(r -> r.getQuery().getResults().getQuote())
                        .flatMap(Observable::fromIterable)
                        .map(StockUpdate::create),
                observeTwitterStream(configuration, filterQuery)
                        .sample(2700, TimeUnit.MILLISECONDS)
                        .map(StockUpdate::create)
        )
                .compose(bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .doOnError(ErrorHandler.get())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnError(error -> {
                    Toast.makeText(this, "We couldn't reach internet - falling back to local data",
                            Toast.LENGTH_SHORT)
                            .show();
                })
                .observeOn(Schedulers.io())
                .doOnNext(this::saveStockUpdate)
                .onExceptionResumeNext(
                        v2(StorIOFactory.get(this)
                                .get()
                                .listOfObjects(StockUpdate.class)
                                .withQuery(Query.builder()
                                        .table(StockUpdateTable.TABLE)
                                        .orderBy("date DESC")
                                        .limit(50)
                                        .build())
                                .prepare()
                                .asRxObservable())
                                .take(1)
                                .flatMap(Observable::fromIterable)
                )
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(stockUpdate -> {
                    Log.d("APP", "New update " + stockUpdate.getStockSymbol());
                    noDataAvailableView.setVisibility(View.GONE);
                    stockDataAdapter.add(stockUpdate);
                    recyclerView.smoothScrollToPosition(0);
                }, error -> {
                    if (stockDataAdapter.getItemCount() == 0) {
                        noDataAvailableView.setVisibility(View.VISIBLE);
                    }
                });

//        demo0();
//        demo3();

    }
 
Example #12
Source File: MainActivity.java    From Reactive-Android-Programming with MIT License 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    log(this.toString());

    RxJavaPlugins.setErrorHandler(ErrorHandler.get());

    ButterKnife.bind(this);

    recyclerView.setHasFixedSize(true);

    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    stockDataAdapter = new StockDataAdapter();
    recyclerView.setAdapter(stockDataAdapter);

    Observable.just("Please use this app responsibly!")
            .subscribe(s -> helloText.setText(s));

    YahooService yahooService = new RetrofitYahooServiceFactory().create();

    String query = "select * from yahoo.finance.quote where symbol in ('YHOO','AAPL','GOOG','MSFT')";
    String env = "store://datatables.org/alltableswithkeys";

    Observable.interval(0, 5, TimeUnit.SECONDS)
            .compose(bindToLifecycle())
            .flatMap(
                    i -> yahooService.yqlQuery(query, env)
                            .toObservable()
            )
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .doOnError(error -> {
                Toast.makeText(this, "We couldn't reach internet - falling back to local data",
                        Toast.LENGTH_SHORT)
                        .show();
            })
            .observeOn(Schedulers.io())
            .map(r -> r.getQuery().getResults().getQuote())
            .flatMap(Observable::fromIterable)
            .map(StockUpdate::create)
            .doOnNext(this::saveStockUpdate)
            .onExceptionResumeNext(
                    v2(StorIOFactory.get(this)
                            .get()
                            .listOfObjects(StockUpdate.class)
                            .withQuery(Query.builder()
                                    .table(StockUpdateTable.TABLE)
                                    .orderBy("date DESC")
                                    .limit(50)
                                    .build())
                            .prepare()
                            .asRxObservable())
                            .take(1)
                            .flatMap(Observable::fromIterable)
            )
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(stockUpdate -> {
                Log.d("APP", "New update " + stockUpdate.getStockSymbol());
                noDataAvailableView.setVisibility(View.GONE);
                stockDataAdapter.add(stockUpdate);
            }, error -> {
                if (stockDataAdapter.getItemCount() == 0) {
                    noDataAvailableView.setVisibility(View.VISIBLE);
                }
            });

    demo01();
}