Skip to content

Commit

Permalink
feat: add snackbar type
Browse files Browse the repository at this point in the history
  • Loading branch information
juandahurt committed May 20, 2022
1 parent ae8646a commit 898912b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions PYLayoutDemo/PYLayoutDemo/Examples/SnackbarExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct SnackBarExample: View {
Spacer()
PYSnackbarView(
title: "Hay una actualización disponible",
type: .alert,
isVisible: $showSnackbar,
buttonTitle: "ACTUALIZAR"
)
Expand Down
12 changes: 12 additions & 0 deletions Sources/PYLayout/Views/Basic/Snackbar/PYSnackbarType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// PYSnackbarType.swift
//
//
// Created by Juan Hurtado on 20/05/22.
//

import Foundation

public enum PYSnackbarType {
case info, alert
}
15 changes: 13 additions & 2 deletions Sources/PYLayout/Views/Basic/Snackbar/PYSnackbarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,28 @@ public struct PYSnackbarView: View {
@State var opacity: Double = 0

let title: String
let type: PYSnackbarType
@Binding var isVisible: Bool
let buttonTitle: String?
let buttonOnTap: (() -> Void)?

public init(title: String, isVisible: Binding<Bool>, buttonTitle: String? = nil, buttonOnTap: (() -> Void)? = nil) {
public init(title: String, type: PYSnackbarType = .info, isVisible: Binding<Bool>, buttonTitle: String? = nil, buttonOnTap: (() -> Void)? = nil) {
self.title = title
self.type = type
self._isVisible = isVisible
self.buttonTitle = buttonTitle
self.buttonOnTap = buttonOnTap
}

private func getBackgroundColor() -> Color {
switch type {
case .info:
return .gray
case .alert:
return .blue
}
}

public var body: some View {
HStack {
PYTextView(title, fontSize: 14, textColor: .white)
Expand All @@ -37,7 +48,7 @@ public struct PYSnackbarView: View {
.lineLimit(2)
.padding()
.frame(width: UIScreen.main.bounds.width - 40)
.background(Color.blue)
.background(getBackgroundColor())
.cornerRadius(5)
.onChange(of: isVisible, perform: { isPresented in
withAnimation(.spring()) {
Expand Down

0 comments on commit 898912b

Please sign in to comment.