Convert MarkdownPresenterImpl to Kotlin

This commit is contained in:
Billy Brawner 2019-08-15 18:44:55 -05:00 committed by William Brawner
parent 04e8bb1c03
commit b002881dc3
2 changed files with 166 additions and 197 deletions

View file

@ -1,197 +0,0 @@
package com.wbrawner.simplemarkdown.presentation;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.provider.OpenableColumns;
import com.commonsware.cwac.anddown.AndDown;
import com.wbrawner.simplemarkdown.model.MarkdownFile;
import com.wbrawner.simplemarkdown.utility.ErrorHandler;
import com.wbrawner.simplemarkdown.view.MarkdownEditView;
import com.wbrawner.simplemarkdown.view.MarkdownPreviewView;
import java.io.InputStream;
import java.io.OutputStream;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public class MarkdownPresenterImpl implements MarkdownPresenter {
private final Object fileLock = new Object();
private MarkdownFile file;
private volatile MarkdownEditView editView;
private volatile MarkdownPreviewView previewView;
private Handler fileHandler = new Handler();
private final ErrorHandler errorHandler;
@Inject
public MarkdownPresenterImpl(ErrorHandler errorHandler) {
this.errorHandler = errorHandler;
synchronized (fileLock) {
this.file = new MarkdownFile();
}
}
@Override
public void loadMarkdown(
final String fileName,
final InputStream in,
final FileLoadedListener listener,
boolean replaceCurrentFile
) {
Runnable fileLoader = () -> {
MarkdownFile tmpFile = new MarkdownFile();
if (tmpFile.load(fileName, in)) {
if (listener != null) {
String html = generateHTML(tmpFile.getContent());
listener.onSuccess(html);
}
if (replaceCurrentFile) {
synchronized (fileLock) {
this.file = tmpFile;
MarkdownEditView currentEditView = editView;
if (currentEditView != null) {
currentEditView.onFileLoaded(true);
currentEditView.setTitle(fileName);
currentEditView.setMarkdown(this.file.getContent());
onMarkdownEdited(null);
}
}
}
} else {
if (listener != null) {
listener.onError();
}
}
};
fileHandler.post(fileLoader);
}
@Override
public void newFile(String newName) {
synchronized (fileLock) {
MarkdownEditView currentEditView = editView;
if (currentEditView != null) {
file.setContent(currentEditView.getMarkdown());
currentEditView.setTitle(newName);
currentEditView.setMarkdown("");
}
file = new MarkdownFile(newName, "");
}
}
@Override
public void setEditView(MarkdownEditView editView) {
this.editView = editView;
onMarkdownEdited(null);
}
@Override
public void setPreviewView(MarkdownPreviewView previewView) {
this.previewView = previewView;
}
@Override
public void saveMarkdown(MarkdownSavedListener listener, String name, OutputStream outputStream) {
Runnable fileSaver = () -> {
boolean result;
synchronized (fileLock) {
result = file.save(name, outputStream);
}
if (listener != null) {
listener.saveComplete(result);
}
MarkdownEditView currentEditView = editView;
if (currentEditView != null) {
synchronized (fileLock) {
currentEditView.setTitle(file.getName());
}
currentEditView.onFileSaved(result);
}
};
fileHandler.post(fileSaver);
}
@Override
public void onMarkdownEdited(String markdown) {
setMarkdown(markdown);
Runnable generateMarkdown = () -> {
MarkdownPreviewView currentPreviewView = previewView;
if (currentPreviewView != null)
currentPreviewView.updatePreview(generateHTML(null));
};
fileHandler.post(generateMarkdown);
}
@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);
}
@Override
public String getFileName() {
synchronized (fileLock) {
return file.getName();
}
}
@Override
public void setFileName(String name) {
synchronized (fileLock) {
file.setName(name);
}
}
@Override
public String getMarkdown() {
synchronized (fileLock) {
return file.getContent();
}
}
@Override
public void setMarkdown(String markdown) {
synchronized (fileLock) {
file.setContent(markdown);
}
}
@Override
public void loadFromUri(Context context, Uri fileUri) {
try {
InputStream in =
context.getContentResolver().openInputStream(fileUri);
String fileName = null;
if ("content".equals(fileUri.getScheme())) {
Cursor retCur = context.getContentResolver()
.query(fileUri, null, null, null, null);
if (retCur != null) {
int nameIndex = retCur
.getColumnIndex(OpenableColumns.DISPLAY_NAME);
retCur.moveToFirst();
fileName = retCur.getString(nameIndex);
retCur.close();
}
} else if ("file".equals(fileUri.getScheme())) {
fileName = fileUri.getLastPathSegment();
}
if (fileName == null) {
fileName = "Untitled.md";
}
loadMarkdown(fileName, in, null, true);
} catch (Exception e) {
errorHandler.reportException(e);
MarkdownEditView currentEditView = editView;
if (currentEditView != null) {
currentEditView.onFileLoaded(false);
}
}
}
}

