Skip to content

Commit

Permalink
Improvements to showing and blinkig a file
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Nov 9, 2023
1 parent 064eb64 commit 867f930
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ public static void launch(Activity activity, File path, Boolean doPreview, Inten
GsContextUtils.instance.animateToActivity(activity, intent, false, null);
}


public static void handleFileClick(Activity activity, File file, Integer lineNumber) {
if (activity != null && file != null) {
if (FormatRegistry.isFileSupported(file)) {
if (activity != null && file != null && file.canRead()) {
if (FormatRegistry.isFileSupported(file) || file.isDirectory()) {
launch(activity, file, null, null, lineNumber);
} else if (GsFileUtils.getFilenameExtension(file).equals(".apk")) {
GsContextUtils.instance.requestApkInstallation(activity, file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public void onClickFab(final View view) {
if (f.isFile()) {
DocumentActivity.launch(MainActivity.this, f, false, null, null);
} else if (f.isDirectory()) {
_notebook.reloadCurrentFolder();
_notebook.getAdapter().showFile(f);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private AlertDialog.Builder makeDialog(final File basedir, final boolean allowCr
return;
}
final String usedFoldername = getFileNameWithoutExtension(fileNameEdit.getText().toString().trim(), templateSpinner.getSelectedItemPosition());
File f = new File(basedir, usedFoldername);
final File f = new File(basedir, usedFoldername);
if (cu.isUnderStorageAccessFolder(getContext(), f, true)) {
DocumentFile dof = cu.getDocumentFile(getContext(), f, true);
callback(dof != null && dof.exists(), f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,7 @@ private void showNewDirDialog() {
dopt.titleText = _dopt.newDirButtonText;
dopt.textColor = rcolor(_dopt.primaryTextColor);
dopt.searchHintText = android.R.string.untitled;
dopt.callback = (name) -> {
final File file = _filesystemViewerAdapter.createDirectoryHere(name);
if (file != null) {
_recyclerList.postDelayed(() -> _filesystemViewerAdapter.showAndBlink(file, _recyclerList), 200);
}
};
dopt.callback = (name) -> _filesystemViewerAdapter.createDirectoryHere(name, true);

GsSearchOrCustomTextDialog.showMultiChoiceDialogWithSearchFilterUI(activity, dopt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void onViewCreated(@NonNull View root, @Nullable Bundle savedInstanceStat
_recyclerList.addItemDecoration(dividerItemDecoration);
_previousNotebookDirectory = _appSettings.getNotebookDirectory();

_filesystemViewerAdapter = new GsFileBrowserListAdapter(_dopt, context, _recyclerList);
_filesystemViewerAdapter = new GsFileBrowserListAdapter(_dopt, context);
_recyclerList.setAdapter(_filesystemViewerAdapter);
_filesystemViewerAdapter.getFilter().filter("");
onFsViewerDoUiUpdate(_filesystemViewerAdapter);
Expand Down Expand Up @@ -525,7 +525,7 @@ private void executeSearchAction() {
onFsViewerSelected("", load, lineNumber);
}
} else {
showFile(load);
_filesystemViewerAdapter.showFile(load);
}
});
}
Expand Down Expand Up @@ -642,45 +642,6 @@ private void importFileToCurrentDirectory(Context context, File sourceFile) {
Toast.makeText(context, getString(R.string.import_) + ": " + sourceFile.getName(), Toast.LENGTH_LONG).show();
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);

}

// Switch to folder and show the file
public void showFile(final File file) {
final GsFileBrowserListAdapter adapter = getAdapter();
if (adapter == null || !file.exists()) {
return;
}

final File dir = file.getParentFile();
if (dir == null) {
return;
}

final File current = adapter.getCurrentFolder();

if (!current.equals(dir)) {
// Wait up to 2s for the folder to load
final long init = System.currentTimeMillis();
adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onChanged() {
super.onChanged();
if ((System.currentTimeMillis() - init) < 2000) {
_recyclerList.postDelayed(() -> adapter.showAndBlink(file, _recyclerList), 250);
}
adapter.unregisterAdapterDataObserver(this);
}
});
adapter.setCurrentFolder(dir);
} else {
adapter.showAndBlink(file, _recyclerList);
}
}

public void setCurrentFolder(final File folder) {
if (folder != null && folder.canRead() && _filesystemViewerAdapter != null) {
_filesystemViewerAdapter.setCurrentFolder(folder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,21 @@ public class GsFileBrowserListAdapter extends RecyclerView.Adapter<GsFileBrowser
private final Context _context;
private StringFilter _filter;
private final HashMap<File, File> _virtualMapping = new HashMap<>();
private final RecyclerView _recyclerView;
private RecyclerView _recyclerView;
private final SharedPreferences _prefApp;

//########################
//## Methods
//########################

public GsFileBrowserListAdapter(GsFileBrowserOptions.Options options, Context context) {
this(options, context, null);
}

public GsFileBrowserListAdapter(GsFileBrowserOptions.Options options, Context context, RecyclerView recyclerView) {
_dopt = options;
_adapterData = new ArrayList<>();
_adapterDataFiltered = new ArrayList<>();
_currentSelection = new HashSet<>();
_context = context;
loadFolder((options.startFolder != null) ? options.startFolder : options.rootFolder);
_recyclerView = recyclerView;
_prefApp = _context.getSharedPreferences("app", Context.MODE_PRIVATE);

// Prevents view flicker - https://stackoverflow.com/a/32488059
Expand Down Expand Up @@ -202,6 +198,12 @@ public void onBindViewHolder(@NonNull FilesystemViewerViewHolder holder, int pos
holder.itemRoot.setOnLongClickListener(this);
}

@Override
public void onAttachedToRecyclerView(final RecyclerView view) {
super.onAttachedToRecyclerView(view);
_recyclerView = view;
}

public String formatFileDescription(final File file, String format) {
if (TextUtils.isEmpty(format)) {
return DateUtils.formatDateTime(_context, file.lastModified(), (DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_NUMERIC_DATE));
Expand Down Expand Up @@ -460,7 +462,7 @@ public boolean onLongClick(View view) {
return false;
}

public File createDirectoryHere(final CharSequence name) {
public File createDirectoryHere(final CharSequence name, final boolean show) {
if (name == null || _currentFolder == null || !_currentFolder.canWrite()) {
return null;
}
Expand All @@ -474,7 +476,11 @@ public File createDirectoryHere(final CharSequence name) {
try {
final File file = new File(_currentFolder, trimmed);
if (file.exists() || file.mkdir()) {
reloadCurrentFolder();
if (show) {
showFile(file);
} else {
reloadCurrentFolder();
}
return file;
}
} catch (SecurityException ignored) {
Expand All @@ -484,19 +490,48 @@ public File createDirectoryHere(final CharSequence name) {
return null;
}

// Switch to folder and show the file
public void showFile(final File file) {
if (file == null || !file.exists() || _recyclerView == null) {
return;
}

final File dir = file.getParentFile();
if (dir == null) {
return;
}

if (getFilePosition(file) < 0) {
// Wait up to 2s for the folder to load or reload
final long init = System.currentTimeMillis();
registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onChanged() {
super.onChanged();
if ((System.currentTimeMillis() - init) < 2000) {
_recyclerView.postDelayed(() -> showAndBlink(file), 250);
}
unregisterAdapterDataObserver(this);
}
});
loadFolder(dir); // Will reload folder if necessary
} else {
showAndBlink(file);
}
}

/**
* Show a file in the current folder and blink it
*
* @param file File to blink
* @param recycler RecyclerView which holds the file items
*/
public void showAndBlink(final File file, final RecyclerView recycler) {
private void showAndBlink(final File file) {
final int pos = getFilePosition(file);
final LinearLayoutManager manager = (LinearLayoutManager) recycler.getLayoutManager();
final LinearLayoutManager manager = (LinearLayoutManager) _recyclerView.getLayoutManager();
if (manager != null && pos >= 0) {
manager.scrollToPositionWithOffset(pos, 1);
recycler.postDelayed(() -> {
final RecyclerView.ViewHolder holder = recycler.findViewHolderForLayoutPosition(pos);
_recyclerView.postDelayed(() -> {
final RecyclerView.ViewHolder holder = _recyclerView.findViewHolderForLayoutPosition(pos);
if (holder != null) {
GsContextUtils.blinkView(holder.itemView);
}
Expand Down

0 comments on commit 867f930

Please sign in to comment.