Fix autosave
This commit is contained in:
parent
29967932f1
commit
329b36cc95
4 changed files with 41 additions and 8 deletions
|
@ -13,7 +13,8 @@ public interface MarkdownPresenter {
|
|||
void loadMarkdown(String fileName, InputStream in);
|
||||
void loadFromUri(Context context, Uri fileUri);
|
||||
|
||||
void loadMarkdown(String fileName, InputStream in, OnTempFileLoadedListener listener);
|
||||
void loadMarkdown(String fileName, InputStream in, FileLoadedListener listener,
|
||||
boolean replaceCurrentFile);
|
||||
|
||||
void newFile(String newName);
|
||||
void setEditView(MarkdownEditView editView);
|
||||
|
@ -29,7 +30,7 @@ public interface MarkdownPresenter {
|
|||
String getMarkdown();
|
||||
void setMarkdown(String markdown);
|
||||
|
||||
interface OnTempFileLoadedListener {
|
||||
interface FileLoadedListener {
|
||||
void onSuccess(String markdown);
|
||||
|
||||
void onError();
|
||||
|
|
|
@ -37,14 +37,15 @@ public class MarkdownPresenterImpl implements MarkdownPresenter {
|
|||
|
||||
@Override
|
||||
public void loadMarkdown(final String fileName, final InputStream in) {
|
||||
this.loadMarkdown(fileName, in, null);
|
||||
this.loadMarkdown(fileName, in, null, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadMarkdown(
|
||||
final String fileName,
|
||||
final InputStream in,
|
||||
final OnTempFileLoadedListener listener
|
||||
final FileLoadedListener listener,
|
||||
boolean replaceCurrentFile
|
||||
) {
|
||||
Runnable fileLoader = () -> {
|
||||
MarkdownFile tmpFile = new MarkdownFile(errorHandler);
|
||||
|
@ -52,7 +53,8 @@ public class MarkdownPresenterImpl implements MarkdownPresenter {
|
|||
if (listener != null) {
|
||||
String html = generateHTML(tmpFile.getContent());
|
||||
listener.onSuccess(html);
|
||||
} else {
|
||||
}
|
||||
if (replaceCurrentFile) {
|
||||
synchronized (fileLock) {
|
||||
this.file = tmpFile;
|
||||
MarkdownEditView currentEditView = editView;
|
||||
|
|
|
@ -24,6 +24,7 @@ import com.wbrawner.simplemarkdown.utility.ErrorHandler
|
|||
import com.wbrawner.simplemarkdown.utility.Utils
|
||||
import com.wbrawner.simplemarkdown.view.adapter.EditPagerAdapter
|
||||
import kotlinx.android.synthetic.main.activity_main.*
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.InputStream
|
||||
import javax.inject.Inject
|
||||
|
@ -61,7 +62,8 @@ class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsRes
|
|||
override fun onUserLeaveHint() {
|
||||
super.onUserLeaveHint()
|
||||
if (shouldAutoSave && presenter.markdown.isNotEmpty() && Utils.isAutosaveEnabled(this)) {
|
||||
// presenter.saveMarkdown(null, null)
|
||||
|
||||
presenter.saveMarkdown(null, "autosave.md", File(filesDir, "autosave.md").outputStream())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -132,7 +134,7 @@ class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsRes
|
|||
if (assetManager != null) {
|
||||
`in` = assetManager.open(fileName)
|
||||
}
|
||||
presenter.loadMarkdown(fileName, `in`, object : MarkdownPresenter.OnTempFileLoadedListener {
|
||||
presenter.loadMarkdown(fileName, `in`, object : MarkdownPresenter.FileLoadedListener {
|
||||
override fun onSuccess(html: String) {
|
||||
infoIntent.putExtra("html", html)
|
||||
startActivity(infoIntent)
|
||||
|
@ -142,7 +144,7 @@ class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsRes
|
|||
Toast.makeText(this@MainActivity, R.string.file_load_error, Toast.LENGTH_SHORT)
|
||||
.show()
|
||||
}
|
||||
})
|
||||
}, false)
|
||||
} catch (e: Exception) {
|
||||
errorHandler.reportException(e)
|
||||
Toast.makeText(this@MainActivity, R.string.file_load_error, Toast.LENGTH_SHORT).show()
|
||||
|
|
|
@ -15,6 +15,10 @@ import com.wbrawner.simplemarkdown.R;
|
|||
import com.wbrawner.simplemarkdown.presentation.MarkdownPresenter;
|
||||
import com.wbrawner.simplemarkdown.utility.ErrorHandler;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class SplashActivity extends AppCompatActivity {
|
||||
|
@ -60,6 +64,30 @@ public class SplashActivity extends AppCompatActivity {
|
|||
presenter.loadFromUri(getApplicationContext(), intent.getData());
|
||||
} else {
|
||||
presenter.setFileName("Untitled.md");
|
||||
final File autosave = new File(getFilesDir(), "autosave.md");
|
||||
if (autosave.exists()) {
|
||||
try {
|
||||
FileInputStream fileInputStream = new FileInputStream(autosave);
|
||||
presenter.loadMarkdown(
|
||||
"Untitled.md",
|
||||
fileInputStream,
|
||||
new MarkdownPresenter.FileLoadedListener() {
|
||||
@Override
|
||||
public void onSuccess(String markdown) {
|
||||
autosave.delete();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError() {
|
||||
autosave.delete();
|
||||
}
|
||||
},
|
||||
true
|
||||
);
|
||||
} catch (FileNotFoundException ignored) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Intent startIntent = new Intent(this, MainActivity.class);
|
||||
|
|
Loading…
Reference in a new issue