Skip to content
This repository was archived by the owner on Jul 25, 2024. It is now read-only.

Commit 68ea66c

Browse files
jainkuniyatimabbott
authored andcommitted
Created custom TopSnackBar.
New layout for in app notification which slides from top of the screen. part 8th of #309
1 parent f023049 commit 68ea66c

File tree

8 files changed

+331
-29
lines changed

8 files changed

+331
-29
lines changed

app/src/main/java/com/zulip/android/activities/ZulipActivity.java

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.database.MergeCursor;
2222
import android.graphics.Bitmap;
2323
import android.graphics.PorterDuff;
24+
import android.graphics.Rect;
2425
import android.graphics.drawable.Drawable;
2526
import android.net.Uri;
2627
import android.os.Build;
@@ -32,7 +33,6 @@
3233
import android.support.design.widget.AppBarLayout;
3334
import android.support.design.widget.CoordinatorLayout;
3435
import android.support.design.widget.FloatingActionButton;
35-
import android.support.design.widget.Snackbar;
3636
import android.support.v4.app.ActionBarDrawerToggle;
3737
import android.support.v4.app.ActivityCompat;
3838
import android.support.v4.app.FragmentManager;
@@ -58,6 +58,7 @@
5858
import android.view.View;
5959
import android.view.ViewPropertyAnimator;
6060
import android.view.WindowManager;
61+
import android.view.Window;
6162
import android.view.animation.Interpolator;
6263
import android.view.inputmethod.InputMethodManager;
6364
import android.widget.AdapterView;
@@ -107,6 +108,7 @@
107108
import com.zulip.android.util.SwipeRemoveLinearLayout;
108109
import com.zulip.android.util.UrlHelper;
109110
import com.zulip.android.util.ZLog;
111+
import com.zulip.android.viewholders.TopSnackBar;
110112

111113
import org.json.JSONObject;
112114

@@ -167,6 +169,7 @@ public class ZulipActivity extends BaseActivity implements
167169
private Runnable statusUpdateRunnable;
168170

