Search in sources :

Example 1 with Page

use of org.edx.mobile.model.Page in project edx-app-android by edx.

the class CourseDiscussionCommentsFragment method getCommentsList.

protected void getCommentsList(@NonNull final InfiniteScrollUtils.PageLoadCallback<DiscussionComment> callback) {
    if (getCommentsListCall != null) {
        getCommentsListCall.cancel();
    }
    final List<String> requestedFields = Collections.singletonList(DiscussionRequestFields.PROFILE_IMAGE.getQueryParamValue());
    getCommentsListCall = discussionService.getCommentsList(discussionResponse.getIdentifier(), nextPage, requestedFields);
    final Activity activity = getActivity();
    final TaskMessageCallback mCallback = activity instanceof TaskMessageCallback ? (TaskMessageCallback) activity : null;
    getCommentsListCall.enqueue(new ErrorHandlingCallback<Page<DiscussionComment>>(activity, null, mCallback, CallTrigger.LOADING_UNCACHED) {

        @Override
        protected void onResponse(@NonNull final Page<DiscussionComment> threadCommentsPage) {
            ++nextPage;
            callback.onPageLoaded(threadCommentsPage);
            discussionCommentsAdapter.notifyDataSetChanged();
            hasMorePages = threadCommentsPage.hasNext();
        }

        @Override
        protected void onFailure(@NonNull final Throwable error) {
            callback.onError();
            nextPage = 1;
            hasMorePages = false;
        }
    });
}
Also used : DiscussionComment(org.edx.mobile.discussion.DiscussionComment) BaseFragmentActivity(org.edx.mobile.base.BaseFragmentActivity) Activity(android.app.Activity) Page(org.edx.mobile.model.Page) TaskMessageCallback(org.edx.mobile.view.common.TaskMessageCallback)

Example 2 with Page

use of org.edx.mobile.model.Page 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)

Example 3 with Page

use of org.edx.mobile.model.Page in project edx-app-android by edx.

the class CourseDiscussionPostsThreadFragment method populatePostList.

