Fix file opening
This commit is contained in:
parent
731377a420
commit
5eab2285c4
6 changed files with 58 additions and 48 deletions
|
@ -1,11 +1,15 @@
|
|||
package com.wbrawner.simplemarkdown.model;
|
||||
|
||||
import com.wbrawner.simplemarkdown.Utils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.util.Scanner;
|
||||
|
||||
|
@ -96,19 +100,21 @@ public class MarkdownFile {
|
|||
|
||||
public boolean load(InputStream in) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Scanner s = new java.util.Scanner(in).useDelimiter("\\n");
|
||||
if (s == null) {
|
||||
return false;
|
||||
}
|
||||
while (s.hasNext()) {
|
||||
sb.append(s.next()).append("\n");
|
||||
}
|
||||
this.content = sb.toString();
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
in.close();
|
||||
} catch (Exception ignored) {
|
||||
reader = new BufferedReader(new InputStreamReader(in));
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
sb.append(line).append('\n');
|
||||
}
|
||||
this.content = sb.toString();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} finally {
|
||||
Utils.closeQuietly(reader);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean load(String path) {
|
||||
|
|
|
@ -12,12 +12,12 @@ import java.io.InputStream;
|
|||
public interface MarkdownPresenter {
|
||||
File getFile();
|
||||
void loadMarkdown();
|
||||
void loadMarkdown(String filePath);
|
||||
void loadMarkdown(InputStream in);
|
||||
|
||||
void loadMarkdown(String fileName, InputStream in);
|
||||
void loadMarkdown(File file);
|
||||
void loadFromUri(Context context, Uri fileUri);
|
||||
|
||||
void loadMarkdown(InputStream in, OnTempFileLoadedListener listener);
|
||||
void loadMarkdown(String fileName, InputStream in, OnTempFileLoadedListener listener);
|
||||
void newFile(String path);
|
||||
void setEditView(MarkdownEditView editView);
|
||||
void setPreviewView(MarkdownPreviewView previewView);
|
||||
|
|
|
@ -27,12 +27,6 @@ public class MarkdownPresenterImpl implements MarkdownPresenter {
|
|||
this.file = file;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadMarkdown(String filePath) {
|
||||
File markdownFile = new File(filePath);
|
||||
loadMarkdown(markdownFile);
|
||||
}
|
||||
|
||||
@Override
|
||||
public File getFile() {
|
||||
return new File(file.getFullPath());
|
||||
|
@ -54,31 +48,41 @@ public class MarkdownPresenterImpl implements MarkdownPresenter {
|
|||
|
||||
@Override
|
||||
public void loadMarkdown(File file) {
|
||||
InputStream in = null;
|
||||
try {
|
||||
in = new FileInputStream(file);
|
||||
loadMarkdown(in);
|
||||
InputStream in = new FileInputStream(file);
|
||||
loadMarkdown(file.getName(), in);
|
||||
} catch (FileNotFoundException e) {
|
||||
System.err.println(e.getLocalizedMessage());
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
Utils.closeQuietly(in);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadMarkdown(InputStream in) {
|
||||
this.loadMarkdown(in, null);
|
||||
public void loadMarkdown(final String fileName, final InputStream in) {
|
||||
this.loadMarkdown(fileName, in, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadMarkdown(final InputStream in, final OnTempFileLoadedListener listener) {
|
||||
public void loadMarkdown(
|
||||
final String fileName,
|
||||
final InputStream in,
|
||||
final OnTempFileLoadedListener listener
|
||||
) {
|
||||
Runnable fileLoader = () -> {
|
||||
MarkdownFile tmpFile = new MarkdownFile();
|
||||
if (tmpFile.load(in)) {
|
||||
String html = generateHTML(tmpFile.getContent());
|
||||
tmpFile.setName(fileName);
|
||||
if (listener != null) {
|
||||
String html = generateHTML(tmpFile.getContent());
|
||||
listener.onSuccess(html);
|
||||
} else {
|
||||
this.file = tmpFile;
|
||||
if (this.editView != null) {
|
||||
editView.onFileLoaded(true);
|
||||
editView.setTitle(fileName);
|
||||
editView.setMarkdown(this.file.getContent());
|
||||
onMarkdownEdited();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (listener != null) {
|
||||
|
@ -182,6 +186,7 @@ public class MarkdownPresenterImpl implements MarkdownPresenter {
|
|||
try {
|
||||
InputStream in =
|
||||
context.getContentResolver().openInputStream(fileUri);
|
||||
String fileName = null;
|
||||
if (fileUri.getScheme().equals("content")) {
|
||||
Cursor retCur = context.getContentResolver()
|
||||
.query(fileUri, null, null, null, null);
|
||||
|
@ -189,13 +194,16 @@ public class MarkdownPresenterImpl implements MarkdownPresenter {
|
|||
int nameIndex = retCur
|
||||
.getColumnIndex(OpenableColumns.DISPLAY_NAME);
|
||||
retCur.moveToFirst();
|
||||
setFileName(retCur.getString(nameIndex));
|
||||
fileName = retCur.getString(nameIndex);
|
||||
retCur.close();
|
||||
}
|
||||
} else if (fileUri.getScheme().equals("file")) {
|
||||
setFileName(fileUri.getLastPathSegment());
|
||||
fileName = fileUri.getLastPathSegment();
|
||||
}
|
||||
loadMarkdown(in);
|
||||
if (fileName == null) {
|
||||
fileName = Utils.getDefaultFileName(context);
|
||||
}
|
||||
loadMarkdown(fileName, in);
|
||||
} catch (Exception e) {
|
||||
if (editView != null) {
|
||||
editView.onFileLoaded(false);
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package com.wbrawner.simplemarkdown.view;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public interface MarkdownEditView {
|
||||
String getMarkdown();
|
||||
void setMarkdown(String markdown);
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
package com.wbrawner.simplemarkdown.view.activity;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.os.Handler;
|
||||
import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.design.widget.Snackbar;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.view.Menu;
|
||||
|
@ -26,17 +25,18 @@ import java.util.List;
|
|||
import java.util.Locale;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import io.reactivex.annotations.BackpressureSupport;
|
||||
|
||||
public class ExplorerActivity extends AppCompatActivity {
|
||||
private Handler fileHandler = new Handler();
|
||||
private ListView listView;
|
||||
private File[] mounts;
|
||||
private String docsDirPath;
|
||||
private int requestCode;
|
||||
private String filePath;
|
||||
private boolean isSave = false;
|
||||
private EditText fileName;
|
||||
private Button saveButton;
|
||||
|
||||
@SuppressLint("SetTextI18n")
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
@ -52,7 +52,7 @@ public class ExplorerActivity extends AppCompatActivity {
|
|||
|
||||
docsDirPath = Utils.getDocsPath(this);
|
||||
|
||||
requestCode = intent.getIntExtra(MainActivity.EXTRA_REQUEST_CODE, -1);
|
||||
int requestCode = intent.getIntExtra(MainActivity.EXTRA_REQUEST_CODE, -1);
|
||||
switch (requestCode) {
|
||||
case MainActivity.OPEN_FILE_REQUEST:
|
||||
break;
|
||||
|
@ -69,7 +69,7 @@ public class ExplorerActivity extends AppCompatActivity {
|
|||
fileName.setText("Untitled.md");
|
||||
}
|
||||
}
|
||||
saveButton = findViewById(R.id.button_save);
|
||||
Button saveButton = findViewById(R.id.button_save);
|
||||
saveButton.setVisibility(View.VISIBLE);
|
||||
saveButton.setOnClickListener((v) -> {
|
||||
Intent fileIntent = new Intent();
|
||||
|
@ -138,17 +138,19 @@ public class ExplorerActivity extends AppCompatActivity {
|
|||
return mounts.length > 1;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private List<HashMap<String, Object>> loadFiles(File docsDir) {
|
||||
TreeSet files = new TreeSet<HashMap<String, Object>>((o1, o2) ->
|
||||
TreeSet<HashMap<String, Object>> files = new TreeSet<HashMap<String, Object>>((o1, o2) ->
|
||||
((String) o1.get("name")).compareToIgnoreCase((String) o2.get("name"))) {
|
||||
};
|
||||
TreeSet dirs = new TreeSet<HashMap<String, Object>>((o1, o2) ->
|
||||
TreeSet<HashMap<String, Object>> dirs = new TreeSet<HashMap<String, Object>>((o1, o2) ->
|
||||
((String) o1.get("name")).compareToIgnoreCase((String) o2.get("name"))) {
|
||||
};
|
||||
if (docsDir.getParentFile() != null && docsDir.getParentFile().canRead()) {
|
||||
HashMap<String, Object> fileHashMap = new HashMap<>();
|
||||
fileHashMap.put("name", "..");
|
||||
fileHashMap.put("file", docsDir.getParentFile());
|
||||
|
||||
dirs.add(fileHashMap);
|
||||
}
|
||||
|
||||
|
|
|
@ -75,7 +75,7 @@ public class MainActivity extends AppCompatActivity
|
|||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
if (Utils.isAutosaveEnabled(this)) {
|
||||
if (!presenter.getMarkdown().isEmpty() && Utils.isAutosaveEnabled(this)) {
|
||||
presenter.saveMarkdown(null, null);
|
||||
}
|
||||
}
|
||||
|
@ -179,7 +179,7 @@ public class MainActivity extends AppCompatActivity
|
|||
if (assetManager != null) {
|
||||
in = assetManager.open(fileName);
|
||||
}
|
||||
presenter.loadMarkdown(in, new MarkdownPresenter.OnTempFileLoadedListener() {
|
||||
presenter.loadMarkdown(fileName, in, new MarkdownPresenter.OnTempFileLoadedListener() {
|
||||
@Override
|
||||
public void onSuccess(String html) {
|
||||
infoIntent.putExtra("html", html);
|
||||
|
@ -250,10 +250,6 @@ public class MainActivity extends AppCompatActivity
|
|||
|
||||
File markdownFile = (File) data.getSerializableExtra(EXTRA_FILE);
|
||||
presenter.loadMarkdown(markdownFile);
|
||||
String title = markdownFile.getName();
|
||||
if (!title.isEmpty()) {
|
||||
setTitle(title);
|
||||
}
|
||||
break;
|
||||
case SAVE_FILE_REQUEST:
|
||||
if (resultCode != RESULT_OK
|
||||
|
|
Loading…
Reference in a new issue