Skip to content

Commit

Permalink
feat: add snackbar builder
Browse files Browse the repository at this point in the history
  • Loading branch information
juandahurt committed Dec 15, 2022
1 parent 754a9e2 commit d9c25d2
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 32 deletions.
45 changes: 41 additions & 4 deletions PuraceDemo/PuraceDemo/Examples/Basic/SnackbarExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,51 @@ import SwiftUI
import Purace

struct SnackBarExample: View {
@State var showActionButton = false
private let types = ["info", "error", "alert"]
@State var selectedType = "info"

func getType() -> PuraceSnackbarType {
switch selectedType {
case "info":
return .info
case "error":
return .error
case "alert":
return .alert
default: return .error
}
}

var body: some View {
VStack {
Text("`PuraceSnackbarView(title: ...)`")
.padding()
HStack {
Toggle(isOn: $showActionButton) {
PuraceTextView("Mostrar el botón de acción")
}
}

HStack {
PuraceTextView("Tipo")
Picker("", selection: $selectedType) {
ForEach(types, id: \.self) {
Text($0)
}
}
Spacer()
}

PuraceButtonView("Mostrar snackbar", fontSize: 14) {
PuraceSnackbarManager.instance.show(withTitle: "Test", type: .info)
PuraceSnackbarBuilder()
.withTitle("Hubo un error")
.withType(getType())
.withAction(title: showActionButton ? "REINTENTAR" : nil, handler: {
print("Action button tapped")
})
.build()
.show()
}
Spacer()
}
}.padding(.horizontal)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@

import UIKit

// TODO: update name to `PuraceSnackbarView`
class PuraceSnackbar: UIView, NibLoadable {
@IBOutlet var contentView: UIView!
@IBOutlet weak var actionButton: UIButton!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet private weak var actionButton: UIButton!
@IBOutlet private weak var titleLabel: UILabel!

private var actionHandler: (() -> Void)?

override init(frame: CGRect) {
super.init(frame: frame)
Expand All @@ -26,12 +29,18 @@ class PuraceSnackbar: UIView, NibLoadable {
private func setupSubviews() {
titleLabel.font = UIFont(name: "Poppins-Regular", size: 14)
titleLabel.textColor = .white

actionButton.titleLabel?.textColor = .white
actionButton.titleLabel?.font = UIFont(name: "Poppins-Medium", size: 14)

contentView.layer.cornerRadius = 10
}

func setup(with title: String, type: PuraceSnackbarType) {
func setTitle(_ title: String) {
titleLabel.text = title
}

func setType(_ type: PuraceSnackbarType) {
switch type {
case .info:
contentView.backgroundColor = .init(PuraceStyle.Color.G1)
Expand All @@ -40,7 +49,17 @@ class PuraceSnackbar: UIView, NibLoadable {
case .error:
contentView.backgroundColor = .init(PuraceStyle.Color.R1)
}
titleLabel.text = title
}

func setAction(withTitle title: String?, _ handler: (() -> Void)?) {
actionButton.setTitle(title, for: .normal)
if title != nil {
actionHandler = handler
}
}

@IBAction private func onActionButtonTap(_ sender: UIButton) {
actionHandler?()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
</constraints>
<inset key="imageEdgeInsets" minX="0.0" minY="0.0" maxX="2.2250738585072014e-308" maxY="0.0"/>
<state key="normal" title="Button"/>
<connections>
<action selector="onActionButtonTap:" destination="-1" eventType="touchUpInside" id="Q4Y-Oo-hvy"/>
</connections>
</button>
</subviews>
</stackView>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// PuraceSnackbarContent.swift
//
//
// Created by Juan Hurtado on 21/11/22.
//

import Foundation

public struct PuraceSnackbarContent {
var title: String = ""
var type: PuraceSnackbarType = .info
var action: (() -> Void)?
var actionTitle: String?
}
47 changes: 47 additions & 0 deletions Sources/Purace/Views/Basic/Snackbar v2/PuraceSnackbarBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// PuraceSnackbarBuilder.swift
//
//
// Created by Juan Hurtado on 14/12/22.
//

import Foundation

/// Snackbar builder.
///
/// If you want to build and show a snackbar:
/// ```
/// let builder = PuraceSnackbarBuilder()
///
/// builder
/// .withTitle("This is a snackbar!")
/// .build()
/// .show()
/// ```
public class PuraceSnackbarBuilder {
private var content: PuraceSnackbarContent

public init() {
content = PuraceSnackbarContent()
}

public func withTitle(_ title: String) -> PuraceSnackbarBuilder {
content.title = title
return self
}

public func withType(_ type: PuraceSnackbarType) -> PuraceSnackbarBuilder {
content.type = type
return self
}

public func withAction(title: String?, handler: @escaping () -> Void) -> PuraceSnackbarBuilder {
content.actionTitle = title
content.action = handler
return self
}

public func build() -> PuraceSnackbarDisplayableContent {
PuraceSnackbarDisplayableContent(content: content)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// PuraceSnackbarDisplayableContent.swift
//
//
// Created by Juan Hurtado on 14/12/22.
//

import Foundation

public class PuraceSnackbarDisplayableContent {
private let content: PuraceSnackbarContent

init(content: PuraceSnackbarContent) {
self.content = content
}

public func show() {
PuraceSnackbarManager.instance.show(using: content)
}
}
23 changes: 14 additions & 9 deletions Sources/Purace/Views/Basic/Snackbar v2/PuraceSnackbarManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation
import UIKit

public class PuraceSnackbarManager {
class PuraceSnackbarManager {
private static var _instance: PuraceSnackbarManager? = nil
public static var instance: PuraceSnackbarManager {
if _instance == nil {
Expand All @@ -25,12 +25,18 @@ public class PuraceSnackbarManager {

private init() {}

private func setupSnackbarView(withTitle title: String, type: PuraceSnackbarType) {
private func setupSnackbarView(using content: PuraceSnackbarContent) {
guard let window = UIApplication.shared.keyWindow else { return }
var topController = window.rootViewController
while let presentedViewController = topController?.presentedViewController {
topController = presentedViewController
}
let bottomPadding = window.safeAreaInsets.bottom

snackbar = PuraceSnackbar(frame: .init(x: 0, y: 0, width: 100, height: 0))
snackbar?.setup(with: title, type: type)
snackbar?.setTitle(content.title)
snackbar?.setType(content.type)
snackbar?.setAction(withTitle: content.actionTitle, content.action)
window.addSubview(snackbar!)

// Height constriant
Expand All @@ -51,16 +57,16 @@ public class PuraceSnackbarManager {
/// When called this function, some verbose logs will be printed on the console.
///
/// Call this function **only** for debug purpuses.
public func debug() -> PuraceSnackbarManager {
func debug() -> PuraceSnackbarManager {
verbose = true
return self
}

public func show(withTitle title: String, type: PuraceSnackbarType) {
func show(using content: PuraceSnackbarContent) {
if isPresented {
enqueue(label: "show") { [weak self] in
guard let self else { return }
self.show(withTitle: title, type: type)
self.show(using: content)
}
return
}
Expand All @@ -69,7 +75,7 @@ public class PuraceSnackbarManager {
print("showing snackbar")
}

setupSnackbarView(withTitle: title, type: type)
setupSnackbarView(using: content)
UIView.animate(withDuration: 0.4, delay: 0, animations: { [weak self] in
guard let self else { return }
self.snackbar?.transform = .init(translationX: 0, y: -PuraceSnackbar.height - PuraceSnackbar.padding)
Expand All @@ -82,10 +88,9 @@ public class PuraceSnackbarManager {
}
}
isPresented = true

}

public func hide() {
func hide() {
if verbose {
print("hiding snackbar")
}
Expand Down

0 comments on commit d9c25d2

Please sign in to comment.