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;
}
});
}
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();
}
}
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;
}
});
}
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());
}
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());
}
}
Aggregations