Skip to content

Commit 9c23c6a

Browse files
Merge pull request #9 from smartdevelopers-ir/changes_for_android_11
2022/03/08
2 parents b44eb8c + 5d29941 commit 9c23c6a

File tree

8 files changed

+87
-22
lines changed

8 files changed

+87
-22
lines changed

SmartFileBrowser/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ android {
3131
}
3232
}
3333
group = 'ir.smartdevelopers'
34-
version = '1.4.7'
34+
version = '1.4.8'
3535
task sourcesJar(type: Jar) {
3636
archiveClassifier.set("sources")
3737
from android.sourceSets.main.java.srcDirs

SmartFileBrowser/src/main/java/ir/smartdevelopers/smartfilebrowser/acitivties/FileBrowserMainActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import androidx.lifecycle.LiveData;
1515
import androidx.lifecycle.Observer;
1616
import androidx.lifecycle.ViewModelProvider;
17+
import androidx.recyclerview.widget.DiffUtil;
1718
import androidx.recyclerview.widget.GridLayoutManager;
1819
import androidx.recyclerview.widget.LinearLayoutManager;
1920
import androidx.recyclerview.widget.RecyclerView;

SmartFileBrowser/src/main/java/ir/smartdevelopers/smartfilebrowser/adapters/FileBrowserAdapter.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
import android.widget.TextView;
1010

1111
import androidx.annotation.NonNull;
12+
import androidx.recyclerview.widget.DiffUtil;
1213
import androidx.recyclerview.widget.RecyclerView;
1314

1415
import com.bumptech.glide.Glide;
1516

1617
import java.io.File;
1718
import java.util.ArrayList;
1819
import java.util.List;
20+
import java.util.Objects;
1921

2022
import ir.smartdevelopers.smartfilebrowser.R;
2123
import ir.smartdevelopers.smartfilebrowser.customClasses.FileUtil;
@@ -47,12 +49,48 @@ public FileBrowserAdapter(List<File> selectedFiles) {
4749
mSelectedFiles=selectedFiles;
4850
}
4951

52+
class DiffCallback extends DiffUtil.Callback{
53+
List<FileBrowserModel> oldList;
54+
List<FileBrowserModel> newList;
55+
56+
public DiffCallback(List<FileBrowserModel> oldList, List<FileBrowserModel> newList) {
57+
this.oldList = oldList;
58+
this.newList = newList;
59+
}
60+
61+
@Override
62+
public int getOldListSize() {
63+
return oldList.size();
64+
}
65+
66+
@Override
67+
public int getNewListSize() {
68+
return newList.size();
69+
}
70+
71+
@Override
72+
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
73+
if (oldList.get(oldItemPosition).getId()==FileBrowserModel.ID_RECENT_FILES ||
74+
newList.get(newItemPosition).getId() == FileBrowserModel.ID_RECENT_FILES){
75+
return false;
76+
}
77+
return Objects.equals(oldList.get(oldItemPosition),newList.get(newItemPosition));
78+
}
79+
80+
@Override
81+
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
82+
return Objects.equals(oldList.get(oldItemPosition),newList.get(newItemPosition));
83+
}
84+
}
5085
public void setList(List<FileBrowserModel> fileBrowserModels) {
5186
// notifyItemRangeRemoved(0,mFileBrowserModelsCopy.size());
87+
88+
// notifyItemRangeInserted(0,mFileBrowserModelsCopy.size());
89+
DiffUtil.DiffResult result=DiffUtil.calculateDiff(new DiffCallback(mFileBrowserModelsCopy,fileBrowserModels),false);
5290
mFileBrowserModels = fileBrowserModels;
5391
mFileBrowserModelsCopy = fileBrowserModels;
54-
// notifyItemRangeInserted(0,mFileBrowserModelsCopy.size());
55-
notifyDataSetChanged();
92+
result.dispatchUpdatesTo(this);
93+
//notifyDataSetChanged();
5694
}
5795

5896
@Override

SmartFileBrowser/src/main/java/ir/smartdevelopers/smartfilebrowser/adapters/GalleryAdapter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.bumptech.glide.GlideBuilder;
2020
import com.bumptech.glide.Priority;
2121
import com.bumptech.glide.load.engine.executor.GlideExecutor;
22+
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
2223

