Save on ref images / edit

pull/167/head
M66B 5 years ago
parent b3480453ad
commit 1e945e69e5

@ -219,13 +219,17 @@ public class EntityMessage implements Serializable {
return new File(dir, id + "." + revision); return new File(dir, id + "." + revision);
} }
File getRefFile(Context context) { static File getRefFile(Context context, Long id) {
File dir = new File(context.getFilesDir(), "references"); File dir = new File(context.getFilesDir(), "references");
if (!dir.exists()) if (!dir.exists())
dir.mkdir(); dir.mkdir();
return new File(dir, id.toString()); return new File(dir, id.toString());
} }
File getRefFile(Context context) {
return getRefFile(context, id);
}
File getRawFile(Context context) { File getRawFile(Context context) {
File dir = new File(context.getFilesDir(), "raw"); File dir = new File(context.getFilesDir(), "raw");
if (!dir.exists()) if (!dir.exists())

@ -112,11 +112,9 @@ import org.openintents.openpgp.util.OpenPgpApi;
import org.openintents.openpgp.util.OpenPgpServiceConnection; import org.openintents.openpgp.util.OpenPgpServiceConnection;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
@ -754,9 +752,10 @@ public class FragmentCompose extends FragmentBase {
private void convertRef(boolean plain) { private void convertRef(boolean plain) {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", working); args.putLong("id", working);
args.putBoolean("plain", plain);
args.putString("body", HtmlHelper.toHtml(etBody.getText())); args.putString("body", HtmlHelper.toHtml(etBody.getText()));
new SimpleTask<EntityMessage>() { new SimpleTask<Spanned>() {
@Override @Override
protected void onPreExecute(Bundle args) { protected void onPreExecute(Bundle args) {
ibReferenceDelete.setEnabled(false); ibReferenceDelete.setEnabled(false);
@ -770,46 +769,30 @@ public class FragmentCompose extends FragmentBase {
} }
@Override @Override
protected EntityMessage onExecute(Context context, Bundle args) throws Throwable { protected Spanned onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id"); long id = args.getLong("id");
boolean plain = args.getBoolean("plain");
String body = args.getString("body"); String body = args.getString("body");
DB db = DB.getInstance(context);
EntityMessage draft = db.message().getMessage(id);
if (draft == null || !draft.content)
throw new IllegalArgumentException(context.getString(R.string.title_no_body));
File file = draft.getFile(context);
File refFile = draft.getRefFile(context);
String html; String html;
File refFile = EntityMessage.getRefFile(context, id);
String ref = Helper.readText(refFile); String ref = Helper.readText(refFile);
if (plain) { if (plain) {
String plain = HtmlHelper.getText(ref); String text = HtmlHelper.getText(ref);
html = "<p>" + plain.replaceAll("\\r?\\n", "<br>") + "</p>"; html = "<p>" + text.replaceAll("\\r?\\n", "<br>") + "</p>";
} else } else
html = HtmlHelper.sanitize(context, ref, true); html = HtmlHelper.sanitize(context, ref, true);
try (BufferedWriter out = new BufferedWriter(new FileWriter(file))) {
out.write(body);
out.write(html);
}
refFile.delete(); refFile.delete();
draft.revision = null; return HtmlHelper.fromHtml(body + html);
draft.revisions = null;
db.message().setMessageRevision(draft.id, null);
db.message().setMessageRevisions(draft.id, null);
return draft;
} }
@Override @Override
protected void onExecuted(Bundle args, EntityMessage draft) { protected void onExecuted(Bundle args, Spanned body) {
showDraft(draft); etBody.setText(body);
Bundle extras = new Bundle();
extras.putBoolean("show", true);
onAction(R.id.action_save, extras);
} }
@Override @Override
@ -825,7 +808,9 @@ public class FragmentCompose extends FragmentBase {
private void onReferenceImages() { private void onReferenceImages() {
show_images = true; show_images = true;
showDraft(working); Bundle extras = new Bundle();
extras.putBoolean("show", true);
onAction(R.id.action_save, extras);
} }
@Override @Override
@ -1953,10 +1938,10 @@ public class FragmentCompose extends FragmentBase {
} }
private void onAction(int action) { private void onAction(int action) {
onAction(action, null); onAction(action, new Bundle());
} }
private void onAction(int action, Bundle extras) { private void onAction(int action, @NonNull Bundle extras) {
EntityIdentity identity = (EntityIdentity) spIdentity.getSelectedItem(); EntityIdentity identity = (EntityIdentity) spIdentity.getSelectedItem();
if (identity == null) if (identity == null)
throw new IllegalArgumentException(getString(R.string.title_from_missing)); throw new IllegalArgumentException(getString(R.string.title_from_missing));
@ -1976,8 +1961,7 @@ public class FragmentCompose extends FragmentBase {
args.putString("subject", etSubject.getText().toString().trim()); args.putString("subject", etSubject.getText().toString().trim());
args.putString("body", HtmlHelper.toHtml(etBody.getText())); args.putString("body", HtmlHelper.toHtml(etBody.getText()));
args.putBoolean("empty", isEmpty()); args.putBoolean("empty", isEmpty());
if (extras != null) args.putBundle("extras", extras);
args.putBundle("extras", extras);
Log.i("Run execute id=" + working); Log.i("Run execute id=" + working);
actionLoader.execute(this, args, "compose:action:" + action); actionLoader.execute(this, args, "compose:action:" + action);
@ -3191,7 +3175,9 @@ public class FragmentCompose extends FragmentBase {
showDraft(draft); showDraft(draft);
} else if (action == R.id.action_save) { } else if (action == R.id.action_save) {
// Do nothing boolean show = args.getBundle("extras").getBoolean("show");
if (show)
showDraft(draft);
} else if (action == R.id.action_check) { } else if (action == R.id.action_check) {
boolean dialog = args.getBundle("extras").getBoolean("dialog"); boolean dialog = args.getBundle("extras").getBoolean("dialog");
@ -3265,29 +3251,6 @@ public class FragmentCompose extends FragmentBase {
return subject; return subject;
} }
private void showDraft(long id) {
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<EntityMessage>() {
@Override
protected EntityMessage onExecute(Context context, Bundle args) {
long id = args.getLong("id");
return DB.getInstance(context).message().getMessage(id);
}
@Override
protected void onExecuted(Bundle args, EntityMessage draft) {
showDraft(draft);
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(this, args, "compose:show");
}
private void showDraft(final EntityMessage draft) { private void showDraft(final EntityMessage draft) {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", draft.id); args.putLong("id", draft.id);

Loading…
Cancel
Save