Skip to content

Commit

Permalink
Merge pull request #30 from payan-app/snackbar-improvements
Browse files Browse the repository at this point in the history
feat: add snackbar improvements
  • Loading branch information
juandahurt authored Aug 25, 2022
2 parents 0fb6218 + c6e8781 commit 14aff73
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 51 deletions.
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/onevcat/Kingfisher.git", .upToNextMinor(from: "7.2.0")),
.package(url: "https://github.com/GeorgeElsham/ViewExtractor", .exact("1.1.0")),
.package(url: "https://github.com/exyte/PopupView.git", from: "1.0.0")
],
targets: [
.target(
name: "Purace",
dependencies: ["Kingfisher", "ViewExtractor"],
dependencies: ["Kingfisher", "ViewExtractor", "PopupView"],
resources: [
.copy("Resources/Fonts/Poppins/Poppins-Regular.ttf"),
.copy("Resources/Fonts/Poppins/Poppins-Medium.ttf"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"version": "7.2.2"
}
},
{
"package": "PopupView",
"repositoryURL": "https://github.com/exyte/PopupView.git",
"state": {
"branch": null,
"revision": "a25e22b46d47558b1e7e049b5734522c6706600c",
"version": "1.1.5"
}
},
{
"package": "ViewExtractor",
"repositoryURL": "https://github.com/GeorgeElsham/ViewExtractor",
Expand Down
2 changes: 1 addition & 1 deletion PuraceDemo/PuraceDemo/Examples/Basic/SnackbarExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ struct SnackBarExample: View {
showSnackbar = !showSnackbar
}
Spacer()
}.snackBar(title: "Parece que ha ocurrido un error", isVisible: $showSnackbar, type: .error, buttonTitle: "REINTENTAR")
}.snackBar(title: "Parece que ha ocurrido un error", isVisible: $showSnackbar, type: .info, buttonTitle: "REINTENTAR", duration: .long, dismissOnDrag: true)
}
}
75 changes: 26 additions & 49 deletions Sources/Purace/Views/Basic/Snackbar/PuraceSnackbarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
//

import Foundation
import PopupView
import SwiftUI

struct PuraceSnackbarView: View {
@State var yOffset: CGFloat = 120
@State var opacity: Double = 0

@Binding var isVisible: Bool
let title: String
let type: PuraceSnackbarType
@State var isVisibleAux: Bool = true
@Binding var isVisible: Bool
let buttonTitle: String?
let buttonOnTap: (() -> Void)?

Expand All @@ -39,72 +36,52 @@ struct PuraceSnackbarView: View {
}

public var body: some View {
VStack {
HStack {
PuraceTextView(title, textColor: .white)
Spacer()
HStack {
PuraceTextView(title, fontSize: 14, textColor: .white)
Spacer()
if let buttonTitle = buttonTitle {
PuraceButtonView(buttonTitle, fontSize: 14, type: .custom(.clear, .white.opacity(0.1), .white)) {
buttonOnTap?()
isVisible = false
}
if let buttonTitle = buttonTitle {
PuraceButtonView(buttonTitle, fontSize: 14, type: .custom(.clear, .white.opacity(0.1), .white)) {
buttonOnTap?()
isVisible = false
}
}
.lineLimit(2)
.padding()
.background(getBackgroundColor())
.offset(x: 0, y: yOffset)
.onAppear {
withAnimation(.spring()) {
yOffset = 0
opacity = 1
}
}
.edgesIgnoringSafeArea(.bottom)
}.onChange(of: isVisible, perform: { _ in
withAnimation(.spring()) {
yOffset = 120
opacity = 0
}
})
}
.lineLimit(2)
.padding(.vertical)
.padding(.horizontal, 20)
.background(getBackgroundColor())
}
}


struct PuraceSnackbarViewModifier: ViewModifier {
@Binding var isVisible: Bool
@State var isVisibleAux = false
var title: String
var buttonTitle: String?
var type: PuraceSnackbarType
var buttonOnTap: (() -> Void)?
var onDissappear: (() -> Void)?
var duration: PuraceSnackbarDuration
var dismissOnDrag = false
var closeOnTap = false

func body(content: Content) -> some View {
ZStack {
content
if isVisibleAux {
PuraceSnackbarView(title: title, type: type, isVisible: $isVisible, buttonTitle: buttonTitle, buttonOnTap: buttonOnTap)
.onDisappear {
onDissappear?()
}
}
}.onChange(of: isVisible) { newValue in
if !newValue {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.35) {
isVisibleAux = newValue
}
} else {
isVisibleAux = newValue
}
}
.popup(isPresented: $isVisible, type: .toast, autohideIn: Double(duration.rawValue), dragToDismiss: dismissOnDrag, closeOnTap: closeOnTap) {
PuraceSnackbarView(title: title, type: type, isVisible: $isVisible, buttonTitle: buttonTitle, buttonOnTap: buttonOnTap)
}
}
}

public enum PuraceSnackbarDuration: String {
case short = "2.0"
case long = "3.5"
case endless = ""
}

public extension View {
func snackBar(title: String, isVisible: Binding<Bool>, type: PuraceSnackbarType = .info, buttonTitle: String? = nil, buttonOnTap: (() -> Void)? = nil, onDissappear: (() -> Void)? = nil) -> some View {
modifier(PuraceSnackbarViewModifier(isVisible: isVisible, title: title, buttonTitle: buttonTitle, type: type, buttonOnTap: buttonOnTap, onDissappear: onDissappear))
func snackBar(title: String, isVisible: Binding<Bool>, type: PuraceSnackbarType = .info, buttonTitle: String? = nil, duration: PuraceSnackbarDuration = .endless, dismissOnDrag: Bool = false, closeOnTap: Bool = false, buttonOnTap: (() -> Void)? = nil) -> some View {
modifier(PuraceSnackbarViewModifier(isVisible: isVisible, title: title, buttonTitle: buttonTitle, type: type, buttonOnTap: buttonOnTap, duration: duration, dismissOnDrag: dismissOnDrag, closeOnTap: closeOnTap))
}
}

0 comments on commit 14aff73

Please sign in to comment.