2324
import java.io.File;
2425
import java.util.ArrayList;
@@ -303,8 +304,9 @@ void bindView(GalleryModel model){
303304
.override(THUMBNAIL_SIZE,THUMBNAIL_SIZE)
304305
//.dontAnimate()
305306
//.thumbnail(0.2f)
306-
.encodeQuality(30)
307+
.encodeQuality(50)
307308
.priority(Priority.LOW)
309+
.transition(DrawableTransitionOptions.withCrossFade())
308310
.into(mImageView);
309311

310312

SmartFileBrowser/src/main/java/ir/smartdevelopers/smartfilebrowser/customClasses/Utils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static List<FileBrowserModel> generateFirstPageList(Context context,List<
5858
fileBrowserModels.add(downloadFolder);
5959

6060
/*Recent file*/
61-
FileBrowserModel recent=new FileBrowserModel(-4,context.getString(R.string.recent),"",FileBrowserModel.MODEL_TYPE_ALL_FILE_TITLE,
61+
FileBrowserModel recent=new FileBrowserModel(FileBrowserModel.ID_RECENT_FILES,context.getString(R.string.recent),"",FileBrowserModel.MODEL_TYPE_ALL_FILE_TITLE,
6262
null,"",null);
6363
fileBrowserModels.add(recent);
6464

SmartFileBrowser/src/main/java/ir/smartdevelopers/smartfilebrowser/models/FileBrowserModel.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@
33
import android.text.TextUtils;
44

55
import java.io.File;
6+
import java.util.Objects;
67

78
import ir.smartdevelopers.smartfilebrowser.customClasses.FileUtil;
89
import ir.smartdevelopers.smartfilebrowser.customClasses.Utils;
910

1011
public class FileBrowserModel implements FileModel {
11-
public static final int MODEL_TYPE_FOLDER=0;
12-
public static final int MODEL_TYPE_INTERNAL_STORAGE=1;
13-
public static final int MODEL_TYPE_EXTERNAL_STORAGE=2;
14-
public static final int MODEL_TYPE_DOWNLOAD_FOLDER=3;
15-
public static final int MODEL_TYPE_VIDEO=4;
16-
public static final int MODEL_TYPE_IMAGE=5;
17-
public static final int MODEL_TYPE_AUDIO=6;
18-
public static final int MODEL_TYPE_PDF=7;
19-
public static final int MODEL_TYPE_FILE=8;
20-
public static final int MODEL_TYPE_ALL_FILE_TITLE = 9;
21-
public static final int MODEL_TYPE_GO_BACK = 10;
12+
public static final int MODEL_TYPE_FOLDER=1;
13+
public static final int MODEL_TYPE_INTERNAL_STORAGE=2;
14+
public static final int MODEL_TYPE_EXTERNAL_STORAGE=3;
15+
public static final int MODEL_TYPE_DOWNLOAD_FOLDER=4;
16+
public static final int MODEL_TYPE_VIDEO=5;
17+
public static final int MODEL_TYPE_IMAGE=6;
18+
public static final int MODEL_TYPE_AUDIO=7;
19+
public static final int MODEL_TYPE_PDF=8;
20+
public static final int MODEL_TYPE_FILE=9;
21+
public static final int MODEL_TYPE_ALL_FILE_TITLE = 10;
22+
public static final int MODEL_TYPE_GO_BACK = 11;
2223
public static final int ID_EXTERNAL_STORAGE = -2;
2324
public static final int ID_INTERNAL_STORAGE = -1;
2425
public static final int ID_DOWNLOAD_FOLDER = -3;
26+
public static final int ID_RECENT_FILES = -4;
2527

2628

2729
private long id;
@@ -168,4 +170,17 @@ public boolean isSelected() {
168170
public void setSelected(boolean selected) {
169171
mSelected = selected;
170172
}
173+
174+
@Override
175+
public boolean equals(Object o) {
176+
if (this == o) return true;
177+
if (o == null || getClass() != o.getClass()) return false;
178+
FileBrowserModel model = (FileBrowserModel) o;
179+
return id == model.id && Objects.equals(mTitle, model.mTitle) && Objects.equals(mPath, model.mPath);
180+
}
181+
182+
@Override
183+
public int hashCode() {
184+
return Objects.hash(id, mTitle, mPath);
185+
}
171186
}

SmartFileBrowser/src/main/java/ir/smartdevelopers/smartfilebrowser/viewModel/FilesViewModel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void getFirstPagePdfLiveData(FileFilter fileFilter) {
5454
String selection= MediaStore.Files.FileColumns.MIME_TYPE+" LIKE ?";
5555
String[] selectionArgs=new String[]{"%pdf%"};
5656
mRepository.getFirstBrowserPageList(selection, selectionArgs,
57-
FileBrowserModel.MODEL_TYPE_FILE, fileFilter, mFilesLiveData);
57+
FileBrowserModel.MODEL_TYPE_PDF, fileFilter, mFilesLiveData);
5858
// mAllLiveData.put("pdf", mFirstPagePdfLiveData);
5959
// }
6060

@@ -65,7 +65,7 @@ public void getFirstPageAudiosLiveData(FileFilter fileFilter) {
6565
String selection= MediaStore.Files.FileColumns.MIME_TYPE+" LIKE ?";
6666
String[] selectionArgs=new String[]{"%audio%"};
6767
mRepository.getFirstBrowserPageList(selection,selectionArgs,
68-
FileBrowserModel.MODEL_TYPE_FILE,fileFilter, mFilesLiveData);
68+
FileBrowserModel.MODEL_TYPE_AUDIO,fileFilter, mFilesLiveData);
6969
// mAllLiveData.put("audio", mFirstPageAudiosLiveData);
7070
// }
7171

@@ -76,7 +76,7 @@ public void getFirstPageVideosLiveData(FileFilter fileFilter) {
7676
String selection= MediaStore.Files.FileColumns.MIME_TYPE+" LIKE ?";
7777
String[] selectionArgs=new String[]{"%video%"};
7878
mRepository.getFirstBrowserPageList(selection,selectionArgs,
79-
FileBrowserModel.MODEL_TYPE_FILE,fileFilter, mFilesLiveData);
79+
FileBrowserModel.MODEL_TYPE_VIDEO,fileFilter, mFilesLiveData);
8080
// mAllLiveData.put("video", mFirstPageVideosLiveData);
8181
// }
8282

SmartFileBrowser/src/main/java/ir/smartdevelopers/smartfilebrowser/viewModel/Repository.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public class Repository {
4242
private final ContentResolver mContentResolver;
4343
private final WeakReference<Context> wContext;
4444
private final MutableLiveData<List<GalleryModel>> galleryList;
45+
private List<FileBrowserModel> mCurrentFirstFiles;
46+
private int mCurrentModelType;
4547
public Repository(Application application) {
4648
mExecutorService= Executors.newCachedThreadPool();
4749
mContentResolver=application.getContentResolver();
@@ -195,8 +197,14 @@ private List<GalleryModel> getGalleryModel(Cursor cursor,String[] projection){
195197
public void getFirstBrowserPageList(String selection,String[] selectionArgs, int modelType,
196198
FileFilter fileFilter,
197199
MutableLiveData<List<FileBrowserModel>> filesLiveData){
198-
199-
200+
if (mCurrentFirstFiles==null){
201+
filesLiveData.setValue(Utils.generateFirstPageList(wContext.get(),Collections.emptyList()));
202+
}else {
203+
if (mCurrentModelType == modelType){
204+
filesLiveData.setValue(mCurrentFirstFiles);
205+
return;
206+
}
207+
}
200208
String extraQuery=MediaStore.Files.FileColumns.MIME_TYPE+" IS NOT NULL AND " +
201209
MediaStore.Files.FileColumns.DATA+" NOT LIKE '%.thumbnail%' AND "+
202210
MediaStore.Files.FileColumns.DATA+" NOT LIKE '%Android/%' ";
@@ -259,7 +267,8 @@ public void getFirstBrowserPageList(String selection,String[] selectionArgs, int
259267
}
260268
cursor.close();
261269
if (wContext.get()==null){return;}
262-
filesLiveData.postValue(Utils.generateFirstPageList(wContext.get(),fileBrowserModels));
270+
mCurrentFirstFiles=Utils.generateFirstPageList(wContext.get(),fileBrowserModels);
271+
filesLiveData.postValue(mCurrentFirstFiles);
263272

264273
});
265274

0 commit comments

Comments
 (0)