Search in sources :

Example 1 with AuthException

use of org.edx.mobile.exception.AuthException in project edx-app-android by edx.

the class MyCoursesListFragment method onLoadFinished.

@Override
public void onLoadFinished(Loader<AsyncTaskResult<List<EnrolledCoursesResponse>>> asyncTaskResultLoader, AsyncTaskResult<List<EnrolledCoursesResponse>> result) {
    adapter.clear();
    final Exception exception = result.getEx();
    if (exception != null) {
        if (exception instanceof AuthException) {
            loginPrefs.clear();
            getActivity().finish();
        } else if (exception instanceof HttpStatusException) {
            final HttpStatusException httpStatusException = (HttpStatusException) exception;
            switch(httpStatusException.getStatusCode()) {
                case HttpStatus.UNAUTHORIZED:
                    {
                        environment.getRouter().forceLogout(getContext(), environment.getAnalyticsRegistry(), environment.getNotificationDelegate());
                        break;
                    }
            }
        } else {
            logger.error(exception);
        }
        errorNotification.showError(getActivity(), exception, R.string.lbl_reload, new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (NetworkUtil.isConnected(getContext())) {
                    onRefresh();
                }
            }
        });
    } else if (result.getResult() != null) {
        ArrayList<EnrolledCoursesResponse> newItems = new ArrayList<EnrolledCoursesResponse>(result.getResult());
        updateDatabaseAfterDownload(newItems);
        if (result.getResult().size() > 0) {
            adapter.setItems(newItems);
            adapter.notifyDataSetChanged();
        }
        if (adapter.isEmpty() && !environment.getConfig().getCourseDiscoveryConfig().isCourseDiscoveryEnabled()) {
            errorNotification.showError(R.string.no_courses_to_display, FontAwesomeIcons.fa_exclamation_circle, 0, null);
            binding.myCourseList.setVisibility(View.GONE);
        } else {
            binding.myCourseList.setVisibility(View.VISIBLE);
            errorNotification.hideError();
        }
    }
    binding.swipeContainer.setRefreshing(false);
    binding.loadingIndicator.getRoot().setVisibility(View.GONE);
    if (!EventBus.getDefault().isRegistered(MyCoursesListFragment.this)) {
        EventBus.getDefault().registerSticky(MyCoursesListFragment.this);
    }
}
Also used : EnrolledCoursesResponse(org.edx.mobile.model.api.EnrolledCoursesResponse) ArrayList(java.util.ArrayList) AuthException(org.edx.mobile.exception.AuthException) HttpStatusException(org.edx.mobile.http.HttpStatusException) View(android.view.View) HttpStatusException(org.edx.mobile.http.HttpStatusException) AuthException(org.edx.mobile.exception.AuthException)

Example 2 with AuthException

use of org.edx.mobile.exception.AuthException in project edx-app-android by edx.

the class OldMyCoursesListFragment method onLoadFinished.

@Override
public void onLoadFinished(Loader<AsyncTaskResult<List<EnrolledCoursesResponse>>> asyncTaskResultLoader, AsyncTaskResult<List<EnrolledCoursesResponse>> result) {
    adapter.clear();
    final Exception exception = result.getEx();
    if (exception != null) {
        if (exception instanceof AuthException) {
            loginPrefs.clear();
            getActivity().finish();
        } else if (exception instanceof HttpStatusException) {
            final HttpStatusException httpStatusException = (HttpStatusException) exception;
            switch(httpStatusException.getStatusCode()) {
                case HttpStatus.UNAUTHORIZED:
                    {
                        environment.getRouter().forceLogout(getContext(), environment.getAnalyticsRegistry(), environment.getNotificationDelegate());
                        break;
                    }
            }
        } else {
            logger.error(exception);
        }
        errorNotification.showError(getActivity(), exception, R.string.lbl_reload, new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (NetworkUtil.isConnected(getContext())) {
                    onRefresh();
                }
            }
        });
    } else if (result.getResult() != null) {
        ArrayList<EnrolledCoursesResponse> newItems = new ArrayList<EnrolledCoursesResponse>(result.getResult());
        updateDatabaseAfterDownload(newItems);
        if (result.getResult().size() > 0) {
            adapter.setItems(newItems);
            adapter.notifyDataSetChanged();
        }
        if (adapter.isEmpty() && !environment.getConfig().getCourseDiscoveryConfig().isCourseDiscoveryEnabled()) {
            errorNotification.showError(R.string.no_courses_to_display, FontAwesomeIcons.fa_exclamation_circle, 0, null);
            binding.myCourseList.setVisibility(View.GONE);
        } else {
            binding.myCourseList.setVisibility(View.VISIBLE);
            errorNotification.hideError();
        }
    }
    binding.swipeContainer.setRefreshing(false);
    binding.loadingIndicator.getRoot().setVisibility(View.GONE);
    isInitialServerCallDone = true;
    if (!(NetworkUtil.isConnected(getActivity()))) {
        onOffline();
    } else {
        onOnline();
    }
}
Also used : EnrolledCoursesResponse(org.edx.mobile.model.api.EnrolledCoursesResponse) ArrayList(java.util.ArrayList) AuthException(org.edx.mobile.exception.AuthException) HttpStatusException(org.edx.mobile.http.HttpStatusException) View(android.view.View) HttpStatusException(org.edx.mobile.http.HttpStatusException) AuthException(org.edx.mobile.exception.AuthException)

Aggregations

View (android.view.View)2 ArrayList (java.util.ArrayList)2 AuthException (org.edx.mobile.exception.AuthException)2 HttpStatusException (org.edx.mobile.http.HttpStatusException)2 EnrolledCoursesResponse (org.edx.mobile.model.api.EnrolledCoursesResponse)2