Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More deterministic save / resume #1736

Merged
merged 3 commits into from
Jun 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Comment on lines +237 to +239
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a plan/idea for things like resume/pause, but OK for now


@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
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public class MainActivity extends MarkorBaseActivity implements FilesystemViewer

private boolean _doubleBackToExitPressedOnce;
private ShareUtil _shareUtil;
private int _prevPos = -1;

@SuppressLint("SdCardPath")
@Override
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down