Search in sources :

Example 1 with Subreddit

use of com.winsonchiu.reader.data.reddit.Subreddit 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 2 with Subreddit

use of com.winsonchiu.reader.data.reddit.Subreddit in project Reader by TheKeeperOfPie.

the class FragmentSearch method onCreateView.

// TODO: Remove/fix ResourceType warning
@SuppressWarnings("ResourceType")
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = bind(inflater.inflate(R.layout.fragment_search, container, false));
    listenerSearch = new ControllerSearch.Listener() {

        @Override
        public AdapterSearchSubreddits getAdapterSearchSubreddits() {
            return adapterSearchSubreddits;
        }

        @Override
        public AdapterSearchSubreddits getAdapterSearchSubredditsRecommended() {
            return adapterSearchSubredditsRecommended;
        }

        @Override
        public void scrollToLinks(int position) {
            layoutManagerLinks.scrollToPositionWithOffset(0, 0);
        }

        @Override
        public void scrollToLinksSubreddit(int position) {
            layoutManagerLinksSubreddit.scrollToPositionWithOffset(0, 0);
        }
    };
    int windowBackground = UtilsTheme.getAttributeColor(getContext(), android.R.attr.windowBackground, 0);
    setUpToolbar();
    adapterSearchSubreddits = new AdapterSearchSubreddits(getActivity(), new ControllerSearchBase() {

        @Override
        public Subreddit getSubreddit(int position) {
            return controllerSearch.getSubreddit(position);
        }

        @Override
        public int getSubredditCount() {
            return controllerSearch.getCountSubreddit();
        }
    }, new AdapterSearchSubreddits.ViewHolder.EventListener() {

        @Override
        public void onClickSubreddit(Subreddit subreddit) {
            controllerSearch.addViewedSubreddit(subreddit);
            controllerLinks.setParameters(subreddit.getDisplayName(), Sort.HOT, Time.ALL);
            closeKeyboard();
            mListener.onNavigationBackClick();
        }

        @Override
        public boolean supportsDrag() {
            return true;
        }

        @Override
        public void onStartDrag(AdapterSearchSubreddits.ViewHolder viewHolder) {
            itemTouchHelperSubreddits.startDrag(viewHolder);
        }

        @Override
        public void sendToTop(AdapterSearchSubreddits.ViewHolder viewHolder) {
            controllerSearch.moveSubreddit(viewHolder.getAdapterPosition(), 0);
            recyclerSearchSubreddits.postOnAnimationDelayed(() -> {
                if (layoutManagerSubreddits.findFirstVisibleItemPosition() <= 1) {
                    layoutManagerSubreddits.smoothScrollToPosition(recyclerSearchSubreddits, null, 0);
                }
            }, 150);
        }

        @Override
        public boolean isSubscriptionListShown() {
            return controllerSearch.isSubscriptionListShown();
        }
    });
    layoutManagerSubreddits = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    recyclerSearchSubreddits.setLayoutManager(layoutManagerSubreddits);
    recyclerSearchSubreddits.setAdapter(adapterSearchSubreddits);
    recyclerSearchSubreddits.addItemDecoration(new ItemDecorationDivider(getActivity(), ItemDecorationDivider.VERTICAL_LIST));
    itemTouchHelperSubreddits = new ItemTouchHelper(new SimpleCallbackBackground(0, 0, windowBackground) {

        @Override
        public int getDragDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            if (controllerSearch.isSubscriptionListShown()) {
                return ItemTouchHelper.UP | ItemTouchHelper.DOWN;
            }
            return super.getDragDirs(recyclerView, viewHolder);
        }

        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            controllerSearch.moveSubreddit(viewHolder.getAdapterPosition(), target.getAdapterPosition());
            return true;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        }

        @Override
        public boolean isLongPressDragEnabled() {
            return false;
        }
    });
    itemTouchHelperSubreddits.attachToRecyclerView(recyclerSearchSubreddits);
    AdapterLink.ViewHolderLink.Listener listenerLink = new AdapterLink.ViewHolderLink.Listener() {

        @Override
        public void onSubmitComment(Link link, String text) {
        }

        @Override
        public void onDownloadImage(Link link) {
        }

        @Override
        public void onDownloadImage(Link link, String title, String fileName, String url) {
        }

        @Override
        public void onLoadUrl(Link link, boolean forceExternal) {
        }

        @Override
        public void onShowFullEditor(Link link) {
        }

        @Override
        public void onVote(Link link, AdapterLink.ViewHolderLink viewHolderLink, Likes vote) {
        }

        @Override
        public void onCopyText(Link link) {
        }

        @Override
        public void onEdit(Link link) {
        }

        @Override
        public void onDelete(Link link) {
        }

        @Override
        public void onReport(Link link) {
        }

        @Override
        public void onSave(Link link) {
        }

        @Override
        public void onShowComments(Link link, AdapterLink.ViewHolderLink viewHolderLink, Source source) {
        }

        @Override
        public void onShowError(String error) {
        }

        @Override
        public void onMarkNsfw(Link link) {
        }
    };
    adapterLinks = new AdapterSearchLinkList(getActivity(), new AdapterListener() {

        @Override
        public void requestMore() {
            controllerSearch.loadMoreLinks().subscribe(getReloadObserver());
        }

        @Override
        public void scrollAndCenter(int position, int height) {
            UtilsAnimation.scrollToPositionWithCentering(position, recyclerSearchLinks, false);
        }

        @Override
        public void hideToolbar() {
            AppBarLayout.Behavior behaviorAppBar = (AppBarLayout.Behavior) ((CoordinatorLayout.LayoutParams) layoutAppBar.getLayoutParams()).getBehavior();
            behaviorAppBar.onNestedFling(layoutCoordinator, layoutAppBar, null, 0, 1000, true);
        }

        @Override
        public void clearDecoration() {
            AppBarLayout.Behavior behaviorAppBar = (AppBarLayout.Behavior) ((CoordinatorLayout.LayoutParams) layoutAppBar.getLayoutParams()).getBehavior();
            behaviorAppBar.onNestedFling(layoutCoordinator, layoutAppBar, null, 0, 1000, true);
        }

        @Override
        public void requestDisallowInterceptTouchEventVertical(boolean disallow) {
            recyclerSearchLinks.requestDisallowInterceptTouchEvent(disallow);
            recyclerSearchLinksSubreddit.requestDisallowInterceptTouchEvent(disallow);
            pagerSearch.requestDisallowInterceptTouchEvent(disallow);
        }

        @Override
        public void requestDisallowInterceptTouchEventHorizontal(boolean disallow) {
        }
    }, new AdapterLink.ViewHolderHeader.EventListener() {

        @Override
        public void onClickSubmit(Reddit.PostType postType) {
        }

        @Override
        public void showSidebar() {
        }
    }, listenerLink, Source.SEARCH_LINKS);
    adapterLinksSubreddit = new AdapterSearchLinkList(getActivity(), new AdapterListener() {

        @Override
        public void requestMore() {
            controllerSearch.loadMoreLinksSubreddit().subscribe(getReloadObserver());
        }

        @Override
        public void scrollAndCenter(int position, int height) {
            UtilsAnimation.scrollToPositionWithCentering(position, recyclerSearchLinksSubreddit, false);
        }

        @Override
        public void hideToolbar() {
            AppBarLayout.Behavior behaviorAppBar = (AppBarLayout.Behavior) ((CoordinatorLayout.LayoutParams) layoutAppBar.getLayoutParams()).getBehavior();
            behaviorAppBar.onNestedFling(layoutCoordinator, layoutAppBar, null, 0, 1000, true);
        }

        @Override
        public void clearDecoration() {
            AppBarLayout.Behavior behaviorAppBar = (AppBarLayout.Behavior) ((CoordinatorLayout.LayoutParams) layoutAppBar.getLayoutParams()).getBehavior();
            behaviorAppBar.onNestedFling(layoutCoordinator, layoutAppBar, null, 0, 1000, true);
        }

        @Override
        public void requestDisallowInterceptTouchEventVertical(boolean disallow) {
            recyclerSearchLinks.requestDisallowInterceptTouchEvent(disallow);
            recyclerSearchLinksSubreddit.requestDisallowInterceptTouchEvent(disallow);
            pagerSearch.requestDisallowInterceptTouchEvent(disallow);
        }

        @Override
        public void requestDisallowInterceptTouchEventHorizontal(boolean disallow) {
        }
    }, new AdapterLink.ViewHolderHeader.EventListener() {

        @Override
        public void onClickSubmit(Reddit.PostType postType) {
        }

        @Override
        public void showSidebar() {
        }
    }, listenerLink, Source.SEARCH_LINKS_SUBREDDIT);
    layoutManagerLinks = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    recyclerSearchLinks.setLayoutManager(layoutManagerLinks);
    recyclerSearchLinks.setAdapter(adapterLinks);
    recyclerSearchLinks.addItemDecoration(new ItemDecorationDivider(getActivity(), ItemDecorationDivider.VERTICAL_LIST));
    layoutManagerLinksSubreddit = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    recyclerSearchLinksSubreddit.setLayoutManager(layoutManagerLinksSubreddit);
    recyclerSearchLinksSubreddit.setAdapter(adapterLinksSubreddit);
    recyclerSearchLinksSubreddit.addItemDecoration(new ItemDecorationDivider(getActivity(), ItemDecorationDivider.VERTICAL_LIST));
    adapterSearchSubredditsRecommended = new AdapterSearchSubreddits(getActivity(), new ControllerSearchBase() {

        @Override
        public Subreddit getSubreddit(int position) {
            return controllerSearch.getSubredditRecommended(position);
        }

        @Override
        public int getSubredditCount() {
            return controllerSearch.getCountSubredditRecommended();
        }
    }, new AdapterSearchSubreddits.ViewHolder.EventListener() {

        @Override
        public void onClickSubreddit(Subreddit subreddit) {
            controllerLinks.setParameters(subreddit.getDisplayName(), Sort.HOT, Time.ALL);
            closeKeyboard();
            mListener.onNavigationBackClick();
        }

        @Override
        public boolean supportsDrag() {
            return false;
        }

        @Override
        public void onStartDrag(AdapterSearchSubreddits.ViewHolder viewHolder) {
        }

        @Override
        public void sendToTop(AdapterSearchSubreddits.ViewHolder viewHolder) {
        }

        @Override
        public boolean isSubscriptionListShown() {
            return controllerSearch.isSubscriptionListShown();
        }
    });
    layoutManagerSubredditsRecommended = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    recyclerSearchSubredditsRecommended.setLayoutManager(layoutManagerSubredditsRecommended);
    recyclerSearchSubredditsRecommended.setAdapter(adapterSearchSubredditsRecommended);
    recyclerSearchSubredditsRecommended.addItemDecoration(new ItemDecorationDivider(getActivity(), ItemDecorationDivider.VERTICAL_LIST));
    pagerSearch.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            controllerSearch.setCurrentPage(position).subscribe(getReloadObserver());
            boolean sortSubredditsShown = controllerSearch.getCurrentPage() == ControllerSearch.PAGE_SUBREDDITS || controllerSearch.getCurrentPage() == ControllerSearch.PAGE_SUBREDDITS_RECOMMENDED;
            menu.findItem(R.id.item_sort_subreddits).setEnabled(sortSubredditsShown);
            menu.findItem(R.id.item_sort_subreddits).setVisible(sortSubredditsShown);
            menu.findItem(R.id.item_sort).setEnabled(!sortSubredditsShown);
            menu.findItem(R.id.item_sort).setVisible(!sortSubredditsShown);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });
    final int count = controllerLinks.isOnSpecificSubreddit() ? pagerSearch.getChildCount() : pagerSearch.getChildCount() - 1;
    pagerAdapter = new PagerAdapter() {

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            return pagerSearch.getChildAt(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
        // No need to destroy the RecyclerViews since they'll be reused for a new query
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch(position) {
                case ControllerSearch.PAGE_SUBREDDITS:
                    return getString(R.string.subreddit);
                case ControllerSearch.PAGE_LINKS:
                    return getString(R.string.all);
                case ControllerSearch.PAGE_LINKS_SUBREDDIT:
                    return controllerLinks.getSubredditName();
                case ControllerSearch.PAGE_SUBREDDITS_RECOMMENDED:
                    return getString(R.string.recommended);
            }
            return super.getPageTitle(position);
        }

        @Override
        public int getCount() {
            return count;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    };
    pagerSearch.setAdapter(pagerAdapter);
    layoutTabs.setTabMode(TabLayout.MODE_SCROLLABLE);
    layoutTabs.setTabTextColors(themer.getColorFilterTextMuted().getColor(), themer.getColorFilterPrimary().getColor());
    layoutTabs.setBackgroundColor(getResources().getColor(android.R.color.transparent));
    layoutTabs.setupWithViewPager(pagerSearch);
    pagerSearch.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(layoutTabs));
    return view;
}
Also used : ItemDecorationDivider(com.winsonchiu.reader.utils.ItemDecorationDivider) AdapterListener(com.winsonchiu.reader.adapter.AdapterListener) AdapterLink(com.winsonchiu.reader.links.AdapterLink) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) Source(com.winsonchiu.reader.comments.Source) PagerAdapter(android.support.v4.view.PagerAdapter) ItemTouchHelper(android.support.v7.widget.helper.ItemTouchHelper) TabLayout(android.support.design.widget.TabLayout) AppBarLayout(android.support.design.widget.AppBarLayout) SimpleCallbackBackground(com.winsonchiu.reader.utils.SimpleCallbackBackground) AdapterListener(com.winsonchiu.reader.adapter.AdapterListener) ViewGroup(android.view.ViewGroup) ViewPager(android.support.v4.view.ViewPager) SearchView(android.support.v7.widget.SearchView) ImageView(android.widget.ImageView) BindView(butterknife.BindView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) CoordinatorLayout(android.support.design.widget.CoordinatorLayout) RecyclerView(android.support.v7.widget.RecyclerView) Subreddit(com.winsonchiu.reader.data.reddit.Subreddit) Likes(com.winsonchiu.reader.data.reddit.Likes) AdapterLink(com.winsonchiu.reader.links.AdapterLink) Link(com.winsonchiu.reader.data.reddit.Link)

