Search in sources :

Example 1 with DiscussionThread

use of org.edx.mobile.discussion.DiscussionThread 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 2 with DiscussionThread

use of org.edx.mobile.discussion.DiscussionThread in project edx-app-android by edx.

the class CourseDiscussionPostsThreadFragment method onEventMainThread.

@SuppressWarnings("unused")
public void onEventMainThread(DiscussionThreadPostedEvent event) {
    DiscussionThread newThread = event.getDiscussionThread();
    // If a new post is created in this topic, insert it at the top of the list, after any pinned posts
    if (discussionTopic.containsThread(newThread)) {
        if (postsFilter == DiscussionPostsFilter.UNANSWERED && newThread.getType() != DiscussionThread.ThreadType.QUESTION) {
            return;
        }
        int i = 0;
        for (; i < discussionPostsAdapter.getCount(); ++i) {
            if (!discussionPostsAdapter.getItem(i).isPinned()) {
                break;
            }
        }
        discussionPostsAdapter.insert(newThread, i);
        // move the ListView's scroll to that newly added post's position
        discussionPostsListView.setSelection(i);
        // In case this is the first addition, we need to hide the no-item-view
        setScreenStateUponResult();
    }
}
Also used : DiscussionThread(org.edx.mobile.discussion.DiscussionThread)

Example 3 with DiscussionThread

use of org.edx.mobile.discussion.DiscussionThread 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 DiscussionThread

use of org.edx.mobile.discussion.DiscussionThread in project edx-app-android by edx.

the class CourseDiscussionResponsesFragment method onViewCreated.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    final Activity activity = getActivity();
    responsesLoader = new ResponsesLoader(activity, discussionThread.getIdentifier(), discussionThread.getType() == DiscussionThread.ThreadType.QUESTION);
    courseDiscussionResponsesAdapter = new CourseDiscussionResponsesAdapter(activity, this, this, discussionThread, courseData);
    controller = InfiniteScrollUtils.configureRecyclerViewWithInfiniteList(discussionResponsesRecyclerView, courseDiscussionResponsesAdapter, responsesLoader);
    discussionResponsesRecyclerView.setAdapter(courseDiscussionResponsesAdapter);
    responsesLoader.freeze();
    if (getAndReadThreadCall != null) {
        getAndReadThreadCall.cancel();
    }
    final TaskMessageCallback mCallback = activity instanceof TaskMessageCallback ? (TaskMessageCallback) activity : null;
    getAndReadThreadCall = discussionService.setThreadRead(discussionThread.getIdentifier(), new ReadBody(true));
    // Setting a thread's "read" state gives us back the updated Thread object.
    getAndReadThreadCall.enqueue(new ErrorHandlingCallback<DiscussionThread>(activity, null, mCallback, CallTrigger.LOADING_UNCACHED) {

        @Override
        protected void onResponse(@NonNull final DiscussionThread discussionThread) {
            courseDiscussionResponsesAdapter.updateDiscussionThread(discussionThread);
            responsesLoader.unfreeze();
            EventBus.getDefault().post(new DiscussionThreadUpdatedEvent(discussionThread));
        }
    });
    DiscussionUtils.setStateOnTopicClosed(discussionThread.isClosed(), addResponseTextView, R.string.discussion_responses_add_response_button, R.string.discussion_add_response_disabled_title, addResponseLayout, new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            router.showCourseDiscussionAddResponse(activity, discussionThread);
        }
    });
    addResponseLayout.setEnabled(!courseData.isDiscussionBlackedOut());
}
Also used : ReadBody(org.edx.mobile.discussion.DiscussionService.ReadBody) CourseDiscussionResponsesAdapter(org.edx.mobile.view.adapters.CourseDiscussionResponsesAdapter) BaseFragmentActivity(org.edx.mobile.base.BaseFragmentActivity) Activity(android.app.Activity) View(android.view.View) InjectView(roboguice.inject.InjectView) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) DiscussionThread(org.edx.mobile.discussion.DiscussionThread) DiscussionThreadUpdatedEvent(org.edx.mobile.discussion.DiscussionThreadUpdatedEvent) TaskMessageCallback(org.edx.mobile.view.common.TaskMessageCallback)

