Search in sources :

Example 1 with FinalizingSubscriber

use of com.winsonchiu.reader.rx.FinalizingSubscriber in project Reader by TheKeeperOfPie.

the class ActivityMain method addNewAccount.

public void addNewAccount() {
    accountManager.addAccount(Reddit.ACCOUNT_TYPE, Reddit.AUTH_TOKEN_FULL_ACCESS, null, null, this, new AccountManagerCallback<Bundle>() {

        @Override
        public void run(final AccountManagerFuture<Bundle> future) {
            Observable.just(future).subscribeOn(Schedulers.computation()).observeOn(Schedulers.computation()).flatMap(UtilsRx.flatMapWrapError(AccountManagerFuture::getResult)).observeOn(AndroidSchedulers.mainThread()).subscribe(new FinalizingSubscriber<Bundle>() {

                @Override
                public void next(Bundle next) {
                    super.next(next);
                    String name = next.getString(AccountManager.KEY_ACCOUNT_NAME);
                    Account[] accounts = accountManager.getAccountsByType(Reddit.ACCOUNT_TYPE);
                    for (Account account : accounts) {
                        if (account.name.equals(name)) {
                            setAccount(account);
                            break;
                        }
                    }
                }

                @Override
                public void error(Throwable e) {
                    super.error(e);
                    Toast.makeText(ActivityMain.this, R.string.error_account, Toast.LENGTH_LONG).show();
                }

                @Override
                public void finish() {
                    super.finish();
                    resetAccountList();
                }
            });
        }
    }, null);
}
Also used : Account(android.accounts.Account) Bundle(android.os.Bundle) AccountManagerFuture(android.accounts.AccountManagerFuture) FinalizingSubscriber(com.winsonchiu.reader.rx.FinalizingSubscriber)

Example 2 with FinalizingSubscriber

use of com.winsonchiu.reader.rx.FinalizingSubscriber in project Reader by TheKeeperOfPie.

the class ControllerSearch method loadMoreLinksSubreddit.

public Observable<Listing> loadMoreLinksSubreddit() {
    if (isLoadingLinksSubreddit()) {
        return Observable.empty();
    }
    Subreddit subreddit = controllerLinks.getSubreddit();
    if (subscriptionLinksSubreddit != null && !subscriptionLinksSubreddit.isUnsubscribed()) {
        subscriptionLinksSubreddit.unsubscribe();
        subscriptionLinksSubreddit = null;
    }
    String pathSubreddit = subreddit.getUrl();
    if (pathSubreddit.length() < 2) {
        pathSubreddit = "";
    }
    Observable<Listing> observable = reddit.search(pathSubreddit, query, sort.toString(), time.toString(), linksSubreddit.getAfter(), true).flatMap(UtilsRx.flatMapWrapError(response -> Listing.fromJson(ComponentStatic.getObjectMapper().readValue(response, JsonNode.class))));
    subscriptionLinksSubreddit = observable.subscribe(new FinalizingSubscriber<Listing>() {

        @Override
        public void start() {
            setLoadingLinksSubreddit(true);
        }

        @Override
        public void next(Listing listing) {
            if (listing.getChildren().isEmpty() || listing.getChildren().get(0) instanceof Subreddit) {
                return;
            }
            int startSize = linksSubreddit.getChildren().size();
            int positionStart = startSize + 1;
            linksSubreddit.addChildren(listing.getChildren());
            linksSubreddit.setAfter(listing.getAfter());
            eventHolder.callLinksSubreddit(new RxAdapterEvent<>(getLinksSubredditModel(), RxAdapterEvent.Type.INSERT, positionStart, linksSubreddit.getChildren().size() - startSize));
        }

        @Override
        public void finish() {
            setLoadingLinksSubreddit(false);
        }
    });
    return observable;
}
Also used : Context(android.content.Context) CustomApplication(com.winsonchiu.reader.CustomApplication) Subreddit(com.winsonchiu.reader.data.reddit.Subreddit) ControllerLinks(com.winsonchiu.reader.links.ControllerLinks) ListIterator(java.util.ListIterator) UtilsRx(com.winsonchiu.reader.utils.UtilsRx) AppSettings(com.winsonchiu.reader.AppSettings) LinksModel(com.winsonchiu.reader.links.LinksModel) ArrayList(java.util.ArrayList) Observable(rx.Observable) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Reddit(com.winsonchiu.reader.data.reddit.Reddit) JSONObject(org.json.JSONObject) ComponentStatic(com.winsonchiu.reader.dagger.components.ComponentStatic) Link(com.winsonchiu.reader.data.reddit.Link) JsonNode(com.fasterxml.jackson.databind.JsonNode) PreferenceManager(android.preference.PreferenceManager) Log(android.util.Log) AccountManager(android.accounts.AccountManager) BehaviorRelay(com.jakewharton.rxrelay.BehaviorRelay) Iterator(java.util.Iterator) ControllerUser(com.winsonchiu.reader.ControllerUser) Sort(com.winsonchiu.reader.data.reddit.Sort) Listing(com.winsonchiu.reader.data.reddit.Listing) Set(java.util.Set) TextUtils(android.text.TextUtils) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) FinalizingSubscriber(com.winsonchiu.reader.rx.FinalizingSubscriber) IOException(java.io.IOException) Thing(com.winsonchiu.reader.data.reddit.Thing) Observer(rx.Observer) URLEncoder(java.net.URLEncoder) List(java.util.List) SharedPreferences(android.content.SharedPreferences) Replyable(com.winsonchiu.reader.data.reddit.Replyable) Time(com.winsonchiu.reader.data.reddit.Time) RxAdapterEvent(com.winsonchiu.reader.adapter.RxAdapterEvent) Nullable(android.support.annotation.Nullable) Comparator(java.util.Comparator) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) Subscription(rx.Subscription) JSONArray(org.json.JSONArray) Listing(com.winsonchiu.reader.data.reddit.Listing) JsonNode(com.fasterxml.jackson.databind.JsonNode) Subreddit(com.winsonchiu.reader.data.reddit.Subreddit) FinalizingSubscriber(com.winsonchiu.reader.rx.FinalizingSubscriber)