Example 3 with Subreddit

use of com.winsonchiu.reader.data.reddit.Subreddit in project Reader by TheKeeperOfPie.

the class ControllerSearch method saveSubscriptions.

private void saveSubscriptions(Listing listing) {
    Log.d(TAG, "saveSubscriptions with listing: " + listing.getChildren());
    boolean sort = subredditsSubscribed.getChildren().isEmpty();
    if (controllerUser.hasUser()) {
        ListIterator<Thing> iterator = subredditsSubscribed.getChildren().listIterator();
        while (iterator.hasNext()) {
            Thing next = iterator.next();
            int index = listing.getChildren().indexOf(next);
            if (index > -1) {
                iterator.set(listing.getChildren().get(index));
            } else {
                iterator.remove();
            }
        }
    }
    subredditsSubscribed.addChildren(listing.getChildren());
    subredditsLoaded = new Listing();
    if (sort) {
        Collections.sort(subredditsSubscribed.getChildren(), new Comparator<Thing>() {

            @Override
            public int compare(Thing lhs, Thing rhs) {
                return ((Subreddit) lhs).getDisplayName().compareToIgnoreCase(((Subreddit) rhs).getDisplayName());
            }
        });
    }
    saveSubscriptions();
}
Also used : Listing(com.winsonchiu.reader.data.reddit.Listing) Subreddit(com.winsonchiu.reader.data.reddit.Subreddit) Thing(com.winsonchiu.reader.data.reddit.Thing)

