Add UnreadMessageButton to threaded conversations

This commit is contained in:
Torsten Grote
2016-12-22 17:04:59 -02:00
parent 279f4d668a
commit b4c669243b
11 changed files with 211 additions and 106 deletions

View File

@@ -4,7 +4,6 @@ import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.StringRes;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
@@ -90,12 +89,6 @@ public class ForumActivity extends
setTitle(forum.getName());
}
@Override
@LayoutRes
protected int getLayout() {
return R.layout.activity_threaded_conversation;
}
@Override
protected ThreadItemAdapter<ForumItem> createAdapter(
LinearLayoutManager layoutManager) {
@@ -158,12 +151,6 @@ public class ForumActivity extends
return R.string.forum_new_entry_posted;
}
@Override
@StringRes
protected int getItemReceivedString() {
return R.string.forum_new_entry_received;
}
private void showUnsubscribeDialog() {
OnClickListener okListener = new OnClickListener() {
@Override

View File

@@ -4,7 +4,6 @@ import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.StringRes;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
@@ -92,12 +91,6 @@ public class GroupActivity extends
setGroupEnabled(false);
}
@Override
@LayoutRes
protected int getLayout() {
return R.layout.activity_threaded_conversation;
}
@Override
protected GroupMessageAdapter createAdapter(
LinearLayoutManager layoutManager) {
@@ -205,12 +198,6 @@ public class GroupActivity extends
return R.string.groups_message_sent;
}
@Override
@StringRes
protected int getItemReceivedString() {
return R.string.groups_message_received;
}
@Override
public void onReplyClick(GroupMessageItem item) {
if (!isDissolved) super.onReplyClick(item);

View File

@@ -11,6 +11,7 @@ import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.widget.TextView;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
@@ -19,6 +20,9 @@ import org.briarproject.briar.R;
import org.briarproject.briar.android.threaded.ThreadItemAdapter.ThreadItemListener;
import org.briarproject.briar.android.view.AuthorView;
import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;
@UiThread
@NotNullByDefault
public abstract class BaseThreadItemViewHolder<I extends ThreadItem>
@@ -48,9 +52,9 @@ public abstract class BaseThreadItemViewHolder<I extends ThreadItem>
textView.setText(StringUtils.trim(item.getText()));
if (pos == 0) {
topDivider.setVisibility(View.INVISIBLE);
topDivider.setVisibility(INVISIBLE);
} else {
topDivider.setVisibility(View.VISIBLE);
topDivider.setVisibility(VISIBLE);
}
author.setAuthor(item.getAuthor());
@@ -58,16 +62,13 @@ public abstract class BaseThreadItemViewHolder<I extends ThreadItem>
author.setAuthorStatus(item.getStatus());
if (item.equals(adapter.getReplyItem())) {
layout.setBackgroundColor(ContextCompat
.getColor(getContext(), R.color.forum_cell_highlight));
} else if (item.equals(adapter.getAddedItem())) {
layout.setBackgroundColor(ContextCompat
.getColor(getContext(), R.color.forum_cell_highlight));
animateFadeOut(adapter, adapter.getAddedItem());
adapter.clearAddedItem();
layout.setActivated(true);
} else if (!item.isRead()) {
layout.setActivated(true);
animateFadeOut(adapter, item);
listener.onUnreadItemVisible(item);
} else {
layout.setBackgroundColor(ContextCompat
.getColor(getContext(), R.color.window_background));
layout.setActivated(false);
}
}
@@ -77,31 +78,29 @@ public abstract class BaseThreadItemViewHolder<I extends ThreadItem>
setIsRecyclable(false);
ValueAnimator anim = new ValueAnimator();
adapter.addAnimatingItem(addedItem, anim);
ColorDrawable viewColor = (ColorDrawable) layout.getBackground();
ColorDrawable viewColor = new ColorDrawable(ContextCompat
.getColor(getContext(), R.color.forum_cell_highlight));
anim.setIntValues(viewColor.getColor(), ContextCompat
.getColor(getContext(), R.color.window_background));
anim.setEvaluator(new ArgbEvaluator());
anim.setInterpolator(new AccelerateInterpolator());
anim.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
layout.setBackgroundResource(
R.drawable.list_item_thread_background);
layout.setActivated(false);
setIsRecyclable(true);
adapter.removeAnimatingItem(addedItem);
}
@Override
public void onAnimationCancel(Animator animation) {
setIsRecyclable(true);
adapter.removeAnimatingItem(addedItem);
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

View File

@@ -1,6 +1,7 @@
package org.briarproject.briar.android.threaded;
import android.animation.ValueAnimator;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
@@ -18,8 +19,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import static android.support.v7.widget.RecyclerView.NO_POSITION;
public class ThreadItemAdapter<I extends ThreadItem>
@@ -33,10 +32,8 @@ public class ThreadItemAdapter<I extends ThreadItem>
private final ThreadItemListener<I> listener;
private final LinearLayoutManager layoutManager;
// highlight not dependant on time
@Nullable
private I replyItem;
// temporary highlight
private I addedItem;
private volatile int revision = 0;
@@ -58,7 +55,6 @@ public class ThreadItemAdapter<I extends ThreadItem>
public void onBindViewHolder(BaseThreadItemViewHolder<I> ui, int position) {
I item = getVisibleItem(position);
if (item == null) return;
listener.onItemVisible(item);
ui.bind(this, listener, item, position);
}
@@ -72,6 +68,7 @@ public class ThreadItemAdapter<I extends ThreadItem>
return getVisiblePos(null);
}
@Nullable
I getReplyItem() {
return replyItem;
}
@@ -84,7 +81,6 @@ public class ThreadItemAdapter<I extends ThreadItem>
public void add(I item) {
items.add(item);
addedItem = item;
if (item.getParentId() == null) {
notifyItemInserted(getVisiblePos(item));
} else {
@@ -262,10 +258,6 @@ public class ThreadItemAdapter<I extends ThreadItem>
return null;
}
boolean isVisible(I item) {
return getVisiblePos(item) != NO_POSITION;
}
/**
* Returns the visible position of the given item.
*
@@ -296,14 +288,6 @@ public class ThreadItemAdapter<I extends ThreadItem>
return item == null ? visibleCounter : NO_POSITION;
}
I getAddedItem() {
return addedItem;
}
void clearAddedItem() {
addedItem = null;
}
void addAnimatingItem(I item, ValueAnimator anim) {
animatingItems.put(item, anim);
}
@@ -323,10 +307,108 @@ public class ThreadItemAdapter<I extends ThreadItem>
revision++;
}
public interface ThreadItemListener<I> {
void onItemVisible(I item);
/**
* Gets the number of unread items above and below the current view port.
*
* Attention: Do not call this when the list is still scrolling,
* because then the view port is unknown.
*/
public UnreadCount getUnreadCount() {
final int positionTop = layoutManager.findFirstVisibleItemPosition();
final int positionBottom = layoutManager.findLastVisibleItemPosition();
if (positionTop == NO_POSITION && positionBottom == NO_POSITION)
return new UnreadCount(0, 0);
int unreadCounterTop = 0, unreadCounterBottom = 0;
int visibleCounter = 0;
int levelLimit = UNDEFINED;
for (I i : items) {
if (levelLimit >= 0) {
if (i.getLevel() > levelLimit) {
// skip all the items below a non visible branch
continue;
}
levelLimit = UNDEFINED;
}
if (visibleCounter > positionBottom && !i.isRead()) {
unreadCounterBottom++;
} else if (visibleCounter < positionTop && !i.isRead()) {
unreadCounterTop++;
} else if (!i.isShowingDescendants()) {
levelLimit = i.getLevel();
}
visibleCounter++;
}
return new UnreadCount(unreadCounterTop, unreadCounterBottom);
}
/**
* Returns the position of the first unread item below the current viewport
*/
public int getVisibleUnreadPosBottom() {
final int positionBottom = layoutManager.findLastVisibleItemPosition();
int visibleCounter = 0;
int levelLimit = UNDEFINED;
for (I i : items) {
if (levelLimit >= 0) {
if (i.getLevel() > levelLimit) {
// skip all the items below a non visible branch
continue;
}
levelLimit = UNDEFINED;
}
if (visibleCounter > positionBottom && !i.isRead()) {
return visibleCounter;
} else if (!i.isShowingDescendants()) {
levelLimit = i.getLevel();
}
visibleCounter++;
}
return NO_POSITION;
}
/**
* Returns the position of the first unread item above the current viewport
*/
public int getVisibleUnreadPosTop() {
final int positionTop = layoutManager.findFirstVisibleItemPosition();
int position = NO_POSITION;
int visibleCounter = 0;
int levelLimit = UNDEFINED;
for (I i : items) {
if (levelLimit >= 0) {
if (i.getLevel() > levelLimit) {
// skip all the items below a non visible branch
continue;
}
levelLimit = UNDEFINED;
}
if (visibleCounter < positionTop && !i.isRead()) {
position = visibleCounter;
} if (visibleCounter >= positionTop) {
return position;
} else if (!i.isShowingDescendants()) {
levelLimit = i.getLevel();
}
visibleCounter++;
}
return NO_POSITION;
}
static class UnreadCount {
final int top, bottom;
private UnreadCount(int top, int bottom) {
this.top = top;
this.bottom = bottom;
}
}
public interface ThreadItemListener<I> {
void onUnreadItemVisible(I item);
void onReplyClick(I item);
}
}

