Feat: FileListView can be used as a standalone view, show/hide settings

This commit is contained in:
Boulay Mathias 2022-11-05 17:20:15 +01:00
parent f74a77826d
commit f90d985443

View File

@ -26,6 +26,8 @@ public class FileListView extends LinearLayout
//For filtering by file types: //For filtering by file types:
private final String[] fileSuffixes; private final String[] fileSuffixes;
private boolean showFiles = true;
private boolean showFolders = true;
public FileListView(AlertDialog build) { public FileListView(AlertDialog build) {
this(build.getContext(), null, new String[0]); this(build.getContext(), null, new String[0]);
@ -42,6 +44,14 @@ public class FileListView extends LinearLayout
this.build = build; this.build = build;
} }
public FileListView(Context context){
this(context, null);
}
public FileListView(Context context, AttributeSet attrs){
this(context, attrs, new String[0]);
}
public FileListView(Context context, AttributeSet attrs, String[] fileSuffixes) { public FileListView(Context context, AttributeSet attrs, String[] fileSuffixes) {
this(context, attrs, 0, fileSuffixes); this(context, attrs, 0, fileSuffixes);
} }
@ -62,32 +72,25 @@ public class FileListView extends LinearLayout
mainLv = new ListView(context); mainLv = new ListView(context);
mainLv.setOnItemClickListener(new OnItemClickListener(){ mainLv.setOnItemClickListener((p1, p2, p3, p4) -> {
@Override // TODO: Implement this method
public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4) File mainFile = new File(p1.getItemAtPosition(p3).toString());
{ if (p3 == 0 && !lockPath.equals(fullPath)) {
// TODO: Implement this method parentDir();
File mainFile = new File(p1.getItemAtPosition(p3).toString()); } else {
if (p3 == 0 && !lockPath.equals(fullPath)) { listFileAt(mainFile.getAbsolutePath());
parentDir(); }
} else { });
listFileAt(mainFile.getAbsolutePath());
} mainLv.setOnItemLongClickListener((p1, p2, p3, p4) -> {
} // TODO: Implement this method
}); File mainFile = new File(p1.getItemAtPosition(p3).toString());
mainLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){ if (mainFile.isFile()) {
@Override listener.onFileLongClick(mainFile, mainFile.getAbsolutePath());
public boolean onItemLongClick(AdapterView<?> p1, View p2, int p3, long p4) return true;
{ }
// TODO: Implement this method return false;
File mainFile = new File(p1.getItemAtPosition(p3).toString()); });
if (mainFile.isFile()) {
listener.onFileLongClick(mainFile, mainFile.getAbsolutePath());
return true;
}
return false;
}
});
addView(mainLv, layParam); addView(mainLv, layParam);
try { try {
@ -99,8 +102,7 @@ public class FileListView extends LinearLayout
this.listener = listener; this.listener = listener;
} }
public void listFileAt(final String path) public void listFileAt(final String path) {
{
try{ try{
final File mainPath = new File(path); final File mainPath = new File(path);
if(mainPath.exists()){ if(mainPath.exists()){
@ -115,27 +117,27 @@ public class FileListView extends LinearLayout
if(listFile != null && listFile.length != 0){ if(listFile != null && listFile.length != 0){
Arrays.sort(listFile, new SortFileName()); Arrays.sort(listFile, new SortFileName());
if(fileSuffixes.length > 0){ //Meaning we want only specific files
for(File file : listFile){
if(file.isDirectory()){
if((!file.getName().startsWith(".")) || file.getName().equals(".minecraft"))
fileAdapter.add(file);
continue;
}
for(String suffix : fileSuffixes){ for(File file : listFile){
if(file.getName().endsWith("." + suffix)){ if(file.isDirectory()){
fileAdapter.add(file); if(showFolders && ((!file.getName().startsWith(".")) || file.getName().equals(".minecraft")))
break; fileAdapter.add(file);
} continue;
}
} }
}else{ //We get every file
for(File file : listFile){ if(showFiles){
fileAdapter.add(file); if(fileSuffixes.length > 0){
for(String suffix : fileSuffixes){
if(file.getName().endsWith("." + suffix)){
fileAdapter.add(file);
break;
}
}
}else {
fileAdapter.add(file);
}
} }
} }
} }
mainLv.setAdapter(fileAdapter); mainLv.setAdapter(fileAdapter);
if (build != null) build.setTitle(new File(path).getName()); if (build != null) build.setTitle(new File(path).getName());
@ -158,16 +160,24 @@ public class FileListView extends LinearLayout
public void refreshPath() { public void refreshPath() {
listFileAt(getFullPath()); listFileAt(getFullPath());
} }
public void parentDir() { public void parentDir() {
File pathFile = new File(fullPath); File pathFile = new File(fullPath);
if(!pathFile.getAbsolutePath().equals("/")){ if(!pathFile.getAbsolutePath().equals("/")){
listFileAt(pathFile.getParent()); listFileAt(pathFile.getParent());
} }
} }
public void lockPathAt(String path) { public void lockPathAt(String path) {
lockPath = path; lockPath = path;
listFileAt(path); listFileAt(path);
} }
public void setShowFiles(boolean showFiles){
this.showFiles = showFiles;
}
public void setShowFolders(boolean showFolders){
this.showFolders = showFolders;
}
} }