diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/presentation/MarkdownPresenter.java b/app/src/main/java/com/wbrawner/simplemarkdown/presentation/MarkdownPresenter.java index 548a125..010f6a8 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/presentation/MarkdownPresenter.java +++ b/app/src/main/java/com/wbrawner/simplemarkdown/presentation/MarkdownPresenter.java @@ -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(); diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/presentation/MarkdownPresenterImpl.java b/app/src/main/java/com/wbrawner/simplemarkdown/presentation/MarkdownPresenterImpl.java index 59d753b..1f824f0 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/presentation/MarkdownPresenterImpl.java +++ b/app/src/main/java/com/wbrawner/simplemarkdown/presentation/MarkdownPresenterImpl.java @@ -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; diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/MainActivity.kt b/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/MainActivity.kt index 1aa8597..3556e40 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/MainActivity.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/MainActivity.kt @@ -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() diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/SplashActivity.java b/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/SplashActivity.java index ad07715..5323a17 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/SplashActivity.java +++ b/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/SplashActivity.java @@ -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);