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);
}
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;
}
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;
}
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();
}
}
});
}
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());
}
}
Aggregations