documentation

This commit is contained in:
Daniel Arantes Loverde
2025-06-23 09:48:57 -03:00
parent 189efd7154
commit 0910973e9a
5 changed files with 307 additions and 22 deletions

View File

@@ -24,10 +24,19 @@ import UIKit
// MARK: - Protocols
/// A protocol that defines optional methods for `LCSnackBarView` delegate.
@objc
public protocol LCSnackBarViewDelegate {
/// Called when the snackbar starts its exhibition.
/// - Parameter didStartExibition: The `LCSnackBarView` instance that started exhibition.
@objc optional func snackbar(didStartExibition: LCSnackBarView)
/// Called when the snackbar is touched.
/// - Parameter snackbar: The `LCSnackBarView` instance that was touched.
@objc optional func snackbar(didTouchOn snackbar: LCSnackBarView)
/// Called when the snackbar ends its exhibition.
/// - Parameter didEndExibition: The `LCSnackBarView` instance that ended exhibition.
@objc optional func snackbar(didEndExibition: LCSnackBarView)
}
@@ -36,23 +45,36 @@ public protocol LCSnackBarViewDelegate {
// MARK: - Local Defines / ENUMS
/// Enumeration defining the visual style of the `LCSnackBarView`.
public enum LCSnackBarViewType {
case `default`, rounded
/// The default style, typically rectangular.
case `default`
/// A rounded style for the snackbar.
case rounded
}
/// Enumeration defining the orientation of the `LCSnackBarView`.
public enum LCSnackBarOrientation {
case top, bottom
/// The snackbar appears at the top of the screen.
case top
/// The snackbar appears at the bottom of the screen.
case bottom
}
/// Enumeration defining the display duration for the `LCSnackBarView`.
public enum LCSnackBarTimer: CGFloat {
/// The snackbar remains visible indefinitely until dismissed manually.
case infinity = 0
/// Minimum display duration (2 seconds).
case minimum = 2
/// Medium display duration (5 seconds).
case medium = 5
/// Maximum display duration (10 seconds).
case maximum = 10
}
// MARK: - Class
/// LCSnackBarView is a simple SnackBar that you can display notifications in app to improve your app comunication
/// `LCSnackBarView` is a simple SnackBar that you can display notifications in-app to improve your app communication.
///
/// Usage example:
///
@@ -62,7 +84,7 @@ public enum LCSnackBarTimer: CGFloat {
/// .configure(text: "Hello World!")
/// .present()
///```
///You can set delegate to interact with it
///You can set a delegate to interact with it:
///
///```swift
///let notification = LCSnackBarView(delegate: self)
@@ -78,6 +100,7 @@ public final class LCSnackBarView: UIView {
// MARK: - Private properties
/// The content view that holds the snackbar's elements.
private lazy var contentView: UIView = {
$0.backgroundColor = .white
$0.translatesAutoresizingMaskIntoConstraints = false
@@ -85,6 +108,7 @@ public final class LCSnackBarView: UIView {
return $0
}(UIView())
/// The label that displays the main text of the snackbar.
private lazy var descriptionLabel: UILabel = {
$0.font = .systemFont(ofSize: 12, weight: .regular)
$0.text = nil
@@ -117,10 +141,16 @@ public final class LCSnackBarView: UIView {
// MARK: - Public properties
/// The delegate for the snackbar view.
public weak var delegate: LCSnackBarViewDelegate?
// MARK: - Initializers
/// Initializes a new `LCSnackBarView` instance.
/// - Parameters:
/// - style: The visual style of the snackbar. Defaults to `.default`.
/// - orientation: The orientation (top or bottom) of the snackbar. Defaults to `.top`.
/// - delegate: The delegate to receive snackbar events. Defaults to `nil`.
public init(
style: LCSnackBarViewType = .default,
orientation: LCSnackBarOrientation = .top,
@@ -161,12 +191,14 @@ public extension LCSnackBarView {
// MARK: - Private methods
/// Sets up the default layout properties for the snackbar.
private func setupDefaultLayout() {
backgroundColor = .white
contentView.backgroundColor = .white
clipsToBounds = true
}
/// Sets up a tap gesture recognizer for the snackbar.
private func setupGestureRecognizer() {
let gesture = UITapGestureRecognizer(target: self, action: #selector(onTapGestureAction))
gesture.numberOfTapsRequired = 1
@@ -174,6 +206,7 @@ public extension LCSnackBarView {
addGestureRecognizer(gesture)
}
/// Configures observers for keyboard appearance and disappearance notifications.
private func setKeyboardObserver() {
// Show
NotificationCenter
@@ -196,6 +229,8 @@ public extension LCSnackBarView {
)
}
/// Handles the `keyboardWillShowNotification` to adjust the snackbar's position.
/// - Parameter notification: The `Notification` object containing keyboard information.
@objc private func keyboardWillShow(_ notification: Notification?) -> Void {
if let info = notification?.userInfo {
@@ -236,6 +271,8 @@ public extension LCSnackBarView {
}
}
/// Handles the `keyboardWillHideNotification`.
/// - Parameter notification: The `Notification` object.
@objc private func keyboardWillHide(_ notification: Notification?) -> Void {
DispatchQueue.main.async { [weak self] in
self?.systemKeyboardVisible = false
@@ -243,6 +280,7 @@ public extension LCSnackBarView {
}
}
/// Updates the snackbar's style properties, such as width and corner radius, based on `_style`.
private func updateStyle() {
switch _style {
case .rounded:
@@ -256,6 +294,8 @@ public extension LCSnackBarView {
}
}
/// Positions the snackbar view within the given superview based on its `_orientation`.
/// - Parameter view: The `UIView` that will contain the snackbar.
private func positioningView(_ view: UIView) {
view
.addSubview(self,
@@ -286,6 +326,10 @@ public extension LCSnackBarView {
height: _height)
}
/// Displays the snackbar with an animation.
/// - Parameters:
/// - controller: The `UIViewController` on which the snackbar will be presented.
/// - completion: A closure to be executed once the presentation animation completes.
private func showSnackBar(controller: UIViewController, completion: @escaping (() -> Void)) {
if isOpen {
closeSnackBar(controller: controller) {
@@ -320,6 +364,10 @@ public extension LCSnackBarView {
}
}
/// Hides the snackbar with an animation.
/// - Parameters:
/// - controller: The `UIViewController` from which the snackbar is being dismissed.
/// - completion: A closure to be executed once the dismissal animation completes.
private func closeSnackBar(controller: UIViewController, completion: @escaping (() -> Void)) {
let distance = CGFloat(_style == .rounded ? (_orientation == .top ? UIDevice.topNotch : UIDevice.bottomNotch) : 0)
layoutIfNeeded()
@@ -341,6 +389,8 @@ public extension LCSnackBarView {
}
}
/// Handles tap gestures on the snackbar.
/// - Parameter _: The `UITapGestureRecognizer` instance.
@objc
private func onTapGestureAction(_ : UITapGestureRecognizer) {
self.delegate?.snackbar?(didTouchOn: self)
@@ -350,6 +400,7 @@ public extension LCSnackBarView {
}
}
/// Adds the subviews to the snackbar and sets up their constraints.
private func addComponentsAndConstraints() {
// MARK: - Add Subviews
@@ -376,18 +427,29 @@ public extension LCSnackBarView {
// MARK: - Public methods
/// Configures the text displayed in the snackbar.
/// - Parameter text: The `String` text to display.
/// - Returns: The `LCSnackBarView` instance for chaining.
@discardableResult
func configure(text: String) -> Self {
descriptionLabel.text = text
return self
}
/// Configures the color of the text in the snackbar.
/// - Parameter textColor: The `UIColor` for the text.
/// - Returns: The `LCSnackBarView` instance for chaining.
@discardableResult
func configure(textColor: UIColor) -> Self {
descriptionLabel.textColor = textColor
return self
}
/// Configures the font and text alignment of the snackbar's text.
/// - Parameters:
/// - textFont: The `UIFont` for the text.
/// - alignment: The `NSTextAlignment` for the text. Defaults to `.center`.
/// - Returns: The `LCSnackBarView` instance for chaining.
@discardableResult
func configure(textFont: UIFont, alignment: NSTextAlignment = .center) -> Self {
descriptionLabel.font = textFont
@@ -395,6 +457,9 @@ public extension LCSnackBarView {
return self
}
/// Configures the background color of the snackbar.
/// - Parameter backgroundColor: The `UIColor` for the background.
/// - Returns: The `LCSnackBarView` instance for chaining.
@discardableResult
func configure(backgroundColor: UIColor) -> Self {
self.backgroundColor = backgroundColor
@@ -402,12 +467,20 @@ public extension LCSnackBarView {
return self
}
/// Configures the exhibition duration (timer) of the snackbar.
/// - Parameter timer: The `LCSnackBarTimer` value.
/// - Returns: The `LCSnackBarView` instance for chaining.
@discardableResult
func configure(exibition timer: LCSnackBarTimer) -> Self {
_timer = timer
return self
}
/// Configures an image icon to be displayed before the text in the snackbar.
/// - Parameters:
/// - icon: The `UIImageView` to use as the icon.
/// - withTintColor: An optional `UIColor` to tint the icon. Defaults to `nil`.
/// - Returns: The `LCSnackBarView` instance for chaining.
@discardableResult
func configure(imageIconBefore icon: UIImageView, withTintColor: UIColor? = nil) -> Self {
icon.setHeight(size: 24)
@@ -428,6 +501,8 @@ public extension LCSnackBarView {
return self
}
/// Presents the snackbar on the top-most view controller.
/// - Parameter completion: An optional closure to be executed after the presentation.
func present(completion: (()->())? = nil) {
if isOpen { return }
if let controller = LCEssentials.getTopViewController(aboveBars: true) {