Skip to content

Commit

Permalink
New file in file browser dialog (PR #2120 @harshad1)
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 authored Nov 14, 2023
1 parent 06da44a commit 6ffe87d
Show file tree
Hide file tree
Showing 17 changed files with 189 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,20 @@ public static void launch(Activity activity, File path, Boolean doPreview, Inten
}

public static void handleFileClick(Activity activity, File file, Integer lineNumber) {
if (activity != null && file != null) {
if (FormatRegistry.isFileSupported(file)) {
if (activity == null || file == null) {
return;
}

if (file.isDirectory()) {
if (file.canRead()) {
launch(activity, file, null, null, lineNumber);
} else if (GsFileUtils.getFilenameExtension(file).equals(".apk")) {
GsContextUtils.instance.requestApkInstallation(activity, file);
} else {
askUserIfWantsToOpenFileInThisApp(activity, file);
}
} else if (FormatRegistry.isFileSupported(file) && GsFileUtils.canCreate(file)) {
launch(activity, file, null, null, lineNumber);
} else if (GsFileUtils.getFilenameExtension(file).equals(".apk")) {
GsContextUtils.instance.requestApkInstallation(activity, file);
} else {
askUserIfWantsToOpenFileInThisApp(activity, file);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ public class DocumentShareIntoFragment extends MarkorBaseFragment {
public static final String TEXT_TOKEN = "{{text}}";

public static DocumentShareIntoFragment newInstance(Intent intent) {
DocumentShareIntoFragment f = new DocumentShareIntoFragment();
Bundle args = new Bundle();
final DocumentShareIntoFragment f = new DocumentShareIntoFragment();
final Bundle args = new Bundle();

final String sharedText = formatLink(intent.getStringExtra(Intent.EXTRA_SUBJECT), intent.getStringExtra(Intent.EXTRA_TEXT));

Object intentFile = intent.getSerializableExtra(Document.EXTRA_PATH);
final Object intentFile = intent.getSerializableExtra(Document.EXTRA_PATH);
if (intentFile instanceof File && ((File) intentFile).isDirectory()) {
f.workingDir = (File) intentFile;
}
Expand Down Expand Up @@ -219,12 +219,12 @@ private void appendToExistingDocumentAndClose(final File file, final boolean sho
Toast.makeText(context, R.string.error_could_not_open_file, Toast.LENGTH_LONG).show();
}

_appSettings.addRecentDocument(file);
if (showEditor) {
showInDocumentActivity(document);
} else {
context.finish();
}
_appSettings.addRecentDocument(file);

context.finish();
}

private String formatShare(final String shared) {
Expand Down Expand Up @@ -264,6 +264,7 @@ private void showAppendDialog(int keyId) {
break;
}
}

MarkorFileBrowserFactory.showFileDialog(new GsFileBrowserOptions.SelectionListenerAdapter() {
@Override
public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
Expand All @@ -280,23 +281,39 @@ public void onFsViewerSelected(String request, File file, final Integer lineNumb
}


private void createNewDocument() {
MarkorFileBrowserFactory.showFolderDialog(new GsFileBrowserOptions.SelectionListenerAdapter() {
private void createSelectNewDocument() {
MarkorFileBrowserFactory.showFileDialog(new GsFileBrowserOptions.SelectionListenerAdapter() {
GsFileBrowserOptions.Options _dopt = null;

@Override
public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
dopt.rootFolder = (workingDir == null) ? _appSettings.getNotebookDirectory() : workingDir;
dopt.rootFolder = _appSettings.getNotebookDirectory();
dopt.startFolder = workingDir;
dopt.okButtonText = R.string.create_new_document;
dopt.okButtonEnable = true;
dopt.dismissAfterCallback = false;
_dopt = dopt;
}

@Override
public void onFsViewerSelected(String request, File dir, final Integer lineNumber) {
NewFileDialog dialog = NewFileDialog.newInstance(dir, false, (ok, f) -> {
if (ok && f.isFile()) {
appendToExistingDocumentAndClose(f, true);
}
});
dialog.show(getActivity().getSupportFragmentManager(), NewFileDialog.FRAGMENT_TAG);
public void onFsViewerSelected(final String request, final File sel, final Integer lineNumber) {
if (sel.isDirectory()) {
NewFileDialog.newInstance(sel, false, (ok, f) -> {
if (ok && f.isFile()) {
appendToExistingDocumentAndClose(f, true);
}
}).show(getChildFragmentManager(), NewFileDialog.FRAGMENT_TAG);
} else {
appendToExistingDocumentAndClose(sel, true);
}
}
}, getFragmentManager(), getActivity());

@Override
public void onFsViewerCancel(final String request) {
// Will cause the dialog to dismiss after this callback
_dopt.dismissAfterCallback = true;
}
}, getParentFragmentManager(), getActivity(), MarkorFileBrowserFactory.IsMimeText);
}

private void showInDocumentActivity(final Document document) {
Expand All @@ -321,14 +338,13 @@ public Boolean onPreferenceClicked(Preference preference, String key, int keyId)
close = true;
break;
}
case R.string.pref_key__share_into__create_document: {
createNewDocument();
case R.string.pref_key__select_create_document: {
createSelectNewDocument();
return true;
}
case R.string.pref_key__favourite_files:
case R.string.pref_key__popular_documents:
case R.string.pref_key__recent_documents:
case R.string.pref_key__share_into__existing_document: {
case R.string.pref_key__recent_documents: {
showAppendDialog(keyId);
return true;
}
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 @@ -88,7 +88,7 @@ public class FormatRegistry {
};

public static boolean isFileSupported(final File file, final boolean... textOnly) {
boolean textonly = textOnly != null && textOnly.length > 0 && textOnly[0];
final boolean textonly = textOnly != null && textOnly.length > 0 && textOnly[0];
if (file != null) {
final String filepath = file.getAbsolutePath().toLowerCase(Locale.ROOT);
for (TextConverterBase converter : CONVERTERS) {
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 @@ -29,7 +29,11 @@ public class MarkorFileBrowserFactory {
public static GsCallback.b2<Context, File> IsMimeAudio = (context, file) -> file != null && GsContextUtils.instance.getMimeType(context, file).startsWith("audio/");
public static GsCallback.b2<Context, File> IsMimeVideo = (context, file) -> file != null && GsContextUtils.instance.getMimeType(context, file).startsWith("video/");

public static GsFileBrowserOptions.Options prepareFsViewerOpts(Context context, boolean doSelectFolder, GsFileBrowserOptions.SelectionListener listener) {
public static GsFileBrowserOptions.Options prepareFsViewerOpts(
final Context context,
final boolean doSelectFolder,
final GsFileBrowserOptions.SelectionListener listener
) {
final GsFileBrowserOptions.Options opts = new GsFileBrowserOptions.Options();
final MarkorContextUtils cu = new MarkorContextUtils(context);
final AppSettings appSettings = ApplicationObject.settings();
Expand All @@ -40,6 +44,8 @@ public static GsFileBrowserOptions.Options prepareFsViewerOpts(Context context,
opts.doSelectFolder = doSelectFolder;
opts.doSelectFile = !doSelectFolder;

opts.okButtonEnable = opts.doSelectFolder || opts.doSelectMultiple;

opts.searchButtonImage = R.drawable.ic_search_black_24dp;
opts.newDirButtonImage = R.drawable.baseline_create_new_folder_24;
opts.homeButtonImage = R.drawable.ic_home_black_24dp;
Expand Down Expand Up @@ -84,20 +90,30 @@ public static File[] strlistToArray(List<String> strlist) {
return files;
}

private static void showDialog(FragmentManager fm, GsFileBrowserOptions.Options opts) {
GsFileBrowserDialog filesystemViewerDialog = GsFileBrowserDialog.newInstance(opts);
private static GsFileBrowserDialog showDialog(final FragmentManager fm, final GsFileBrowserOptions.Options opts) {
final GsFileBrowserDialog filesystemViewerDialog = GsFileBrowserDialog.newInstance(opts);
filesystemViewerDialog.show(fm, GsFileBrowserDialog.FRAGMENT_TAG);
return filesystemViewerDialog;
}

public static void showFileDialog(GsFileBrowserOptions.SelectionListener listener, FragmentManager fm, Context context, GsCallback.b2<Context, File> fileOverallFilter) {
public static GsFileBrowserDialog showFileDialog(
final GsFileBrowserOptions.SelectionListener listener,
final FragmentManager fm,
final Context context,
final GsCallback.b2<Context, File> fileOverallFilter
) {
final GsFileBrowserOptions.Options opts = prepareFsViewerOpts(context, false, listener);
opts.fileOverallFilter = fileOverallFilter;
showDialog(fm, opts);
return showDialog(fm, opts);
}

public static void showFolderDialog(GsFileBrowserOptions.SelectionListener listener, FragmentManager fm, Context context) {
public static GsFileBrowserDialog showFolderDialog(
final GsFileBrowserOptions.SelectionListener listener,
final FragmentManager fm,
final Context context
) {
final GsFileBrowserOptions.Options opts = prepareFsViewerOpts(context, true, listener);
opts.okButtonText = R.string.select_this_folder;
showDialog(fm, opts);
return showDialog(fm, opts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import net.gsantner.opoc.wrapper.GsCallback;
import net.gsantner.opoc.wrapper.GsTextWatcherAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -53,8 +52,8 @@ public class GsFileBrowserDialog extends DialogFragment implements GsFileBrowser
//########################
public static final String FRAGMENT_TAG = "FilesystemViewerCreator";

public static GsFileBrowserDialog newInstance(GsFileBrowserOptions.Options options) {
GsFileBrowserDialog f = new GsFileBrowserDialog();
public static GsFileBrowserDialog newInstance(final GsFileBrowserOptions.Options options) {
final GsFileBrowserDialog f = new GsFileBrowserDialog();
f.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
options.listener.onFsViewerConfig(options);
f.setDialogOptions(options);
Expand Down Expand Up @@ -93,9 +92,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
}

@Override
public void onViewCreated(View root, @Nullable Bundle savedInstanceState) {
public void onViewCreated(final View root, final @Nullable Bundle savedInstanceState) {
super.onViewCreated(root, savedInstanceState);
Context context = getContext();
final Activity activity = getActivity();

_recyclerList = root.findViewById(R.id.ui__filesystem_dialog__list);
_dialogTitle = root.findViewById(R.id.ui__filesystem_dialog__title_text);
Expand Down Expand Up @@ -151,10 +150,10 @@ public void onViewCreated(View root, @Nullable Bundle savedInstanceState) {
root.setBackgroundColor(rcolor(_dopt.backgroundColor));

final LinearLayoutManager lam = (LinearLayoutManager) _recyclerList.getLayoutManager();
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getActivity(), lam.getOrientation());
final DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(activity, lam.getOrientation());
_recyclerList.addItemDecoration(dividerItemDecoration);

_filesystemViewerAdapter = new GsFileBrowserListAdapter(_dopt, context);
_filesystemViewerAdapter = new GsFileBrowserListAdapter(_dopt, activity);
_recyclerList.setAdapter(_filesystemViewerAdapter);
_filesystemViewerAdapter.getFilter().filter("");
onFsViewerDoUiUpdate(_filesystemViewerAdapter);
Expand All @@ -168,7 +167,6 @@ private void setDialogOptions(GsFileBrowserOptions.Options options) {
_dopt = options;
_callback = _dopt.listener;
_dopt.listener = this;
checkOptions();
}

public void changeAdapterFilter(CharSequence s, int start, int before, int count) {
Expand All @@ -177,7 +175,7 @@ public void changeAdapterFilter(CharSequence s, int start, int before, int count
}
}

public void onClicked(View view) {
public void onClicked(final View view) {
switch (view.getId()) {
case R.id.ui__filesystem_dialog__button_ok:
case R.id.ui__filesystem_dialog__home: {
Expand All @@ -197,7 +195,7 @@ public void onClicked(View view) {
break;
}
case R.id.ui__filesystem_dialog__button_cancel: {
onFsViewerNothingSelected(_dopt.requestId);
onFsViewerCancel(_dopt.requestId);
break;
}
case R.id.ui__filesystem_dialog__new_dir: {
Expand Down Expand Up @@ -225,44 +223,39 @@ 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), 100);
}
};
dopt.callback = (name) -> _filesystemViewerAdapter.createDirectoryHere(name, true);

GsSearchOrCustomTextDialog.showMultiChoiceDialogWithSearchFilterUI(activity, dopt);
}

private void checkOptions() {
if (_dopt.doSelectFile && !_dopt.doSelectMultiple) {
_dopt.okButtonEnable = false;
}
}

@Override
public void onFsViewerSelected(String request, File file, final Integer lineNumber) {
public void onFsViewerSelected(final String request, final File file, final Integer lineNumber) {
if (_callback != null) {
_callback.onFsViewerSelected(_dopt.requestId, file, lineNumber);
}
dismiss();
if (_dopt.dismissAfterCallback) {
dismiss();
}
}

@Override
public void onFsViewerMultiSelected(String request, File... files) {
if (_callback != null) {
_callback.onFsViewerMultiSelected(_dopt.requestId, files);
}
dismiss();
if (_dopt.dismissAfterCallback) {
dismiss();
}
}

@Override
public void onFsViewerNothingSelected(String request) {
public void onFsViewerCancel(String request) {
if (_callback != null) {
_callback.onFsViewerNothingSelected(_dopt.requestId);
_callback.onFsViewerCancel(_dopt.requestId);
}
if (_dopt.dismissAfterCallback) {
dismiss();
}
dismiss();
}

@Override
Expand Down
Loading

0 comments on commit 6ffe87d

Please sign in to comment.