Skip to content

Commit ecfabdb

Browse files
authored
Merge pull request #8 from anonomi-org/enhancement/text-with-images
Enhancement/text with images
2 parents 045d534 + f80f642 commit ecfabdb

13 files changed

Lines changed: 253 additions & 58 deletions

anonomi-android/src/main/java/org/anonomi/android/blog/WriteBlogPostActivity.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ private void storePost(String text) {
200200

201201
private void onImageSelected(@Nullable Uri uri) {
202202
if (uri == null) return;
203+
String text = input.getText();
204+
input.clearText();
203205
input.hideSoftKeyboard();
204206
input.setVisibility(GONE);
205207
progressBar.setVisibility(VISIBLE);
@@ -226,7 +228,7 @@ private void onImageSelected(@Nullable Uri uri) {
226228
while ((len = compressed.read(buf)) != -1)
227229
bos.write(buf, 0, len);
228230
byte[] imageBytes = bos.toByteArray();
229-
storeImagePost(imageBytes, "image/jpeg");
231+
storeImagePost(imageBytes, "image/jpeg", text);
230232
} catch (IOException e) {
231233
logException(LOG, WARNING, e);
232234
runOnUiThread(() -> {
@@ -239,13 +241,15 @@ private void onImageSelected(@Nullable Uri uri) {
239241
}).start();
240242
}
241243

242-
private void storeImagePost(byte[] imageData, String contentType) {
244+
private void storeImagePost(byte[] imageData, String contentType,
245+
@Nullable String text) {
243246
runOnDbThread(() -> {
244247
long timestamp = System.currentTimeMillis();
245248
try {
246249
LocalAuthor author = identityManager.getLocalAuthor();
247250
BlogPost p = blogPostFactory.createBlogImagePost(
248-
groupId, timestamp, null, author, "", imageData,
251+
groupId, timestamp, null, author,
252+
text != null ? text : "", imageData,
249253
contentType);
250254
blogManager.addLocalImagePost(p);
251255
postPublished();

anonomi-android/src/main/java/org/anonomi/android/forum/ForumActivity.java

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ public void onAllPlaybackFinished() {
251251
compositeSendButton = textInput.findViewById(R.id.compositeSendButton);
252252
updateMicColor();
253253

254+
// Image preview
255+
initImagePreview();
256+
254257
// Image picker
255258
compositeSendButton.setImagesSupported();
256259
compositeSendButton.setOnImageClickListener(
@@ -402,16 +405,21 @@ private void openSendLocationScreen() {
402405

403406
private void onImageSelected(@Nullable Uri uri) {
404407
if (uri == null) return;
405-
MessageId replyId = viewModel.getCurrentReplyId();
408+
showImagePreview(uri);
409+
compositeSendButton.showProgress(true);
406410
new Thread(() -> {
407411
try {
408412
String mimeType = getContentResolver().getType(uri);
409413
if (mimeType == null) mimeType = "image/jpeg";
410414
InputStream is = getContentResolver().openInputStream(uri);
411415
if (is == null) {
412-
runOnUiThread(() -> Toast.makeText(this,
413-
getString(R.string.image_compression_failed),
414-
Toast.LENGTH_SHORT).show());
416+
runOnUiThread(() -> {
417+
compositeSendButton.showProgress(false);
418+
clearPendingImage();
419+
Toast.makeText(this,
420+
getString(R.string.image_compression_failed),
421+
Toast.LENGTH_SHORT).show();
422+
});
415423
return;
416424
}
417425
InputStream compressed = imageCompressor.compressImage(
@@ -423,21 +431,32 @@ private void onImageSelected(@Nullable Uri uri) {
423431
bos.write(buf, 0, len);
424432
byte[] imageBytes = bos.toByteArray();
425433
runOnUiThread(() -> {
426-
viewModel.createAndStoreImageMessage(
427-
imageBytes, "image/jpeg", replyId);
428-
viewModel.clearReplyId();
434+
compositeSendButton.showProgress(false);
435+
setPendingImage(imageBytes, "image/jpeg");
436+
});
437+
} catch (IOException e) {
438+
runOnUiThread(() -> {
439+
compositeSendButton.showProgress(false);
440+
clearPendingImage();
429441
Toast.makeText(this,
430-
getString(R.string.image_sent),
442+
getString(R.string.image_compression_failed),
431443
Toast.LENGTH_SHORT).show();
432444
});
433-
} catch (IOException e) {
434-
runOnUiThread(() -> Toast.makeText(this,
435-
getString(R.string.image_compression_failed),
436-
Toast.LENGTH_SHORT).show());
437445
}
438446
}).start();
439447
}
440448

449+
@Override
450+
protected void onSendImageWithText(byte[] imageBytes,
451+
@javax.annotation.Nullable String contentType,
452+
@javax.annotation.Nullable MessageId replyId,
453+
@javax.annotation.Nullable String text) {
454+
viewModel.createAndStoreImageMessage(
455+
imageBytes, contentType != null ? contentType : "image/jpeg",
456+
replyId, text);
457+
viewModel.clearReplyId();
458+
}
459+
441460
// Voice recording
442461

443462
private void startRecording() {

anonomi-android/src/main/java/org/anonomi/android/forum/ForumViewModel.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -278,15 +278,15 @@ private void storeAudioPost(ForumPost msg, byte[] audioData,
278278
}
279279

280280
void createAndStoreImageMessage(byte[] imageData, String contentType,
281-
@Nullable MessageId parentId) {
281+
@Nullable MessageId parentId, @Nullable String text) {
282282
runOnDbThread(() -> {
283283
try {
284284
LocalAuthor author = identityManager.getLocalAuthor();
285285
GroupCount count = forumManager.getGroupCount(groupId);
286286
long timestamp = max(count.getLatestMsgTime() + 1,
287287
clock.currentTimeMillis());
288288
createImageMessage(imageData, contentType, timestamp,
289-
parentId, author);
289+
parentId, author, text);
290290
} catch (DbException e) {
291291
handleException(e);
292292
}
@@ -295,23 +295,24 @@ void createAndStoreImageMessage(byte[] imageData, String contentType,
295295

296296
private void createImageMessage(byte[] imageData, String contentType,
297297
long timestamp, @Nullable MessageId parentId,
298-
LocalAuthor author) {
298+
LocalAuthor author, @Nullable String text) {
299299
cryptoExecutor.execute(() -> {
300300
LOG.info("Creating forum image post...");
301-
ForumPost msg = forumManager.createLocalImagePost(groupId, "",
301+
String t = text != null ? text : "";
302+
ForumPost msg = forumManager.createLocalImagePost(groupId, t,
302303
timestamp, parentId, author, imageData, contentType);
303-
storeImagePost(msg, imageData, contentType);
304+
storeImagePost(msg, t, imageData, contentType);
304305
});
305306
}
306307

307-
private void storeImagePost(ForumPost msg, byte[] imageData,
308-
String contentType) {
308+
private void storeImagePost(ForumPost msg, String text,
309+
byte[] imageData, String contentType) {
309310
runOnDbThread(false, txn -> {
310311
long start = now();
311312
ForumPostHeader header = forumManager.addLocalPost(txn, msg);
312313
logDuration(LOG, "Storing forum image post", start);
313314
txn.attach(() -> {
314-
ForumPostItem item = new ForumPostItem(header, "",
315+
ForumPostItem item = new ForumPostItem(header, text,
315316
null, null, imageData, contentType);
316317
addItem(item, true);
317318
});

anonomi-android/src/main/java/org/anonomi/android/privategroup/conversation/GroupActivity.java

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ public void onAllPlaybackFinished() {
249249
compositeSendButton = textInput.findViewById(R.id.compositeSendButton);
250250
updateMicColor();
251251

252+
// Image preview
253+
initImagePreview();
254+
252255
// Image picker
253256
compositeSendButton.setImagesSupported();
254257
compositeSendButton.setOnImageClickListener(
@@ -440,16 +443,21 @@ private void openSendLocationScreen() {
440443

441444
private void onImageSelected(@Nullable Uri uri) {
442445
if (uri == null) return;
443-
MessageId replyId = viewModel.getCurrentReplyId();
446+
showImagePreview(uri);
447+
compositeSendButton.showProgress(true);
444448
new Thread(() -> {
445449
try {
446450
String mimeType = getContentResolver().getType(uri);
447451
if (mimeType == null) mimeType = "image/jpeg";
448452
InputStream is = getContentResolver().openInputStream(uri);
449453
if (is == null) {
450-
runOnUiThread(() -> Toast.makeText(this,
451-
getString(R.string.image_compression_failed),
452-
Toast.LENGTH_SHORT).show());
454+
runOnUiThread(() -> {
455+
compositeSendButton.showProgress(false);
456+
clearPendingImage();
457+
Toast.makeText(this,
458+
getString(R.string.image_compression_failed),
459+
Toast.LENGTH_SHORT).show();
460+
});
453461
return;
454462
}
455463
InputStream compressed = imageCompressor.compressImage(
@@ -461,21 +469,32 @@ private void onImageSelected(@Nullable Uri uri) {
461469
bos.write(buf, 0, len);
462470
byte[] imageBytes = bos.toByteArray();
463471
runOnUiThread(() -> {
464-
viewModel.createAndStoreImageMessage(
465-
imageBytes, "image/jpeg", replyId);
466-
viewModel.clearReplyId();
472+
compositeSendButton.showProgress(false);
473+
setPendingImage(imageBytes, "image/jpeg");
474+
});
475+
} catch (IOException e) {
476+
runOnUiThread(() -> {
477+
compositeSendButton.showProgress(false);
478+
clearPendingImage();
467479
Toast.makeText(this,
468-
getString(R.string.image_sent),
480+
getString(R.string.image_compression_failed),
469481
Toast.LENGTH_SHORT).show();
470482
});
471-
} catch (IOException e) {
472-
runOnUiThread(() -> Toast.makeText(this,
473-
getString(R.string.image_compression_failed),
474-
Toast.LENGTH_SHORT).show());
475483
}
476484
}).start();
477485
}
478486

487+
@Override
488+
protected void onSendImageWithText(byte[] imageBytes,
489+
@javax.annotation.Nullable String contentType,
490+
@javax.annotation.Nullable MessageId replyId,
491+
@javax.annotation.Nullable String text) {
492+
viewModel.createAndStoreImageMessage(
493+
imageBytes, contentType != null ? contentType : "image/jpeg",
494+
replyId, text);
495+
viewModel.clearReplyId();
496+
}
497+
479498
// Voice recording
480499

481500
private void startRecording() {

anonomi-android/src/main/java/org/anonomi/android/privategroup/conversation/GroupViewModel.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ private void storeAudioPost(GroupMessage msg, byte[] audioData,
341341
}
342342

343343
void createAndStoreImageMessage(byte[] imageData, String contentType,
344-
@Nullable MessageId parentId) {
344+
@Nullable MessageId parentId, @Nullable String text) {
345345
runOnDbThread(() -> {
346346
try {
347347
LocalAuthor author = identityManager.getLocalAuthor();
@@ -351,7 +351,7 @@ void createAndStoreImageMessage(byte[] imageData, String contentType,
351351
long timestamp = count.getLatestMsgTime();
352352
timestamp = max(clock.currentTimeMillis(), timestamp + 1);
353353
createImageMessage(imageData, contentType, timestamp,
354-
parentId, author, previousMsgId);
354+
parentId, author, previousMsgId, text);
355355
} catch (DbException e) {
356356
handleException(e);
357357
}
@@ -360,25 +360,27 @@ void createAndStoreImageMessage(byte[] imageData, String contentType,
360360

361361
private void createImageMessage(byte[] imageData, String contentType,
362362
long timestamp, @Nullable MessageId parentId,
363-
LocalAuthor author, MessageId previousMsgId) {
363+
LocalAuthor author, MessageId previousMsgId,
364+
@Nullable String text) {
364365
cryptoExecutor.execute(() -> {
365366
LOG.info("Creating group image message...");
367+
String t = text != null ? text : "";
366368
GroupMessage msg = groupMessageFactory.createGroupImageMessage(
367-
groupId, timestamp, parentId, author, "",
369+
groupId, timestamp, parentId, author, t,
368370
imageData, contentType, previousMsgId);
369-
storeImagePost(msg, imageData, contentType);
371+
storeImagePost(msg, t, imageData, contentType);
370372
});
371373
}
372374

373-
private void storeImagePost(GroupMessage msg, byte[] imageData,
374-
String contentType) {
375+
private void storeImagePost(GroupMessage msg, String text,
376+
byte[] imageData, String contentType) {
375377
runOnDbThread(false, txn -> {
376378
long start = now();
377379
GroupMessageHeader header =
378380
privateGroupManager.addLocalMessage(txn, msg);
379381
logDuration(LOG, "Storing group image message", start);
380382
txn.attach(() ->
381-
addItem(buildItemWithImage(header, "", imageData,
383+
addItem(buildItemWithImage(header, text, imageData,
382384
contentType), true)
383385
);
384386
}, this::handleException);

anonomi-android/src/main/java/org/anonomi/android/threaded/BaseThreadItemViewHolder.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,15 @@ public void bind(I item, ThreadItemListener<I> listener) {
5555
R.string.tap_to_view_on_map));
5656
textView.setOnClickListener(
5757
v -> listener.onMapMessageClicked(mapData));
58-
} else {
58+
textView.setVisibility(View.VISIBLE);
59+
} else if (trimmedText != null && !trimmedText.isEmpty()) {
5960
textView.setText(trimmedText);
6061
Linkify.addLinks(textView, Linkify.WEB_URLS);
6162
makeLinksClickable(textView, listener::onLinkClick);
63+
textView.setVisibility(View.VISIBLE);
64+
} else {
65+
textView.setText(null);
66+
textView.setVisibility(View.GONE);
6267
}
6368

6469
author.setAuthor(item.getAuthor(), item.getAuthorInfo());

0 commit comments

Comments
 (0)