Skip to content

Commit fbc718d

Browse files
committed
feat: NotificationLayer支持移除已显示和自定义滑动关闭方向
NotificationLayer支持移除已显示和自定义滑动关闭方向 Closes #14
1 parent 01c5c04 commit fbc718d

File tree

5 files changed

+121
-19
lines changed

5 files changed

+121
-19
lines changed

layer-core/src/main/java/per/goweii/layer/core/FrameLayer.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,19 @@ public FrameLayer setCancelableOnClickKeyBack(boolean cancelable) {
164164

165165
@NonNull
166166
private ViewGroup installParent() {
167-
LayerRootLayout layerRootLayout = findLayerLayoutFromRoot();
168-
if (layerRootLayout == null) layerRootLayout = tryGetLayerLayoutFormHolder();
169-
if (layerRootLayout == null) layerRootLayout = createLayerLayout();
167+
LayerRootLayout layerRootLayout = findLayerRootLayoutFromRoot();
168+
if (layerRootLayout == null) layerRootLayout = tryGetLayerRootLayoutFormHolder();
169+
if (layerRootLayout == null) layerRootLayout = createLayerRootLayout();
170170
if (layerRootLayout.getParent() == null) {
171171
getViewHolder().getRoot().addView(layerRootLayout);
172172
} else if (layerRootLayout.getParent() != getViewHolder().getRoot()) {
173173
((ViewGroup) layerRootLayout.getParent()).removeView(layerRootLayout);
174174
getViewHolder().getRoot().addView(layerRootLayout);
175175
}
176176
layerRootLayout.registerOnConfigurationChangedListener(mOnConfigurationChangedListener);
177-
LayerLevelLayout layerLevelLayout = findLevelLayoutFromLayerLayout(layerRootLayout);
178-
if (layerLevelLayout == null) layerLevelLayout = tryGetLevelLayoutFormHolder();
179-
if (layerLevelLayout == null) layerLevelLayout = createLevelLayout();
177+
LayerLevelLayout layerLevelLayout = findLayerLevelLayoutFromLayerLayout(layerRootLayout);
178+
if (layerLevelLayout == null) layerLevelLayout = tryGetLayerLevelLayoutFormHolder();
179+
if (layerLevelLayout == null) layerLevelLayout = createLayerLevelLayout();
180180
if (layerLevelLayout.getParent() == null) {
181181
layerRootLayout.addView(layerLevelLayout);
182182
} else if (layerLevelLayout.getParent() != layerRootLayout) {
@@ -187,10 +187,10 @@ private ViewGroup installParent() {
187187
}
188188

189189
private void uninstallParent() {
190-
final LayerRootLayout layerRootLayout = findLayerLayoutFromRoot();
190+
final LayerRootLayout layerRootLayout = findLayerRootLayoutFromRoot();
191191
if (layerRootLayout == null) return;
192192
layerRootLayout.unregisterOnConfigurationChangedListener(mOnConfigurationChangedListener);
193-
final LayerLevelLayout layerLevelLayout = findLevelLayoutFromLayerLayout(layerRootLayout);
193+
final LayerLevelLayout layerLevelLayout = findLayerLevelLayoutFromLayerLayout(layerRootLayout);
194194
if (layerLevelLayout == null) return;
195195
if (layerLevelLayout.getChildCount() == 0) {
196196
layerRootLayout.removeView(layerLevelLayout);
@@ -204,7 +204,7 @@ private void ensureLayerLayoutIsFront() {
204204
final ViewGroup root = getViewHolder().getRoot();
205205
int count = root.getChildCount();
206206
if (count <= 1) return;
207-
LayerRootLayout layerRootLayout = findLayerLayoutFromRoot();
207+
LayerRootLayout layerRootLayout = findLayerRootLayoutFromRoot();
208208
if (layerRootLayout == null) return;
209209
int index = root.indexOfChild(layerRootLayout);
210210
if (index < 0) return;
@@ -213,7 +213,7 @@ private void ensureLayerLayoutIsFront() {
213213
}
214214

215215
@Nullable
216-
private LayerRootLayout findLayerLayoutFromRoot() {
216+
protected LayerRootLayout findLayerRootLayoutFromRoot() {
217217
final ViewGroup root = getViewHolder().getRoot();
218218
LayerRootLayout layerRootLayout = null;
219219
final int count = root.getChildCount();
@@ -233,15 +233,15 @@ private LayerRootLayout findLayerLayoutFromRoot() {
233233
}
234234

235235
@Nullable
236-
private LayerRootLayout tryGetLayerLayoutFormHolder() {
236+
private LayerRootLayout tryGetLayerRootLayoutFormHolder() {
237237
if (getViewHolder().getLayerRootLayout() == null) return null;
238238
LayerRootLayout layerRootLayout = getViewHolder().getLayerRootLayout();
239239
Utils.removeViewParent(layerRootLayout);
240240
return layerRootLayout;
241241
}
242242

243243
@NonNull
244-
private LayerRootLayout createLayerLayout() {
244+
private LayerRootLayout createLayerRootLayout() {
245245
final ViewGroup root = getViewHolder().getRoot();
246246
LayerRootLayout layerRootLayout = new LayerRootLayout(root.getContext());
247247
layerRootLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
@@ -250,7 +250,7 @@ private LayerRootLayout createLayerLayout() {
250250
}
251251

252252
@Nullable
253-
private LayerLevelLayout findLevelLayoutFromLayerLayout(LayerRootLayout group) {
253+
protected LayerLevelLayout findLayerLevelLayoutFromLayerLayout(@NonNull LayerRootLayout group) {
254254
LayerLevelLayout layerLevelLayout = group.findLevelLayout(getRealLevel());
255255
if (layerLevelLayout != null) {
256256
if (layerLevelLayout != getViewHolder().getLayerLevelLayout()) {
@@ -261,7 +261,7 @@ private LayerLevelLayout findLevelLayoutFromLayerLayout(LayerRootLayout group) {
261261
}
262262

263263
@Nullable
264-
private LayerLevelLayout tryGetLevelLayoutFormHolder() {
264+
private LayerLevelLayout tryGetLayerLevelLayoutFormHolder() {
265265
if (getViewHolder().getLayerLevelLayout() == null) {
266266
return null;
267267
}
@@ -278,7 +278,7 @@ private LayerLevelLayout tryGetLevelLayoutFormHolder() {
278278
}
279279

280280
@NonNull
281-
private LayerLevelLayout createLevelLayout() {
281+
private LayerLevelLayout createLayerLevelLayout() {
282282
final ViewGroup root = getViewHolder().getRoot();
283283
LayerLevelLayout layerLevelLayout = new LayerLevelLayout(root.getContext(), getRealLevel());
284284
layerLevelLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

layer-core/src/main/java/per/goweii/layer/core/Layer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ protected boolean onGlobalPreDraw() {
268268
return true;
269269
}
270270

271-
private void handleShow() {
271+
protected void handleShow() {
272272
if (isShown()) {
273273
if (isOutAnimRunning()) {
274274
startAnimatorIn();
@@ -327,7 +327,7 @@ private void handleInAnimEnd() {
327327
onPostShow();
328328
}
329329

330-
private void handleDismiss() {
330+
protected void handleDismiss() {
331331
if (!isShown()) return;
332332
if (isOutAnimRunning()) return;
333333
if (mShowOnPreDrawListener != null) {

layer-core/src/main/java/per/goweii/layer/core/widget/SwipeLayout.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
@SuppressWarnings("NullableProblems")
3333
public class SwipeLayout extends FrameLayout implements NestedScrollingParent3 {
3434

35-
@IntDef({Direction.NONE, Direction.LEFT, Direction.TOP, Direction.RIGHT, Direction.BOTTOM})
35+
@IntDef(value = {Direction.NONE, Direction.LEFT, Direction.TOP, Direction.RIGHT, Direction.BOTTOM}, flag = true)
3636
@Retention(RetentionPolicy.SOURCE)
3737
public @interface Direction {
3838
int NONE = 0;

layer-notification-ktx/src/main/java/per/goweii/layer/notification/ktx/NotificationLayer.kt

+13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import androidx.annotation.LayoutRes
66
import per.goweii.layer.core.widget.SwipeLayout
77
import per.goweii.layer.notification.DefaultNotificationOnSwipeListener
88
import per.goweii.layer.notification.NotificationLayer
9+
import per.goweii.layer.notification.NotificationLayer.SwipeTransformer
910

1011
fun <T : NotificationLayer> T.contentView(contentView: View) = this.apply {
1112
this.setContentView(contentView)
@@ -23,6 +24,18 @@ fun <T : NotificationLayer> T.maxHeight(maxHeight: Int) = this.apply {
2324
this.setMaxHeight(maxHeight)
2425
}
2526

27+
fun <T : NotificationLayer> T.removeOthers(removeOthers: Boolean) = this.apply {
28+
this.setRemoveOthers(removeOthers)
29+
}
30+
31+
fun <T : NotificationLayer> T.swipeDirection(@SwipeLayout.Direction direction: Int) = this.apply {
32+
this.setSwipeDirection(direction)
33+
}
34+
35+
fun <T : NotificationLayer> T.swipeTransformer(swipeTransformer: SwipeTransformer) = this.apply {
36+
this.setSwipeTransformer(swipeTransformer)
37+
}
38+
2639
fun <T : NotificationLayer> T.duration(duration: Long) = this.apply {
2740
this.setDuration(duration)
2841
}

layer-notification/src/main/java/per/goweii/layer/notification/NotificationLayer.java

+90-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.app.Activity;
55
import android.content.Context;
66
import android.graphics.Rect;
7+
import android.os.SystemClock;
78
import android.view.Gravity;
89
import android.view.LayoutInflater;
910
import android.view.MotionEvent;
@@ -22,13 +23,16 @@
2223
import java.util.List;
2324

2425
import per.goweii.layer.core.DecorLayer;
26+
import per.goweii.layer.core.Layer;
2527
import per.goweii.layer.core.anim.AnimatorHelper;
2628
import per.goweii.layer.core.utils.Utils;
2729
import per.goweii.layer.core.widget.MaxSizeFrameLayout;
2830
import per.goweii.layer.core.widget.SwipeLayout;
2931

3032
public class NotificationLayer extends DecorLayer {
33+
private static long sShowAfterTime = 0L;
3134

35+
private long mShowTime = 0L;
3236
private Runnable mDismissRunnable = null;
3337
private boolean mSwiping = false;
3438

@@ -82,11 +86,34 @@ public ListenerHolder getListenerHolder() {
8286
return (ListenerHolder) super.getListenerHolder();
8387
}
8488

89+
@Override
90+
public void show(final boolean withAnim) {
91+
mShowTime = SystemClock.elapsedRealtimeNanos();
92+
if (getConfig().mRemoveOthers) {
93+
sShowAfterTime = mShowTime;
94+
removeAndWaitOthersDismissed(new Runnable() {
95+
@Override
96+
public void run() {
97+
NotificationLayer.super.show(withAnim);
98+
}
99+
});
100+
} else {
101+
NotificationLayer.super.show(withAnim);
102+
}
103+
}
104+
105+
@Override
106+
protected void handleShow() {
107+
if (sShowAfterTime <= mShowTime) {
108+
super.handleShow();
109+
}
110+
}
111+
85112
@CallSuper
86113
@Override
87114
protected void onAttach() {
88115
super.onAttach();
89-
getViewHolder().getChild().setSwipeDirection(SwipeLayout.Direction.TOP | SwipeLayout.Direction.LEFT | SwipeLayout.Direction.RIGHT);
116+
getViewHolder().getChild().setSwipeDirection(getConfig().mSwipeDirection);
90117
getViewHolder().getChild().setOnSwipeListener(new SwipeLayout.OnSwipeListener() {
91118
@Override
92119
public void onStart(@SwipeLayout.Direction int direction, @FloatRange(from = 0F, to = 1F) float fraction) {
@@ -164,6 +191,11 @@ protected void onPreDismiss() {
164191
super.onPreDismiss();
165192
}
166193

194+
@Override
195+
protected void onDetach() {
196+
super.onDetach();
197+
}
198+
167199
@NonNull
168200
@Override
169201
protected View onCreateChild(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
@@ -305,12 +337,24 @@ public void run() {
305337
}
306338
}
307339

340+
@NonNull
341+
public NotificationLayer setRemoveOthers(boolean removeOthers) {
342+
getConfig().mRemoveOthers = removeOthers;
343+
return this;
344+
}
345+
308346
@NonNull
309347
public NotificationLayer setDuration(long duration) {
310348
getConfig().mDuration = duration;
311349
return this;
312350
}
313351

352+
@NonNull
353+
public NotificationLayer setSwipeDirection(@SwipeLayout.Direction int swipeDirection) {
354+
getConfig().mSwipeDirection = swipeDirection;
355+
return this;
356+
}
357+
314358
@NonNull
315359
public NotificationLayer setSwipeTransformer(@Nullable SwipeTransformer swipeTransformer) {
316360
getConfig().mSwipeTransformer = swipeTransformer;
@@ -328,6 +372,48 @@ public NotificationLayer addOnSwipeListener(@NonNull OnSwipeListener swipeListen
328372
return this;
329373
}
330374

375+
private void removeAndWaitOthersDismissed(@NonNull final Runnable onDismissed) {
376+
final LayerRootLayout layerRootLayout = findLayerRootLayoutFromRoot();
377+
if (layerRootLayout == null) {
378+
onDismissed.run();
379+
return;
380+
}
381+
final LayerLevelLayout layerLevelLayout = findLayerLevelLayoutFromLayerLayout(layerRootLayout);
382+
if (layerLevelLayout == null) {
383+
onDismissed.run();
384+
return;
385+
}
386+
final List<Layer> shownLayers = new ArrayList<>();
387+
final List<Layer> layers = layerLevelLayout.getLayers();
388+
for (Layer layer : layers) {
389+
if (layer.isShown()) {
390+
shownLayers.add(layer);
391+
} else {
392+
layer.dismiss(false);
393+
}
394+
}
395+
if (shownLayers.isEmpty()) {
396+
onDismissed.run();
397+
return;
398+
}
399+
for (final Layer layer : shownLayers) {
400+
layer.addOnDismissListener(new OnDismissListener() {
401+
@Override
402+
public void onPreDismiss(@NonNull Layer layer) {
403+
}
404+
405+
@Override
406+
public void onPostDismiss(@NonNull Layer layer) {
407+
shownLayers.remove(layer);
408+
if (shownLayers.isEmpty()) {
409+
onDismissed.run();
410+
}
411+
}
412+
});
413+
layer.dismiss();
414+
}
415+
}
416+
331417
public static class ViewHolder extends DecorLayer.ViewHolder {
332418
private MaxSizeFrameLayout mContentWrapper;
333419
private View mContent;
@@ -368,10 +454,13 @@ protected static class Config extends DecorLayer.Config {
368454
protected View mContentView = null;
369455
protected int mContentViewId = -1;
370456

457+
protected boolean mRemoveOthers = true;
371458
protected long mDuration = 5000L;
372459
protected int mMaxWidth = -1;
373460
protected int mMaxHeight = -1;
374461

462+
@SwipeLayout.Direction
463+
protected int mSwipeDirection = SwipeLayout.Direction.TOP | SwipeLayout.Direction.LEFT | SwipeLayout.Direction.RIGHT;
375464
@Nullable
376465
protected SwipeTransformer mSwipeTransformer = null;
377466
}

0 commit comments

Comments
 (0)