Skip to content

Commit

Permalink
Defensive logic for indices
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Aug 23, 2024
1 parent a656226 commit 4631024
Show file tree
Hide file tree
Showing 16 changed files with 278 additions and 276 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
_webView.setWebChromeClient(new GsWebViewChromeClient(_webView, activity, view.findViewById(R.id.document__fragment_fullscreen_overlay)));
_webView.setWebViewClient(_webViewClient);
_webView.addJavascriptInterface(this, "Android");
_webView.setBackgroundColor(Color.TRANSPARENT);
WebSettings webSettings = _webView.getSettings();
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
Expand All @@ -176,9 +177,7 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {

// 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.path, _document.getFormat()));
applyTextFormat(_document.getFormat());
_format.getActions().setDocument(_document);
applyTextFormat(_appSettings.getDocumentFormat(_document.path, _document.getFormat()));

if (activity instanceof DocumentActivity) {
((DocumentActivity) activity).setDocumentTitle(_document.title);
Expand Down Expand Up @@ -217,7 +216,6 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
// Do not need to send contents to accessibility
_hlEditor.setImportantForAccessibility(View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
}
_webView.setBackgroundColor(Color.TRANSPARENT);

// Various settings
setHorizontalScrollMode(isDisplayedAtMainActivity() || _appSettings.getDocumentWrapState(_document.path));
Expand Down Expand Up @@ -251,20 +249,26 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
}
});
}
}

if (savedInstanceState == null) {
int startPos = _appSettings.getLastEditPosition(_document.path, _hlEditor.length());
if (args != null && args.containsKey(Document.EXTRA_FILE_LINE_NUMBER)) {
final int lno = args.getInt(Document.EXTRA_FILE_LINE_NUMBER);
if (lno >= 0) {
startPos = TextViewUtils.getIndexFromLineOffset(_hlEditor.getText(), lno, 0);
} else if (lno == Document.EXTRA_FILE_LINE_NUMBER_LAST) {
startPos = _hlEditor.length();
}
}
@Override
protected void onFragmentFirstTimeVisible() {
final Bundle args = getArguments();

TextViewUtils.setSelectionAndShow(_hlEditor, startPos);
int startPos = _appSettings.getLastEditPosition(_document.path, _hlEditor.length());
if (args != null && args.containsKey(Document.EXTRA_FILE_LINE_NUMBER)) {
final int lno = args.getInt(Document.EXTRA_FILE_LINE_NUMBER);
if (lno >= 0) {
startPos = TextViewUtils.getIndexFromLineOffset(_hlEditor.getText(), lno, 0);
} else if (lno == Document.EXTRA_FILE_LINE_NUMBER_LAST) {
startPos = _hlEditor.length();
}
}

_primaryScrollView.invalidate();
// Can affect layout so run before setting scroll position
_hlEditor.recomputeHighlighting();
TextViewUtils.setSelectionAndShow(_hlEditor, startPos);
}

@Override
Expand All @@ -280,7 +284,7 @@ public void onPause() {
_webView.onPause();
_appSettings.addRecentFile(_document.file);
_appSettings.setDocumentPreviewState(_document.path, _isPreviewVisible);
_appSettings.setLastEditPosition(_document.path, _hlEditor.getSelectionStart());
_appSettings.setLastEditPosition(_document.path, TextViewUtils.getSelection(_hlEditor)[0]);
super.onPause();
}

Expand Down Expand Up @@ -657,6 +661,7 @@ public void checkTextChangeState() {
}
}

@Override
public void applyTextFormat(final int textFormatId) {
final Activity activity = getActivity();
if (activity == null) {
Expand All @@ -669,6 +674,7 @@ public void applyTextFormat(final int textFormatId) {
_hlEditor.setAutoFormatters(_format.getAutoFormatInputFilter(), _format.getAutoFormatTextWatcher());
_hlEditor.setAutoFormatEnabled(_appSettings.getDocumentAutoFormatEnabled(_document.path));
_format.getActions()
.setDocument(_document)
.setUiReferences(activity, _hlEditor, _webView)
.recreateActionButtons(_textActionsBar, _isPreviewVisible ? ActionButtonBase.ActionItem.DisplayMode.VIEW : ActionButtonBase.ActionItem.DisplayMode.EDIT);
updateMenuToggleStates(_format.getFormatId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ protected void afterOnCreate(Bundle savedInstances, Context context) {
if (_editor != null && _linkCheckBox != null) {
doUpdatePreferences();
_linkCheckBox.setVisibility(hasLinks(_editor.getText()) ? View.VISIBLE : View.GONE);
_linkCheckBox.setChecked(true);
_linkCheckBox.setChecked(_appSettings.getFormatShareAsLink());
_editor.addTextChangedListener(GsTextWatcherAdapter.on((ctext, arg2, arg3, arg4) ->
_linkCheckBox.setVisibility(hasLinks(_editor.getText()) ? View.VISIBLE : View.GONE)));
}
Expand All @@ -242,7 +242,8 @@ private void appendToExistingDocumentAndClose(final File file, final boolean sho

final Document document = new Document(file);
final int format = _appSettings.getDocumentFormat(document.path, document.getFormat());
final String formatted = getFormatted(shareAsLink(), file, format);
final boolean asLink = shareAsLink();
final String formatted = getFormatted(asLink, file, format);

final String oldContent = document.loadContent(activity);
if (oldContent != null) {
Expand All @@ -254,6 +255,7 @@ private void appendToExistingDocumentAndClose(final File file, final boolean sho
}

_appSettings.addRecentFile(file);
_appSettings.setFormatShareAsLink(asLink);

if (showEditor) {
DocumentActivity.launch(activity, document.file, null, -1);
Expand Down
Loading

0 comments on commit 4631024

Please sign in to comment.