Search in sources :

Example 1 with TaskProcessCallback

use of org.edx.mobile.view.common.TaskProcessCallback in project edx-app-android by edx.

the class CourseDiscussionPostsSearchFragment method loadNextPage.

@Override
public void loadNextPage(@NonNull final InfiniteScrollUtils.PageLoadCallback<DiscussionThread> callback) {
    final Activity activity = getActivity();
    final TaskProgressCallback progressCallback = activity instanceof TaskProgressCallback ? (TaskProgressCallback) activity : null;
    final TaskMessageCallback mCallback = activity instanceof TaskMessageCallback ? (TaskMessageCallback) activity : null;
    if (mCallback != null) {
        mCallback.onMessage(MessageType.EMPTY, "");
    }
    if (searchThreadListCall != null) {
        searchThreadListCall.cancel();
    }
    final List<String> requestedFields = Collections.singletonList(DiscussionRequestFields.PROFILE_IMAGE.getQueryParamValue());
    searchThreadListCall = discussionService.searchThreadList(courseData.getCourse().getId(), searchQuery, nextPage, requestedFields);
    final boolean isRefreshingSilently = callback.isRefreshingSilently();
    searchThreadListCall.enqueue(new ErrorHandlingCallback<Page<DiscussionThread>>(activity, // the ListView will start showing a footer-based loading indicator.
    nextPage > 1 || isRefreshingSilently ? null : progressCallback, mCallback, CallTrigger.LOADING_UNCACHED) {

        @Override
        protected void onResponse(@NonNull final Page<DiscussionThread> threadsPage) {
            ++nextPage;
            callback.onPageLoaded(threadsPage);
            if (activity instanceof TaskProcessCallback) {
                if (discussionPostsAdapter.getCount() == 0) {
                    String escapedTitle = TextUtils.htmlEncode(searchQuery);
                    String resultsText = ResourceUtil.getFormattedString(getContext().getResources(), R.string.forum_no_results_for_search_query, "search_query", escapedTitle).toString();
                    // CharSequence styledResults = Html.fromHtml(resultsText);
                    ((TaskProcessCallback) activity).onMessage(MessageType.ERROR, resultsText);
                } else {
                    ((TaskProcessCallback) activity).onMessage(MessageType.EMPTY, "");
                    discussionPostsListView.setVisibility(View.VISIBLE);
                }
            }
        }

        @Override
        public void onFailure(@NonNull Call<Page<DiscussionThread>> call, @NonNull Throwable error) {
            // refresh, as that would be confusing to the user.
            if (!callback.isRefreshingSilently()) {
                super.onFailure(call, error);
            }
            callback.onError();
            nextPage = 1;
        }
    });
}
Also used : Activity(android.app.Activity) Page(org.edx.mobile.model.Page) DiscussionThread(org.edx.mobile.discussion.DiscussionThread) TaskMessageCallback(org.edx.mobile.view.common.TaskMessageCallback) TaskProgressCallback(org.edx.mobile.view.common.TaskProgressCallback) TaskProcessCallback(org.edx.mobile.view.common.TaskProcessCallback)

Aggregations

Activity (android.app.Activity)1 DiscussionThread (org.edx.mobile.discussion.DiscussionThread)1 Page (org.edx.mobile.model.Page)1 TaskMessageCallback (org.edx.mobile.view.common.TaskMessageCallback)1 TaskProcessCallback (org.edx.mobile.view.common.TaskProcessCallback)1 TaskProgressCallback (org.edx.mobile.view.common.TaskProgressCallback)1