Example 5 with DiscussionThread

use of org.edx.mobile.discussion.DiscussionThread in project edx-app-android by edx.

the class CourseDiscussionResponsesAdapter method bindViewHolderToThreadRow.

private void bindViewHolderToThreadRow(DiscussionThreadViewHolder holder) {
    holder.authorLayoutViewHolder.populateViewHolder(config, discussionThread, discussionThread, initialTimeStampMs, new Runnable() {

        @Override
        public void run() {
            listener.onClickAuthor(discussionThread.getAuthor());
        }
    });
    holder.threadTitleTextView.setText(discussionThread.getTitle());
    DiscussionTextUtils.renderHtml(holder.threadBodyTextView, discussionThread.getRenderedBody());
    String groupName = discussionThread.getGroupName();
    if (groupName == null) {
        holder.threadVisibilityTextView.setText(R.string.discussion_post_visibility_everyone);
    } else {
        holder.threadVisibilityTextView.setText(ResourceUtil.getFormattedString(context.getResources(), R.string.discussion_post_visibility_cohort, "cohort", groupName));
    }
    bindNumberResponsesView(holder.numberResponsesViewHolder);
    if (TextUtils.equals(loginPrefs.getUsername(), discussionThread.getAuthor())) {
        holder.actionsBar.setVisibility(View.GONE);
    } else {
        holder.actionsBar.setVisibility(View.VISIBLE);
        bindSocialView(holder.socialLayoutViewHolder, discussionThread);
        holder.discussionReportViewHolder.reportLayout.setOnClickListener(new View.OnClickListener() {

            public void onClick(final View v) {
                discussionService.setThreadFlagged(discussionThread.getIdentifier(), new FlagBody(!discussionThread.isAbuseFlagged())).enqueue(new ErrorHandlingCallback<DiscussionThread>(context, null, new DialogErrorNotification(baseFragment)) {

                    @Override
                    protected void onResponse(@NonNull final DiscussionThread topicThread) {
                        discussionThread = discussionThread.patchObject(topicThread);
                        notifyItemChanged(0);
                        EventBus.getDefault().post(new DiscussionThreadUpdatedEvent(discussionThread));
                    }
                });
            }
        });
        holder.discussionReportViewHolder.setReported(discussionThread.isAbuseFlagged());
    }
}
Also used : ErrorHandlingCallback(org.edx.mobile.http.callback.ErrorHandlingCallback) NonNull(android.support.annotation.NonNull) FlagBody(org.edx.mobile.discussion.DiscussionService.FlagBody) DialogErrorNotification(org.edx.mobile.http.notifications.DialogErrorNotification) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) DiscussionThread(org.edx.mobile.discussion.DiscussionThread) DiscussionThreadUpdatedEvent(org.edx.mobile.discussion.DiscussionThreadUpdatedEvent)

Aggregations

DiscussionThread (org.edx.mobile.discussion.DiscussionThread)6 Activity (android.app.Activity)3 RecyclerView (android.support.v7.widget.RecyclerView)3 View (android.view.View)3 TextView (android.widget.TextView)3 DiscussionThreadUpdatedEvent (org.edx.mobile.discussion.DiscussionThreadUpdatedEvent)3 NonNull (android.support.annotation.NonNull)2 ErrorHandlingCallback (org.edx.mobile.http.callback.ErrorHandlingCallback)2 DialogErrorNotification (org.edx.mobile.http.notifications.DialogErrorNotification)2 Page (org.edx.mobile.model.Page)2 TaskMessageCallback (org.edx.mobile.view.common.TaskMessageCallback)2 BaseFragmentActivity (org.edx.mobile.base.BaseFragmentActivity)1 FlagBody (org.edx.mobile.discussion.DiscussionService.FlagBody)1 FollowBody (org.edx.mobile.discussion.DiscussionService.FollowBody)1 ReadBody (org.edx.mobile.discussion.DiscussionService.ReadBody)1 VoteBody (org.edx.mobile.discussion.DiscussionService.VoteBody)1 CourseDiscussionResponsesAdapter (org.edx.mobile.view.adapters.CourseDiscussionResponsesAdapter)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