Skip to content

Commit

Permalink
Add SafeArea Extension, Pin to Superview, and CollectionView Cell Reg…
Browse files Browse the repository at this point in the history
…istration Extension

- Added a SafeArea extension for safe area handling.
- Added a Pin to Superview extension for view positioning.
- Added an extension for easier registration and dequeuing of cells in collection views.
  • Loading branch information
helloItsHEssam committed Nov 5, 2023
1 parent 40a0eb2 commit c26f532
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Data/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ let package = Package(
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "Data",
dependencies: ["Alamofire", "Domain"]),
dependencies: ["Alamofire", "Domain"],
resources: [.process("Resources")]),
.testTarget(
name: "DataTests",
dependencies: ["Data"]),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// UICollectionView+RegisterAndDequeue.swift
//
//
// Created by Hessam Mahdiabadi on 11/6/23.
//

import UIKit

public extension UICollectionView {

func dequeueReusableCell<T: BaseCollectionCell>(for indexPath: IndexPath) -> T {
let identifier = T.uiIdentifier
guard let cell = dequeueReusableCell(withReuseIdentifier: identifier,
for: indexPath) as? T else {
fatalError("Unable to Dequeue Reusable CollectionView Cell")
}
return cell
}

func registerReusableCell<T: BaseCollectionCell>(type: T.Type) {
let identifier = T.uiIdentifier
register(T.self, forCellWithReuseIdentifier: identifier)
}

func registerSupplementaryView<T: UICollectionReusableView & UISequenceIdentifier>(for kind: String,
type: T.Type) {
let identifier = T.uiIdentifier
register(T.self, forSupplementaryViewOfKind: kind, withReuseIdentifier: identifier)
}

func dequeueSupplementaryView<T: UICollectionReusableView & UISequenceIdentifier>(for kind: String,
at indexPath: IndexPath) -> T {
let identifier = T.uiIdentifier
let view = dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: identifier, for: indexPath)
guard let supplementaryView = view as? T else {
fatalError("Unable to Dequeue Reusable Supplementary View")
}
return supplementaryView
}
}
38 changes: 38 additions & 0 deletions UI/Sources/UI/View/Extension/UIView+pin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// UIView+pin.swift
//
//
// Created by Hessam Mahdiabadi on 11/6/23.
//

import UIKit

public extension UIView {

func pinToSuperview() {
guard let superview = self.superview else {
fatalError("unable to find superview, you must first add to any view and then call this method")
}

NSLayoutConstraint.activate([
self.topAnchor.constraint(equalTo: superview.topAnchor),
self.leadingAnchor.constraint(equalTo: superview.leadingAnchor),
self.trailingAnchor.constraint(equalTo: superview.trailingAnchor),
self.bottomAnchor.constraint(equalTo: superview.bottomAnchor)
])
}

func pinToSuperviewWithSafeArea() {
guard let superview = self.superview else {
fatalError("unable to find superview, you must first add to any view and then call this method")
}

NSLayoutConstraint.activate([
self.topAnchor.constraint(equalTo: superview.topSafeMargin),
self.leadingAnchor.constraint(equalTo: superview.leadingSafeMargin),
self.trailingAnchor.constraint(equalTo: superview.trailingSafeMargin),
self.bottomAnchor.constraint(equalTo: superview.bottomSafeMargin)
])
}
}

43 changes: 43 additions & 0 deletions UI/Sources/UI/View/Extension/UIView+safeArea.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// UIView+safeArea.swift
//
//
// Created by Hessam Mahdiabadi on 11/6/23.
//

import UIKit

public extension UIView {

var topSafeMargin: NSLayoutYAxisAnchor {
self.safeAreaLayoutGuide.topAnchor
}

var bottomSafeMargin: NSLayoutYAxisAnchor {
self.safeAreaLayoutGuide.bottomAnchor
}

var leadingSafeMargin: NSLayoutXAxisAnchor {
self.safeAreaLayoutGuide.leadingAnchor
}

var trailingSafeMargin: NSLayoutXAxisAnchor {
self.safeAreaLayoutGuide.trailingAnchor
}

var widthSafeMargin: NSLayoutDimension {
self.safeAreaLayoutGuide.widthAnchor
}

var heightSafeMargin: NSLayoutDimension {
self.safeAreaLayoutGuide.heightAnchor
}

var centerXSafeMargin: NSLayoutXAxisAnchor {
self.safeAreaLayoutGuide.centerXAnchor
}

var centerYSafeMargin: NSLayoutYAxisAnchor {
self.safeAreaLayoutGuide.centerYAnchor
}
}

0 comments on commit c26f532

Please sign in to comment.