Skip to content

Commit e5bacd6

Browse files
authored
File filters and sorting (README docs)
1 parent d191575 commit e5bacd6

1 file changed

Lines changed: 47 additions & 55 deletions

File tree

README.md

Lines changed: 47 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Key features in the library include the following:
4646

4747
<img src="https://raw.githubusercontent.com/maxieds/AndroidFileChooserLight/master/Screenshots/WorkingUI-Screenshot_20201112-052224.png" width="250" /> <img src="https://raw.githubusercontent.com/maxieds/AndroidFileChooserLight/master/Screenshots/WorkingUI-Screenshot_20201113-134724.png" width="250" /> <img src="https://raw.githubusercontent.com/maxieds/AndroidFilePickerLight/master/Screenshots/SampleApplicationDemo-ProgressBarDisplay.png" width="250" />
4848

49-
## Including the library in an Android application
49+
## Including the library for use in a client Android application
5050

5151
There are a couple of quickstart items covered in the sections below to handle before this
5252
library can be included in the client Android application:
@@ -189,70 +189,62 @@ These can be set using the ``AndroidFilePickerLight.Builder`` class as follows:
189189

190190
### Extending file types for filtering and sorting purposes in the picker UI
191191

192+
Many other good file chooser libraries for Android implement extendable ways for users to filter,
193+
select and sort the files that are presented to the user. We choose to offer the same extendable
194+
functionality here while staying tightly coupled with more Java language standard constructs.
192195

193-
#### Overriding the default file and directory sorting (TODO)
194-
195-
An example of how to do this is already seen by glancing through the sources.
196-
In particular, we define an extendable base class as follows:
196+
The following is an example of how to create a custom file filter for use with this library.
197+
The full interface specification is found in the source file
198+
[FileFilter.java](https://github.com/maxieds/AndroidFileChooserLight/blob/master/AndroidFilePickerLightLibrary/src/main/java/com/maxieds/androidfilepickerlightlibrary/FileFilter.java#L35):
197199
```java
198-
public static class FileItemsSortFunc implements Comparator<File> {
199-
public File[] sortFileItemsList(File[] folderContentsList) {
200-
Arrays.sort(folderContentsList, this);
201-
return folderContentsList;
202-
}
203-
@Override
204-
public int compare(File f1, File f2) {
205-
// default is standard lexicographical ordering (override the compare functor base classes for customized sorting):
206-
return f1.getAbsolutePath().compareTo(f2.getAbsolutePath());
200+
public static class FileFilterByRegex extends FileFilterBase {
201+
private Pattern patternSpec;
202+
public FileFilterByRegex(String regexPatternSpec, boolean inclExcl) {
203+
patternSpec = Pattern.compile(regexPatternSpec);
204+
setIncludeExcludeMatchesOption(inclExcl);
205+
}
206+
public boolean fileMatchesFilter(String fileAbsName) {
207+
if(patternSpec.matcher(fileAbsName).matches()) {
208+
return includeExcludeMatches == INCLUDE_FILES_IN_FILTER_PATTERN;
207209
}
210+
return includeExcludeMatches == EXCLUDE_FILES_IN_FILTER_PATTERN;
208211
}
212+
}
209213
```
210-
Subclasses that override the method above are free to sort the file
211-
contents in the order that they are best displayed in the client application.
212-
Some examples would be to override the default of placing directories at the top of the
213-
files list, to perform a case-insensitive lexicographical sort (like happens on many Linux),
214-
or otherwise to prioritize the file rankings where the most importantly valued presentations
215-
are shown first.
216-
217-
Note that in the source the custom objects to filter (match, then include/exclude) the full
218-
directory listings, and then sort those files that remain are applied together. See, for example,
219-
the next code:
214+
The main interface in the base class for the example above extends the stock Java ``FilenameFilter``
215+
interface. There is a difference in what our derived classes must implement. Namely, subject to the
216+
next defines, the code can decide whether to include or exclude the file matches based on whether the
217+
filename filter matches the user specified pattern:
220218
```java
221-
public static class FileFilterByRegex extends FileFilterBase {
222-
private Pattern patternSpec;
223-
public FileFilterByRegex(String regexPatternSpec, boolean inclExcl) {
224-
patternSpec = Pattern.compile(regexPatternSpec);
225-
setIncludeExcludeMatchesOption(inclExcl);
226-
}
227-
public boolean fileMatchesFilter(String fileAbsName) {
228-
if(patternSpec.matcher(fileAbsName).matches()) {
229-
return includeExcludeMatches == INCLUDE_FILES_IN_FILTER_PATTERN;
230-
}
231-
return includeExcludeMatches == EXCLUDE_FILES_IN_FILTER_PATTERN;
232-
}
233-
}
219+
static final boolean INCLUDE_FILES_IN_FILTER_PATTERN = FileChooserBuilder.INCLUDE_FILES_IN_FILTER_PATTERN;
220+
static final boolean EXCLUDE_FILES_IN_FILTER_PATTERN = FileChooserBuilder.EXCLUDE_FILES_IN_FILTER_PATTERN;
234221
```
235-
236-
#### Extending the inclusion/exclusion mechanism of files by type
237-
238-
The specification (by Java interface and a few utility derived instances) are found in the
239-
library source file ``FileFilter.java``. The bulk of the interface is reproduced as
240-
follows:
222+
Similarly, an overloaded sorting class that can be extended is sampled below:
241223
```java
242-
public interface FileFilterInterface {
243-
244-
static final boolean INCLUDE_FILES_IN_FILTER_PATTERN = FilePickerBuilder.INCLUDE_FILES_IN_FILTER_PATTERN;
245-
static final boolean EXCLUDE_FILES_IN_FILTER_PATTERN = FilePickerBuilder.EXCLUDE_FILES_IN_FILTER_PATTERN;
246-
247-
void setIncludeExcludeMatchesOption(boolean includeExcludeParam);
248-
boolean includeFileInSearchResults(FileTypes.FileType fileItem);
249-
boolean fileMatchesFilter(FileTypes.FileType fileItem);
250-
224+
public static class FileItemsSortFunc implements Comparator<File> {
225+
public File[] sortFileItemsList(File[] folderContentsList) {
226+
Arrays.sort(folderContentsList, this);
227+
return folderContentsList;
228+
}
229+
@Override
230+
public int compare(File f1, File f2) {
231+
// default is standard lexicographical ordering (override the compare functor base classes for customized sorting):
232+
return f1.getAbsolutePath().compareTo(f2.getAbsolutePath());
233+
}
251234
}
252235
```
253-
254-
255-
236+
Here is an example of how to utilize these customized classes with the library's core
237+
``FileChooserBuilder`` class instances:
238+
```java
239+
FileChooserBuilder fcConfig = new FileChooserBuilder();
240+
fcConfig.setFilesListSortCompareFunction(FileFilter.FileItemsSortFunc);
241+
// TODO
242+
243+
// Some defaults for convenience:
244+
fcConfig.filterByDefaultFileTypes(List<DefaultFileTypes> fileTypesList, boolean includeExcludeInList);
245+
fcConfig.filterByMimeTypes(List<String> fileTypesList, boolean includeExcludeInList);
246+
fcConfig.filterByRegex(String fileFilterPattern, boolean includeExcludeInList);
247+
```
256248

257249
### Configuring the client theme and UI look-and-feel properties
258250

0 commit comments

Comments
 (0)