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