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

Fix v2.8.1 saving issues #1473

Merged
merged 19 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -179,6 +179,7 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {

if (savedInstanceState != null && savedInstanceState.containsKey(SAVESTATE_DOCUMENT)) {
_document = (Document) savedInstanceState.getSerializable(SAVESTATE_DOCUMENT);
_document.resetModTime(); // Ensure document is loaded on restore state
} else {
_document = Document.fromArguments(getActivity(), getArguments());
}
Expand Down Expand Up @@ -315,6 +316,8 @@ public boolean onQueryTextChange(String text) {

public void loadDocument() {
int editorpos = _hlEditor.getSelectionStart();

// Load document if mod time newer than that recorded on last load
if (_document.hasNewerModTime()) {
harshad1 marked this conversation as resolved.
Show resolved Hide resolved
_hlEditor.setText(_document.loadContent(getContext()));
}
Expand Down
19 changes: 8 additions & 11 deletions app/src/main/java/net/gsantner/markor/model/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
@SuppressWarnings({"WeakerAccess", "UnusedReturnValue", "unused"})
public class Document implements Serializable {
private static final int MAX_TITLE_EXTRACTION_LENGTH = 25;
private static final long MINIMUM_WAIT_TIME = 5000; // 5 seconds

public static final String EXTRA_DOCUMENT = "EXTRA_DOCUMENT"; // Document
public static final String EXTRA_PATH = "EXTRA_PATH"; // java.io.File
Expand All @@ -53,10 +52,9 @@ public class Document implements Serializable {
private final String _fileExtension;
private int _format = TextFormat.FORMAT_UNKNOWN;
private String _title = "";
private long _modTime = 0; // Modtime of last loaded content
private long _modTime = 0; // Modtime as of when the file was last loaded / written
private int _initialLineNumber = -1;
private String _lastHash = null;
private long _lastWriteTime = 0;

public Document(File file) {
_file = file;
Expand Down Expand Up @@ -150,12 +148,12 @@ public void setFormat(int format) {
_format = format;
}

public long getModTime() {
return _modTime;
public void resetModTime() {
_modTime = 0;
}

public void setModTime(long modTime) {
_modTime = modTime;
public long getModTime() {
return _modTime;
}

public boolean hasNewerModTime() {
Expand Down Expand Up @@ -291,10 +289,9 @@ public synchronized boolean saveContent(final Context context, final String cont
shareUtil = shareUtil != null ? shareUtil : new ShareUtil(context);

final String newHash = FileUtils.sha512sum(content.getBytes());
final long curTime = currentTimeMillis();

// Don't write the same content in a short duration
if ((newHash != null && newHash.equals(_lastHash)) || (curTime - _lastWriteTime) < MINIMUM_WAIT_TIME) {
// Don't write if content same and file hasn't changed
if (newHash != null && newHash.equals(_lastHash) && !hasNewerModTime()) {
gsantner marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

Expand Down Expand Up @@ -328,7 +325,7 @@ public synchronized boolean saveContent(final Context context, final String cont

if (success) {
_lastHash = newHash;
_lastWriteTime = curTime;
_modTime = _file.lastModified(); // Should be == now
}

return success;
Expand Down