fedilab-Android-App/app/src/main/java/app/fedilab/android/drawers/SliderAdapter.java

312 lines
13 KiB
Java
Raw Normal View History

2019-09-28 10:16:47 +02:00
package app.fedilab.android.drawers;
/* Copyright 2019 Thomas Schneider
*
* This file is a part of Fedilab
*
* 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.
*
* Fedilab 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 Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
2019-09-28 15:49:56 +02:00
import android.app.Activity;
2019-09-28 10:16:47 +02:00
import android.content.Intent;
2019-09-28 15:49:56 +02:00
import android.content.SharedPreferences;
import android.graphics.Bitmap;
2020-04-09 14:59:36 +02:00
import android.graphics.drawable.Drawable;
2019-09-28 15:49:56 +02:00
import android.os.Build;
2019-09-28 10:16:47 +02:00
import android.os.Bundle;
2020-08-09 17:52:47 +02:00
import android.os.CountDownTimer;
2019-09-28 15:49:56 +02:00
import android.text.InputFilter;
2019-09-28 10:16:47 +02:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
2019-09-28 15:49:56 +02:00
import android.widget.EditText;
2019-09-28 10:16:47 +02:00
import android.widget.ImageView;
2019-09-28 15:49:56 +02:00
import android.widget.LinearLayout;
2019-09-28 10:16:47 +02:00
import android.widget.TextView;
2019-09-28 15:49:56 +02:00
import androidx.annotation.NonNull;
2020-04-09 14:59:36 +02:00
import androidx.annotation.Nullable;
2019-09-28 15:49:56 +02:00
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
2020-07-06 11:48:59 +02:00
import androidx.core.app.ActivityOptionsCompat;
2019-09-28 15:49:56 +02:00
2019-09-28 10:16:47 +02:00
import com.bumptech.glide.Glide;
2020-08-09 17:52:47 +02:00
import com.bumptech.glide.request.RequestOptions;
2020-04-09 14:59:36 +02:00
import com.bumptech.glide.request.target.CustomTarget;
2019-09-28 15:49:56 +02:00
import com.bumptech.glide.request.transition.Transition;
2019-09-28 10:16:47 +02:00
import com.smarteist.autoimageslider.SliderViewAdapter;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import app.fedilab.android.R;
2021-01-31 17:06:22 +01:00
import app.fedilab.android.activities.BasePixelfedComposeActivity;
2019-10-09 18:49:33 +02:00
import app.fedilab.android.activities.SlideMediaActivity;
2019-09-28 15:49:56 +02:00
import app.fedilab.android.asynctasks.UpdateDescriptionAttachmentAsyncTask;
2019-09-28 10:16:47 +02:00
import app.fedilab.android.client.Entities.Attachment;
2019-09-28 15:49:56 +02:00
import app.fedilab.android.client.Entities.Error;
2020-08-09 17:52:47 +02:00
import app.fedilab.android.client.Entities.Status;
2021-01-27 17:41:35 +01:00
import app.fedilab.android.client.PixelfedAPI;
2019-09-28 15:49:56 +02:00
import app.fedilab.android.helper.Helper;
import app.fedilab.android.interfaces.OnRetrieveAttachmentInterface;
2020-08-09 17:52:47 +02:00
import jp.wasabeef.glide.transformations.BlurTransformation;
2019-09-28 15:49:56 +02:00
import static android.content.Context.MODE_PRIVATE;
2019-09-28 10:16:47 +02:00
2019-09-28 15:49:56 +02:00
public class SliderAdapter extends SliderViewAdapter<SliderAdapter.SliderAdapterVH> implements OnRetrieveAttachmentInterface {
2019-09-28 10:16:47 +02:00
2021-01-19 17:43:51 +01:00
private final ArrayList<Attachment> attachments;
2020-08-09 17:52:47 +02:00
private Status status;
2021-01-19 17:43:51 +01:00
private final WeakReference<Activity> contextWeakReference;
private final boolean canDelete;
private final SliderAdapter sliderAdapter;
2021-01-27 17:41:35 +01:00
private boolean isStory = false;
private String userId;
2019-09-28 10:16:47 +02:00
2021-01-27 17:41:35 +01:00
public SliderAdapter(WeakReference<Activity> contextWeakReference, boolean delete, ArrayList<Attachment> attachments, String userId) {
this.attachments = attachments;
this.contextWeakReference = contextWeakReference;
this.canDelete = delete;
this.sliderAdapter = this;
this.isStory = true;
this.userId = userId;
}
2020-08-09 17:52:47 +02:00
2019-09-28 15:49:56 +02:00
public SliderAdapter(WeakReference<Activity> contextWeakReference, boolean delete, ArrayList<Attachment> attachments) {
2019-09-28 10:16:47 +02:00
this.attachments = attachments;
this.contextWeakReference = contextWeakReference;
2019-09-28 15:16:19 +02:00
this.canDelete = delete;
2019-09-28 17:25:51 +02:00
this.sliderAdapter = this;
2019-09-28 10:16:47 +02:00
}
2020-08-09 17:52:47 +02:00
public SliderAdapter(WeakReference<Activity> contextWeakReference, boolean delete, Status status) {
this.status = status;
this.contextWeakReference = contextWeakReference;
this.canDelete = delete;
this.sliderAdapter = this;
this.attachments = status.getMedia_attachments();
}
2019-09-28 10:16:47 +02:00
@Override
public SliderAdapterVH onCreateViewHolder(ViewGroup parent) {
View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_slider_layout_item, parent, false);
return new SliderAdapterVH(inflate);
}
@Override
public void onBindViewHolder(SliderAdapterVH viewHolder, int position) {
2020-08-09 17:52:47 +02:00
SharedPreferences sharedpreferences = contextWeakReference.get().getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
boolean isSensitive = false;
boolean expand_media = sharedpreferences.getBoolean(Helper.SET_EXPAND_MEDIA, false);
2021-01-19 17:43:51 +01:00
if (status != null) {
2020-08-09 17:52:47 +02:00
isSensitive = status.isSensitive();
}
2019-11-15 16:32:25 +01:00
if (attachments.size() > 1) {
2019-09-28 10:16:47 +02:00
viewHolder.textViewDescription.setText(String.format("%s/%s", (position + 1), attachments.size()));
}
2019-11-15 16:32:25 +01:00
if (canDelete) {
2019-09-29 17:41:23 +02:00
viewHolder.delete_media.setVisibility(View.VISIBLE);
2019-11-15 16:32:25 +01:00
} else {
2019-09-29 17:41:23 +02:00
viewHolder.delete_media.setVisibility(View.GONE);
}
String url;
2019-11-15 16:32:25 +01:00
if (attachments.get(position).getPreview_url().endsWith("no-preview.png")) {
url = attachments.get(position).getUrl();
2019-11-15 16:32:25 +01:00
} else {
url = attachments.get(position).getPreview_url();
}
2021-01-19 17:43:51 +01:00
if (expand_media || !isSensitive || (status != null && status.isAttachmentShown())) {
2020-08-09 17:52:47 +02:00
Glide.with(viewHolder.imageViewBackground.getContext())
2021-01-27 15:59:07 +01:00
.load(url)
2020-08-09 17:52:47 +02:00
.thumbnail(0.1f)
.into(viewHolder.imageViewBackground);
2021-01-19 17:43:51 +01:00
} else {
2020-08-09 17:52:47 +02:00
Glide.with(viewHolder.imageViewBackground.getContext())
2021-01-27 15:59:07 +01:00
.load(url)
2020-08-09 17:52:47 +02:00
.thumbnail(0.1f)
.apply(new RequestOptions().transform(new BlurTransformation(50, 3)))
.into(viewHolder.imageViewBackground);
}
2019-09-28 10:16:47 +02:00
viewHolder.imageViewBackground.setContentDescription(attachments.get(position).getDescription());
2019-11-15 16:32:25 +01:00
if (!this.canDelete) {
2020-08-09 17:52:47 +02:00
boolean finalIsSensitive = isSensitive;
2020-03-07 14:16:28 +01:00
viewHolder.imageViewBackground.setOnClickListener(v -> {
2021-01-19 17:43:51 +01:00
if (status == null || expand_media || !finalIsSensitive || status.isAttachmentShown()) {
2020-08-09 17:52:47 +02:00
Intent intent = new Intent(contextWeakReference.get(), SlideMediaActivity.class);
Bundle b = new Bundle();
intent.putParcelableArrayListExtra("mediaArray", attachments);
b.putInt("position", (position + 1));
b.putInt("bgcolor", contextWeakReference.get().getResources().getColor(R.color.cyanea_primary_dark));
intent.putExtras(b);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
ActivityOptionsCompat options = ActivityOptionsCompat
.makeSceneTransitionAnimation(contextWeakReference.get(), viewHolder.imageViewBackground, attachments.get((position)).getUrl());
// start the new activity
contextWeakReference.get().startActivity(intent, options.toBundle());
} else {
// start the new activity
contextWeakReference.get().startActivity(intent);
}
2021-01-19 17:43:51 +01:00
} else {
2020-08-09 17:52:47 +02:00
status.setAttachmentShown(true);
notifyDataSetChanged();
final int timeout = sharedpreferences.getInt(Helper.SET_NSFW_TIMEOUT, 5);
if (timeout > 0) {
new CountDownTimer((timeout * 1000), 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
status.setAttachmentShown(false);
notifyDataSetChanged();
}
}.start();
}
2020-07-06 11:48:59 +02:00
}
2019-09-28 15:49:56 +02:00
});
2019-11-15 16:32:25 +01:00
} else {
2021-01-27 17:41:35 +01:00
if (!isStory) {
viewHolder.imageViewBackground.setOnClickListener(v -> showAddDescription(attachments.get(position)));
}
2020-03-07 14:16:28 +01:00
viewHolder.delete_media.setOnClickListener(view -> showRemove(position));
2019-09-28 15:49:56 +02:00
}
}
private void showAddDescription(final Attachment attachment) {
SharedPreferences sharedpreferences = contextWeakReference.get().getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
int style;
if (theme == Helper.THEME_DARK) {
style = R.style.DialogDark;
} else if (theme == Helper.THEME_BLACK) {
style = R.style.DialogBlack;
} else {
style = R.style.Dialog;
}
AlertDialog.Builder builderInner = new AlertDialog.Builder(contextWeakReference.get(), style);
builderInner.setTitle(R.string.upload_form_description);
View popup_media_description = contextWeakReference.get().getLayoutInflater().inflate(R.layout.popup_media_description, new LinearLayout(contextWeakReference.get()), false);
builderInner.setView(popup_media_description);
//Text for report
final EditText input = popup_media_description.findViewById(R.id.media_description);
2020-06-19 17:17:17 +02:00
input.setFilters(new InputFilter[]{new InputFilter.LengthFilter(1500)});
2019-09-28 15:49:56 +02:00
final ImageView media_picture = popup_media_description.findViewById(R.id.media_picture);
Glide.with(contextWeakReference.get())
.asBitmap()
2021-01-27 15:59:07 +01:00
.load(attachment.getUrl())
2020-04-09 14:59:36 +02:00
.into(new CustomTarget<Bitmap>() {
2019-09-28 15:49:56 +02:00
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
media_picture.setImageBitmap(resource);
media_picture.setImageAlpha(60);
}
2020-04-09 14:59:36 +02:00
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
2019-09-28 15:49:56 +02:00
});
2020-03-07 14:16:28 +01:00
builderInner.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
2019-09-28 10:16:47 +02:00
2019-09-28 15:49:56 +02:00
if (attachment.getDescription() != null && !attachment.getDescription().equals("null")) {
input.setText(attachment.getDescription());
input.setSelection(input.getText().length());
}
2020-03-07 14:16:28 +01:00
builderInner.setPositiveButton(R.string.validate, (dialog, which) -> {
2021-01-19 17:43:51 +01:00
new UpdateDescriptionAttachmentAsyncTask(contextWeakReference.get(), attachment.getId(), input.getText().toString(), null, SliderAdapter.this);
2020-03-07 14:16:28 +01:00
attachment.setDescription(input.getText().toString());
dialog.dismiss();
2019-09-28 15:49:56 +02:00
});
AlertDialog alertDialog = builderInner.create();
alertDialog.show();
2019-09-28 10:16:47 +02:00
}
2019-09-28 15:49:56 +02:00
2019-09-28 17:25:51 +02:00
/**
* Removes a media
*
* @param position int
*/
private void showRemove(final int position) {
SharedPreferences sharedpreferences = contextWeakReference.get().getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
int style;
if (theme == Helper.THEME_DARK) {
style = R.style.DialogDark;
} else if (theme == Helper.THEME_BLACK) {
style = R.style.DialogBlack;
} else {
style = R.style.Dialog;
}
AlertDialog.Builder dialog = new AlertDialog.Builder(contextWeakReference.get(), style);
dialog.setMessage(R.string.toot_delete_media);
2020-03-07 14:16:28 +01:00
dialog.setNegativeButton(R.string.cancel, (dialog1, which) -> dialog1.dismiss());
dialog.setPositiveButton(R.string.yes, (dialog12, which) -> {
2021-01-27 17:41:35 +01:00
String userIdOwner = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
if (attachments.size() > position) {
2021-01-31 17:06:22 +01:00
Attachment attachment = attachments.get(position);
if (isStory && userIdOwner != null && userId.compareTo(userIdOwner) == 0 && attachments.size() > position) {
new Thread(() -> new PixelfedAPI(contextWeakReference.get()).deleteStory(attachment.getId())).start();
2021-01-27 17:41:35 +01:00
}
2021-01-31 17:06:22 +01:00
attachments.remove(attachment);
2021-01-27 17:41:35 +01:00
sliderAdapter.notifyDataSetChanged();
2021-01-31 17:06:22 +01:00
if (contextWeakReference.get() instanceof BasePixelfedComposeActivity) {
((BasePixelfedComposeActivity) contextWeakReference.get()).redraw();
2021-01-27 17:41:35 +01:00
}
2019-09-28 17:25:51 +02:00
}
2020-03-07 14:16:28 +01:00
dialog12.dismiss();
2019-09-28 17:25:51 +02:00
});
dialog.show();
}
2019-09-28 15:49:56 +02:00
2019-09-28 10:16:47 +02:00
@Override
public int getCount() {
//slider view count could be dynamic size
return attachments.size();
}
2019-09-28 15:49:56 +02:00
@Override
public void onRetrieveAttachment(Attachment attachment, String fileName, Error error) {
}
@Override
public void onUpdateProgress(int progress) {
}
2020-03-07 14:16:28 +01:00
static class SliderAdapterVH extends SliderViewAdapter.ViewHolder {
2019-09-28 10:16:47 +02:00
2019-09-29 17:41:23 +02:00
ImageView imageViewBackground, delete_media;
2019-09-28 10:16:47 +02:00
TextView textViewDescription;
SliderAdapterVH(View itemView) {
super(itemView);
imageViewBackground = itemView.findViewById(R.id.iv_auto_image_slider);
textViewDescription = itemView.findViewById(R.id.tv_auto_image_slider);
2019-09-29 17:41:23 +02:00
delete_media = itemView.findViewById(R.id.delete_media);
2019-09-28 10:16:47 +02:00
}
}
}