[android] scroll down when new messages arrive while conversation is visible

Also shows new message notification when ConversationActivity is paused
This commit is contained in:
Torsten Grote
2018-12-18 13:47:49 -02:00
committed by akwizgran
parent ed8c09282d
commit a53345a3c9

View File

@@ -103,6 +103,7 @@ import im.delight.android.identicons.IdenticonDrawable;
import uk.co.samuelwall.materialtaptargetprompt.MaterialTapTargetPrompt; import uk.co.samuelwall.materialtaptargetprompt.MaterialTapTargetPrompt;
import uk.co.samuelwall.materialtaptargetprompt.MaterialTapTargetPrompt.PromptStateChangeListener; import uk.co.samuelwall.materialtaptargetprompt.MaterialTapTargetPrompt.PromptStateChangeListener;
import static android.arch.lifecycle.Lifecycle.State.RESUMED;
import static android.os.Build.VERSION.SDK_INT; import static android.os.Build.VERSION.SDK_INT;
import static android.support.v4.app.ActivityOptionsCompat.makeSceneTransitionAnimation; import static android.support.v4.app.ActivityOptionsCompat.makeSceneTransitionAnimation;
import static android.support.v4.view.ViewCompat.setTransitionName; import static android.support.v4.view.ViewCompat.setTransitionName;
@@ -294,8 +295,6 @@ public class ConversationActivity extends BriarActivity
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
eventBus.addListener(this); eventBus.addListener(this);
notificationManager.blockContactNotification(contactId);
notificationManager.clearContactNotification(contactId);
displayContactOnlineStatus(); displayContactOnlineStatus();
viewModel.getContactDisplayName().observe(this, contactNameObserver); viewModel.getContactDisplayName().observe(this, contactNameObserver);
list.startPeriodicUpdate(); list.startPeriodicUpdate();
@@ -304,6 +303,9 @@ public class ConversationActivity extends BriarActivity
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
// TODO move back to onStart() when we have unread msg indicators
notificationManager.blockContactNotification(contactId);
notificationManager.clearContactNotification(contactId);
// Trigger loading of contact data, noop if data was loaded already. // Trigger loading of contact data, noop if data was loaded already.
// //
// We can only start loading data *after* we are sure // We can only start loading data *after* we are sure
@@ -311,11 +313,17 @@ public class ConversationActivity extends BriarActivity
if (signedIn()) viewModel.setContactId(contactId); if (signedIn()) viewModel.setContactId(contactId);
} }
@Override
protected void onPause() {
super.onPause();
// TODO move back to onStop() when we have unread msg indicators
notificationManager.unblockContactNotification(contactId);
}
@Override @Override
public void onStop() { public void onStop() {
super.onStop(); super.onStop();
eventBus.removeListener(this); eventBus.removeListener(this);
notificationManager.unblockContactNotification(contactId);
viewModel.getContactDisplayName().removeObserver(contactNameObserver); viewModel.getContactDisplayName().removeObserver(contactNameObserver);
list.stopPeriodicUpdate(); list.stopPeriodicUpdate();
} }
@@ -501,10 +509,12 @@ public class ConversationActivity extends BriarActivity
adapter.getMessageItem(m); adapter.getMessageItem(m);
if (pair != null) { if (pair != null) {
pair.getSecond().setText(text); pair.getSecond().setText(text);
boolean bottom = layoutManagerState == null && boolean shouldScroll = layoutManagerState == null &&
adapter.isScrolledToBottom(layoutManager); adapter.isScrolledToBottom(layoutManager);
adapter.notifyItemChanged(pair.getFirst()); adapter.notifyItemChanged(pair.getFirst());
if (bottom) list.scrollToPosition(adapter.getItemCount() - 1); if (shouldScroll) {
list.scrollToPosition(adapter.getItemCount() - 1);
}
} }
}); });
} }
@@ -533,10 +543,12 @@ public class ConversationActivity extends BriarActivity
adapter.getMessageItem(m); adapter.getMessageItem(m);
if (pair != null) { if (pair != null) {
pair.getSecond().setAttachments(items); pair.getSecond().setAttachments(items);
boolean bottom = layoutManagerState == null && boolean shouldScroll = layoutManagerState == null &&
adapter.isScrolledToBottom(layoutManager); adapter.isScrolledToBottom(layoutManager);
adapter.notifyItemChanged(pair.getFirst()); adapter.notifyItemChanged(pair.getFirst());
if (bottom) list.scrollToPosition(adapter.getItemCount() - 1); if (shouldScroll) {
list.scrollToPosition(adapter.getItemCount() - 1);
}
} }
}); });
} }
@@ -585,11 +597,11 @@ public class ConversationActivity extends BriarActivity
private void addConversationItem(ConversationItem item) { private void addConversationItem(ConversationItem item) {
runOnUiThreadUnlessDestroyed(() -> { runOnUiThreadUnlessDestroyed(() -> {
boolean bottom = layoutManagerState == null && // whenever the activity is shown, we'll scroll down for new msgs
adapter.isScrolledToBottom(layoutManager); boolean shouldScroll = getLifecycle().getCurrentState() == RESUMED;
adapter.incrementRevision(); adapter.incrementRevision();
adapter.add(item); adapter.add(item);
if (bottom) list.scrollToPosition(adapter.getItemCount() - 1); if (shouldScroll) list.scrollToPosition(adapter.getItemCount() - 1);
}); });
} }