Skip to content

Instantly share code, notes, and snippets.

View BrentMifsud's full-sized avatar

Brent Mifsud BrentMifsud

View GitHub Profile
@BrentMifsud
BrentMifsud / HTTPStatusCode.swift
Last active February 21, 2021 07:35 — forked from ollieatkinson/HTTPStatusCode.swift
HTTP status codes as a Swift enum.
/// This is a list of Hypertext Transfer Protocol (HTTP) response status codes.
/// It includes codes from IETF internet standards, other IETF RFCs, other specifications, and some additional commonly used codes.
/// The first digit of the status code specifies one of five classes of response; an HTTP client must recognise these five classes at a minimum.
#if canImport(Foundation)
import Foundation
#else
import FoundationNetworking
#endif
@BrentMifsud
BrentMifsud / MKCoordinateRegion+OffsetZoomHelpers.swift
Last active February 24, 2021 23:25
Helper Extensions for zooming Annotations and Overlays with an offset from the center of the map.
extension MKCoordinateRegion {
/// Initialize a `MKCoordinateRegion` from a set of `MKAnnotations`
/// - Parameter annotations: Annotations
init(for annotations: [MKAnnotation]) {
var minLat: CLLocationDegrees = 90.0
var maxLat: CLLocationDegrees = -90.0
var minLon: CLLocationDegrees = 180.0
var maxLon: CLLocationDegrees = -180.0
for annotation in annotations {
@BrentMifsud
BrentMifsud / NilCodable.swift
Last active June 16, 2021 22:40
Allows more control over encoding or omitting nil values in JSON.
/// Property wrapper that provides control over whether to explicitly encode nil values to json or not.
/// - Parameters:
/// - wrappedValue: the value to be encoded.
/// - encodeNil: Should the value be explicitly encoded as "null" in json if nil.
/// - Note:
/// Usage:
///
/// ```swift
/// struct MyStruct: Codable {
/// @NilCodable var myNullable: String? // defaults to omitting null (codable default behavior)
@BrentMifsud
BrentMifsud / ContentResizingTableView.swift
Created November 3, 2021 02:45
UITableView that resizes to fit content
/// Resizes to fit its content. Or until size reaches provided constraints.
final class ContentResizingTableView: UITableView {
override var contentSize: CGSize {
didSet {
invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
layoutIfNeeded()
@BrentMifsud
BrentMifsud / HTTPMethod.swift
Created November 5, 2021 21:16
Standard HTTPMethods that can be used with URLRequests
import Foundation
/// Standard HTTP Methods
enum HTTPMethod: String {
case options = "OPTIONS"
case get = "GET"
case head = "HEAD"
case post = "POST"
case put = "PUT"
case patch = "PATCH"
@BrentMifsud
BrentMifsud / PreviewDevice+Devices.swift
Last active October 31, 2022 18:50
Extension on PreviewDevice that includes all available devices
import SwiftUI
/// Static properties for all preview devices.
///
/// Usage:
///
/// ```swift
/// struct TestView_Previews: PreviewProvider {
/// static var previews: some View {
/// Group {
@BrentMifsud
BrentMifsud / SizeReader.swift
Last active February 16, 2023 01:38
A SwiftUI ViewModifier that reads a view's size
import SwiftUI
public extension View {
/// Exposes the size of the specified view
/// - Parameter onChange: This function will be called when the size of a view changes.
/// - Returns: some View
func onSizeChange(onChange: @escaping (CGSize) -> Void) -> some View {
modifier(SizeReader(onChange: onChange))
}
}
@BrentMifsud
BrentMifsud / SafeAreaReader.swift
Last active February 16, 2023 01:38
View Modifier that will fetch a SwiftUI view's safe area inset and expose it via the preferences system.
import SwiftUI
public extension View {
func onSafeAreaChange(onChange: @escaping (EdgeInsets) -> Void) -> some View {
modifier(SafeAreaReader(onChange: onChange))
}
}
internal struct SafeAreaReader: ViewModifier {
var onChange: (EdgeInsets) -> Void
@BrentMifsud
BrentMifsud / UITestHelpers.swift
Last active February 16, 2023 02:17
Xcode UI Testing Helpers
import XCTest
/// Bundle identifiers for apples native apps. Used to open other apps during UI testing.
///
/// More can be found here: https://support.apple.com/en-ca/guide/deployment/depece748c41/web
enum AppleBundleIdentifiers: String {
case safari = "com.apple.mobilesafari"
case springboard = "com.apple.springboard"
}
import UIKit
extension UITabBarController {
/// Extends the size of the `UITabBarController` view frame, pushing the tab bar controller off screen.
/// - Parameters:
/// - hidden: Hide or Show the `UITabBar`
/// - animated: Animate the change
func setTabBarHidden(_ hidden: Bool, animated: Bool) {
guard let vc = selectedViewController else { return }
guard tabBarHidden != hidden else { return }