Example 4 with Subreddit

use of com.winsonchiu.reader.data.reddit.Subreddit in project Reader by TheKeeperOfPie.

the class ControllerSearch method setSort.

public Observable<Listing> setSort(Sort sort) {
    Listing listingSort = getCurrentPage() == PAGE_SUBREDDITS_RECOMMENDED ? subredditsRecommended : subreddits;
    if (Sort.ALPHABETICAL.equals(sort)) {
        if (Sort.ALPHABETICAL.equals(sortSubreddits)) {
            Collections.sort(listingSort.getChildren(), new Comparator<Thing>() {

                @Override
                public int compare(Thing lhs, Thing rhs) {
                    return ((Subreddit) rhs).getDisplayName().compareToIgnoreCase(((Subreddit) lhs).getDisplayName());
                }
            });
            sortSubreddits = null;
        } else {
            Collections.sort(listingSort.getChildren(), new Comparator<Thing>() {

                @Override
                public int compare(Thing lhs, Thing rhs) {
                    return ((Subreddit) lhs).getDisplayName().compareToIgnoreCase(((Subreddit) rhs).getDisplayName());
                }
            });
            sortSubreddits = Sort.ALPHABETICAL;
        }
        if (listingSort == subredditsSubscribed) {
            saveSubscriptions();
        } else if (listingSort == subredditsRecommended) {
            for (Listener listener : listeners) {
                listener.getAdapterSearchSubredditsRecommended().notifyDataSetChanged();
            }
        } else {
            for (Listener listener : listeners) {
                listener.getAdapterSearchSubreddits().notifyDataSetChanged();
            }
        }
    } else if (Sort.SUBSCRIBERS.equals(sort)) {
        if (Sort.SUBSCRIBERS.equals(sortSubreddits)) {
            Collections.sort(listingSort.getChildren(), new Comparator<Thing>() {

                @Override
                public int compare(Thing lhs, Thing rhs) {
                    long subscribersFirst = ((Subreddit) lhs).getSubscribers();
                    long subscribersSecond = ((Subreddit) rhs).getSubscribers();
                    return subscribersFirst < subscribersSecond ? -1 : (subscribersFirst == subscribersSecond ? 0 : 1);
                }
            });
            sortSubreddits = null;
        } else {
            Collections.sort(listingSort.getChildren(), new Comparator<Thing>() {

                @Override
                public int compare(Thing lhs, Thing rhs) {
                    long subscribersFirst = ((Subreddit) lhs).getSubscribers();
                    long subscribersSecond = ((Subreddit) rhs).getSubscribers();
                    return subscribersSecond < subscribersFirst ? -1 : (subscribersFirst == subscribersSecond ? 0 : 1);
                }
            });
            sortSubreddits = Sort.SUBSCRIBERS;
        }
        if (listingSort == subredditsSubscribed) {
            saveSubscriptions();
        } else if (listingSort == subredditsRecommended) {
            for (Listener listener : listeners) {
                listener.getAdapterSearchSubredditsRecommended().notifyDataSetChanged();
            }
        } else {
            for (Listener listener : listeners) {
                listener.getAdapterSearchSubreddits().notifyDataSetChanged();
            }
        }
    } else if (this.sort != sort) {
        this.sort = sort;
        eventHolder.getSort().call(sort);
        return reloadCurrentPage();
    }
    return Observable.empty();
}
Also used : Listing(com.winsonchiu.reader.data.reddit.Listing) Subreddit(com.winsonchiu.reader.data.reddit.Subreddit) Thing(com.winsonchiu.reader.data.reddit.Thing) Comparator(java.util.Comparator)

Example 5 with Subreddit

use of com.winsonchiu.reader.data.reddit.Subreddit 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)

Aggregations

Subreddit (com.winsonchiu.reader.data.reddit.Subreddit)11 Thing (com.winsonchiu.reader.data.reddit.Thing)7 Link (com.winsonchiu.reader.data.reddit.Link)6 Listing (com.winsonchiu.reader.data.reddit.Listing)6 Sort (com.winsonchiu.reader.data.reddit.Sort)6 Time (com.winsonchiu.reader.data.reddit.Time)6 SharedPreferences (android.content.SharedPreferences)5 Nullable (android.support.annotation.Nullable)5 TextUtils (android.text.TextUtils)5 Log (android.util.Log)5 AppSettings (com.winsonchiu.reader.AppSettings)5 ControllerUser (com.winsonchiu.reader.ControllerUser)5 Reddit (com.winsonchiu.reader.data.reddit.Reddit)5 UtilsRx (com.winsonchiu.reader.utils.UtilsRx)5 Comparator (java.util.Comparator)5 Inject (javax.inject.Inject)5 AccountManager (android.accounts.AccountManager)4 Context (android.content.Context)4 PreferenceManager (android.preference.PreferenceManager)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4