use of com.odysee.app.model.Reactions in project odysee-android by OdyseeTeam.
the class FileViewFragment method loadComments.
private void loadComments() {
Claim actualClaim = collectionClaimItem != null ? collectionClaimItem : fileClaim;
View root = getView();
if (root != null && actualClaim != null) {
ProgressBar commentsLoading = root.findViewById(R.id.file_view_comments_progress);
CommentListTask task = new CommentListTask(1, 200, actualClaim.getClaimId(), commentsLoading, new CommentListHandler() {
@Override
public void onSuccess(List<Comment> comments, boolean hasReachedEnd) {
if (!comments.isEmpty()) {
// Load and process comments reactions on a different thread so main thread is not blocked
Helper.setViewVisibility(commentsLoading, View.VISIBLE);
new Thread(new Runnable() {
@Override
public void run() {
Map<String, Reactions> commentReactions = loadReactions(comments);
Activity activity = getActivity();
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Helper.setViewVisibility(commentsLoading, View.GONE);
processCommentReactions(comments, commentReactions);
}
});
}
}
}).start();
}
}
@Override
public void onError(Exception error) {
if (error != null) {
error.printStackTrace();
}
checkNoComments();
}
private void processCommentReactions(List<Comment> comments, Map<String, Reactions> commentReactions) {
for (Comment c : comments) {
if (commentReactions != null) {
c.setReactions(commentReactions.get(c.getId()));
} else {
c.setReactions(new Reactions(0, 0, false, false));
}
}
List<Comment> rootComments = new ArrayList<>();
for (Comment c : comments) {
if (c.getParentId() == null) {
rootComments.add(c);
}
}
if (commentReactions != null) {
Collections.sort(rootComments, new Comparator<Comment>() {
@Override
public int compare(Comment o1, Comment o2) {
int o1SelfLiked = (Lbryio.isSignedIn() && o1.getReactions() != null && o1.getReactions().isLiked()) ? 1 : 0;
int o2SelfLiked = (Lbryio.isSignedIn() && o2.getReactions() != null && o2.getReactions().isLiked()) ? 1 : 0;
int o1OtherLikes = o1.getReactions() != null ? o1.getReactions().getOthersLikes() : 0;
int o2OtherLikes = o2.getReactions() != null ? o2.getReactions().getOthersLikes() : 0;
return (o2OtherLikes + o2SelfLiked) - (o1OtherLikes + o1SelfLiked);
}
});
}
// Direct comments are now sorted by their amount of likes. We can now pick the
// one to be displayed as the collapsed single comment.
Comment singleComment = rootComments.get(0);
TextView commentText = singleCommentRoot.findViewById(R.id.comment_text);
ImageView thumbnailView = singleCommentRoot.findViewById(R.id.comment_thumbnail);
View noThumbnailView = singleCommentRoot.findViewById(R.id.comment_no_thumbnail);
TextView alphaView = singleCommentRoot.findViewById(R.id.comment_thumbnail_alpha);
commentText.setText(singleComment.getText());
commentText.setMaxLines(3);
commentText.setEllipsize(TextUtils.TruncateAt.END);
commentText.setClickable(true);
commentText.setTextIsSelectable(false);
boolean hasThumbnail = singleComment.getPoster() != null && !Helper.isNullOrEmpty(singleComment.getPoster().getThumbnailUrl());
thumbnailView.setVisibility(hasThumbnail ? View.VISIBLE : View.INVISIBLE);
noThumbnailView.setVisibility(!hasThumbnail ? View.VISIBLE : View.INVISIBLE);
int bgColor = Helper.generateRandomColorForValue(singleComment.getChannelId());
Helper.setIconViewBackgroundColor(noThumbnailView, bgColor, false, getContext());
if (hasThumbnail) {
Context ctx = getContext();
if (ctx != null) {
Context appCtx = ctx.getApplicationContext();
Glide.with(appCtx).asBitmap().load(singleComment.getPoster().getThumbnailUrl()).apply(RequestOptions.circleCropTransform()).into(thumbnailView);
}
}
alphaView.setText(singleComment.getChannelName() != null ? singleComment.getChannelName().substring(1, 2).toUpperCase() : null);
singleCommentRoot.findViewById(R.id.comment_actions_area).setVisibility(View.GONE);
singleCommentRoot.findViewById(R.id.comment_time).setVisibility(View.GONE);
singleCommentRoot.findViewById(R.id.comment_channel_name).setVisibility(View.GONE);
singleCommentRoot.findViewById(R.id.comment_more_options).setVisibility(View.GONE);
singleCommentRoot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expandButton.performClick();
}
});
List<Comment> parentComments = rootComments;
// rootComments.clear();
for (Comment c : comments) {
if (!parentComments.contains(c)) {
if (c.getParentId() != null) {
Comment item = parentComments.stream().filter(v -> c.getParentId().equalsIgnoreCase(v.getId())).findFirst().orElse(null);
if (item != null) {
parentComments.add(parentComments.indexOf(item) + 1, c);
}
}
}
}
comments = parentComments;
Context ctx = getContext();
View root = getView();
if (ctx != null && root != null) {
ensureCommentListAdapterCreated(comments);
}
}
});
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
use of com.odysee.app.model.Reactions in project odysee-android by OdyseeTeam.
the class FileViewFragment method getMyReactions.
@Nullable
private Reactions getMyReactions(JSONObject jsonResult, JSONObject responseOthersReactions, String key) throws JSONException {
JSONObject value = (JSONObject) responseOthersReactions.get(key);
Reactions reactions = getReactionsForValue(value);
if (jsonResult.has("my_reactions")) {
JSONObject responseMyReactions = jsonResult.getJSONObject("my_reactions");
if (responseMyReactions.has(key) && reactions != null) {
JSONObject myReaction = (JSONObject) responseMyReactions.get(key);
reactions.setLiked(myReaction.getInt("like") > 0);
reactions.setDisliked(myReaction.getInt("dislike") > 0);
}
}
return reactions;
}
use of com.odysee.app.model.Reactions in project odysee-android by OdyseeTeam.
the class FileViewFragment method loadReactions.
private Map<String, Reactions> loadReactions(List<Comment> comments) {
List<String> commentIds = new ArrayList<>();
for (Comment c : comments) {
commentIds.add(c.getId());
}
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Map<String, Reactions>> future = executor.submit(() -> {
Comments.checkCommentsEndpointStatus();
JSONObject jsonParams = new JSONObject();
jsonParams.put("comment_ids", TextUtils.join(",", commentIds));
AccountManager am = AccountManager.get(getContext());
Account odyseeAccount = Helper.getOdyseeAccount(am.getAccounts());
if (odyseeAccount != null) {
jsonParams.put("auth_token", am.peekAuthToken(odyseeAccount, "auth_token_type"));
}
if (!Lbry.ownChannels.isEmpty()) {
jsonParams.put("channel_id", Lbry.ownChannels.get(0).getClaimId());
jsonParams.put("channel_name", Lbry.ownChannels.get(0).getName());
try {
JSONObject jsonChannelSign = Comments.channelSignName(jsonParams, jsonParams.getString("channel_id"), jsonParams.getString("channel_name"));
if (jsonChannelSign.has("signature") && jsonChannelSign.has("signing_ts")) {
jsonParams.put("signature", jsonChannelSign.getString("signature"));
jsonParams.put("signing_ts", jsonChannelSign.getString("signing_ts"));
}
} catch (ApiCallException | JSONException e) {
e.printStackTrace();
}
}
Map<String, Reactions> result = new HashMap<>();
try {
Response response = Comments.performRequest(jsonParams, "reaction.List");
String responseString = response.body().string();
response.close();
JSONObject jsonResponse = new JSONObject(responseString);
if (jsonResponse.has("result")) {
JSONObject jsonResult = jsonResponse.getJSONObject("result");
if (jsonResult.has("others_reactions")) {
JSONObject responseOthersReactions = jsonResult.getJSONObject("others_reactions");
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
responseOthersReactions.keys().forEachRemaining(key -> {
try {
result.put(key, getMyReactions(jsonResult, responseOthersReactions, key));
} catch (JSONException e) {
e.printStackTrace();
}
});
} else {
// Android versions prior to API 24 lack forEachRemaining()
Iterator<String> itr = responseOthersReactions.keys();
while (itr.hasNext()) {
try {
String nextKey = itr.next();
result.put(nextKey, getMyReactions(jsonResult, responseOthersReactions, nextKey));
} catch (JSONException e) {
Log.e(TAG, "loadReactions for Comment: ".concat(e.getLocalizedMessage()));
}
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
});
try {
return future.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
return null;
}
}
use of com.odysee.app.model.Reactions in project odysee-android by OdyseeTeam.
the class CommentListAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Comment comment = items.get(position);
switchViewReplies(comment, (TextView) holder.viewReplies);
ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
if (collapsed || (comment.getParentId() != null && !childsToBeShown.contains(comment.getParentId()))) {
holder.itemView.setVisibility(View.GONE);
lp.height = 0;
lp.width = 0;
holder.itemView.setLayoutParams(lp);
} else {
if (comment.getParentId() == null || (comment.getParentId() != null && childsToBeShown.contains(comment.getParentId()))) {
lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
holder.itemView.setLayoutParams(lp);
holder.itemView.setVisibility(View.VISIBLE);
}
}
holder.comment = comment;
holder.blockChannelView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Claim channel = comment.getPoster();
if (channel != null) {
if (context instanceof MainActivity) {
((MainActivity) context).handleBlockChannel(channel);
}
}
}
});
if (CommentAction.areAnyActionsAvailable(comment, claim)) {
holder.moreOptionsView.setVisibility(collapsed ? View.INVISIBLE : View.VISIBLE);
holder.moreOptionsView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.showContextMenu();
}
});
} else {
holder.moreOptionsView.setVisibility(View.INVISIBLE);
holder.moreOptionsView.setOnClickListener(null);
}
holder.channelName.setText(comment.getChannelName());
holder.commentTimeView.setText(DateUtils.getRelativeTimeSpanString((comment.getTimestamp() * 1000), System.currentTimeMillis(), 0, DateUtils.FORMAT_ABBREV_RELATIVE));
holder.commentText.setText(comment.getText());
Reactions commentReactions = comment.getReactions();
if (commentReactions != null) {
int countTextColor;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
countTextColor = context.getResources().getColor(R.color.foreground, null);
} else {
countTextColor = context.getResources().getColor(R.color.foreground);
}
String likesAmount = String.valueOf(commentReactions.getOthersLikes());
String dislikesAmount = String.valueOf(commentReactions.getOthersDislikes());
if (commentReactions.isLiked()) {
int fireActive;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
fireActive = context.getResources().getColor(R.color.fireActive, null);
} else {
fireActive = context.getResources().getColor(R.color.fireActive);
}
holder.likesCount.setText(String.valueOf(Integer.parseInt(likesAmount) + 1));
holder.likesCount.setTextColor(fireActive);
for (Drawable d : holder.likesCount.getCompoundDrawablesRelative()) {
if (d != null) {
d.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(holder.likesCount.getContext(), R.color.fireActive), PorterDuff.Mode.SRC_IN));
}
}
} else {
holder.likesCount.setText(likesAmount);
holder.likesCount.setTextColor(countTextColor);
for (Drawable d : holder.likesCount.getCompoundDrawablesRelative()) {
if (d != null) {
d.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(holder.likesCount.getContext(), R.color.foreground), PorterDuff.Mode.SRC_IN));
}
}
}
if (commentReactions.isDisliked()) {
int slimeActive;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
slimeActive = context.getResources().getColor(R.color.slimeActive, null);
} else {
slimeActive = context.getResources().getColor(R.color.slimeActive);
}
holder.dislikesCount.setText(String.valueOf(Integer.parseInt(dislikesAmount) + 1));
holder.dislikesCount.setTextColor(slimeActive);
for (Drawable d : holder.dislikesCount.getCompoundDrawablesRelative()) {
if (d != null) {
d.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(holder.dislikesCount.getContext(), R.color.slimeActive), PorterDuff.Mode.SRC_IN));
}
}
} else {
holder.dislikesCount.setText(dislikesAmount);
holder.dislikesCount.setTextColor(countTextColor);
for (Drawable d : holder.dislikesCount.getCompoundDrawablesRelative()) {
if (d != null) {
d.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(holder.dislikesCount.getContext(), R.color.foreground), PorterDuff.Mode.SRC_IN));
}
}
}
}
holder.likesCount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AccountManager am = AccountManager.get(context);
Account odyseeAccount = Helper.getOdyseeAccount(am.getAccounts());
if (odyseeAccount != null && comment.getClaimId() != null && commentListListener != null) {
commentListListener.onCommentReactClicked(comment, true);
}
}
});
holder.dislikesCount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AccountManager am = AccountManager.get(context);
Account odyseeAccount = Helper.getOdyseeAccount(am.getAccounts());
if (odyseeAccount != null && comment.getClaimId() != null && commentListListener != null) {
commentListListener.onCommentReactClicked(comment, false);
}
}
});
boolean hasThumbnail = comment.getPoster() != null && !Helper.isNullOrEmpty(comment.getPoster().getThumbnailUrl());
holder.thumbnailView.setVisibility(hasThumbnail ? View.VISIBLE : View.INVISIBLE);
holder.noThumbnailView.setVisibility(!hasThumbnail ? View.VISIBLE : View.INVISIBLE);
int bgColor = Helper.generateRandomColorForValue(comment.getChannelId());
Helper.setIconViewBackgroundColor(holder.noThumbnailView, bgColor, false, context);
if (hasThumbnail) {
Glide.with(context.getApplicationContext()).asBitmap().load(comment.getPoster().getThumbnailUrl(holder.thumbnailView.getLayoutParams().width, holder.thumbnailView.getLayoutParams().height, 85)).apply(RequestOptions.circleCropTransform()).into(holder.thumbnailView);
}
holder.alphaView.setText(comment.getChannelName() != null ? comment.getChannelName().substring(1, 2).toUpperCase() : null);
holder.channelName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener != null && comment.getPoster() != null) {
listener.onClaimClicked(comment.getPoster(), position);
}
}
});
holder.replyLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (commentListListener != null) {
commentListListener.onReplyClicked(comment);
}
}
});
if (position != (items.size() - 1)) {
String pId = items.get(position + 1).getParentId();
if (pId != null && pId.equalsIgnoreCase(comment.getId())) {
holder.viewReplies.setVisibility(View.VISIBLE);
holder.viewReplies.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switchRepliesVisibility(comment.getId());
switchViewReplies(comment, (TextView) holder.viewReplies);
}
});
} else {
holder.viewReplies.setVisibility(View.GONE);
}
} else {
holder.viewReplies.setVisibility(View.GONE);
}
}
use of com.odysee.app.model.Reactions in project odysee-android by OdyseeTeam.
the class FileViewFragment method loadReactions.
private void loadReactions(Claim c) {
if (scheduledExecutor == null) {
scheduledExecutor = new ScheduledThreadPoolExecutor(1);
}
if (futureReactions != null)
futureReactions.cancel(true);
if (reactions == null)
reactions = new Reactions();
Runnable runnable = () -> {
Map<String, String> options = new HashMap<>();
options.put("claim_ids", c.getClaimId());
JSONObject data;
try {
data = (JSONObject) Lbryio.parseResponse(Lbryio.call("reaction", "list", options, Helper.METHOD_POST, getContext()));
if (data != null && data.has("others_reactions")) {
JSONObject othersReactions = (JSONObject) data.get("others_reactions");
if (othersReactions.has(c.getClaimId())) {
int likesFromOthers = ((JSONObject) othersReactions.get(c.getClaimId())).getInt("like");
int dislikesFromOthers = ((JSONObject) othersReactions.get(c.getClaimId())).getInt("dislike");
reactions.setOthersLikes(likesFromOthers);
reactions.setOthersDislikes(dislikesFromOthers);
}
}
if (data != null && data.has("my_reactions")) {
JSONObject othersReactions = (JSONObject) data.get("my_reactions");
if (othersReactions.has(c.getClaimId())) {
int likes = ((JSONObject) othersReactions.get(c.getClaimId())).getInt("like");
reactions.setLiked(likes > 0);
c.setLiked(likes > 0);
int dislikes = ((JSONObject) othersReactions.get(c.getClaimId())).getInt("dislike");
reactions.setDisliked(dislikes > 0);
c.setDisliked(dislikes > 0);
}
}
updateContentReactions();
} catch (LbryioRequestException | LbryioResponseException | JSONException e) {
e.printStackTrace();
}
};
futureReactions = scheduledExecutor.scheduleAtFixedRate(runnable, 0, 5, TimeUnit.SECONDS);
}
Aggregations