View File

@@ -3,13 +3,12 @@ package org.briarproject.briar.android.threaded;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.annotation.LayoutRes;
import android.support.annotation.StringRes;
import android.support.annotation.UiThread;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;
@@ -30,6 +29,7 @@ import org.briarproject.briar.android.threaded.ThreadListController.ThreadListLi
import org.briarproject.briar.android.view.BriarRecyclerView;
import org.briarproject.briar.android.view.TextInputView;
import org.briarproject.briar.android.view.TextInputView.TextInputListener;
import org.briarproject.briar.android.view.UnreadMessageButton;
import org.briarproject.briar.api.client.NamedGroup;
import org.briarproject.briar.api.client.PostHeader;
@@ -40,8 +40,12 @@ import javax.annotation.Nullable;
import javax.inject.Inject;
import static android.support.design.widget.Snackbar.make;
import static android.support.v7.widget.RecyclerView.NO_POSITION;
import static android.support.v7.widget.RecyclerView.SCROLL_STATE_IDLE;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static java.util.logging.Level.INFO;
import static org.briarproject.briar.android.threaded.ThreadItemAdapter.UnreadCount;
@MethodsNotNullByDefault
@ParametersNotNullByDefault
@@ -60,6 +64,7 @@ public abstract class ThreadListActivity<G extends NamedGroup, A extends ThreadI
protected BriarRecyclerView list;
protected TextInputView textInput;
protected GroupId groupId;
private UnreadMessageButton upButton, downButton;
private MessageId replyId;
protected abstract ThreadListController<G, I, H> getController();
@@ -72,7 +77,7 @@ public abstract class ThreadListActivity<G extends NamedGroup, A extends ThreadI
public void onCreate(@Nullable Bundle state) {
super.onCreate(state);
setContentView(getLayout());
setContentView(R.layout.activity_threaded_conversation);
Intent i = getIntent();
byte[] b = i.getByteArrayExtra(GROUP_ID);
@@ -89,6 +94,47 @@ public abstract class ThreadListActivity<G extends NamedGroup, A extends ThreadI
adapter = createAdapter(linearLayoutManager);
list.setAdapter(adapter);
list.getRecyclerView().addOnScrollListener(
new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx,
int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dx == 0 && dy == 0) {
// scrollToPosition has been called and finished
updateUnreadCount();
}
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView,
int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == SCROLL_STATE_IDLE) {
updateUnreadCount();
}
}
});
upButton = (UnreadMessageButton) findViewById(R.id.upButton);
downButton = (UnreadMessageButton) findViewById(R.id.downButton);
upButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = adapter.getVisibleUnreadPosTop();
if (position != NO_POSITION) {
list.getRecyclerView().scrollToPosition(position);
}
}
});
downButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = adapter.getVisibleUnreadPosBottom();
if (position != NO_POSITION) {
list.getRecyclerView().scrollToPosition(position);
}
}
});
if (state != null) {
byte[] replyIdBytes = state.getByteArray(KEY_REPLY_ID);
if (replyIdBytes != null) replyId = new MessageId(replyIdBytes);
@@ -99,9 +145,6 @@ public abstract class ThreadListActivity<G extends NamedGroup, A extends ThreadI
loadSharingContacts();
}
@LayoutRes
protected abstract int getLayout();
protected abstract A createAdapter(LinearLayoutManager layoutManager);
protected void loadNamedGroup() {
@@ -225,7 +268,7 @@ public abstract class ThreadListActivity<G extends NamedGroup, A extends ThreadI
}
@Override
public void onItemVisible(I item) {
public void onUnreadItemVisible(I item) {
if (!item.isRead()) {
item.setRead(true);
getController().markItemRead(item);
@@ -329,34 +372,28 @@ public abstract class ThreadListActivity<G extends NamedGroup, A extends ThreadI
supportFinishAfterTransition();
}
protected void addItem(final I item, boolean isLocal) {
protected void addItem(I item, boolean isLocal) {
adapter.incrementRevision();
adapter.add(item);
if (isLocal && adapter.isVisible(item)) {
if (isLocal) {
displaySnackbarShort(getItemPostedString());
} else {
Snackbar snackbar = Snackbar.make(list,
isLocal ? getItemPostedString() : getItemReceivedString(),
Snackbar.LENGTH_LONG);
snackbar.getView().setBackgroundResource(R.color.briar_primary);
snackbar.setActionTextColor(ContextCompat
.getColor(ThreadListActivity.this,
R.color.briar_button_positive));
snackbar.setAction(R.string.show, new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.scrollTo(item);
}
});
snackbar.getView().setBackgroundResource(R.color.briar_primary);
snackbar.show();
updateUnreadCount();
}
}
private void updateUnreadCount() {
UnreadCount unreadCount = adapter.getUnreadCount();
if (LOG.isLoggable(INFO)) {
LOG.info("Updating unread count: top=" + unreadCount.top +
" bottom=" + unreadCount.bottom);
}
upButton.setUnreadCount(unreadCount.top);
downButton.setUnreadCount(unreadCount.bottom);
}
@StringRes
protected abstract int getItemPostedString();
@StringRes
protected abstract int getItemReceivedString();
}

View File

@@ -9,7 +9,6 @@ import org.briarproject.briar.R;
import org.briarproject.briar.android.threaded.ThreadItemAdapter.ThreadItemListener;
import static android.view.View.GONE;
import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;
@UiThread
@@ -64,7 +63,7 @@ public class ThreadPostViewHolder<I extends ThreadItem>
}
if (item.hasDescendants()) {
chevron.setVisibility(VISIBLE);
// chevron.setVisibility(VISIBLE);
if (item.isShowingDescendants()) {
chevron.setSelected(false);
} else {
@@ -82,7 +81,7 @@ public class ThreadPostViewHolder<I extends ThreadItem>
}
});
} else {
chevron.setVisibility(INVISIBLE);
// chevron.setVisibility(INVISIBLE);
}
replyButton.setOnClickListener(new View.OnClickListener() {
@Override

View File

@@ -64,12 +64,16 @@ public class UnreadMessageButton extends FrameLayout {
public void setUnreadCount(int count) {
if (count == 0) {
unread.setVisibility(INVISIBLE);
fab.hide();
fab.setVisibility(GONE);
// fab.hide();
unread.setVisibility(GONE);
} else {
// FIXME: Use animations when upgrading to support library 24.2.0
// https://code.google.com/p/android/issues/detail?id=216469
fab.setVisibility(VISIBLE);
// if (!fab.isShown()) fab.show();
unread.setVisibility(VISIBLE);
unread.setText(String.valueOf(count));
if (!fab.isShown()) fab.show();
}
}

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/forum_cell_highlight"
android:state_activated="true"/>
<item
android:drawable="@color/window_background"/>
</selector>

View File

@@ -10,7 +10,7 @@
<include layout="@layout/toolbar"/>
<android.support.design.widget.CoordinatorLayout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
@@ -27,7 +27,6 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
app:layout_anchorGravity="top|right"
app:direction="up"/>
<org.briarproject.briar.android.view.UnreadMessageButton
@@ -35,10 +34,9 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
app:layout_anchorGravity="bottom|right"
app:direction="down"/>
</android.support.design.widget.CoordinatorLayout>
</FrameLayout>
<org.briarproject.briar.android.view.TextInputView
android:id="@+id/text_input_container"

View File

@@ -6,6 +6,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_item_thread_background"
android:baselineAligned="false"
android:orientation="horizontal">
@@ -113,6 +114,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/author"
android:layout_alignParentRight="true"
android:layout_toLeftOf="@+id/chevron"
android:text="@string/btn_reply"
android:textSize="@dimen/text_size_tiny"/>
@@ -127,7 +129,8 @@
android:clickable="true"
android:padding="@dimen/margin_medium"
android:scaleType="center"
android:src="@drawable/selector_chevron"/>
android:src="@drawable/selector_chevron"
android:visibility="gone"/>
<View
android:id="@+id/top_divider"

View File

@@ -164,7 +164,6 @@
<string name="groups_invitation_sent">Group invitation has been sent</string>
<string name="groups_compose_message">Compose Message</string>
<string name="groups_message_sent">Message sent</string>
<string name="groups_message_received">Message received</string>
<string name="groups_member_list">Member List</string>
<string name="groups_invite_members">Invite Members</string>
<string name="groups_member_created_you">You created the group</string>
@@ -218,7 +217,6 @@
</plurals>
<string name="forum_compose_post">New Forum Post</string>
<string name="forum_new_entry_posted">Forum entry posted</string>
<string name="forum_new_entry_received">New forum entry</string>
<string name="forum_new_message_hint">New Entry</string>
<string name="forum_message_reply_hint">New Reply</string>
<string name="btn_reply">Reply</string>