Example 3 with FinalizingSubscriber

use of com.winsonchiu.reader.rx.FinalizingSubscriber in project Reader by TheKeeperOfPie.

the class ControllerSearch method reloadLinksSubreddit.

public Observable<Listing> reloadLinksSubreddit() {
    if (subscriptionLinksSubreddit != null && !subscriptionLinksSubreddit.isUnsubscribed()) {
        subscriptionLinksSubreddit.unsubscribe();
        subscriptionLinksSubreddit = null;
    }
    Subreddit subreddit = controllerLinks.getSubreddit();
    String pathSubreddit = subreddit.getUrl();
    if (pathSubreddit.length() < 2) {
        pathSubreddit = "";
    }
    Observable<Listing> observable = reddit.search(pathSubreddit, query, sort.toString(), time.toString(), null, true).flatMap(UtilsRx.flatMapWrapError(response -> Listing.fromJson(ComponentStatic.getObjectMapper().readValue(response, JsonNode.class))));
    subscriptionSubreddits = observable.subscribe(new FinalizingSubscriber<Listing>() {

        @Override
        public void start() {
            setLoadingLinksSubreddit(true);
        }

        @Override
        public void next(Listing listing) {
            linksSubreddit = listing;
            eventHolder.callLinksSubreddit(new RxAdapterEvent<>(getLinksSubredditModel()));
            for (Listener listener : listeners) {
                listener.scrollToLinksSubreddit(0);
            }
        }

        @Override
        public void finish() {
            setLoadingLinksSubreddit(false);
        }
    });
    return observable;
}
Also used : Context(android.content.Context) CustomApplication(com.winsonchiu.reader.CustomApplication) Subreddit(com.winsonchiu.reader.data.reddit.Subreddit) ControllerLinks(com.winsonchiu.reader.links.ControllerLinks) ListIterator(java.util.ListIterator) UtilsRx(com.winsonchiu.reader.utils.UtilsRx) AppSettings(com.winsonchiu.reader.AppSettings) LinksModel(com.winsonchiu.reader.links.LinksModel) ArrayList(java.util.ArrayList) Observable(rx.Observable) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Reddit(com.winsonchiu.reader.data.reddit.Reddit) JSONObject(org.json.JSONObject) ComponentStatic(com.winsonchiu.reader.dagger.components.ComponentStatic) Link(com.winsonchiu.reader.data.reddit.Link) JsonNode(com.fasterxml.jackson.databind.JsonNode) PreferenceManager(android.preference.PreferenceManager) Log(android.util.Log) AccountManager(android.accounts.AccountManager) BehaviorRelay(com.jakewharton.rxrelay.BehaviorRelay) Iterator(java.util.Iterator) ControllerUser(com.winsonchiu.reader.ControllerUser) Sort(com.winsonchiu.reader.data.reddit.Sort) Listing(com.winsonchiu.reader.data.reddit.Listing) Set(java.util.Set) TextUtils(android.text.TextUtils) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) FinalizingSubscriber(com.winsonchiu.reader.rx.FinalizingSubscriber) IOException(java.io.IOException) Thing(com.winsonchiu.reader.data.reddit.Thing) Observer(rx.Observer) URLEncoder(java.net.URLEncoder) List(java.util.List) SharedPreferences(android.content.SharedPreferences) Replyable(com.winsonchiu.reader.data.reddit.Replyable) Time(com.winsonchiu.reader.data.reddit.Time) RxAdapterEvent(com.winsonchiu.reader.adapter.RxAdapterEvent) Nullable(android.support.annotation.Nullable) Comparator(java.util.Comparator) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) Subscription(rx.Subscription) JSONArray(org.json.JSONArray) Listing(com.winsonchiu.reader.data.reddit.Listing) JsonNode(com.fasterxml.jackson.databind.JsonNode) Subreddit(com.winsonchiu.reader.data.reddit.Subreddit) FinalizingSubscriber(com.winsonchiu.reader.rx.FinalizingSubscriber)

