Search in sources :

Example 1 with FindCoursesListAdapter

use of org.edx.mobile.view.adapters.FindCoursesListAdapter 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

Activity (android.app.Activity)1 NonNull (android.support.annotation.NonNull)1 View (android.view.View)1 ListView (android.widget.ListView)1 CourseDetail (org.edx.mobile.course.CourseDetail)1 ErrorHandlingCallback (org.edx.mobile.http.callback.ErrorHandlingCallback)1 Page (org.edx.mobile.model.Page)1 FindCoursesListAdapter (org.edx.mobile.view.adapters.FindCoursesListAdapter)1 InfiniteScrollUtils (org.edx.mobile.view.adapters.InfiniteScrollUtils)1 TaskMessageCallback (org.edx.mobile.view.common.TaskMessageCallback)1