Skip to content

Instantly share code, notes, and snippets.

Avatar

Brent Mifsud BrentMifsud

View GitHub Profile
@BrentMifsud
BrentMifsud / PreviewDevice+Devices.swift
Last active Nov 30, 2021
Extension on PreviewDevice that includes all available devices
View PreviewDevice+Devices.swift
import SwiftUI
/// Static properties for all preview devices.
///
/// Usage:
///
/// ```swift
/// struct TestView_Previews: PreviewProvider {
/// static var previews: some View {
/// Group {
@BrentMifsud
BrentMifsud / HTTPMethod.swift
Created Nov 5, 2021
Standard HTTPMethods that can be used with URLRequests
View HTTPMethod.swift
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 / ContentResizingTableView.swift
Created Nov 3, 2021
UITableView that resizes to fit content
View ContentResizingTableView.swift
/// 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 / ObservableObject+ObjectDidChange.swift
Last active Sep 24, 2021
A publisher that emits after the object has changed.
View ObservableObject+ObjectDidChange.swift
import Combine
import SwiftUI
extension ObservableObject {
/// A publisher that emits after the object has changed.
///
/// Since `ObjectWillChangePublisher` is triggered right before a value has changed, it is not suitable to be used in UIKit contexts
/// where the intention is to react to the new state of an `ObservableObject`.
///
/// By dispatching the publisher on the main thread, we are able to publish right after the `ObservableObject` changes.
@BrentMifsud
BrentMifsud / NilCodable.swift
Last active Jun 16, 2021
Allows more control over encoding or omitting nil values in JSON.
View NilCodable.swift
/// 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 / MKCoordinateRegion+OffsetZoomHelpers.swift
Last active Feb 24, 2021
Helper Extensions for zooming Annotations and Overlays with an offset from the center of the map.
View MKCoordinateRegion+OffsetZoomHelpers.swift
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 / HTTPStatusCode.swift
Last active Feb 21, 2021 — forked from ollieatkinson/HTTPStatusCode.swift
HTTP status codes as a Swift enum.
View HTTPStatusCode.swift
/// 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 / Image+Data.swift
Last active Jul 12, 2021
SwiftUI Image from data
View Image+Data.swift
import Foundation
import SwiftUI
#if canImport(UIKit)
import UIKit
#elseif canImport(AppKit)
import AppKit
#endif
extension Image {
/// Initializes a SwiftUI `Image` from data.
View UITabBarController+HideTabBar.swift
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 }