Fix scrolling for code blocks and clean up warnings
This commit is contained in:
parent
f82330a92f
commit
731377a420
14 changed files with 257 additions and 237 deletions
|
@ -4,10 +4,6 @@ import android.app.Application;
|
|||
import com.crashlytics.android.Crashlytics;
|
||||
import io.fabric.sdk.android.Fabric;
|
||||
|
||||
/**
|
||||
* Created by billy on 8/22/17.
|
||||
*/
|
||||
|
||||
public class MarkdownApplication extends Application {
|
||||
|
||||
private AppComponent component;
|
||||
|
|
|
@ -7,8 +7,10 @@ import android.preference.PreferenceManager;
|
|||
|
||||
import com.wbrawner.simplemarkdown.view.activity.SettingsActivity;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
@ -75,4 +77,13 @@ public class Utils {
|
|||
);
|
||||
}
|
||||
|
||||
public static void closeQuietly(Closeable closeable) {
|
||||
if (closeable != null) {
|
||||
try {
|
||||
closeable.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,39 +1,23 @@
|
|||
package com.wbrawner.simplemarkdown.model;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.util.Locale;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static android.content.ContentValues.TAG;
|
||||
|
||||
/**
|
||||
* This class serves as a wrapper to manage the manage the file input and output operations, as well
|
||||
* as to keep track of the data itself in memory.
|
||||
*/
|
||||
public class MarkdownFile {
|
||||
public static final int SUCCESS = 0;
|
||||
public static final int FILE_EXISTS = 1;
|
||||
public static final int FILE_NOT_EXISTS = 2;
|
||||
public static final int READ_ERROR = 3;
|
||||
public static final int WRITE_ERROR = 4;
|
||||
public static final int PARAMETERS_MISSING = 5;
|
||||
|
||||
public static void setDefaultRootDir(String defaultRootDir) {
|
||||
MarkdownFile.defaultRootDir = defaultRootDir;
|
||||
}
|
||||
|
||||
private static String defaultRootDir = "";
|
||||
private String name;
|
||||
private String path;
|
||||
private String content;
|
||||
|
||||
public MarkdownFile(String name, String path, String content) {
|
||||
this.name = name;
|
||||
if (path == null || path.isEmpty()) {
|
||||
|
@ -44,8 +28,7 @@ public class MarkdownFile {
|
|||
}
|
||||
|
||||
public MarkdownFile(String path) {
|
||||
int code = load(path);
|
||||
if (code != SUCCESS) {
|
||||
if (load(path)) {
|
||||
this.name = path.substring(
|
||||
path.lastIndexOf("/") + 1
|
||||
);
|
||||
|
@ -63,6 +46,10 @@ public class MarkdownFile {
|
|||
this.content = "";
|
||||
}
|
||||
|
||||
public static void setDefaultRootDir(String defaultRootDir) {
|
||||
MarkdownFile.defaultRootDir = defaultRootDir;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
@ -75,6 +62,10 @@ public class MarkdownFile {
|
|||
return path;
|
||||
}
|
||||
|
||||
public void setPath(String path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public String getFullPath() {
|
||||
String fullPath;
|
||||
|
||||
|
@ -95,10 +86,6 @@ public class MarkdownFile {
|
|||
return fullPath + this.name;
|
||||
}
|
||||
|
||||
public void setPath(String path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
@ -107,61 +94,46 @@ public class MarkdownFile {
|
|||
this.content = content;
|
||||
}
|
||||
|
||||
public int load(InputStream in) {
|
||||
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() + "\n");
|
||||
sb.append(s.next()).append("\n");
|
||||
}
|
||||
this.content = sb.toString();
|
||||
return SUCCESS;
|
||||
try {
|
||||
in.close();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public int load(String path) {
|
||||
private boolean load(String path) {
|
||||
return load(new File(path));
|
||||
}
|
||||
|
||||
public int load(File markdownFile) {
|
||||
int code;
|
||||
if (markdownFile.exists() && markdownFile.canRead()) {
|
||||
BufferedReader reader = null;
|
||||
private boolean load(File markdownFile) {
|
||||
if (!markdownFile.exists() || !markdownFile.canRead()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
this.name = markdownFile.getName();
|
||||
this.path = markdownFile.getParentFile().getAbsolutePath();
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line;
|
||||
reader = new BufferedReader(new FileReader(markdownFile));
|
||||
while ((line = reader.readLine()) != null)
|
||||
sb.append(line + "\n");
|
||||
this.content = sb.toString();
|
||||
code = SUCCESS;
|
||||
return load(new FileInputStream(markdownFile));
|
||||
} catch (FileNotFoundException e) {
|
||||
code = FILE_NOT_EXISTS;
|
||||
} catch (IOException e) {
|
||||
code = READ_ERROR;
|
||||
return false;
|
||||
}
|
||||
if (reader != null) {
|
||||
try {
|
||||
reader.close();
|
||||
} catch (IOException e) {
|
||||
// closing the reader failed
|
||||
}
|
||||
}
|
||||
} else {
|
||||
code = READ_ERROR;
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
public int load() {
|
||||
if (this.name.isEmpty())
|
||||
return PARAMETERS_MISSING;
|
||||
return load(getFullPath());
|
||||
public boolean load() {
|
||||
return !this.name.isEmpty() && load(getFullPath());
|
||||
}
|
||||
|
||||
public int save(String path) {
|
||||
int code;
|
||||
public boolean save(String path) {
|
||||
if (path == null) {
|
||||
path = this.getFullPath();
|
||||
}
|
||||
|
@ -169,23 +141,28 @@ public class MarkdownFile {
|
|||
File markdownFile = new File(path);
|
||||
if (!markdownFile.exists()) {
|
||||
try {
|
||||
markdownFile.createNewFile();
|
||||
if (!markdownFile.createNewFile()) {
|
||||
return false;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return WRITE_ERROR;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (markdownFile.canWrite()) {
|
||||
|
||||
if (!markdownFile.canWrite()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
OutputStreamWriter writer = null;
|
||||
try {
|
||||
writer = new OutputStreamWriter(
|
||||
new FileOutputStream(markdownFile)
|
||||
);
|
||||
writer.write(this.content);
|
||||
code = SUCCESS;
|
||||
} catch (IOException e) {
|
||||
code = WRITE_ERROR;
|
||||
}
|
||||
return false;
|
||||
} finally {
|
||||
if (writer != null) {
|
||||
try {
|
||||
writer.close();
|
||||
|
@ -193,11 +170,10 @@ public class MarkdownFile {
|
|||
// closing the reader failed
|
||||
}
|
||||
}
|
||||
} else {
|
||||
code = WRITE_ERROR;
|
||||
}
|
||||
|
||||
this.name = markdownFile.getName();
|
||||
this.path = markdownFile.getParentFile().getAbsolutePath();
|
||||
return code;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,8 @@ public interface MarkdownPresenter {
|
|||
void loadMarkdown(InputStream in);
|
||||
void loadMarkdown(File file);
|
||||
void loadFromUri(Context context, Uri fileUri);
|
||||
void loadTempMarkdown(InputStream in, OnTempFileLoadedListener listener);
|
||||
|
||||
void loadMarkdown(InputStream in, OnTempFileLoadedListener listener);
|
||||
void newFile(String path);
|
||||
void setEditView(MarkdownEditView editView);
|
||||
void setPreviewView(MarkdownPreviewView previewView);
|
||||
|
@ -31,12 +32,13 @@ public interface MarkdownPresenter {
|
|||
String getMarkdown();
|
||||
void setMarkdown(String markdown);
|
||||
|
||||
abstract class OnTempFileLoadedListener {
|
||||
public abstract void onSuccess(String markdown);
|
||||
public abstract void onError(int code);
|
||||
interface OnTempFileLoadedListener {
|
||||
void onSuccess(String markdown);
|
||||
|
||||
void onError();
|
||||
}
|
||||
|
||||
interface MarkdownSavedListener {
|
||||
void saveComplete();
|
||||
void saveComplete(boolean success);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,25 +5,22 @@ import android.database.Cursor;
|
|||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.provider.OpenableColumns;
|
||||
import android.util.Log;
|
||||
|
||||
import com.commonsware.cwac.anddown.AndDown;
|
||||
import com.wbrawner.simplemarkdown.Utils;
|
||||
import com.wbrawner.simplemarkdown.model.MarkdownFile;
|
||||
import com.wbrawner.simplemarkdown.view.MarkdownEditView;
|
||||
import com.wbrawner.simplemarkdown.view.MarkdownPreviewView;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class MarkdownPresenterImpl implements MarkdownPresenter {
|
||||
private MarkdownFile file;
|
||||
private MarkdownEditView editView;
|
||||
private MarkdownPreviewView previewView;
|
||||
private String TAG = MarkdownPresenterImpl.class.getSimpleName();
|
||||
private int HOEDOWN_FLAGS =
|
||||
AndDown.HOEDOWN_EXT_STRIKETHROUGH | AndDown.HOEDOWN_EXT_TABLES |
|
||||
AndDown.HOEDOWN_EXT_UNDERLINE | AndDown.HOEDOWN_EXT_SUPERSCRIPT |
|
||||
AndDown.HOEDOWN_EXT_FENCED_CODE;
|
||||
private Handler fileHandler = new Handler();
|
||||
|
||||
public MarkdownPresenterImpl(MarkdownFile file) {
|
||||
|
@ -44,14 +41,12 @@ public class MarkdownPresenterImpl implements MarkdownPresenter {
|
|||
@Override
|
||||
public void loadMarkdown() {
|
||||
Runnable fileLoader = () -> {
|
||||
int result = this.file.load();
|
||||
boolean result = this.file.load();
|
||||
|
||||
if (editView != null) {
|
||||
if (result == MarkdownFile.SUCCESS) {
|
||||
editView.onFileLoaded(result);
|
||||
editView.setMarkdown(getMarkdown());
|
||||
onMarkdownEdited();
|
||||
} else {
|
||||
editView.showFileLoadeddError(result);
|
||||
}
|
||||
}
|
||||
};
|
||||
fileHandler.post(fileLoader);
|
||||
|
@ -59,47 +54,36 @@ public class MarkdownPresenterImpl implements MarkdownPresenter {
|
|||
|
||||
@Override
|
||||
public void loadMarkdown(File file) {
|
||||
Runnable fileLoader = () -> {
|
||||
int result = this.file.load(file);
|
||||
if (editView != null) {
|
||||
if (result == MarkdownFile.SUCCESS) {
|
||||
editView.setTitle(this.file.getName());
|
||||
editView.setMarkdown(getMarkdown());
|
||||
onMarkdownEdited();
|
||||
} else {
|
||||
editView.showFileLoadeddError(result);
|
||||
InputStream in = null;
|
||||
try {
|
||||
in = new FileInputStream(file);
|
||||
loadMarkdown(in);
|
||||
} catch (FileNotFoundException e) {
|
||||
System.err.println(e.getLocalizedMessage());
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
Utils.closeQuietly(in);
|
||||
}
|
||||
}
|
||||
};
|
||||
fileHandler.post(fileLoader);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadMarkdown(InputStream in) {
|
||||
Runnable fileLoader = () -> {
|
||||
int result = file.load(in);
|
||||
if (result == MarkdownFile.SUCCESS) {
|
||||
if (editView != null)
|
||||
editView.setMarkdown(getMarkdown());
|
||||
onMarkdownEdited();
|
||||
} else {
|
||||
if (editView != null)
|
||||
editView.showFileLoadeddError(result);
|
||||
}
|
||||
};
|
||||
fileHandler.post(fileLoader);
|
||||
this.loadMarkdown(in, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadTempMarkdown(InputStream in, OnTempFileLoadedListener listener) {
|
||||
public void loadMarkdown(final InputStream in, final OnTempFileLoadedListener listener) {
|
||||
Runnable fileLoader = () -> {
|
||||
MarkdownFile tmpFile = new MarkdownFile();
|
||||
int result = tmpFile.load(in);
|
||||
if (result == MarkdownFile.SUCCESS) {
|
||||
if (tmpFile.load(in)) {
|
||||
String html = generateHTML(tmpFile.getContent());
|
||||
if (listener != null) {
|
||||
listener.onSuccess(html);
|
||||
}
|
||||
} else {
|
||||
listener.onError(result);
|
||||
if (listener != null) {
|
||||
listener.onError();
|
||||
}
|
||||
}
|
||||
};
|
||||
fileHandler.post(fileLoader);
|
||||
|
@ -128,19 +112,12 @@ public class MarkdownPresenterImpl implements MarkdownPresenter {
|
|||
@Override
|
||||
public void saveMarkdown(MarkdownSavedListener listener, String filePath) {
|
||||
Runnable fileSaver = () -> {
|
||||
int code;
|
||||
code = file.save(filePath);
|
||||
|
||||
boolean result = file.save(filePath);
|
||||
if (listener != null) {
|
||||
listener.saveComplete();
|
||||
listener.saveComplete(result);
|
||||
}
|
||||
|
||||
if (editView != null) {
|
||||
if (code == MarkdownFile.SUCCESS) {
|
||||
editView.showFileSavedMessage();
|
||||
} else {
|
||||
editView.showFileSavedError(code);
|
||||
}
|
||||
editView.onFileSaved(result);
|
||||
}
|
||||
};
|
||||
fileHandler.post(fileSaver);
|
||||
|
@ -164,6 +141,9 @@ public class MarkdownPresenterImpl implements MarkdownPresenter {
|
|||
@Override
|
||||
public String generateHTML(String markdown) {
|
||||
AndDown andDown = new AndDown();
|
||||
int HOEDOWN_FLAGS = AndDown.HOEDOWN_EXT_STRIKETHROUGH | AndDown.HOEDOWN_EXT_TABLES |
|
||||
AndDown.HOEDOWN_EXT_UNDERLINE | AndDown.HOEDOWN_EXT_SUPERSCRIPT |
|
||||
AndDown.HOEDOWN_EXT_FENCED_CODE;
|
||||
return andDown.markdownToHtml(markdown, HOEDOWN_FLAGS, 0);
|
||||
}
|
||||
|
||||
|
@ -210,14 +190,16 @@ public class MarkdownPresenterImpl implements MarkdownPresenter {
|
|||
.getColumnIndex(OpenableColumns.DISPLAY_NAME);
|
||||
retCur.moveToFirst();
|
||||
setFileName(retCur.getString(nameIndex));
|
||||
retCur.close();
|
||||
}
|
||||
} else if (fileUri.getScheme().equals("file")) {
|
||||
setFileName(fileUri.getLastPathSegment());
|
||||
}
|
||||
loadMarkdown(in);
|
||||
} catch (Exception e) {
|
||||
if (editView != null)
|
||||
editView.showFileLoadeddError(MarkdownFile.READ_ERROR);
|
||||
if (editView != null) {
|
||||
editView.onFileLoaded(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
package com.wbrawner.simplemarkdown.view;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
public class DisableableViewPager extends ViewPager {
|
||||
private boolean isSwipeLocked = false;
|
||||
|
||||
public DisableableViewPager(@NonNull Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public DisableableViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onInterceptTouchEvent(MotionEvent ev) {
|
||||
return !isSwipeLocked && super.onInterceptTouchEvent(ev);
|
||||
}
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent ev) {
|
||||
return !isSwipeLocked && super.onTouchEvent(ev);
|
||||
}
|
||||
|
||||
public void setSwipeLocked(boolean locked) {
|
||||
this.isSwipeLocked = locked;
|
||||
}
|
||||
}
|
|
@ -1,11 +1,13 @@
|
|||
package com.wbrawner.simplemarkdown.view;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public interface MarkdownEditView {
|
||||
String getMarkdown();
|
||||
void setMarkdown(String markdown);
|
||||
void setTitle(String title);
|
||||
void showFileSavedMessage();
|
||||
void showFileSavedError(int code);
|
||||
void showFileLoadedMessage();
|
||||
void showFileLoadeddError(int code);
|
||||
|
||||
void onFileSaved(boolean success);
|
||||
|
||||
void onFileLoaded(boolean success);
|
||||
}
|
||||
|
|
|
@ -3,12 +3,13 @@ package com.wbrawner.simplemarkdown.view.activity;
|
|||
import android.Manifest;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.res.AssetManager;
|
||||
import android.content.res.Configuration;
|
||||
import android.os.Build;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.design.widget.TabLayout;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.view.Menu;
|
||||
|
@ -20,6 +21,7 @@ import com.wbrawner.simplemarkdown.MarkdownApplication;
|
|||
import com.wbrawner.simplemarkdown.R;
|
||||
import com.wbrawner.simplemarkdown.Utils;
|
||||
import com.wbrawner.simplemarkdown.presentation.MarkdownPresenter;
|
||||
import com.wbrawner.simplemarkdown.view.DisableableViewPager;
|
||||
import com.wbrawner.simplemarkdown.view.adapter.EditPagerAdapter;
|
||||
|
||||
import java.io.File;
|
||||
|
@ -39,17 +41,15 @@ public class MainActivity extends AppCompatActivity
|
|||
static final String EXTRA_FILE = "EXTRA_FILE";
|
||||
static final String EXTRA_FILE_PATH = "EXTRA_FILE_PATH";
|
||||
static final String EXTRA_REQUEST_CODE = "EXTRA_REQUEST_CODE";
|
||||
public static final String AUTHORITY = "com.wbrawner.simplemarkdown.fileprovider";
|
||||
|
||||
@Inject
|
||||
MarkdownPresenter presenter;
|
||||
|
||||
@BindView(R.id.pager)
|
||||
ViewPager pager;
|
||||
DisableableViewPager pager;
|
||||
@BindView(R.id.layout_tab)
|
||||
TabLayout tabLayout;
|
||||
|
||||
private static final String TAG = MainActivity.class.getSimpleName();
|
||||
private NewFileHandler newFileHandler;
|
||||
|
||||
@Override
|
||||
|
@ -140,6 +140,10 @@ public class MainActivity extends AppCompatActivity
|
|||
case R.id.action_new:
|
||||
presenter.saveMarkdown(newFileHandler, null);
|
||||
break;
|
||||
case R.id.action_lock_swipe:
|
||||
item.setChecked(!item.isChecked());
|
||||
pager.setSwipeLocked(item.isChecked());
|
||||
break;
|
||||
case R.id.action_help:
|
||||
showInfoActivity(R.id.action_help);
|
||||
break;
|
||||
|
@ -171,8 +175,11 @@ public class MainActivity extends AppCompatActivity
|
|||
infoIntent.putExtra("title", title);
|
||||
InputStream in = null;
|
||||
try {
|
||||
in = getAssets().open(fileName);
|
||||
presenter.loadTempMarkdown(in, new MarkdownPresenter.OnTempFileLoadedListener() {
|
||||
AssetManager assetManager = getAssets();
|
||||
if (assetManager != null) {
|
||||
in = assetManager.open(fileName);
|
||||
}
|
||||
presenter.loadMarkdown(in, new MarkdownPresenter.OnTempFileLoadedListener() {
|
||||
@Override
|
||||
public void onSuccess(String html) {
|
||||
infoIntent.putExtra("html", html);
|
||||
|
@ -180,7 +187,7 @@ public class MainActivity extends AppCompatActivity
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onError(int code) {
|
||||
public void onError() {
|
||||
Toast.makeText(MainActivity.this, R.string.file_load_error, Toast.LENGTH_SHORT)
|
||||
.show();
|
||||
}
|
||||
|
@ -188,17 +195,14 @@ public class MainActivity extends AppCompatActivity
|
|||
} catch (Exception e) {
|
||||
Toast.makeText(MainActivity.this, R.string.file_load_error, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
if (in != null) {
|
||||
try {
|
||||
in.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode,
|
||||
String permissions[], int[] grantResults) {
|
||||
public void onRequestPermissionsResult(
|
||||
int requestCode,
|
||||
@NonNull String permissions[],
|
||||
@NonNull int[] grantResults
|
||||
) {
|
||||
switch (requestCode) {
|
||||
case WRITE_PERMISSION_REQUEST: {
|
||||
// If request is cancelled, the result arrays are empty.
|
||||
|
@ -293,9 +297,17 @@ public class MainActivity extends AppCompatActivity
|
|||
|
||||
private class NewFileHandler implements MarkdownPresenter.MarkdownSavedListener {
|
||||
@Override
|
||||
public void saveComplete() {
|
||||
public void saveComplete(boolean success) {
|
||||
if (success) {
|
||||
String newFile = Utils.getDefaultFileName(MainActivity.this);
|
||||
presenter.newFile(newFile);
|
||||
} else {
|
||||
Toast.makeText(
|
||||
MainActivity.this,
|
||||
R.string.file_save_error,
|
||||
Toast.LENGTH_SHORT
|
||||
).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +1,13 @@
|
|||
package com.wbrawner.simplemarkdown.view.activity;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.support.v7.app.ActionBar;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.webkit.WebSettings;
|
||||
import android.webkit.WebView;
|
||||
|
||||
import com.wbrawner.simplemarkdown.R;
|
||||
import com.wbrawner.simplemarkdown.view.fragment.PreviewFragment;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
|
@ -24,15 +22,19 @@ public class MarkdownInfoActivity extends AppCompatActivity {
|
|||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_markdown_info);
|
||||
ButterKnife.bind(this);
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
ActionBar supportActionBar = getSupportActionBar();
|
||||
if (supportActionBar != null) {
|
||||
supportActionBar.setDisplayHomeAsUpEnabled(true);
|
||||
}
|
||||
Intent intent = getIntent();
|
||||
if (intent == null || !intent.hasExtra("title") || !intent.hasExtra("html"))
|
||||
if (intent == null || !intent.hasExtra("title") || !intent.hasExtra("html")) {
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
setTitle(intent.getStringExtra("title"));
|
||||
String style = "<style>pre {overflow:scroll;}</style>";
|
||||
infoWebview.loadDataWithBaseURL(
|
||||
null,
|
||||
style + intent.getStringExtra("html"),
|
||||
PreviewFragment.style + intent.getStringExtra("html"),
|
||||
"text/html",
|
||||
"UTF-8",
|
||||
null
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
package com.wbrawner.simplemarkdown.view.fragment;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.view.LayoutInflater;
|
||||
|
@ -14,7 +15,6 @@ import com.jakewharton.rxbinding2.widget.RxTextView;
|
|||
import com.wbrawner.simplemarkdown.MarkdownApplication;
|
||||
import com.wbrawner.simplemarkdown.R;
|
||||
import com.wbrawner.simplemarkdown.Utils;
|
||||
import com.wbrawner.simplemarkdown.model.MarkdownFile;
|
||||
import com.wbrawner.simplemarkdown.presentation.MarkdownPresenter;
|
||||
import com.wbrawner.simplemarkdown.view.MarkdownEditView;
|
||||
|
||||
|
@ -30,40 +30,36 @@ import io.reactivex.android.schedulers.AndroidSchedulers;
|
|||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
public class EditFragment extends Fragment implements MarkdownEditView {
|
||||
public static final String SAVE_ACTION = "com.wbrawner.simplemarkdown.ACTION_SAVE";
|
||||
public static final String LOAD_ACTION = "com.wbrawner.simplemarkdown.ACTION_LOAD";
|
||||
private String TAG = EditFragment.class.getSimpleName();
|
||||
private Unbinder unbinder;
|
||||
|
||||
@Inject
|
||||
MarkdownPresenter presenter;
|
||||
|
||||
@BindView(R.id.markdown_edit)
|
||||
EditText markdownEditor;
|
||||
private Unbinder unbinder;
|
||||
|
||||
public EditFragment() {
|
||||
// Required empty public constructor
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
// Inflate the layout for this fragment
|
||||
View view = inflater.inflate(R.layout.fragment_edit, container, false);
|
||||
unbinder = ButterKnife.bind(this, view);
|
||||
((MarkdownApplication) getActivity().getApplication()).getComponent().inject(this);
|
||||
Activity activity = getActivity();
|
||||
if (activity != null) {
|
||||
((MarkdownApplication) activity.getApplication()).getComponent().inject(this);
|
||||
}
|
||||
Observable<String> obs = RxTextView.textChanges(markdownEditor)
|
||||
.debounce(50, TimeUnit.MILLISECONDS).map(CharSequence::toString);
|
||||
obs.subscribeOn(Schedulers.io());
|
||||
obs.observeOn(AndroidSchedulers.mainThread());
|
||||
obs.subscribe(markdown -> {
|
||||
presenter.onMarkdownEdited(markdown);
|
||||
});
|
||||
obs.subscribe(markdown -> presenter.onMarkdownEdited(markdown));
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
presenter.setEditView(EditFragment.this);
|
||||
presenter.loadMarkdown();
|
||||
|
@ -87,47 +83,37 @@ public class EditFragment extends Fragment implements MarkdownEditView {
|
|||
return markdownEditor.getText().toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMarkdown(String markdown) {
|
||||
markdownEditor.setText(markdown);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTitle(String title) {
|
||||
getActivity().setTitle(title);
|
||||
Activity activity = getActivity();
|
||||
if (activity != null) {
|
||||
activity.setTitle(title);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showFileSavedMessage() {
|
||||
public void onFileSaved(boolean success) {
|
||||
String location = Utils.getDocsPath(getActivity()) + presenter.getFileName();
|
||||
String message = getString(R.string.file_saved, location);
|
||||
Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showFileSavedError(int code) {
|
||||
String message = "";
|
||||
switch (code) {
|
||||
case MarkdownFile.WRITE_ERROR:
|
||||
message = getString(R.string.error_write);
|
||||
break;
|
||||
default:
|
||||
String message;
|
||||
if (success) {
|
||||
message = getString(R.string.file_saved, location);
|
||||
} else {
|
||||
message = getString(R.string.file_save_error);
|
||||
}
|
||||
Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showFileLoadedMessage() {
|
||||
Toast.makeText(getActivity(), R.string.file_loaded, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showFileLoadeddError(int code) {
|
||||
String message = "";
|
||||
public void onFileLoaded(boolean success) {
|
||||
int message = success ? R.string.file_loaded : R.string.file_load_error;
|
||||
Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMarkdown(String markdown) {
|
||||
markdownEditor.setText(markdown);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package com.wbrawner.simplemarkdown.view.fragment;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.view.LayoutInflater;
|
||||
|
@ -22,32 +24,39 @@ import butterknife.Unbinder;
|
|||
|
||||
public class PreviewFragment extends Fragment implements MarkdownPreviewView {
|
||||
private static final String TAG = PreviewFragment.class.getSimpleName();
|
||||
private Unbinder unbinder;
|
||||
|
||||
public static String style = "<style>" +
|
||||
"pre {overflow:scroll; padding:15px; background: #F1F1F1;}" +
|
||||
"</style>";
|
||||
@Inject
|
||||
MarkdownPresenter presenter;
|
||||
|
||||
@BindView(R.id.markdown_view)
|
||||
WebView markdownPreview;
|
||||
private Unbinder unbinder;
|
||||
|
||||
public PreviewFragment() {
|
||||
// Required empty public constructor
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
public View onCreateView(
|
||||
@NonNull LayoutInflater inflater,
|
||||
ViewGroup container,
|
||||
Bundle savedInstanceState
|
||||
) {
|
||||
// Inflate the layout for this fragment
|
||||
View view = inflater.inflate(R.layout.fragment_preview, container, false);
|
||||
unbinder = ButterKnife.bind(this, view);
|
||||
((MarkdownApplication) getActivity().getApplication()).getComponent().inject(this);
|
||||
Activity activity = getActivity();
|
||||
if (activity != null) {
|
||||
((MarkdownApplication) activity.getApplication()).getComponent().inject(this);
|
||||
}
|
||||
if (BuildConfig.DEBUG)
|
||||
WebView.setWebContentsDebuggingEnabled(true);
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
}
|
||||
|
||||
|
@ -56,9 +65,13 @@ public class PreviewFragment extends Fragment implements MarkdownPreviewView {
|
|||
@Override
|
||||
public void updatePreview(String html) {
|
||||
if (markdownPreview != null) {
|
||||
markdownPreview.post(() -> {
|
||||
markdownPreview.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
|
||||
});
|
||||
markdownPreview.post(() -> markdownPreview.loadDataWithBaseURL(
|
||||
null,
|
||||
style + html,
|
||||
"text/html",
|
||||
"UTF-8",
|
||||
null
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,26 +3,27 @@
|
|||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
<android.support.v4.view.ViewPager
|
||||
|
||||
<com.wbrawner.simplemarkdown.view.DisableableViewPager
|
||||
android:id="@+id/pager"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
<android.support.design.widget.TabLayout
|
||||
android:id="@+id/layout_tab"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/layout_tab"
|
||||
android:layout_gravity="top">
|
||||
<android.support.design.widget.TabItem
|
||||
android:id="@+id/tab_edit"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/action_edit"
|
||||
android:id="@+id/tab_edit" />
|
||||
android:text="@string/action_edit" />
|
||||
<android.support.design.widget.TabItem
|
||||
android:id="@+id/tab_preview"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/action_preview"
|
||||
android:id="@+id/tab_preview" />
|
||||
android:text="@string/action_preview" />
|
||||
</android.support.design.widget.TabLayout>
|
||||
</android.support.v4.view.ViewPager>
|
||||
</com.wbrawner.simplemarkdown.view.DisableableViewPager>
|
||||
|
||||
</LinearLayout>
|
||||
|
|
|
@ -2,38 +2,38 @@
|
|||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<item
|
||||
android:title="@string/action_share"
|
||||
android:id="@+id/action_share"
|
||||
android:icon="@drawable/ic_action_share"
|
||||
android:title="@string/action_share"
|
||||
app:showAsAction="ifRoom" />
|
||||
<item
|
||||
android:title="@string/action_save"
|
||||
android:id="@+id/action_save"
|
||||
android:icon="@drawable/ic_action_save"
|
||||
android:title="@string/action_save"
|
||||
app:showAsAction="never" />
|
||||
<item
|
||||
android:id="@+id/action_load"
|
||||
android:orderInCategory="100"
|
||||
android:title="@string/action_open"
|
||||
app:showAsAction="never" />
|
||||
<item
|
||||
android:id="@+id/action_new"
|
||||
android:orderInCategory="150"
|
||||
android:title="@string/action_new"
|
||||
app:showAsAction="never" />
|
||||
<item
|
||||
android:id="@+id/action_lock_swipe"
|
||||
android:checkable="true"
|
||||
android:title="@string/action_lock_swipe"
|
||||
app:showAsAction="never" />
|
||||
<item
|
||||
android:id="@+id/action_settings"
|
||||
android:orderInCategory="200"
|
||||
android:title="@string/action_settings"
|
||||
app:showAsAction="never" />
|
||||
<item
|
||||
android:id="@+id/action_help"
|
||||
android:orderInCategory="200"
|
||||
android:title="@string/action_help"
|
||||
app:showAsAction="never" />
|
||||
<item
|
||||
android:id="@+id/action_libraries"
|
||||
android:orderInCategory="300"
|
||||
android:title="@string/action_libraries"
|
||||
app:showAsAction="never" />
|
||||
</menu>
|
|
@ -32,4 +32,5 @@
|
|||
<string name="title_activity_settings">Settings</string>
|
||||
<string name="pref_title_autosave">Enable autosave</string>
|
||||
<string name="pref_description_autosave">Automatically save files when closing the app</string>
|
||||
<string name="action_lock_swipe">Lock Swiping</string>
|
||||
</resources>
|
||||
|
|
Loading…
Reference in a new issue