Skip to content

Commit

Permalink
Improve document handling, by @harshad1 (PR #1579)
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 authored Feb 5, 2022
1 parent 0812c17 commit 53f8740
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ public class DocumentActivity extends MarkorBaseActivity {
TextView _toolbarTitleText;

private FragmentManager _fragManager;
private Document _document;


private static boolean nextLaunchTransparentBg = false;

Expand Down Expand Up @@ -135,9 +133,6 @@ public static void askUserIfWantsToOpenFileInThisApp(final Activity activity, fi
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppSettings.clearDebugLog();
if (savedInstanceState != null && savedInstanceState.containsKey(DocumentEditFragment.SAVESTATE_DOCUMENT)) {
_document = (Document) savedInstanceState.getSerializable(DocumentEditFragment.SAVESTATE_DOCUMENT);
}
if (nextLaunchTransparentBg) {
//getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT);
nextLaunchTransparentBg = false;
Expand All @@ -163,14 +158,13 @@ protected void onNewIntent(Intent intent) {
handleLaunchingIntent(intent);
}

private void handleLaunchingIntent(Intent intent) {
private void handleLaunchingIntent(final Intent intent) {
if (intent == null) return;

String intentAction = intent.getAction();
Uri intentData = intent.getData();

File file = (File) intent.getSerializableExtra(Document.EXTRA_PATH);
boolean fileIsFolder = intent.getBooleanExtra(Document.EXTRA_PATH_IS_FOLDER, false);

boolean intentIsView = Intent.ACTION_VIEW.equals(intentAction);
boolean intentIsSend = Intent.ACTION_SEND.equals(intentAction);
Expand All @@ -187,11 +181,12 @@ private void handleLaunchingIntent(Intent intent) {
}

if (!intentIsSend && file != null) {
final Document doc = new Document(file);
final int paramLineNumber = intent.getIntExtra(Document.EXTRA_FILE_LINE_NUMBER, (intentData != null ? StringUtils.tryParseInt(intentData.getQueryParameter("line"), -1) : -1));
final boolean paramPreview = (paramLineNumber < 0) && (intent.getBooleanExtra(EXTRA_DO_PREVIEW, false)
|| (file.exists() && file.isFile() && _appSettings.getDocumentPreviewState(file.getPath()))
|| (file.exists() && file.isFile() && _appSettings.getDocumentPreviewState(doc.getPath()))
|| file.getName().startsWith("index."));
showTextEditor(null, file, fileIsFolder, paramPreview, paramLineNumber);
showTextEditor(doc, paramPreview, paramLineNumber);
}
}

Expand Down Expand Up @@ -259,23 +254,16 @@ public void setDocumentTitle(final String title) {
}
}

public void showTextEditor(@Nullable Document document, @Nullable File file, boolean fileIsFolder, boolean preview, final Integer lineNumber) {
public void showTextEditor(final Document document, final boolean preview, final Integer lineNumber) {
GsFragmentBase currentFragment = getCurrentVisibleFragment();
File reqFile = (document != null) ? document.getFile() : file;
final int fileLineNumber = lineNumber != null && lineNumber >= 0 ? lineNumber : -1;
final int fileLineNumber = (lineNumber != null && lineNumber >= 0) ? lineNumber : -1;

boolean sameDocumentRequested = false;
if (currentFragment instanceof DocumentEditFragment) {
String reqPath = (reqFile != null) ? reqFile.getPath() : "";
sameDocumentRequested = reqPath.equals(((DocumentEditFragment) currentFragment).getDocument(_document).getPath());
}
final boolean sameDocumentRequested = (
currentFragment instanceof DocumentEditFragment &&
document.getPath().equals(((DocumentEditFragment) currentFragment).getDocument().getPath()));

if (!sameDocumentRequested) {
if (document != null) {
showFragment(DocumentEditFragment.newInstance(document).setPreviewFlag(preview));
} else {
showFragment(DocumentEditFragment.newInstance(file, fileIsFolder, fileLineNumber).setPreviewFlag(preview));
}
showFragment(DocumentEditFragment.newInstance(document, fileLineNumber).setPreviewFlag(preview));
}
}

Expand Down Expand Up @@ -351,21 +339,10 @@ private GsFragmentBase getCurrentVisibleFragment() {
return (GsFragmentBase) getSupportFragmentManager().findFragmentById(R.id.document__placeholder_fragment);
}

public void setDocument(Document document) {
_document = document;
setDocumentTitle(_document.getTitle());
}

private void onToolbarTitleClicked(View v) {
if (getExistingFragment(DocumentEditFragment.FRAGMENT_TAG) != null) {
DocumentEditFragment def = ((DocumentEditFragment) getExistingFragment(DocumentEditFragment.FRAGMENT_TAG));
def.onToolbarTitleClicked(_toolbar);
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(DocumentEditFragment.SAVESTATE_DOCUMENT, _document);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,18 @@ public class DocumentEditFragment extends GsFragmentBase implements TextFormat.T
private boolean wrapText;
private boolean highlightText;

public static DocumentEditFragment newInstance(Document document) {

public static DocumentEditFragment newInstance(final Document document, final int lineNumber) {
DocumentEditFragment f = new DocumentEditFragment();
Bundle args = new Bundle();
args.putSerializable(Document.EXTRA_DOCUMENT, document);
args.putInt(Document.EXTRA_FILE_LINE_NUMBER, lineNumber);
f.setArguments(args);
return f;
}

public static DocumentEditFragment newInstance(File path, boolean pathIsFolder, final int lineNumber) {
DocumentEditFragment f = new DocumentEditFragment();
Bundle args = new Bundle();
args.putSerializable(Document.EXTRA_PATH, path);
args.putBoolean(Document.EXTRA_PATH_IS_FOLDER, pathIsFolder);
args.putInt(Document.EXTRA_FILE_LINE_NUMBER, lineNumber);
f.setArguments(args);
return f;
public static DocumentEditFragment newInstance(final File path, final int lineNumber) {
return newInstance(new Document(path), lineNumber);
}

@BindView(R.id.document__fragment__edit__highlighting_editor)
Expand Down Expand Up @@ -144,6 +140,12 @@ public DocumentEditFragment() {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the document as soon as possible
if (savedInstanceState != null && savedInstanceState.containsKey(SAVESTATE_DOCUMENT)) {
_document = (Document) savedInstanceState.getSerializable(SAVESTATE_DOCUMENT);
} else {
_document = Document.fromArguments(getActivity(), getArguments());
}
}

@Override
Expand Down Expand Up @@ -180,22 +182,14 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
WebView.setWebContentsDebuggingEnabled(true); // Inspect on computer chromium browser: chrome://inspect/#devices
}

int intentLineNumber = -1;
if (savedInstanceState != null && savedInstanceState.containsKey(SAVESTATE_DOCUMENT)) {
_document = (Document) savedInstanceState.getSerializable(SAVESTATE_DOCUMENT);
} else {
_document = Document.fromArguments(activity, getArguments());
intentLineNumber = _document.getIntentLineNumber();
}

// Upon construction, the document format has been determined from extension etc
// Here we replace it with the last saved format.
_document.setFormat(_appSettings.getDocumentFormat(_document.getPath(), _document.getFormat()));
applyTextFormat(_document.getFormat());
_textFormat.getTextActions().setDocument(_document);

if (activity instanceof DocumentActivity) {
((DocumentActivity) activity).setDocument(_document);
((DocumentActivity) activity).setDocumentTitle(_document.getTitle());
}

_hlEditor.setLineSpacing(0, _appSettings.getEditorLineSpacing());
Expand Down Expand Up @@ -233,7 +227,9 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
// Scroll to position
// If Intent contains line number, jump to it
// intentLineNumber only created with document reconstructed from intent
if (intentLineNumber >= 0) {
final Bundle args = getArguments();
final int intentLineNumber = args != null ? args.getInt(Document.EXTRA_FILE_LINE_NUMBER, -1) : -1;
if (savedInstanceState == null && intentLineNumber >= 0) {
_hlEditor.smoothMoveCursorToLine(intentLineNumber);
}

Expand Down Expand Up @@ -817,13 +813,7 @@ public boolean onLongClick(View v) {
//
//

/**
* Get document of this fragment. if no document set yet, fallback to other passed instances (i.e. from onSavedInstanceState)
*/
public Document getDocument(Document... fallback) {
if (_document == null && fallback != null && fallback.length > 0 && fallback[0] != null) {
_document = fallback[0];
}
public Document getDocument() {
return _document;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,10 @@ public void onFsViewerSelected(String request, File dir, final Integer lineNumbe
}, getFragmentManager(), getActivity());
}

private void showInDocumentActivity(Document document) {
private void showInDocumentActivity(final Document document) {
if (getActivity() instanceof DocumentActivity) {
DocumentActivity a = (DocumentActivity) getActivity();
a.setDocument(document);
a.showTextEditor(document, null, false, _appSettings.getDocumentPreviewState(document.getPath()), null);
a.showTextEditor(document, _appSettings.getDocumentPreviewState(document.getPath()), null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,10 @@ public Fragment getItem(int pos) {
return FilesystemViewerFragment.newInstance(getFilesystemFragmentOptions(null));
}
case R.id.nav_quicknote: {
return DocumentEditFragment.newInstance(_appSettings.getQuickNoteFile(), false, -1);
return DocumentEditFragment.newInstance(_appSettings.getQuickNoteFile(), -1);
}
case R.id.nav_todo: {
return DocumentEditFragment.newInstance(_appSettings.getTodoFile(), false, -1);
return DocumentEditFragment.newInstance(_appSettings.getTodoFile(), -1);
}
case R.id.nav_more: {
return MoreFragment.newInstance();
Expand Down
15 changes: 3 additions & 12 deletions app/src/main/java/net/gsantner/markor/model/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public class Document implements Serializable {
private String _title = "";
private String _path = "";
private long _modTime = 0;
private int _intentLineNumber = -1;

// Used to check if string changed
private long _lastHash = 0;
Expand Down Expand Up @@ -121,10 +120,6 @@ public String getName() {
return _file.getName();
}

public int getIntentLineNumber() {
return _intentLineNumber;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof Document) {
Expand Down Expand Up @@ -190,16 +185,12 @@ private static File getValidFile(Context context, Bundle arguments) {
}

public static Document fromArguments(Context context, Bundle arguments) {

// When called directly with a document
if (arguments.containsKey(EXTRA_DOCUMENT)) {
// When called directly with a document
return (Document) arguments.getSerializable(EXTRA_DOCUMENT);
} else {
return new Document(getValidFile(context, arguments));
}

Document document = new Document(getValidFile(context, arguments));
document._intentLineNumber = arguments.getInt(EXTRA_FILE_LINE_NUMBER, -1);

return document;
}

private void setContentHash(final CharSequence s) {
Expand Down

0 comments on commit 53f8740

Please sign in to comment.