Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[trello.com/c/sI4VXuJa] Added swipe gestures (left & down) to Snackbar #595

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,22 @@ import UIKit
import CommonKit

struct NotificationModel: Equatable, Hashable {
static func == (lhs: NotificationModel, rhs: NotificationModel) -> Bool {
return lhs.icon == rhs.icon
&& lhs.title == rhs.title
&& lhs.description == rhs.description
}

func hash(into hasher: inout Hasher) {
hasher.combine(icon)
hasher.combine(title)
hasher.combine(description)
hasher.combine(tapHandler)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove


let icon: UIImage?
let title: String?
let description: String?
let tapHandler: IDWrapper<() -> Void>?
let cancelAutoDismiss: IDWrapper<() -> Void>?
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import CommonKit

struct NotificationView: View {
@State private var dragTranslation: CGFloat = .zero
@State private var horizontalDragTranslation: CGFloat = .zero
@State private var minTranslationForDismiss: CGFloat = .infinity
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Y

@State private var minTranslationXForDismiss: CGFloat = .infinity
@State private var isTextLimited: Bool = true

@Binding var dismissEdge: Edge
var onDismissEdgeChanged: ((Edge) -> Void)?
let model: NotificationModel
let safeAreaInsets: EdgeInsets
let dismissAction: () -> Void
Expand All @@ -27,9 +32,11 @@ struct NotificationView: View {
.padding([.leading, .trailing], 15)
.padding([.top, .bottom], 10)
.background(GeometryReader(content: processGeometry))
.expanded(axes: .horizontal)
.offset(y: dragTranslation < .zero ? dragTranslation : .zero)
.offset(x: horizontalDragTranslation < .zero ? horizontalDragTranslation : .zero)
.gesture(dragGesture)
.onTapGesture(perform: onTap)
.background(Color(.adamant.swipeBlockColor))
.cornerRadius(10)
.padding(.horizontal, 15)
.padding(.top, safeAreaInsets.top)
Expand All @@ -48,38 +55,58 @@ private extension NotificationView {
}

var textStack: some View {
VStack(alignment: .leading, spacing: .zero) {
VStack(alignment: .leading, spacing: 3) {
if let title = model.title {
Text(title)
.font(.system(size: 15, weight: .bold))
}
if let description = model.description {
Text(description)
.font(.system(size: 13))
.lineLimit(3)
.lineLimit(isTextLimited ? 3 : nil)

}
}
}

var dragGesture: some Gesture {
DragGesture()
.onChanged { dragTranslation = $0.translation.height }
.onChanged {
dragTranslation = $0.translation.height
horizontalDragTranslation = $0.translation.width
}
.onEnded {
print(-$0.translation.height, minTranslationForDismiss)
if $0.velocity.height < -100 || -$0.translation.height > minTranslationForDismiss {
dismissAction()
onDismissEdgeChanged?(.top)
Task {
dismissAction()
}
} else if $0.velocity.width < -100 || $0.translation.width > minTranslationXForDismiss {
onDismissEdgeChanged?(.leading)
Task {
dismissAction()
}
} else if $0.velocity.height > -100 || -$0.translation.height < minTranslationForDismiss {
horizontalDragTranslation = .zero
Copy link
Member

@just-software-dev just-software-dev Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove? Animation?

isTextLimited = false
model.cancelAutoDismiss?.value()
} else {
withAnimation { dragTranslation = .zero }
withAnimation {
dragTranslation = .zero
horizontalDragTranslation = .zero
}
}
}
}

func processGeometry(_ geometry: GeometryProxy) -> some View {
DispatchQueue.main.async {
minTranslationForDismiss = geometry.size.height / 2
minTranslationXForDismiss = geometry.size.width / 2
}

return Color.clear
return Color.init(uiColor: .adamant.swipeBlockColor)
.cornerRadius(10)
}

func onTap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import CommonKit

struct PopupCoordinatorView: View {
@ObservedObject var model: PopupCoordinatorModel
@State var dismissEdge: Edge = .top

var body: some View {
GeometryReader { geomerty in
Expand All @@ -34,14 +35,19 @@ private extension PopupCoordinatorView {
VStack {
if let notificationModel = model.notification {
NotificationView(
dismissEdge: $dismissEdge,
onDismissEdgeChanged: { newEdge in
dismissEdge = newEdge
},
model: notificationModel,
safeAreaInsets: safeAreaInsets,
dismissAction: { [weak model] in
model?.notification = nil
dismissEdge = .top
}
)
.id(model.notification?.hashValue)
.transition(.move(edge: .top))
.transition(.move(edge: dismissEdge))
}
Spacer()
}
Expand Down
5 changes: 4 additions & 1 deletion PopupKit/Sources/PopupKit/PopupManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ public extension PopupManager {
icon: icon,
title: title,
description: description,
tapHandler: tapHandler.map { .init(id: .empty, value: $0) }
tapHandler: tapHandler.map { .init(id: .empty, value: $0) },
cancelAutoDismiss: .init(id: .empty, value: { [weak self] in
self?.autoDismissManager.notificationDismissSubscription?.cancel()
})
)

if autoDismiss {
Expand Down