Skip to content

Commit

Permalink
Simpler version
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Oct 9, 2023
1 parent 0014bd5 commit c2ec7f7
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 104 deletions.
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 @@ -245,36 +245,31 @@ private String formatShare(final String shared) {
}

private void showAppendDialog(int keyId) {
final File startFolder;
switch (keyId) {
case R.string.pref_key__favourite_files: {
startFolder = GsFileBrowserListAdapter.VIRTUAL_STORAGE_FAVOURITE;
break;
}
case R.string.pref_key__popular_documents: {
startFolder = GsFileBrowserListAdapter.VIRTUAL_STORAGE_POPULAR;
break;
}
case R.string.pref_key__recent_documents: {
startFolder = GsFileBrowserListAdapter.VIRTUAL_STORAGE_RECENTS;
break;
}
default: {
startFolder = _appSettings.getNotebookDirectory();
break;
}
}

MarkorFileBrowserFactory.showFileDialog(new GsFileBrowserOptions.SelectionListenerAdapter() {
@Override
public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
dopt.rootFolder = startFolder;
dopt.newDirButtonEnable = false;
switch (keyId) {
case R.string.pref_key__favourite_files: {
dopt.rootFolder = GsFileBrowserListAdapter.VIRTUAL_STORAGE_FAVOURITE;
break;
}
case R.string.pref_key__popular_documents: {
dopt.rootFolder = GsFileBrowserListAdapter.VIRTUAL_STORAGE_POPULAR;
break;
}
case R.string.pref_key__recent_documents: {
dopt.rootFolder = GsFileBrowserListAdapter.VIRTUAL_STORAGE_RECENTS;
break;
}
default: {
dopt.rootFolder = _appSettings.getNotebookDirectory();
dopt.newDirButtonEnable = true;
dopt.createFileCallback = (dir, callback) -> NewFileDialog.newInstance(dir, false, (ok, f) -> {
if (ok && f.isFile()) {
appendToExistingDocumentAndClose(f, true);
}
}
).show(getActivity().getSupportFragmentManager(), NewFileDialog.FRAGMENT_TAG);
break;
}
}
}

@Override
Expand All @@ -286,23 +281,29 @@ 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() {
@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;
}

@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(getActivity().getSupportFragmentManager(), NewFileDialog.FRAGMENT_TAG);
} else {
appendToExistingDocumentAndClose(sel, true);
}
}
}, getParentFragmentManager(), getActivity());
}, getParentFragmentManager(), getActivity(), MarkorFileBrowserFactory.IsMimeText);
}