Example 4 with FinalizingSubscriber

use of com.winsonchiu.reader.rx.FinalizingSubscriber in project Reader by TheKeeperOfPie.

the class ControllerSearch method reloadSubredditsRecommended.

public void reloadSubredditsRecommended() {
    for (Subscription subscription : subscriptionsSubredditsRecommended) {
        subscription.unsubscribe();
    }
    subscriptionsSubredditsRecommended.clear();
    subredditsRecommended.getChildren().clear();
    for (Listener listener : listeners) {
        listener.getAdapterSearchSubredditsRecommended().notifyDataSetChanged();
    }
    StringBuilder builderOmit = new StringBuilder();
    for (Thing thing : subredditsSubscribed.getChildren()) {
        Subreddit subreddit = (Subreddit) thing;
        builderOmit.append(subreddit.getDisplayName());
        builderOmit.append(",");
    }
    currentSubreddit = controllerLinks.getSubreddit().getDisplayName();
    reddit.recommend(currentSubreddit, builderOmit.toString()).flatMap(UtilsRx.flatMapWrapError(response -> {
        final JSONArray jsonArray = new JSONArray(response);
        List<String> names = new ArrayList<>(jsonArray.length());
        for (int index = 0; index < jsonArray.length(); index++) {
            /*
                                No idea why the API returns a {"sr_name": "subreddit"} rather than an
                                array of Strings, but we'll convert it.
                             */
            JSONObject dataSubreddit = jsonArray.getJSONObject(index);
            names.add(dataSubreddit.optString("sr_name"));
        }
        return names;
    })).flatMap(Observable::from).flatMap(next -> reddit.about("/r/" + next + "/")).flatMap(UtilsRx.flatMapWrapError(response -> Subreddit.fromJson(ComponentStatic.getObjectMapper().readValue(response, JsonNode.class)))).subscribe(new FinalizingSubscriber<Subreddit>() {

        @Override
        public void start() {
            subredditsRecommended.getChildren().clear();
        }

        @Override
        public void next(Subreddit next) {
            subredditsRecommended.getChildren().add(next);
        }

        @Override
        public void finish() {
            Collections.sort(subredditsRecommended.getChildren(), new Comparator<Thing>() {

                @Override
                public int compare(Thing lhs, Thing rhs) {
                    return ((Subreddit) lhs).getDisplayName().compareToIgnoreCase(((Subreddit) rhs).getDisplayName());
                }
            });
            for (Listener listener : listeners) {
                listener.getAdapterSearchSubredditsRecommended().notifyDataSetChanged();
            }
        }
    });
}
Also used : Context(android.content.Context) CustomApplication(com.winsonchiu.reader.CustomApplication) Subreddit(com.winsonchiu.reader.data.reddit.Subreddit) ControllerLinks(com.winsonchiu.reader.links.ControllerLinks) ListIterator(java.util.ListIterator) UtilsRx(com.winsonchiu.reader.utils.UtilsRx) AppSettings(com.winsonchiu.reader.AppSettings) LinksModel(com.winsonchiu.reader.links.LinksModel) ArrayList(java.util.ArrayList) Observable(rx.Observable) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Reddit(com.winsonchiu.reader.data.reddit.Reddit) JSONObject(org.json.JSONObject) ComponentStatic(com.winsonchiu.reader.dagger.components.ComponentStatic) Link(com.winsonchiu.reader.data.reddit.Link) JsonNode(com.fasterxml.jackson.databind.JsonNode) PreferenceManager(android.preference.PreferenceManager) Log(android.util.Log) AccountManager(android.accounts.AccountManager) BehaviorRelay(com.jakewharton.rxrelay.BehaviorRelay) Iterator(java.util.Iterator) ControllerUser(com.winsonchiu.reader.ControllerUser) Sort(com.winsonchiu.reader.data.reddit.Sort) Listing(com.winsonchiu.reader.data.reddit.Listing) Set(java.util.Set) TextUtils(android.text.TextUtils) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) FinalizingSubscriber(com.winsonchiu.reader.rx.FinalizingSubscriber) IOException(java.io.IOException) Thing(com.winsonchiu.reader.data.reddit.Thing) Observer(rx.Observer) URLEncoder(java.net.URLEncoder) List(java.util.List) SharedPreferences(android.content.SharedPreferences) Replyable(com.winsonchiu.reader.data.reddit.Replyable) Time(com.winsonchiu.reader.data.reddit.Time) RxAdapterEvent(com.winsonchiu.reader.adapter.RxAdapterEvent) Nullable(android.support.annotation.Nullable) Comparator(java.util.Comparator) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) Subscription(rx.Subscription) JSONArray(org.json.JSONArray) JSONArray(org.json.JSONArray) Comparator(java.util.Comparator) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) Subscription(rx.Subscription) Subreddit(com.winsonchiu.reader.data.reddit.Subreddit) Thing(com.winsonchiu.reader.data.reddit.Thing)

