Skip to content

Commit

Permalink
New file in file browser dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Oct 9, 2023
1 parent 8f2e0b2 commit 0014bd5
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 51 deletions.
Original file line number Diff line number Diff line change
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 All @@ -245,30 +245,36 @@ 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 Down Expand Up @@ -296,7 +302,7 @@ public void onFsViewerSelected(String request, File dir, final Integer lineNumbe
});
dialog.show(getActivity().getSupportFragmentManager(), NewFileDialog.FRAGMENT_TAG);
}
}, getFragmentManager(), getActivity());
}, getParentFragmentManager(), getActivity());
}

private void showInDocumentActivity(final Document document) {
Expand All @@ -321,14 +327,10 @@ public Boolean onPreferenceClicked(Preference preference, String key, int keyId)
close = true;
break;
}
case R.string.pref_key__share_into__create_document: {
createNewDocument();
return true;
}
case R.string.pref_key__select_create_document:
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 @@ -42,6 +42,7 @@ public static GsFileBrowserOptions.Options prepareFsViewerOpts(Context context,

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
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 @@ -71,6 +71,7 @@ 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 @@ -102,12 +103,13 @@ 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}) {
for (final View v : new View[]{_homeButton, _buttonSearch, _buttonNewDir, _buttonCancel, _buttonOk, _buttonNewFile}) {
v.setOnClickListener(this::onClicked);
}

Expand Down Expand Up @@ -141,6 +143,10 @@ 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 @@ -177,7 +183,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 @@ -204,6 +210,10 @@ public void onClicked(View view) {
showNewDirDialog();
break;
}
case R.id.ui__filesystem_dialog__new_file: {
showCreateFile();
break;
}
}
}

Expand All @@ -213,6 +223,17 @@ 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 @@ -228,7 +249,7 @@ private void showNewDirDialog() {
dopt.callback = (name) -> {
final File file = _filesystemViewerAdapter.createDirectoryHere(name);
if (file != null) {
_recyclerList.postDelayed(() -> _filesystemViewerAdapter.showAndBlink(file, _recyclerList), 100);
_recyclerList.postDelayed(() -> _filesystemViewerAdapter.showAndBlink(file, _recyclerList), 200);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,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]));
_dopt.listener.onFsViewerMultiSelected(_dopt.requestId, _currentSelection.toArray(new File[0]));
} else if (_dopt.doSelectFolder && (_currentFolder.exists() || isCurrentFolderVirtual())) {
_dopt.listener.onFsViewerSelected(_dopt.requestId, _currentFolder, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#########################################################*/
package net.gsantner.opoc.frontend.filebrowser;

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

Expand Down Expand Up @@ -76,6 +77,13 @@ 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 @@ -99,6 +107,8 @@ 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
32 changes: 26 additions & 6 deletions app/src/main/res/layout/opoc_filesystem_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,26 @@
android:src="@android:drawable/ic_menu_search"
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_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="20dp"
android:layout_marginBottom="20dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="15dp"
android:alpha="0.50"
tools:ignore="ContentDescription"
app:shapeAppearance="@style/ShapeAppearanceOverlay.Material3.FloatingActionButton"/>

Expand All @@ -77,10 +93,12 @@
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__home"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="15dp"
android:alpha="0.50"
tools:ignore="ContentDescription"
app:shapeAppearance="@style/ShapeAppearanceOverlay.Material3.FloatingActionButton"/>

Expand All @@ -89,10 +107,12 @@
android:src="@android:drawable/btn_star"
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__buttons"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="15dp"
android:alpha="0.50"
tools:ignore="ContentDescription"
app:shapeAppearance="@style/ShapeAppearanceOverlay.Material3.FloatingActionButton"/>

Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/values/string-not_translatable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,6 @@ work. If not, see <https://creativecommons.org/publicdomain/zero/1.0/>.
<string name="pref_key__share_into__quicknote" translatable="false">pref_key__share_into__quicknote</string>
<string name="pref_key__share_into__todo" translatable="false">pref_key__share_into__todo</string>
<string name="pref_key__share_into__clipboard" translatable="false">pref_key__share_into__clipboard</string>
<string name="pref_key__share_into__existing_document" translatable="false">pref_key__share_into__existing_document</string>
<string name="pref_key__share_into__create_document" translatable="false">pref_key__share_into__create_document</string>
<string name="pref_key__share_into__reshare" translatable="false">pref_key__share_into__reshare</string>
<string name="pref_key__share_into__calendar_event" translatable="false">pref_key__share_into__calendar_event</string>
<string name="pref_key__more_info__settings" translatable="false">pref_key__more_info__settings</string>
Expand All @@ -185,6 +183,7 @@ work. If not, see <https://creativecommons.org/publicdomain/zero/1.0/>.
<string name="pref_key__recent_documents" translatable="false">pref_key__recent_documents</string>
<string name="pref_key__popular_documents" translatable="false">pref_key__popular_documents</string>
<string name="pref_key__favourite_files" translatable="false">pref_key__favourite_files</string>
<string name="pref_key__select_create_document" translatable="false">pref_key__select_create_document</string>
<string name="pref_key__share_into__open_in_browser" translatable="false">pref_key__share_into__open_in_browser</string>
<string name="pref_key__inject_to_head" translatable="false">pref_key__inject_to_head</string>
<string name="pref_key__inject_to_body" translatable="false">pref_key__inject_to_body</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ work. If not, see <https://creativecommons.org/publicdomain/zero/1.0/>.
<string name="edit_text_in_center_of_screen">Edit text in the center of the screen</string>
<string name="clipboard">Clipboard</string>
<string name="select_existing_document">Select existing document</string>
<string name="select_create_document">Select or create document</string>
<string name="open_source_licenses">Open Source Licenses</string>
<string name="rate_this_app">Rate this app</string>
<string name="translate">Translate</string>
Expand Down
13 changes: 5 additions & 8 deletions app/src/main/res/xml/prefactions_share_into.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@

<PreferenceCategory android:title="@string/file_browser" />

<Preference
android:icon="@drawable/ic_keyboard_return_black_24dp"
android:key="@string/pref_key__select_create_document"
android:title="@string/select_create_document" />

<Preference
android:icon="@drawable/ic_star_black_24dp"
android:key="@string/pref_key__favourite_files"
Expand All @@ -54,14 +59,6 @@
android:key="@string/pref_key__popular_documents"
android:title="@string/popular_documents" />

<Preference
android:icon="@drawable/ic_keyboard_return_black_24dp"
android:key="@string/pref_key__share_into__existing_document"
android:title="@string/select_existing_document" />
<Preference
android:icon="@drawable/ic_add_white_24dp"
android:key="@string/pref_key__share_into__create_document"
android:title="@string/create_new_document" />


</PreferenceScreen> <!-- Screen -->

0 comments on commit 0014bd5

Please sign in to comment.