private void showInDocumentActivity(final Document document) {
Expand All @@ -327,7 +328,10 @@ public Boolean onPreferenceClicked(Preference preference, String key, int keyId)
close = true;
break;
}
case R.string.pref_key__select_create_document:
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: {
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,9 +44,10 @@ 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.newFileButtonImage = R.drawable.ic_add_white_24dp;
opts.homeButtonImage = R.drawable.ic_home_black_24dp;
opts.selectedItemImage = R.drawable.ic_check_black_24dp;
opts.newDirButtonText = R.string.create_folder;
Expand Down Expand Up @@ -85,7 +90,7 @@ public static File[] strlistToArray(List<String> strlist) {
return files;
}

private static void showDialog(FragmentManager fm, GsFileBrowserOptions.Options opts) {
private static void showDialog(final FragmentManager fm, final GsFileBrowserOptions.Options opts) {
GsFileBrowserDialog filesystemViewerDialog = GsFileBrowserDialog.newInstance(opts);
filesystemViewerDialog.show(fm, GsFileBrowserDialog.FRAGMENT_TAG);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,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 All @@ -71,7 +71,6 @@ public static GsFileBrowserDialog newInstance(GsFileBrowserOptions.Options optio
private FloatingActionButton _homeButton;
private FloatingActionButton _buttonSearch;
private FloatingActionButton _buttonNewDir;
private FloatingActionButton _buttonNewFile;
private EditText _searchEdit;

private GsFileBrowserListAdapter _filesystemViewerAdapter;
Expand Down Expand Up @@ -103,13 +102,12 @@ public void onViewCreated(View root, @Nullable Bundle savedInstanceState) {
_buttonCancel = root.findViewById(R.id.ui__filesystem_dialog__button_cancel);
_buttonOk = root.findViewById(R.id.ui__filesystem_dialog__button_ok);
_homeButton = root.findViewById(R.id.ui__filesystem_dialog__home);
_buttonNewFile = root.findViewById(R.id.ui__filesystem_dialog__new_file);
_buttonNewDir = root.findViewById(R.id.ui__filesystem_dialog__new_dir);
_buttonSearch = root.findViewById(R.id.ui__filesystem_dialog__search_button);
_searchEdit = root.findViewById(R.id.ui__filesystem_dialog__search_edit);

_searchEdit.addTextChangedListener(GsTextWatcherAdapter.on(this::changeAdapterFilter));
for (final View v : new View[]{_homeButton, _buttonSearch, _buttonNewDir, _buttonCancel, _buttonOk, _buttonNewFile}) {
for (final View v : new View[]{_homeButton, _buttonSearch, _buttonNewDir, _buttonCancel, _buttonOk}) {
v.setOnClickListener(this::onClicked);
}

Expand Down Expand Up @@ -143,10 +141,6 @@ public void onViewCreated(View root, @Nullable Bundle savedInstanceState) {
_buttonNewDir.setVisibility(_dopt.newDirButtonEnable ? View.VISIBLE : View.GONE);
_buttonNewDir.setColorFilter(rcolor(_dopt.primaryTextColor), android.graphics.PorterDuff.Mode.SRC_ATOP);

_buttonNewFile.setImageResource(_dopt.newFileButtonImage);
_buttonNewFile.setVisibility(_dopt.createFileCallback != null ? View.VISIBLE : View.GONE);
_buttonNewFile.setColorFilter(rcolor(_dopt.primaryTextColor), android.graphics.PorterDuff.Mode.SRC_ATOP);

_searchEdit.setHint(_dopt.searchHint);
_searchEdit.setTextColor(rcolor(_dopt.primaryTextColor));
_searchEdit.setHintTextColor(rcolor(_dopt.secondaryTextColor));
Expand Down Expand Up @@ -174,7 +168,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 Down Expand Up @@ -204,16 +197,13 @@ public void onClicked(final View view) {
}
case R.id.ui__filesystem_dialog__button_cancel: {
onFsViewerNothingSelected(_dopt.requestId);
dismiss();
break;
}
case R.id.ui__filesystem_dialog__new_dir: {
showNewDirDialog();
break;
}
case R.id.ui__filesystem_dialog__new_file: {
showCreateFile();
break;
}
}
}

Expand All @@ -223,17 +213,6 @@ public void onDismiss(final DialogInterface dialog) {
GsContextUtils.instance.showSoftKeyboard(getActivity(), false, _searchEdit);
}

private void showCreateFile() {
// Initially implementing with create and show file
_dopt.createFileCallback.callback(_filesystemViewerAdapter.getCurrentFolder(),
(newFile) -> {
_filesystemViewerAdapter.reloadCurrentFolder();
if (newFile != null) {
_recyclerList.postDelayed(() -> _filesystemViewerAdapter.showAndBlink(newFile, _recyclerList), 200);
}
});
}

private void showNewDirDialog() {
final Activity activity = getActivity();

Expand All @@ -256,14 +235,8 @@ private void showNewDirDialog() {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public void onClick(View view) {
switch (view.getId()) {
case R.id.opoc_filesystem_item__root: {
// A own item was clicked
TagContainer data = (TagContainer) view.getTag();
final TagContainer data = (TagContainer) view.getTag();
if (data != null && data.file != null) {
File file = data.file;
if (_virtualMapping.containsKey(file)) {
Expand Down Expand Up @@ -353,7 +353,7 @@ public void onClick(View view) {
case R.id.ui__filesystem_dialog__button_ok: {
if (_dopt.doSelectMultiple && areItemsSelected()) {
_dopt.listener.onFsViewerMultiSelected(_dopt.requestId, _currentSelection.toArray(new File[0]));
} else if (_dopt.doSelectFolder && (_currentFolder.exists() || isCurrentFolderVirtual())) {
} else {
_dopt.listener.onFsViewerSelected(_dopt.requestId, _currentFolder, null);
}
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#########################################################*/
package net.gsantner.opoc.frontend.filebrowser;

import android.app.Activity;
import android.content.Context;
import android.os.Environment;

Expand Down Expand Up @@ -77,13 +76,6 @@ public static class Options {

public GsCallback.b2<Context, File> fileOverallFilter = (context, file) -> true;

/**
* First argument is the directory in which the file is to be created
* Second argument is a callback which can be called after the file is created.
* Calling this callback is optional. It will show and blink the created file.
*/
public GsCallback.a2<File, GsCallback.a1<File>> createFileCallback = null;

@StringRes
public int cancelButtonText = android.R.string.cancel;
@StringRes
Expand All @@ -107,8 +99,6 @@ public static class Options {
@DrawableRes
public int newDirButtonImage = android.R.drawable.ic_menu_add;
@DrawableRes
public int newFileButtonImage = android.R.drawable.ic_menu_add;
@DrawableRes
public int folderImage = android.R.drawable.ic_menu_view;
@DrawableRes
public int selectedItemImage = android.R.drawable.checkbox_on_background;
Expand Down
20 changes: 3 additions & 17 deletions app/src/main/res/layout/opoc_filesystem_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,10 @@
android:layout_height="wrap_content"
app:fabSize="mini"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@id/ui__filesystem_dialog__new_file"
android:layout_marginRight="10dp"
android:layout_marginBottom="15dp"
android:alpha="0.50"
tools:ignore="ContentDescription"
app:shapeAppearance="@style/ShapeAppearanceOverlay.Material3.FloatingActionButton"/>

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/ui__filesystem_dialog__new_file"
android:src="@android:drawable/ic_input_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="mini"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@id/ui__filesystem_dialog__new_dir"
android:layout_marginRight="10dp"
android:layout_marginBottom="15dp"
android:alpha="0.50"
android:alpha="0.75"
tools:ignore="ContentDescription"
app:shapeAppearance="@style/ShapeAppearanceOverlay.Material3.FloatingActionButton"/>

Expand All @@ -98,7 +84,7 @@
app:layout_constraintBottom_toTopOf="@id/ui__filesystem_dialog__home"
android:layout_marginRight="10dp"
android:layout_marginBottom="15dp"
android:alpha="0.50"
android:alpha="0.75"
tools:ignore="ContentDescription"
app:shapeAppearance="@style/ShapeAppearanceOverlay.Material3.FloatingActionButton"/>

Expand All @@ -112,7 +98,7 @@
app:layout_constraintBottom_toTopOf="@id/ui__filesystem_dialog__buttons"
android:layout_marginRight="10dp"
android:layout_marginBottom="15dp"
android:alpha="0.50"
android:alpha="0.75"
tools:ignore="ContentDescription"
app:shapeAppearance="@style/ShapeAppearanceOverlay.Material3.FloatingActionButton"/>

Expand Down

0 comments on commit c2ec7f7

Please sign in to comment.