Example 5 with FinalizingSubscriber

use of com.winsonchiu.reader.rx.FinalizingSubscriber in project Reader by TheKeeperOfPie.

the class FragmentProfile method setUpOptionsMenu.

private void setUpOptionsMenu() {
    toolbar.inflateMenu(R.menu.menu_profile);
    toolbar.setOnMenuItemClickListener(this);
    menu = toolbar.getMenu();
    menu.findItem(R.id.item_sort_hot).setChecked(true);
    itemSortTime = menu.findItem(R.id.item_sort_time);
    itemSearch = menu.findItem(R.id.item_search);
    final SearchView searchView = (SearchView) itemSearch.getActionView();
    View view = searchView.findViewById(android.support.v7.appcompat.R.id.search_go_btn);
    if (view instanceof ImageView) {
        ((ImageView) view).setColorFilter(themer.getColorFilterPrimary());
    }
    searchView.setQueryHint(getString(R.string.username));
    searchView.setOnCloseListener(new SearchView.OnCloseListener() {

        @Override
        public boolean onClose() {
            return false;
        }
    });
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String query) {
            controllerProfile.loadUser(query.replaceAll("\\s", "")).subscribe(new FinalizingSubscriber<User>() {

                @Override
                public void error(Throwable e) {
                    Toast.makeText(activity, getString(R.string.error_loading), Toast.LENGTH_SHORT).show();
                }
            });
            itemSearch.collapseActionView();
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });
    searchView.setSubmitButtonEnabled(true);
    menu.findItem(R.id.item_sort_hot).setChecked(true);
    menu.findItem(R.id.item_sort_time).setTitle(getString(R.string.time_description, getString(R.string.item_sort_all)));
    if (TextUtils.isEmpty(controllerUser.getUser().getName()) && !controllerProfile.isLoading()) {
        itemSearch.expandActionView();
    }
    for (int index = 0; index < menu.size(); index++) {
        menu.getItem(index).getIcon().mutate().setColorFilter(themer.getColorFilterPrimary());
    }
}
Also used : SearchView(android.support.v7.widget.SearchView) ImageView(android.widget.ImageView) SpannableString(android.text.SpannableString) SearchView(android.support.v7.widget.SearchView) ImageView(android.widget.ImageView) View(android.view.View) AdapterView(android.widget.AdapterView) RecyclerView(android.support.v7.widget.RecyclerView) FinalizingSubscriber(com.winsonchiu.reader.rx.FinalizingSubscriber)

Aggregations

FinalizingSubscriber (com.winsonchiu.reader.rx.FinalizingSubscriber)5 AccountManager (android.accounts.AccountManager)3 Context (android.content.Context)3 SharedPreferences (android.content.SharedPreferences)3 PreferenceManager (android.preference.PreferenceManager)3 Nullable (android.support.annotation.Nullable)3 TextUtils (android.text.TextUtils)3 Log (android.util.Log)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 BehaviorRelay (com.jakewharton.rxrelay.BehaviorRelay)3 AppSettings (com.winsonchiu.reader.AppSettings)3 ControllerUser (com.winsonchiu.reader.ControllerUser)3 CustomApplication (com.winsonchiu.reader.CustomApplication)3 RxAdapterEvent (com.winsonchiu.reader.adapter.RxAdapterEvent)3 ComponentStatic (com.winsonchiu.reader.dagger.components.ComponentStatic)3 Link (com.winsonchiu.reader.data.reddit.Link)3 Listing (com.winsonchiu.reader.data.reddit.Listing)3 Reddit (com.winsonchiu.reader.data.reddit.Reddit)3 Replyable (com.winsonchiu.reader.data.reddit.Replyable)3