fedilab-Android-App/app/src/main/java/app/fedilab/android/mastodon/ui/drawer/SliderAdapter.java

136 lines
5.3 KiB
Java
Raw Normal View History

2023-01-22 15:22:59 +01:00
package app.fedilab.android.mastodon.ui.drawer;
2023-01-06 18:21:41 +01:00
/* Copyright 2023 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>. */
2024-01-12 10:36:29 +01:00
2023-01-06 18:21:41 +01:00
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
2023-01-11 18:04:37 +01:00
import android.content.SharedPreferences;
2023-01-06 18:21:41 +01:00
import android.os.Bundle;
2023-01-11 18:04:37 +01:00
import android.os.CountDownTimer;
2023-01-06 18:21:41 +01:00
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.core.app.ActivityOptionsCompat;
2023-01-11 18:04:37 +01:00
import androidx.preference.PreferenceManager;
2023-01-06 18:21:41 +01:00
import com.bumptech.glide.Glide;
2023-01-11 18:04:37 +01:00
import com.bumptech.glide.request.RequestOptions;
2023-01-06 18:21:41 +01:00
import com.smarteist.autoimageslider.SliderViewAdapter;
import java.util.ArrayList;
import java.util.List;
2023-01-11 18:04:37 +01:00
import app.fedilab.android.R;
2023-01-06 18:21:41 +01:00
import app.fedilab.android.databinding.DrawerSliderBinding;
2023-01-22 15:22:59 +01:00
import app.fedilab.android.mastodon.activities.MediaActivity;
import app.fedilab.android.mastodon.client.entities.api.Attachment;
import app.fedilab.android.mastodon.client.entities.api.Status;
2024-01-12 10:36:29 +01:00
import app.fedilab.android.mastodon.client.entities.app.CachedBundle;
2023-01-22 15:22:59 +01:00
import app.fedilab.android.mastodon.helper.Helper;
2023-01-11 18:04:37 +01:00
import jp.wasabeef.glide.transformations.BlurTransformation;
2023-01-06 18:21:41 +01:00
public class SliderAdapter extends SliderViewAdapter<SliderAdapter.SliderAdapterVH> {
private final Status status;
private final List<Attachment> mSliderItems;
private Context context;
public SliderAdapter(Status status) {
this.status = status;
this.mSliderItems = status.media_attachments;
}
public void addItem(Attachment sliderItem) {
this.mSliderItems.add(sliderItem);
notifyDataSetChanged();
}
@Override
public SliderAdapterVH onCreateViewHolder(ViewGroup parent) {
context = parent.getContext();
DrawerSliderBinding itemBinding = DrawerSliderBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
return new SliderAdapterVH(itemBinding);
}
@Override
public void onBindViewHolder(SliderAdapterVH viewHolder, final int position) {
Attachment sliderItem = mSliderItems.get(position);
2023-01-11 18:04:37 +01:00
if (status.sensitive) {
Glide.with(viewHolder.itemView)
.load(sliderItem.preview_url)
.centerCrop()
.apply(new RequestOptions().transform(new BlurTransformation(50, 3)))
.into(viewHolder.binding.ivAutoImageSlider);
} else {
Glide.with(viewHolder.itemView)
.load(sliderItem.preview_url)
.centerCrop()
.into(viewHolder.binding.ivAutoImageSlider);
}
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context);
final int timeout = sharedpreferences.getInt(context.getString(R.string.SET_NSFW_TIMEOUT), 5);
boolean expand_media = sharedpreferences.getBoolean(context.getString(R.string.SET_EXPAND_MEDIA), false);
2023-01-06 18:21:41 +01:00
viewHolder.itemView.setOnClickListener(v -> {
2023-01-11 18:04:37 +01:00
if (status.sensitive && !expand_media) {
status.sensitive = false;
notifyDataSetChanged();
if (timeout > 0) {
new CountDownTimer((timeout * 1000L), 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
status.sensitive = true;
notifyDataSetChanged();
}
}.start();
}
} else {
Intent mediaIntent = new Intent(context, MediaActivity.class);
2024-01-12 10:36:29 +01:00
Bundle args = new Bundle();
args.putInt(Helper.ARG_MEDIA_POSITION, position + 1);
args.putSerializable(Helper.ARG_MEDIA_ARRAY, new ArrayList<>(status.media_attachments));
2024-01-29 15:02:56 +01:00
new CachedBundle(context).insertBundle(args, Helper.getCurrentAccount(context), bundleId -> {
2024-01-12 10:36:29 +01:00
Bundle bundle = new Bundle();
bundle.putLong(Helper.ARG_INTENT_ID, bundleId);
mediaIntent.putExtras(bundle);
ActivityOptionsCompat options = ActivityOptionsCompat
.makeSceneTransitionAnimation((Activity) context, viewHolder.binding.ivAutoImageSlider, status.media_attachments.get(0).url);
context.startActivity(mediaIntent, options.toBundle());
});
2023-01-11 18:04:37 +01:00
}
2023-01-06 18:21:41 +01:00
});
}
@Override
public int getCount() {
return mSliderItems.size();
}
static class SliderAdapterVH extends ViewHolder {
DrawerSliderBinding binding;
SliderAdapterVH(DrawerSliderBinding itemView) {
super(itemView.getRoot());
binding = itemView;
}
}
}