Skip to content
This repository has been archived by the owner on Mar 30, 2024. It is now read-only.

Commit

Permalink
feat: made some recycler view improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jaiselrahman committed May 4, 2020
1 parent c96cddb commit 5ed384d
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ public void onClick(View v) {
.setShowImages(true)
.setShowAudios(true)
.setShowVideos(true)
.setIgnoreNoMedia(false)
.enableVideoCapture(true)
.enableImageCapture(true)
.setIgnoreHiddenFile(false)
.setMaxSelection(10)
.build());
startActivityForResult(intent, FILE_REQUEST_CODE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class DirSelectActivity extends AppCompatActivity implements DirListAdapter.OnCameraClickListener {

Expand All @@ -63,18 +63,21 @@ public class DirSelectActivity extends AppCompatActivity implements DirListAdapt
private static final int REQUEST_CAMERA_PERMISSION_FOR_VIDEO = 3;
private static final int REQUEST_DOCUMENT = 4;
private static final int REQUEST_FILE = 5;
private static final int INIT_SIZE = 6;
private Configurations configs;
private ArrayList<Dir> dirs = new ArrayList<>();
private DirListAdapter dirAdapter;

private DirResultCallback dirResultCallback = new DirResultCallback() {
@Override
public void onResult(Collection<Dir> dirsResult) {
public void onResult(final List<Dir> dirsResult) {
if (dirs != null) {
dirs.clear();
dirs.ensureCapacity(dirsResult.size());
dirs.addAll(dirsResult);
dirAdapter.notifyDataSetChanged();
if (dirsResult.size() <= INIT_SIZE) {
dirAdapter.submitList(dirsResult);
} else {
dirAdapter.submitList(dirsResult.subList(0, INIT_SIZE));
dirAdapter.submitList(dirsResult);
}
}
}
};
Expand Down Expand Up @@ -126,7 +129,7 @@ protected void onCreate(Bundle savedInstanceState) {
imageSize = Math.min(point.x, point.y) / configs.getPortraitSpanCount();
}

dirAdapter = new DirListAdapter(this, dirs, imageSize,
dirAdapter = new DirListAdapter(this, imageSize,
configs.isImageCaptureEnabled(),
configs.isVideoCaptureEnabled());

Expand All @@ -142,13 +145,20 @@ public void onClick(Dir dir) {

dirAdapter.setOnCameraClickListener(this);
RecyclerView recyclerView = findViewById(R.id.file_gallery);
recyclerView.setLayoutManager(new GridLayoutManager(this, spanCount));
recyclerView.setLayoutManager(new GridLayoutManager(this, spanCount) {
@Override
public boolean isAutoMeasureEnabled() {
return false;
}
});
recyclerView.setAdapter(dirAdapter);
recyclerView.addItemDecoration(new DividerItemDecoration(getResources().getDimensionPixelSize(R.dimen.grid_spacing), spanCount));
recyclerView.setItemAnimator(null);
recyclerView.setHasFixedSize(true);
recyclerView.setItemViewCacheSize(20);

if (requestPermission(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_WRITE_PERMISSION)) {
loadFiles(false);
loadDirs(false);
}
}

Expand All @@ -158,15 +168,15 @@ private boolean useDocumentUi() {
&& !(configs.isShowImages() || configs.isShowVideos() || configs.isShowAudios());
}

private void loadFiles(boolean restart) {
private void loadDirs(boolean restart) {
DirLoader.loadDirs(this, dirResultCallback, configs, restart);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_WRITE_PERMISSION) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
loadFiles(false);
loadDirs(false);
} else {
Toast.makeText(this, R.string.permission_not_given, Toast.LENGTH_SHORT).show();
finish();
Expand Down Expand Up @@ -196,7 +206,7 @@ public void onScanCompleted(String path, final Uri uri) {
runOnUiThread(new Runnable() {
@Override
public void run() {
loadFiles(true);
loadDirs(true);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,22 @@ public class FilePickerActivity extends AppCompatActivity
private static final int REQUEST_CAMERA_PERMISSION_FOR_CAMERA = 2;
private static final int REQUEST_CAMERA_PERMISSION_FOR_VIDEO = 3;
private static final int REQUEST_DOCUMENT = 4;
private static final int INIT_SIZE = 6;
private Configurations configs;
private ArrayList<MediaFile> mediaFiles = new ArrayList<>();
private FileGalleryAdapter fileGalleryAdapter;
private int maxCount;
private Long dirId = null;

private FileResultCallback fileResultCallback = new FileResultCallback() {
@Override
public void onResult(ArrayList<MediaFile> filesResults) {
public void onResult(final ArrayList<MediaFile> filesResults) {
if (filesResults != null) {
mediaFiles.clear();
mediaFiles.ensureCapacity(filesResults.size());
mediaFiles.addAll(filesResults);
fileGalleryAdapter.notifyDataSetChanged();
if (filesResults.size() <= INIT_SIZE) {
fileGalleryAdapter.submitList(filesResults);
} else {
fileGalleryAdapter.submitList(filesResults.subList(0, INIT_SIZE));
fileGalleryAdapter.submitList(filesResults);
}
}
}
};
Expand Down Expand Up @@ -135,9 +137,9 @@ protected void onCreate(Bundle savedInstanceState) {
}

boolean isSingleChoice = configs.isSingleChoiceMode();
fileGalleryAdapter = new FileGalleryAdapter(this, mediaFiles, imageSize,
configs.isImageCaptureEnabled(),
configs.isVideoCaptureEnabled());
fileGalleryAdapter = new FileGalleryAdapter(this, imageSize,
dirId == null && configs.isImageCaptureEnabled(),
dirId == null && configs.isVideoCaptureEnabled());
fileGalleryAdapter.enableSelection(true);
fileGalleryAdapter.enableSingleClickSelection(configs.isSingleClickSelection());
fileGalleryAdapter.setOnSelectionListener(this);
Expand All @@ -146,10 +148,17 @@ protected void onCreate(Bundle savedInstanceState) {
fileGalleryAdapter.setSelectedItems(configs.getSelectedMediaFiles());
fileGalleryAdapter.setOnCameraClickListener(this);
RecyclerView recyclerView = findViewById(R.id.file_gallery);
recyclerView.setLayoutManager(new GridLayoutManager(this, spanCount));
recyclerView.setLayoutManager(new GridLayoutManager(this, spanCount) {
@Override
public boolean isAutoMeasureEnabled() {
return false;
}
});
recyclerView.setAdapter(fileGalleryAdapter);
recyclerView.addItemDecoration(new DividerItemDecoration(getResources().getDimensionPixelSize(R.dimen.grid_spacing), spanCount));
recyclerView.setItemAnimator(null);
recyclerView.setHasFixedSize(false);
recyclerView.setItemViewCacheSize(20);

if (requestPermission(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_WRITE_PERMISSION)) {
loadFiles(false);
Expand Down Expand Up @@ -356,4 +365,14 @@ public void onSelectionEnd() {
@Override
public void onMaxReached() {
}

@Override
public void onBackPressed() {

Intent intent = new Intent();
intent.putExtra(MEDIA_FILES, fileGalleryAdapter.getSelectedItems());
setResult(RESULT_CANCELED, intent);

super.onBackPressed();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.AsyncDifferConfig;
import androidx.recyclerview.widget.AsyncListDiffer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
Expand All @@ -41,18 +45,18 @@

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import static android.os.Environment.DIRECTORY_MOVIES;
import static android.os.Environment.DIRECTORY_PICTURES;
import static android.os.Environment.getExternalStoragePublicDirectory;
import static com.jaiselrahman.filepicker.activity.FilePickerActivity.TAG;

public class DirListAdapter extends RecyclerView.Adapter<DirListAdapter.ViewHolder> {
public class DirListAdapter extends RecyclerView.Adapter<DirListAdapter.ViewHolder> implements ListUpdateCallback {
public static final int CAPTURE_IMAGE_VIDEO = 1;
private ArrayList<Dir> mediaDirs;
// private ArrayList<Dir> mediaDirs;
private Activity activity;
private RequestManager glideRequest;
private OnClickListener onClickListener;
Expand All @@ -61,10 +65,12 @@ public class DirListAdapter extends RecyclerView.Adapter<DirListAdapter.ViewHold
private boolean showVideoCamera;
private File lastCapturedFile;
private Uri lastCapturedUri;
private int itemStartPosition;
private SimpleDateFormat TimeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault());

public DirListAdapter(Activity activity, ArrayList<Dir> mediaDirs, int imageSize, boolean showCamera, boolean showVideoCamera) {
this.mediaDirs = mediaDirs;
private AsyncListDiffer<Dir> differ = new AsyncListDiffer<>(this, new AsyncDifferConfig.Builder<>(DIR_ITEM_CALLBACK).build());

public DirListAdapter(Activity activity, int imageSize, boolean showCamera, boolean showVideoCamera) {
this.activity = activity;
this.showCamera = showCamera;
this.showVideoCamera = showVideoCamera;
Expand All @@ -74,6 +80,11 @@ public DirListAdapter(Activity activity, ArrayList<Dir> mediaDirs, int imageSize
.optionalCenterCrop()
.placeholder(R.drawable.ic_dir)
.override(imageSize));

if (showCamera && showVideoCamera)
itemStartPosition = 2;
else if (showCamera || showVideoCamera)
itemStartPosition = 1;
}

public File getLastCapturedFile() {
Expand Down Expand Up @@ -126,13 +137,17 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
position--;
}

holder.dir = mediaDirs.get(position);
holder.dir = getItem(position);

holder.dirName.setText(holder.dir.getName());
holder.dirCount.setText(String.valueOf(holder.dir.getCount()));

glideRequest.load(holder.dir.getPreview())
.into(holder.dirPreview);
Uri preview = holder.dir.getPreview();
if (preview != null)
glideRequest.load(holder.dir.getPreview())
.into(holder.dirPreview);
else
holder.dirPreview.setImageResource(R.drawable.ic_dir);
}

private void handleCamera(final ImageView openCamera, final boolean forVideo) {
Expand Down Expand Up @@ -193,16 +208,44 @@ public void setOnCameraClickListener(OnCameraClickListener onCameraClickListener
this.onCameraClickListener = onCameraClickListener;
}

private Dir getItem(int position) {
return differ.getCurrentList().get(position);
}

@Override
public int getItemCount() {
if (showCamera) {
if (showVideoCamera)
return mediaDirs.size() + 2;
return mediaDirs.size() + 1;
return differ.getCurrentList().size() + 2;
return differ.getCurrentList().size() + 1;
} else if (showVideoCamera) {
return mediaDirs.size() + 1;
return differ.getCurrentList().size() + 1;
}
return mediaDirs.size();
return differ.getCurrentList().size();
}

@Override
public void onInserted(int position, int count) {
notifyItemRangeInserted(itemStartPosition + position, count);
}

@Override
public void onRemoved(int position, int count) {
notifyItemRangeRemoved(itemStartPosition + position, count);
}

@Override
public void onMoved(int fromPosition, int toPosition) {
notifyItemMoved(itemStartPosition + fromPosition, itemStartPosition + toPosition);
}

@Override
public void onChanged(int position, int count, Object payload) {
notifyItemRangeChanged(itemStartPosition + position, count, payload);
}

public void submitList(List<Dir> dirs) {
differ.submitList(dirs);
}

static class ViewHolder extends RecyclerView.ViewHolder {
Expand Down Expand Up @@ -236,4 +279,19 @@ public interface OnCameraClickListener {
public interface OnClickListener {
void onClick(Dir dir);
}

private static final DiffUtil.ItemCallback<Dir> DIR_ITEM_CALLBACK = new DiffUtil.ItemCallback<Dir>() {
@Override
public boolean areItemsTheSame(@NonNull Dir oldItem, @NonNull Dir newItem) {
return oldItem.getId() == newItem.getId();
}

@Override
public boolean areContentsTheSame(@NonNull Dir oldItem, @NonNull Dir newItem) {
return oldItem.getName().equals(newItem.getName())
&& oldItem.getCount() == newItem.getCount()
&& (oldItem.getPreview() == null && newItem.getPreview() == null)
|| oldItem.getPreview().equals(newItem.getPreview());
}
};
}
Loading

0 comments on commit 5ed384d

Please sign in to comment.