private void populatePostList(@NonNull final InfiniteScrollUtils.PageLoadCallback<DiscussionThread> callback) {
    if (getThreadListCall != null) {
        getThreadListCall.cancel();
    }
    final List<String> requestedFields = Collections.singletonList(DiscussionRequestFields.PROFILE_IMAGE.getQueryParamValue());
    if (!discussionTopic.isFollowingType()) {
        getThreadListCall = discussionService.getThreadList(courseData.getCourse().getId(), getAllTopicIds(), postsFilter.getQueryParamValue(), postsSort.getQueryParamValue(), nextPage, requestedFields);
    } else {
        getThreadListCall = discussionService.getFollowingThreadList(courseData.getCourse().getId(), postsFilter.getQueryParamValue(), postsSort.getQueryParamValue(), nextPage, requestedFields);
    }
    final Activity activity = getActivity();
    final boolean isRefreshingSilently = callback.isRefreshingSilently();
    getThreadListCall.enqueue(new ErrorHandlingCallback<Page<DiscussionThread>>(activity, // the ListView will start showing a footer-based loading indicator.
    nextPage > 1 || isRefreshingSilently ? null : new ProgressViewController(loadingIndicator), // We only require the error to appear if the first server call fails
    nextPage == 1 ? errorNotification : null) {

        @Override
        protected void onResponse(@NonNull final Page<DiscussionThread> threadsPage) {
            if (getView() == null)
                return;
            ++nextPage;
            callback.onPageLoaded(threadsPage);
            if (discussionPostsAdapter.getCount() == 0) {
                if (discussionTopic.isAllType()) {
                    setScreenStateUponError(EmptyQueryResultsFor.COURSE);
                } else if (discussionTopic.isFollowingType()) {
                    setScreenStateUponError(EmptyQueryResultsFor.FOLLOWING);
                } else {
                    setScreenStateUponError(EmptyQueryResultsFor.CATEGORY);
                }
            } else {
                setScreenStateUponResult();
            }
        }

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

Example 4 with Page

use of org.edx.mobile.model.Page in project edx-app-android by edx.

the class JsonPageDeserializer method deserialize.

@Override
public Page<?> deserialize(JsonElement json, final Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    final List<?> list = context.deserialize(json.getAsJsonObject().get("results"), new ParameterizedType() {

        public Type getRawType() {
            return List.class;
        }

        public Type getOwnerType() {
            return null;
        }

        public Type[] getActualTypeArguments() {
            return ((ParameterizedType) typeOfT).getActualTypeArguments();
        }
    });
    JsonElement paginationJson = json.getAsJsonObject().get("pagination");
    if (null == paginationJson || paginationJson.isJsonNull()) {
        paginationJson = json;
    }
    final PaginationData paginationData = context.deserialize(paginationJson, PaginationData.class);
    return new Page<>(paginationData, list);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) PaginationData(org.edx.mobile.model.PaginationData) JsonElement(com.google.gson.JsonElement) Page(org.edx.mobile.model.Page)

Example 5 with Page

use of org.edx.mobile.model.Page in project edx-app-android by edx.

the class NativeFindCoursesFragment method onViewCreated.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    final Activity activity = getActivity();
    this.viewHolder = new ViewHolder(view);
    viewHolder.listView.setVisibility(View.GONE);
    viewHolder.loadingIndicator.setVisibility(View.VISIBLE);
    final FindCoursesListAdapter adapter = new FindCoursesListAdapter(activity, environment) {

        @Override
        public void onItemClicked(CourseDetail model) {
            environment.getRouter().showCourseDetail(activity, model);
        }
    };
    // Add empty views to cause a dividers to render at the top and bottom of the list
    viewHolder.listView.addHeaderView(new View(getContext()), null, false);
    viewHolder.listView.addFooterView(new View(getContext()), null, false);
    InfiniteScrollUtils.configureListViewWithInfiniteList(viewHolder.listView, adapter, new InfiniteScrollUtils.PageLoader<CourseDetail>() {

        @Override
        public void loadNextPage(@NonNull final InfiniteScrollUtils.PageLoadCallback<CourseDetail> callback) {
            if (null != call) {
                call.cancel();
            }
            call = courseAPI.getCourseList(nextPage);
            final TaskMessageCallback mCallback = activity instanceof TaskMessageCallback ? (TaskMessageCallback) activity : null;
            call.enqueue(new ErrorHandlingCallback<Page<CourseDetail>>(activity, null, mCallback, CallTrigger.LOADING_UNCACHED) {

                @Override
                protected void onResponse(@NonNull final Page<CourseDetail> coursesPage) {
                    callback.onPageLoaded(coursesPage);
                    ++nextPage;
                    if (null != viewHolder) {
                        viewHolder.listView.setVisibility(View.VISIBLE);
                        viewHolder.loadingIndicator.setVisibility(View.GONE);
                    }
                }

                @Override
                protected void onFailure(@NonNull final Throwable error) {
                    callback.onError();
                    nextPage = 1;
                    if (null != viewHolder) {
                        viewHolder.loadingIndicator.setVisibility(View.GONE);
                    }
                }
            });
        }
    });
    viewHolder.listView.setOnItemClickListener(adapter);
}
Also used : InfiniteScrollUtils(org.edx.mobile.view.adapters.InfiniteScrollUtils) Activity(android.app.Activity) Page(org.edx.mobile.model.Page) View(android.view.View) ListView(android.widget.ListView) TaskMessageCallback(org.edx.mobile.view.common.TaskMessageCallback) ErrorHandlingCallback(org.edx.mobile.http.callback.ErrorHandlingCallback) CourseDetail(org.edx.mobile.course.CourseDetail) NonNull(android.support.annotation.NonNull) FindCoursesListAdapter(org.edx.mobile.view.adapters.FindCoursesListAdapter)

Aggregations

Page (org.edx.mobile.model.Page)5 Activity (android.app.Activity)4 TaskMessageCallback (org.edx.mobile.view.common.TaskMessageCallback)3 DiscussionThread (org.edx.mobile.discussion.DiscussionThread)2 NonNull (android.support.annotation.NonNull)1 View (android.view.View)1 ListView (android.widget.ListView)1 JsonElement (com.google.gson.JsonElement)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 BaseFragmentActivity (org.edx.mobile.base.BaseFragmentActivity)1 CourseDetail (org.edx.mobile.course.CourseDetail)1 DiscussionComment (org.edx.mobile.discussion.DiscussionComment)1 ErrorHandlingCallback (org.edx.mobile.http.callback.ErrorHandlingCallback)1 PaginationData (org.edx.mobile.model.PaginationData)1 FindCoursesListAdapter (org.edx.mobile.view.adapters.FindCoursesListAdapter)1 InfiniteScrollUtils (org.edx.mobile.view.adapters.InfiniteScrollUtils)1 TaskProcessCallback (org.edx.mobile.view.common.TaskProcessCallback)1 TaskProgressCallback (org.edx.mobile.view.common.TaskProgressCallback)1 ProgressViewController (org.edx.mobile.view.common.TaskProgressCallback.ProgressViewController)1