diff --git a/app/src/main/java/net/gsantner/markor/activity/DocumentEditFragment.java b/app/src/main/java/net/gsantner/markor/activity/DocumentEditFragment.java index 5a54a3c5a4..bd80dc4121 100644 --- a/app/src/main/java/net/gsantner/markor/activity/DocumentEditFragment.java +++ b/app/src/main/java/net/gsantner/markor/activity/DocumentEditFragment.java @@ -114,7 +114,6 @@ public static DocumentEditFragment newInstance(final @NonNull File path, final I private MarkorWebViewClient _webViewClient; private boolean _nextConvertToPrintMode = false; private long _loadModTime = 0; - private boolean _isTextChanged = false; private MenuItem _saveMenuItem, _undoMenuItem, _redoMenuItem; // Wrap text setting and wrap text state are separated as the wrap text state may depend on @@ -214,45 +213,58 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { // Set initial wrap state initDocState(); + } + + + @Override + public void onFragmentFirstTimeVisible() { int startPos = _appSettings.getLastEditPosition(_document.getPath(), _hlEditor.length()); + final Bundle args = getArguments(); // First start - overwrite start position if needed - if (savedInstanceState == null) { - if (isDisplayedAtMainActivity()) { + if (_savedInstanceState == null) { + if (isDisplayedAtMainActivity() || _appSettings.isEditorStartOnBotttom()) { startPos = _hlEditor.length(); - } else if (args.getInt(Document.EXTRA_FILE_LINE_NUMBER, -1) >= 0) { + } else if (args != null && args.getInt(Document.EXTRA_FILE_LINE_NUMBER, -1) >= 0) { startPos = StringUtils.getIndexFromLineOffset(_hlEditor.getText(), args.getInt(Document.EXTRA_FILE_LINE_NUMBER), 0); - } else if (_appSettings.isEditorStartOnBotttom()) { - startPos = _hlEditor.length(); } } StringUtils.setSelectionAndShow(_hlEditor, startPos); } + public void resume() { + loadDocument(); + } + @Override public void onResume() { + resume(); super.onResume(); + } - loadDocument(); + @Override + public void onSaveInstanceState(@NonNull Bundle outState) { + outState.putSerializable(SAVESTATE_DOCUMENT, _document); + super.onSaveInstanceState(outState); + } - if (_document != null) { - _document.testCreateParent(); - boolean permok = _shareUtil.canWriteFile(_document.getFile(), false); - if (!permok && !_document.getFile().isDirectory() && _shareUtil.canWriteFile(_document.getFile(), _document.getFile().isDirectory())) { - permok = true; - } - if (_shareUtil.isUnderStorageAccessFolder(_document.getFile()) && _shareUtil.getStorageAccessFrameworkTreeUri() == null) { - _shareUtil.showMountSdDialog(getActivity()); - return; + public void pause() { + saveDocument(false); + if (_appSettings != null && _document != null) { + _appSettings.addRecentDocument(_document.getFile()); + _appSettings.setDocumentPreviewState(_document.getPath(), _isPreviewVisible); + if (_hlEditor != null) { + _appSettings.setLastEditPosition(_document.getPath(), _hlEditor.getSelectionStart()); } - _textSdWarning.setVisibility(permok ? View.GONE : View.VISIBLE); } + } - if (_document != null && _document.getFile().getAbsolutePath().contains("mordor/1-epub-experiment.md") && getActivity() instanceof DocumentActivity) { - _hlEditor.setText(CoolExperimentalStuff.convertEpubToText(_document.getFile(), getString(R.string.page))); - } + @Override + public void onPause() { + pause(); + super.onPause(); } @Override @@ -565,10 +577,10 @@ public void onContentEditValueChanged(CharSequence text) { } public void checkTextChangeState() { - _isTextChanged = !_document.isContentSame(_hlEditor.getText()); + final boolean isTextChanged = !_document.isContentSame(_hlEditor.getText()); - if (_saveMenuItem != null && _saveMenuItem.isEnabled() != _isTextChanged) { - _saveMenuItem.setEnabled(_isTextChanged).getIcon().mutate().setAlpha(_isTextChanged ? 255 : 40); + if (_saveMenuItem != null && _saveMenuItem.isEnabled() != isTextChanged) { + _saveMenuItem.setEnabled(isTextChanged).getIcon().mutate().setAlpha(isTextChanged ? 255 : 40); } } @@ -654,11 +666,27 @@ public String getFragmentTag() { return FRAGMENT_TAG; } + public boolean checkPermissions() { + + if (_document != null) { + final File file = _document.getFile(); + + if (_shareUtil.isUnderStorageAccessFolder(file) && _shareUtil.getStorageAccessFrameworkTreeUri() == null) { + _shareUtil.showMountSdDialog(getActivity()); + } + + final boolean permok = _document.testCreateParent() && _shareUtil.canWriteFile(file, false); + _textSdWarning.setVisibility(permok ? View.GONE : View.VISIBLE); + return permok; + } + return false; + } + // Save the file // Only supports java.io.File. TODO: Android Content public boolean saveDocument(final boolean forceSaveEmpty) { - // Document is written iff content has changed - if (_isTextChanged && _document != null && _hlEditor != null && isAdded()) { + // Document is written iff writeable && content has changed + if (checkPermissions() && _hlEditor != null && isAdded()) { if (_document.saveContent(getContext(), _hlEditor.getText().toString(), _shareUtil, forceSaveEmpty)) { checkTextChangeState(); return true; @@ -674,45 +702,6 @@ private boolean isDisplayedAtMainActivity() { return getActivity() instanceof MainActivity; } - @Override - public void onSaveInstanceState(@NonNull Bundle outState) { - outState.putSerializable(SAVESTATE_DOCUMENT, _document); - super.onSaveInstanceState(outState); - } - - @Override - public void onPause() { - saveDocument(false); - if (_appSettings != null && _document != null) { - _appSettings.addRecentDocument(_document.getFile()); - _appSettings.setDocumentPreviewState(_document.getPath(), _isPreviewVisible); - if (_hlEditor != null) { - _appSettings.setLastEditPosition(_document.getPath(), _hlEditor.getSelectionStart()); - } - } - super.onPause(); - } - - @Override - public void setUserVisibleHint(final boolean isVisibleToUser) { - // This function can be called _outside_ the normal lifecycle! - // Do nothing if the fragment is not at least created! - if (!getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.CREATED)) { - return; - } - - super.setUserVisibleHint(isVisibleToUser); - if (isVisibleToUser && isDisplayedAtMainActivity()) { - loadDocument(); - _primaryScrollView.postDelayed(() -> _primaryScrollView.fullScroll(View.FOCUS_DOWN), 100); - } else if (!isVisibleToUser && _document != null) { - saveDocument(false); - } - if (isVisibleToUser) { - initDocState(); - } - } - public void updateViewModeText() { _textFormat.getConverter().convertMarkupShowInWebView(_document, _hlEditor.getText().toString(), _webView, _nextConvertToPrintMode); } diff --git a/app/src/main/java/net/gsantner/markor/activity/MainActivity.java b/app/src/main/java/net/gsantner/markor/activity/MainActivity.java index d7192e11a6..20ad8923dc 100644 --- a/app/src/main/java/net/gsantner/markor/activity/MainActivity.java +++ b/app/src/main/java/net/gsantner/markor/activity/MainActivity.java @@ -76,6 +76,7 @@ public class MainActivity extends MarkorBaseActivity implements FilesystemViewer private boolean _doubleBackToExitPressedOnce; private ShareUtil _shareUtil; + private int _prevPos = -1; @SuppressLint("SdCardPath") @Override @@ -109,6 +110,7 @@ protected void onCreate(Bundle savedInstanceState) { _viewPager.setAdapter(_viewPagerAdapter); _viewPager.setOffscreenPageLimit(4); _bottomNav.setOnNavigationItemSelectedListener(this); + _prevPos = tabIdToPos(R.id.nav_notebook); // Default // noinspection PointlessBooleanExpression - Send Test intent if (BuildConfig.IS_TEST_BUILD && false) { @@ -381,10 +383,16 @@ public void onViewPagerPageSelected(int pos) { restoreDefaultToolbar(); } + if (_prevPos == tabIdToPos(R.id.nav_quicknote) || _prevPos == tabIdToPos(R.id.nav_todo)) { + ((DocumentEditFragment) _viewPagerAdapter.getItem(_prevPos)).pause(); + } + if (pos == tabIdToPos(R.id.nav_quicknote) || pos == tabIdToPos(R.id.nav_todo)) { - // cannot prevent bottom tab selection new PermissionChecker(this).doIfExtStoragePermissionGranted(); + ((DocumentEditFragment) _viewPagerAdapter.getItem(pos)).resume(); } + + _prevPos = pos; } private FilesystemViewerData.Options _filesystemDialogOptions = null;