mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-29 05:43:55 +01:00
Convert ThumbnailsAdapter to kotlin
This commit is contained in:
parent
90c2e041f3
commit
7a9e5c59db
3 changed files with 90 additions and 142 deletions
|
|
@ -1,141 +0,0 @@
|
||||||
package fr.free.nrw.commons.upload;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.graphics.drawable.GradientDrawable;
|
|
||||||
import android.net.Uri;
|
|
||||||
import android.os.Build.VERSION;
|
|
||||||
import android.os.Build.VERSION_CODES;
|
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
import android.widget.ImageView;
|
|
||||||
import android.widget.RelativeLayout;
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
|
||||||
import com.facebook.drawee.view.SimpleDraweeView;
|
|
||||||
import fr.free.nrw.commons.R;
|
|
||||||
import fr.free.nrw.commons.databinding.ItemUploadThumbnailBinding;
|
|
||||||
import fr.free.nrw.commons.filepicker.UploadableFile;
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The adapter class for image thumbnails to be shown while uploading.
|
|
||||||
*/
|
|
||||||
class ThumbnailsAdapter extends RecyclerView.Adapter<ThumbnailsAdapter.ViewHolder> {
|
|
||||||
public static Context context;
|
|
||||||
List<UploadableFile> uploadableFiles;
|
|
||||||
private Callback callback;
|
|
||||||
|
|
||||||
private OnThumbnailDeletedListener listener;
|
|
||||||
|
|
||||||
private ItemUploadThumbnailBinding binding;
|
|
||||||
|
|
||||||
public ThumbnailsAdapter(Callback callback) {
|
|
||||||
this.uploadableFiles = new ArrayList<>();
|
|
||||||
this.callback = callback;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the data, the media files
|
|
||||||
* @param uploadableFiles
|
|
||||||
*/
|
|
||||||
public void setUploadableFiles(
|
|
||||||
List<UploadableFile> uploadableFiles) {
|
|
||||||
this.uploadableFiles=uploadableFiles;
|
|
||||||
notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setOnThumbnailDeletedListener(OnThumbnailDeletedListener listener) {
|
|
||||||
this.listener = listener;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
@Override
|
|
||||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
|
|
||||||
binding = ItemUploadThumbnailBinding.inflate(LayoutInflater.from(viewGroup.getContext()), viewGroup, false);
|
|
||||||
return new ViewHolder(binding.getRoot());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
|
|
||||||
viewHolder.bind(position);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getItemCount() {
|
|
||||||
return uploadableFiles.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
public class ViewHolder extends RecyclerView.ViewHolder {
|
|
||||||
|
|
||||||
|
|
||||||
RelativeLayout rlContainer;
|
|
||||||
SimpleDraweeView background;
|
|
||||||
ImageView ivError;
|
|
||||||
|
|
||||||
ImageView ivCross;
|
|
||||||
|
|
||||||
public ViewHolder(@NonNull View itemView) {
|
|
||||||
super(itemView);
|
|
||||||
rlContainer = binding.rlContainer;
|
|
||||||
background = binding.ivThumbnail;
|
|
||||||
ivError = binding.ivError;
|
|
||||||
ivCross = binding.icCross;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Binds a row item to the ViewHolder
|
|
||||||
* @param position
|
|
||||||
*/
|
|
||||||
public void bind(int position) {
|
|
||||||
UploadableFile uploadableFile = uploadableFiles.get(position);
|
|
||||||
Uri uri = uploadableFile.getMediaUri();
|
|
||||||
background.setImageURI(Uri.fromFile(new File(String.valueOf(uri))));
|
|
||||||
if (position == callback.getCurrentSelectedFilePosition()) {
|
|
||||||
GradientDrawable border = new GradientDrawable();
|
|
||||||
border.setShape(GradientDrawable.RECTANGLE);
|
|
||||||
border.setStroke(8, context.getResources().getColor(R.color.primaryColor));
|
|
||||||
rlContainer.setEnabled(true);
|
|
||||||
rlContainer.setClickable(true);
|
|
||||||
rlContainer.setAlpha(1.0f);
|
|
||||||
rlContainer.setBackground(border);
|
|
||||||
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
|
|
||||||
rlContainer.setElevation(10);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
rlContainer.setEnabled(false);
|
|
||||||
rlContainer.setClickable(false);
|
|
||||||
rlContainer.setAlpha(0.7f);
|
|
||||||
rlContainer.setBackground(null);
|
|
||||||
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
|
|
||||||
rlContainer.setElevation(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ivCross.setOnClickListener(v -> {
|
|
||||||
if(listener != null) {
|
|
||||||
listener.onThumbnailDeleted(position);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Callback used to get the current selected file position
|
|
||||||
*/
|
|
||||||
interface Callback {
|
|
||||||
|
|
||||||
int getCurrentSelectedFilePosition();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface to listen to thumbnail delete events
|
|
||||||
*/
|
|
||||||
|
|
||||||
public interface OnThumbnailDeletedListener {
|
|
||||||
void onThumbnailDeleted(int position);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,90 @@
|
||||||
|
package fr.free.nrw.commons.upload
|
||||||
|
|
||||||
|
import android.graphics.drawable.GradientDrawable
|
||||||
|
import android.net.Uri
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.ImageView
|
||||||
|
import android.widget.RelativeLayout
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import com.facebook.drawee.view.SimpleDraweeView
|
||||||
|
import fr.free.nrw.commons.R
|
||||||
|
import fr.free.nrw.commons.databinding.ItemUploadThumbnailBinding
|
||||||
|
import fr.free.nrw.commons.filepicker.UploadableFile
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The adapter class for image thumbnails to be shown while uploading.
|
||||||
|
*/
|
||||||
|
internal class ThumbnailsAdapter(private val callback: Callback) :
|
||||||
|
RecyclerView.Adapter<ThumbnailsAdapter.ViewHolder>() {
|
||||||
|
|
||||||
|
var onThumbnailDeletedListener: OnThumbnailDeletedListener? = null
|
||||||
|
var uploadableFiles: List<UploadableFile> = emptyList()
|
||||||
|
set(value) {
|
||||||
|
field = value
|
||||||
|
notifyDataSetChanged()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int) = ViewHolder(
|
||||||
|
ItemUploadThumbnailBinding.inflate(
|
||||||
|
LayoutInflater.from(viewGroup.context), viewGroup, false
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) = viewHolder.bind(position)
|
||||||
|
|
||||||
|
override fun getItemCount(): Int = uploadableFiles.size
|
||||||
|
|
||||||
|
inner class ViewHolder(val binding: ItemUploadThumbnailBinding) :
|
||||||
|
RecyclerView.ViewHolder(binding.root) {
|
||||||
|
private val rlContainer: RelativeLayout = binding.rlContainer
|
||||||
|
private val background: SimpleDraweeView = binding.ivThumbnail
|
||||||
|
private val ivError: ImageView = binding.ivError
|
||||||
|
private val ivCross: ImageView = binding.icCross
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Binds a row item to the ViewHolder
|
||||||
|
*/
|
||||||
|
fun bind(position: Int) {
|
||||||
|
val uploadableFile = uploadableFiles[position]
|
||||||
|
val uri = uploadableFile.getMediaUri()
|
||||||
|
background.setImageURI(Uri.fromFile(File(uri.toString())))
|
||||||
|
if (position == callback.getCurrentSelectedFilePosition()) {
|
||||||
|
val border = GradientDrawable()
|
||||||
|
border.shape = GradientDrawable.RECTANGLE
|
||||||
|
border.setStroke(8, ContextCompat.getColor(itemView.context, R.color.primaryColor))
|
||||||
|
rlContainer.isEnabled = true
|
||||||
|
rlContainer.isClickable = true
|
||||||
|
rlContainer.alpha = 1.0f
|
||||||
|
rlContainer.background = border
|
||||||
|
rlContainer.elevation = 10f
|
||||||
|
} else {
|
||||||
|
rlContainer.isEnabled = false
|
||||||
|
rlContainer.isClickable = false
|
||||||
|
rlContainer.alpha = 0.7f
|
||||||
|
rlContainer.background = null
|
||||||
|
rlContainer.elevation = 0f
|
||||||
|
}
|
||||||
|
|
||||||
|
ivCross.setOnClickListener {
|
||||||
|
onThumbnailDeletedListener?.onThumbnailDeleted(position)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback used to get the current selected file position
|
||||||
|
*/
|
||||||
|
internal fun interface Callback {
|
||||||
|
fun getCurrentSelectedFilePosition(): Int
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface to listen to thumbnail delete events
|
||||||
|
*/
|
||||||
|
fun interface OnThumbnailDeletedListener {
|
||||||
|
fun onThumbnailDeleted(position: Int)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -448,7 +448,6 @@ public class UploadActivity extends BaseActivity implements
|
||||||
}
|
}
|
||||||
|
|
||||||
private void receiveSharedItems() {
|
private void receiveSharedItems() {
|
||||||
ThumbnailsAdapter.context=this;
|
|
||||||
final Intent intent = getIntent();
|
final Intent intent = getIntent();
|
||||||
final String action = intent.getAction();
|
final String action = intent.getAction();
|
||||||
if (Intent.ACTION_SEND.equals(action) || Intent.ACTION_SEND_MULTIPLE.equals(action)) {
|
if (Intent.ACTION_SEND.equals(action) || Intent.ACTION_SEND_MULTIPLE.equals(action)) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue