Skip to content

Commit

Permalink
Cleanups to code
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Jun 8, 2024
1 parent ec4de19 commit fb65503
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import androidx.recyclerview.widget.RecyclerView;

import net.gsantner.markor.R;
import net.gsantner.markor.frontend.textview.TextViewUtils;
import net.gsantner.opoc.format.GsTextUtils;
import net.gsantner.opoc.util.GsCollectionUtils;
import net.gsantner.opoc.util.GsContextUtils;
Expand All @@ -58,6 +59,8 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import kotlin.reflect.KCallable;

@SuppressWarnings({"WeakerAccess", "unused"})
public class GsFileBrowserListAdapter extends RecyclerView.Adapter<GsFileBrowserListAdapter.FilesystemViewerViewHolder> implements Filterable, View.OnClickListener, View.OnLongClickListener, FilenameFilter {
//########################
Expand Down Expand Up @@ -537,19 +540,22 @@ public File createDirectoryHere(final CharSequence name, final boolean show) {
return null;
}

Runnable _afterRender = null;

@Override
public void onViewRecycled(@NonNull final FilesystemViewerViewHolder holder) {
super.onViewRecycled(holder);
if (_afterRender != null) {
_afterRender.run();
}
}

public void doAfterChange(final GsCallback.a0 callback) {
final long init = System.currentTimeMillis();
registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onChanged() {
super.onChanged();
// Ignore if the load takes too long
if ((System.currentTimeMillis() - init) < 5000) {
_recyclerView.post(callback::callback);
}
unregisterAdapterDataObserver(this);
}
_afterRender = TextViewUtils.makeDebounced(_recyclerView.getHandler(), 250, () -> {
_recyclerView.post(callback::callback);
_afterRender = null;
});
_afterRender.run();
}

// Switch to folder and show the file
Expand Down Expand Up @@ -581,23 +587,23 @@ private void showAndFlash(final File file) {

if (pos >= 0 && _layoutManager != null) {

// Scroll to position if needed
final GsCallback.a0 blink = () -> {
final RecyclerView.ViewHolder holder = _recyclerView.findViewHolderForLayoutPosition(pos);
if (holder != null) {
GsContextUtils.blinkView(holder.itemView);
}
};

final int firstVisible = _layoutManager.findFirstCompletelyVisibleItemPosition();
final int lastVisible = _layoutManager.findLastCompletelyVisibleItemPosition();
final int delay;
if (pos < firstVisible || pos > lastVisible) {
// Scroll to position if needed and call.
// The delay works better than a listener on the scroll state
_layoutManager.scrollToPositionWithOffset(pos, 1);
delay = 500;
_recyclerView.postDelayed(blink::callback, 500);
} else {
delay = 100;
blink.callback();
}

_recyclerView.postDelayed(() -> {
final RecyclerView.ViewHolder holder = _recyclerView.findViewHolderForLayoutPosition(pos);
if (holder != null) {
GsContextUtils.blinkView(holder.itemView);
}
}, delay);
}
}

Expand All @@ -620,7 +626,7 @@ private void loadFolder(final File folder) {
loadFolder(folder, true);
}

private void loadFolder(final File folder, final boolean showChild) {
private void loadFolder(final File folder, final boolean restorePosition) {
final boolean folderChanged = !folder.equals(_currentFolder);
if (folderChanged && _currentFolder != null && _layoutManager != null) {
_folderScrollMap.put(_currentFolder, _layoutManager.onSaveInstanceState());
Expand Down Expand Up @@ -724,23 +730,22 @@ private void loadFolder(final File folder, final boolean showChild) {
final File folderToShow = _currentFolder;
_currentFolder = folder;

// Must be called from UI thread
_recyclerView.post(() -> {

if (folderChanged && showChild) {
doAfterChange(() -> {
final Parcelable scrollState = _folderScrollMap.remove(folder);
if (scrollState != null && _layoutManager != null) {
_layoutManager.onRestoreInstanceState(scrollState);
}
// We still show and flash as the folder contents may have changed
_recyclerView.postDelayed(() -> showAndFlash(folderToShow), 250);
});
}

// Must be called from UI thread
// TODO - add logic to notify the changed bits
notifyDataSetChanged();

if (folderChanged && restorePosition) {
final Parcelable scrollState = _folderScrollMap.remove(folder);
if (scrollState != null && _layoutManager != null) {
_layoutManager.onRestoreInstanceState(scrollState);
}

if (GsFileUtils.isChild(_currentFolder, folderToShow)) {
doAfterChange(() -> showAndFlash(folderToShow));
}
}

if (_dopt.listener != null) {
_dopt.listener.onFsViewerDoUiUpdate(GsFileBrowserListAdapter.this);
}
Expand Down Expand Up @@ -838,7 +843,7 @@ protected void publishResults(CharSequence constraint, FilterResults results) {
}

@SuppressWarnings({"WeakerAccess", "unused"})
static class FilesystemViewerViewHolder extends RecyclerView.ViewHolder {
public static class FilesystemViewerViewHolder extends RecyclerView.ViewHolder {
//########################
//## UI Binding
//########################
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/net/gsantner/opoc/util/GsFileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ public static boolean canCreate(final File file) {
* @return if parent is a child of test
*/
public static boolean isChild(final File parent, File test) {
if (parent.equals(test)) {
if (test == null || parent == null || parent.equals(test)) {
return false;
}

Expand Down

0 comments on commit fb65503

Please sign in to comment.