169171
private int mToolbarHeightInPx;
172+
private int statusBarHeight = 0;
170173
private MessageListFragment narrowedList;
171174
private MessageListFragment homeList;
172175
private AutoCompleteTextView streamActv;
@@ -249,6 +252,7 @@ public boolean setViewValue(View view, Cursor cursor, int i) {
249252
private RefreshableCursorAdapter peopleAdapter;
250253
private LinearLayout composeStatus;
251254
private String tempStreamSave = null;
255+
private TopSnackBar topSnackBar;
252256

253257
@Override
254258
public void removeChatBox(boolean animToRight) {
@@ -1663,6 +1667,9 @@ public void setLayoutBehaviour(LinearLayoutManager linearLayoutManager, Recycler
16631667
layoutParams = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
16641668
layoutParams.setBehavior(new RemoveViewsOnScroll(linearLayoutManager, adapter));
16651669
fab.setLayoutParams(layoutParams);
1670+
1671+
topSnackBar.setMessagesLayoutManager(linearLayoutManager);
1672+
topSnackBar.setMessagesAdapter(adapter);
16661673
}
16671674

16681675
@Override
@@ -2218,32 +2225,27 @@ public void onNewMessages(Message[] messages) {
22182225
showSnackbarNotification(messages); //Show notification
22192226
}
22202227

2221-
Snackbar snackbar;
2222-
CoordinatorLayout.LayoutParams snackBarParams;
22232228
private void setupSnackBar() {
22242229
final CoordinatorLayout coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
2225-
snackbar = Snackbar.make(coordinatorLayout, "", Snackbar.LENGTH_LONG);
2226-
View v = snackbar.getView();
2227-
snackBarParams = (CoordinatorLayout.LayoutParams) v.getLayoutParams();
2228-
snackBarParams.gravity = Gravity.TOP;
2229-
v.setLayoutParams(snackBarParams);
22302230
TypedValue tv = new TypedValue();
22312231
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
22322232
mToolbarHeightInPx = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
2233-
snackbar.setCallback(new Snackbar.Callback() {
2234-
@Override
2235-
public void onDismissed(Snackbar snackbar, int event) {
2236-
super.onDismissed(snackbar, event);
2237-
if (event == Snackbar.Callback.DISMISS_EVENT_TIMEOUT) prevMessageSameCount = -1;
2238-
}
2239-
});
2233+
2234+
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
2235+
if (resourceId > 0) {
2236+
statusBarHeight = getResources().getDimensionPixelSize(resourceId);
2237+
}
2238+
2239+
topSnackBar = new TopSnackBar(this);
2240+
coordinatorLayout.addView(topSnackBar.getLinearLayout());
22402241
}
22412242

22422243
NarrowFilter narrowFilter;
22432244
String prevId = null;
22442245
int prevMessageSameCount = -1;
22452246
private void showSnackbarNotification(Message[] messages) {
22462247
MutedTopics mutedTopics = MutedTopics.get();
2248+
String notificationMessage;
22472249
int nonMutedMessagesCount = 0;
22482250
Message tempMessage = null; //Stores a temporary message which is non-muted, later used for retrieving Stream/Topic
22492251
for (Message message : messages) {
@@ -2266,38 +2268,35 @@ private void showSnackbarNotification(Message[] messages) {
22662268
}
22672269
if (nonMutedMessagesCount == 0) return;
22682270
if (prevId == null && messages.length > 1) {
2269-
snackbar.setText(getResources().getQuantityString(R.plurals.new_message_mul_sender, nonMutedMessagesCount, nonMutedMessagesCount));
2271+
notificationMessage = getResources().getQuantityString(R.plurals.new_message_mul_sender, nonMutedMessagesCount, nonMutedMessagesCount);
22702272
narrowFilter = null;
22712273
if (narrowedList != null) {
22722274
narrowedList = null;
22732275
getSupportFragmentManager().popBackStack(NARROW,
22742276
FragmentManager.POP_BACK_STACK_INCLUSIVE);
22752277
}
2276-
snackbar.setAction(R.string.SHOW, new View.OnClickListener() {
2278+
topSnackBar.setAction(R.string.SHOW, new View.OnClickListener() {
22772279
@Override
22782280
public void onClick(View view) {
2281+
topSnackBar.dismiss();
22792282
homeList.showLatestMessages();
22802283
}
22812284
});
22822285
} else {
22832286
if (messages.length == 1) tempMessage = messages[0];
22842287
String name = (tempMessage.getType() == MessageType.PRIVATE_MESSAGE) ? getString(R.string.notify_private, tempMessage.getSenderFullName()) : getString(R.string.notify_stream, tempMessage.getStream().getName() , tempMessage.getSubject());
22852288
if (prevMessageSameCount > 0) name += " (" + prevMessageSameCount + ")";
2286-
snackbar.setText(getResources().getQuantityString(R.plurals.new_message, nonMutedMessagesCount, nonMutedMessagesCount, name));
2289+
notificationMessage = getResources().getQuantityString(R.plurals.new_message, nonMutedMessagesCount, nonMutedMessagesCount, name);
22872290
narrowFilter = (tempMessage.getType() == MessageType.PRIVATE_MESSAGE) ? new NarrowFilterPM(Arrays.asList(tempMessage.getRecipients(app))) : new NarrowFilterStream(tempMessage.getStream(), tempMessage.getSubject());
2288-
snackbar.setAction(R.string.SHOW, new View.OnClickListener() {
2291+
topSnackBar.setAction(R.string.SHOW, new View.OnClickListener() {
22892292
@Override
22902293
public void onClick(View view) {
2294+
topSnackBar.dismiss();
22912295
doNarrow(narrowFilter);
22922296
}
22932297
});
22942298
}
2295-
if (appBarLayout.getVisibility() == View.GONE) {
2296-
snackBarParams.setMargins(0, 0, 0, 0);
2297-
} else {
2298-
snackBarParams.setMargins(0, mToolbarHeightInPx, 0, 0);
2299-
}
2300-
snackbar.show();
2299+
topSnackBar.show((appBarLayout.getVisibility() == View.GONE) ? statusBarHeight : statusBarHeight + mToolbarHeightInPx, notificationMessage, TopSnackBar.LENGTH_LONG);
23012300
}
23022301

23032302
// Intent Extra constants

app/src/main/java/com/zulip/android/util/RemoveViewsOnScroll.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import android.view.View;
1515
import android.view.ViewPropertyAnimator;
1616
import android.view.animation.Interpolator;
17+
import android.widget.LinearLayout;
1718

1819
import com.zulip.android.R;
1920
import com.zulip.android.activities.RecyclerMessageAdapter;
@@ -62,9 +63,9 @@ public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, V
6263
}
6364

6465
changeInYDir += dy;
65-
if (changeInYDir > toolbarHeight && child.getVisibility() == View.VISIBLE && !isViewHidden)
66+
if ((changeInYDir > toolbarHeight && child.getVisibility() == View.VISIBLE) && (!isViewHidden || isTopSnackBar(child)))
6667
hideView(child);
67-
else if (changeInYDir < 0 && child.getVisibility() == View.GONE && !mIsShowing) {
68+
else if (changeInYDir < 0 && (child.getVisibility() == View.GONE && !mIsShowing) || isTopSnackBar(child)) {
6869
if (child instanceof FloatingActionButton) {
6970
if (chatBox == null)
7071
chatBox = coordinatorLayout.findViewById(R.id.messageBoxContainer);
@@ -77,11 +78,22 @@ else if (changeInYDir < 0 && child.getVisibility() == View.GONE && !mIsShowing)
7778
}
7879
}
7980

81+
private boolean isTopSnackBar(View child) {
82+
return (child.getId() != R.id.appBarLayout && !(child instanceof FloatingActionButton));
83+
}
84+
8085
@SuppressLint("NewApi")
8186
private void hideView(final View view) {
8287
isViewHidden = true;
88+
int y = view.getHeight();
89+
;
90+
if (view instanceof AppBarLayout) {
91+
y = -1 * view.getHeight();
92+
} else if (view instanceof LinearLayout) {
93+
y = 0;
94+
}
8395
ViewPropertyAnimator animator = view.animate()
84-
.translationY((view instanceof AppBarLayout) ? -1 * view.getHeight() : view.getHeight())
96+
.translationY(y)
8597
.setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR)
8698
.setDuration(200);
8799

@@ -114,7 +126,7 @@ public void onAnimationRepeat(Animator animator) {
114126
public void showView(final View view) {
115127
mIsShowing = true;
116128
ViewPropertyAnimator animator = view.animate()
117-
.translationY(0)
129+
.translationY((view.getId() == R.id.appBarLayout || view instanceof FloatingActionButton) ? 0 : toolbarHeight)
118130
.setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR)
119131
.setDuration(200);
120132

0 commit comments

Comments
 (0)