Reputation: 53
I'm Selecting Files From Device Which I Want To Upload On Firebase Storage. After Selecting Files I'm Showing The List Of Files in RecyclerView. I'm Using 3 List To Contain My Three Different Data FileName, Progress (To Show Progressbar of How Much Uploaded) And UploadStatus (To React ProgressBar According To The Status) But Now I Want To Change It To Only One List And Want to use DiffUtil To Update My Data.. But I Don't Know How To Implement it In My Case.
Here's My FileUpdate.java Model Class For DiffUtil.
public class FileUpdate {
private String name;
private int progress = 0;
private String status = "Loading";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getProgress() {
return progress;
}
public void setProgress(int progress) {
this.progress = progress;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FileUpdate that = (FileUpdate) o;
return progress == that.progress && name.equals(that.name) && status.equals(that.status);
}
@Override
public int hashCode() {
return Objects.hash(name, progress, status);
}
public static DiffUtil.ItemCallback<FileUpdate> itemCallback = new DiffUtil.ItemCallback<FileUpdate>() {
@Override
public boolean areItemsTheSame(@NonNull FileUpdate oldItem, @NonNull FileUpdate newItem) {
return false;
}
@Override
public boolean areContentsTheSame(@NonNull FileUpdate oldItem, @NonNull FileUpdate newItem) {
return false;
}
};
}
Here's The Three List I'm Using. in UploadActivity.java
ActivityResultLauncher<Intent> activityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
Intent intentReceived = result.getData();
ClipData clipData = result.getData().getClipData();
if (clipData != null) {
for (int i = 0; i < intentReceived.getClipData().getItemCount(); i++) {
Uri fileUri = intentReceived.getClipData().getItemAt(i).getUri();
String fileName = UploadActivity.this.getFileNameFromUri(fileUri);
uploadFile.add(fileName);
uploadStatus.add("Loading");
adapter.notifyDataSetChanged();
selectFiles.setEnabled(false);
final int index = i;
//Uploading File To Firebase Storage
StorageReference uploader = storageReference.child(Path).child(fileName);
uploader.putFile(fileUri)
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(@NonNull UploadTask.TaskSnapshot snapshot) {
try{
progressValue.remove(index);
}catch(Exception e){
//Do Nothing
}
double progress = (100.0 * snapshot.getBytesTransferred()) / snapshot.getTotalByteCount();
Integer passProgress = Integer.valueOf(currentProgress);
try{
progressValue.add(index,passProgress);
}catch (Exception e){
progressValue.add(passProgress);
}
uploadStatus.remove(index);
uploadStatus.add(index, "Processing");
adapter.notifyDataSetChanged();
}
}).addOnCompleteListener(task -> {
uploadStatus.remove(index);
uploadStatus.add(index,"Done");
adapter.notifyDataSetChanged();
Toast.makeText(UploadActivity.this, "Successfully Uploaded " + fileName, Toast.LENGTH_SHORT).show();
});
}
}
This is Adapter Class I've Updated For DiffUtil.
public class AdminUploadFileAdapter extends ListAdapter<FileUpdate,AdminUploadFileAdapter.uploadViewHolder> {
protected AdminUploadFileAdapter(@NonNull DiffUtil.ItemCallback<FileUpdate> diffCallback) {
super(diffCallback);
}
@NonNull
@Override
public uploadViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.admin_upload_material_list_design,parent,false);
return new uploadViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull uploadViewHolder holder, int position) {
FileUpdate fileUpdate = getItem(position);
holder.bind(fileUpdate);
}
public static class uploadViewHolder extends RecyclerView.ViewHolder{
ImageView fileIcon,uploadComplete;
ProgressBar uploadProgressBar;
TextView uploadFileName;
public uploadViewHolder(@NonNull View itemView) {
super(itemView);
uploadFileName = itemView.findViewById(R.id.txt_uploadFileName);
fileIcon = itemView.findViewById(R.id.imageView_uploadFileIcon);
uploadProgressBar = itemView.findViewById(R.id.progress_fileUpload);
uploadComplete = itemView.findViewById(R.id.imageView_uploadComplete);
uploadProgressBar.setProgress(0);
}
public void bind(FileUpdate fileUpdate){
uploadFileName.setText(fileUpdate.getName());
String fileStatus = fileUpdate.getStatus();
if(fileStatus.equals("Processing")){
uploadProgressBar.setProgress(fileUpdate.getProgress());
}
if(fileStatus.equals("Done")){
uploadProgressBar.setProgress(0);
uploadProgressBar.setVisibility(View.GONE);
uploadComplete.setImageResource(R.drawable.check_circle);
}
}
}
}
Now I Don't Know Which Changes I Need To Do To Add And Update My FileName, UploadStatus And Progressbar. I'm Beginner in This Thing.
Upvotes: 0
Views: 371