231 lines
8.5 KiB
Java
231 lines
8.5 KiB
Java
package app.fedilab.fedilabtube.drawer;
|
|
/* Copyright 2020 Thomas Schneider
|
|
*
|
|
* This file is a part of TubeLab
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
* License, or (at your option) any later version.
|
|
*
|
|
* TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
* Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
import android.annotation.SuppressLint;
|
|
import android.content.Context;
|
|
import android.content.SharedPreferences;
|
|
import android.os.AsyncTask;
|
|
import android.text.Spannable;
|
|
import android.text.SpannableString;
|
|
import android.text.Spanned;
|
|
import android.text.method.LinkMovementMethod;
|
|
import android.text.style.ForegroundColorSpan;
|
|
import android.view.LayoutInflater;
|
|
import android.view.MotionEvent;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.ImageView;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.appcompat.app.AlertDialog;
|
|
import androidx.core.content.ContextCompat;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import java.util.List;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
import app.fedilab.fedilabtube.PeertubeActivity;
|
|
import app.fedilab.fedilabtube.R;
|
|
import app.fedilab.fedilabtube.asynctasks.PostActionAsyncTask;
|
|
import app.fedilab.fedilabtube.client.PeertubeAPI;
|
|
import app.fedilab.fedilabtube.client.entities.Account;
|
|
import app.fedilab.fedilabtube.client.entities.Error;
|
|
import app.fedilab.fedilabtube.client.entities.Status;
|
|
import app.fedilab.fedilabtube.client.entities.StatusDrawerParams;
|
|
import app.fedilab.fedilabtube.helper.Helper;
|
|
import app.fedilab.fedilabtube.interfaces.OnPostActionInterface;
|
|
import es.dmoral.toasty.Toasty;
|
|
|
|
import static android.content.Context.MODE_PRIVATE;
|
|
|
|
|
|
public class StatusListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements OnPostActionInterface {
|
|
|
|
|
|
private Context context;
|
|
private List<Status> statuses;
|
|
|
|
private StatusListAdapter statusListAdapter;
|
|
|
|
|
|
public StatusListAdapter(StatusDrawerParams statusDrawerParams) {
|
|
statuses = statusDrawerParams.getStatuses();
|
|
statusListAdapter = this;
|
|
}
|
|
|
|
@Override
|
|
public long getItemId(int position) {
|
|
return position;
|
|
}
|
|
|
|
@Override
|
|
public int getItemCount() {
|
|
return statuses.size();
|
|
}
|
|
|
|
|
|
@NonNull
|
|
@Override
|
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
|
context = parent.getContext();
|
|
LayoutInflater layoutInflater = LayoutInflater.from(this.context);
|
|
return new ViewHolder(layoutInflater.inflate(R.layout.drawer_status_compact, parent, false));
|
|
}
|
|
|
|
@SuppressLint({"SetJavaScriptEnabled", "ClickableViewAccessibility"})
|
|
@Override
|
|
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, int i) {
|
|
|
|
context = viewHolder.itemView.getContext();
|
|
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
|
|
final String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
|
|
|
|
|
final ViewHolder holder = (ViewHolder) viewHolder;
|
|
|
|
final Status status = statuses.get(i);
|
|
|
|
|
|
if (status == null)
|
|
return;
|
|
if (status.getVisibility() == null) {
|
|
status.setVisibility("public");
|
|
}
|
|
|
|
if (status.getAccount().getId().equals(userId))
|
|
holder.status_peertube_delete.setVisibility(View.VISIBLE);
|
|
else
|
|
holder.status_peertube_delete.setVisibility(View.GONE);
|
|
holder.status_peertube_delete.setOnClickListener(v -> {
|
|
AlertDialog.Builder builderInner;
|
|
builderInner = new AlertDialog.Builder(context);
|
|
builderInner.setTitle(R.string.delete_comment);
|
|
builderInner.setMessage(R.string.delete_comment_confirm);
|
|
builderInner.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
|
|
builderInner.setPositiveButton(R.string.yes, (dialog, which) -> {
|
|
new PostActionAsyncTask(context, PeertubeAPI.StatusAction.PEERTUBEDELETECOMMENT, PeertubeActivity.video_id, status.getId(), StatusListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
dialog.dismiss();
|
|
});
|
|
builderInner.show();
|
|
});
|
|
|
|
|
|
holder.status_content.setOnTouchListener((view, motionEvent) -> {
|
|
if (motionEvent.getAction() == MotionEvent.ACTION_UP && !view.hasFocus()) {
|
|
try {
|
|
view.requestFocus();
|
|
} catch (Exception ignored) {
|
|
}
|
|
}
|
|
return false;
|
|
});
|
|
|
|
//Click on a conversation
|
|
|
|
holder.status_content.setText(status.getContentSpan(), TextView.BufferType.SPANNABLE);
|
|
|
|
holder.status_content.setMovementMethod(LinkMovementMethod.getInstance());
|
|
|
|
|
|
final Account accountForUrl;
|
|
accountForUrl = status.getAccount();
|
|
holder.status_account_displayname.setVisibility(View.GONE);
|
|
holder.status_account_displayname_owner.setText(status.getAccount().getUsername().replace("@", ""), TextView.BufferType.SPANNABLE);
|
|
|
|
|
|
Spannable wordtoSpan;
|
|
Pattern hashAcct;
|
|
wordtoSpan = new SpannableString("@" + status.getAccount().getAcct());
|
|
hashAcct = Pattern.compile("(@" + status.getAccount().getAcct() + ")");
|
|
Matcher matcherAcct = hashAcct.matcher(wordtoSpan);
|
|
while (matcherAcct.find()) {
|
|
int matchStart = matcherAcct.start(1);
|
|
int matchEnd = matcherAcct.end();
|
|
if (wordtoSpan.length() >= matchEnd && matchStart < matchEnd) {
|
|
wordtoSpan.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, android.R.color.darker_gray)), matchStart, matchEnd, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
|
|
}
|
|
|
|
}
|
|
holder.status_account_username.setText(wordtoSpan);
|
|
|
|
|
|
holder.status_toot_date.setText(Helper.dateDiff(context, status.getCreated_at()));
|
|
|
|
|
|
Helper.loadGiF(context, accountForUrl, holder.status_account_profile);
|
|
}
|
|
|
|
@Override
|
|
public void onPostAction(int statusCode, PeertubeAPI.StatusAction statusAction, String targetedId, Error error) {
|
|
|
|
if (error != null) {
|
|
Toasty.error(context, error.getError(), Toast.LENGTH_LONG).show();
|
|
return;
|
|
}
|
|
|
|
if (statusAction == PeertubeAPI.StatusAction.PEERTUBEDELETECOMMENT) {
|
|
int position = 0;
|
|
for (Status status : statuses) {
|
|
if (status.getId().equals(targetedId)) {
|
|
statuses.remove(status);
|
|
statusListAdapter.notifyItemRemoved(position);
|
|
break;
|
|
}
|
|
position++;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
static class ViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
TextView status_content;
|
|
TextView status_account_username;
|
|
TextView status_account_displayname, status_account_displayname_owner;
|
|
ImageView status_account_profile;
|
|
TextView status_toot_date;
|
|
LinearLayout main_container;
|
|
LinearLayout status_content_container;
|
|
TextView status_peertube_delete;
|
|
|
|
@SuppressLint("SetJavaScriptEnabled")
|
|
ViewHolder(View itemView) {
|
|
super(itemView);
|
|
status_content = itemView.findViewById(R.id.status_content);
|
|
status_account_username = itemView.findViewById(R.id.status_account_username);
|
|
status_account_displayname = itemView.findViewById(R.id.status_account_displayname);
|
|
status_account_displayname_owner = itemView.findViewById(R.id.status_account_displayname_owner);
|
|
status_account_profile = itemView.findViewById(R.id.status_account_profile);
|
|
status_toot_date = itemView.findViewById(R.id.status_toot_date);
|
|
main_container = itemView.findViewById(R.id.main_container);
|
|
status_content_container = itemView.findViewById(R.id.status_content_container);
|
|
status_peertube_delete = itemView.findViewById(R.id.status_peertube_delete);
|
|
|
|
}
|
|
|
|
public View getView() {
|
|
return itemView;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
} |