View file

@ -0,0 +1,166 @@
package com.wbrawner.simplemarkdown.presentation
import android.content.Context
import android.net.Uri
import android.os.Handler
import android.provider.OpenableColumns
import com.commonsware.cwac.anddown.AndDown
import com.wbrawner.simplemarkdown.model.MarkdownFile
import com.wbrawner.simplemarkdown.utility.ErrorHandler
import com.wbrawner.simplemarkdown.view.MarkdownEditView
import com.wbrawner.simplemarkdown.view.MarkdownPreviewView
import java.io.InputStream
import java.io.OutputStream
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class MarkdownPresenterImpl @Inject
constructor(private val errorHandler: ErrorHandler) : MarkdownPresenter {
private val fileLock = Any()
private var file: MarkdownFile? = null
@Volatile
private var editView: MarkdownEditView? = null
@Volatile
private var previewView: MarkdownPreviewView? = null
private val fileHandler = Handler()
override var fileName: String
get() = synchronized(fileLock) {
return file!!.name
}
set(name) = synchronized(fileLock) {
file!!.name = name
}
override var markdown: String
get() = synchronized(fileLock) {
return file!!.content
}
set(markdown) = synchronized(fileLock) {
file!!.content = markdown
}
init {
synchronized(fileLock) {
this.file = MarkdownFile()
}
}
override fun loadMarkdown(
fileName: String,
`in`: InputStream,
listener: MarkdownPresenter.FileLoadedListener?,
replaceCurrentFile: Boolean
) {
val fileLoader = {
val tmpFile = MarkdownFile()
if (tmpFile.load(fileName, `in`)) {
if (listener != null) {
val html = generateHTML(tmpFile.content)
listener.onSuccess(html)
}
if (replaceCurrentFile) {
synchronized(fileLock) {
this.file = tmpFile
val currentEditView = editView
if (currentEditView != null) {
currentEditView.onFileLoaded(true)
currentEditView.setTitle(fileName)
currentEditView.markdown = this.file!!.content
onMarkdownEdited(null)
}
}
}
} else {
listener?.onError()
}
}
fileHandler.post(fileLoader)
}
override fun newFile(newName: String) {
synchronized(fileLock) {
val currentEditView = editView
if (currentEditView != null) {
file!!.content = currentEditView.markdown
currentEditView.setTitle(newName)
currentEditView.markdown = ""
}
file = MarkdownFile(newName, "")
}
}
override fun setEditView(editView: MarkdownEditView) {
this.editView = editView
onMarkdownEdited(null)
}
override fun setPreviewView(previewView: MarkdownPreviewView) {
this.previewView = previewView
}
override fun saveMarkdown(listener: MarkdownPresenter.MarkdownSavedListener, name: String, outputStream: OutputStream) {
val fileSaver = {
val result: Boolean
synchronized(fileLock) {
result = file!!.save(name, outputStream)
}
listener?.saveComplete(result)
val currentEditView = editView
if (currentEditView != null) {
synchronized(fileLock) {
currentEditView.setTitle(file!!.name)
}
currentEditView.onFileSaved(result)
}
}
fileHandler.post(fileSaver)
}
override fun onMarkdownEdited(markdown: String?) {
markdown = markdown
val generateMarkdown = {
val currentPreviewView = previewView
currentPreviewView?.updatePreview(generateHTML(null))
}
fileHandler.post(generateMarkdown)
}
override fun generateHTML(markdown: String?): String {
val andDown = AndDown()
val HOEDOWN_FLAGS = AndDown.HOEDOWN_EXT_STRIKETHROUGH or AndDown.HOEDOWN_EXT_TABLES or
AndDown.HOEDOWN_EXT_UNDERLINE or AndDown.HOEDOWN_EXT_SUPERSCRIPT or
AndDown.HOEDOWN_EXT_FENCED_CODE
return andDown.markdownToHtml(markdown, HOEDOWN_FLAGS, 0)
}
override fun loadFromUri(context: Context, fileUri: Uri) {
try {
val `in` = context.contentResolver.openInputStream(fileUri)
var fileName: String? = null
if ("content" == fileUri.scheme) {
val retCur = context.contentResolver
.query(fileUri, null, null, null, null)
if (retCur != null) {
val nameIndex = retCur
.getColumnIndex(OpenableColumns.DISPLAY_NAME)
retCur.moveToFirst()
fileName = retCur.getString(nameIndex)
retCur.close()
}
} else if ("file" == fileUri.scheme) {
fileName = fileUri.lastPathSegment
}
if (fileName == null) {
fileName = "Untitled.md"
}
loadMarkdown(fileName, `in`!!, null, true)
} catch (e: Exception) {
errorHandler.reportException(e)
val currentEditView = editView
currentEditView?.onFileLoaded(false)
}
}
}