Skip to content

Instantly share code, notes, and snippets.

@ajjames
ajjames / Logger.swift
Created August 2, 2021 20:05
Common Logger
import Foundation
import os.log
protocol Logger {
func error(_ error: String)
func warning(_ warning: String)
func event(_ event: String)
func message(_ message: String)
}
@ajjames
ajjames / Float+Extensions.swift
Created August 2, 2021 20:03
Common Float Extension
import Foundation
extension Float {
var toOneDecimalPlace: String { String(format: "%.1f", self) }
}
@ajjames
ajjames / DateFormatter+Extensions.swift
Created August 2, 2021 20:03
Common DateFormatter Extension
import Foundation
extension DateFormatter {
static let mediumDateTimeFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.timeZone = TimeZone.current
formatter.dateStyle = .medium
formatter.timeStyle = .medium
return formatter
@ajjames
ajjames / DemoServicePlayground.swift
Last active August 4, 2021 16:52
Earthquake Service Demo
import Combine
import Foundation
// MARK: - Supporting Models: Once per codebase
enum HTTPMethod: String {
case delete
case get
case post
@ajjames
ajjames / OverlayView.swift
Created April 27, 2020 23:38
SwiftUI fullscreen overlay modifier
import SwiftUI
struct OverlayView<OverlayContent: View>: ViewModifier {
@Binding var isPresented: Bool
var modalContent: OverlayContent
var transition: AnyTransition = .move(edge: .bottom)
var animation: Animation = .easeInOut
func body(content: Content) -> some View {
@ajjames
ajjames / IfViews.swift
Created April 21, 2020 22:18
SwiftUI Helpers
import Combine
import SwiftUI
struct IfNotNil<Value, Content>: View where Content: View {
private let value: Value?
private let content: (Value) -> Content
init(_ value: Value?,
@ViewBuilder show content: @escaping (Value) -> Content) {
@ajjames
ajjames / ProofOfConceptTests.swift
Last active January 25, 2017 22:20
Proof-of-Concept Tests
import XCTest
import Realm
import RealmSwift
class ProofOfConceptTests: RealmTestCase {
// NOTE this is for objects with 1-to-1 relationships
func testUpdateAsset() {
// 1. make a full object
let city = City()
@ajjames
ajjames / Bakery.swift
Created January 19, 2017 20:14
Bakery Example
import Foundation
class Bakery {
func bakeCake() -> Cake? {
let ingredientStorage = IngredientStorage()
if let chocolateFlavor = ingredientStorage.consume(flavor: .chocolate) {
return Cake(flavor: chocolateFlavor)
} else if let vanillaFlavor = ingredientStorage.consume(flavor: .vanilla) {
return Cake(flavor: vanillaFlavor)
}
@ajjames
ajjames / RawRepresentableConstants.swift
Last active April 14, 2022 09:55
Extendable 'enums' using RawRepresentable
import Foundation
/*
This is used by NSNotification.Name to allow enum-like members,
but where new members can be added in an extension!
enum Directions: String {
case north = "North"
}
@ajjames
ajjames / Registry.h
Created March 21, 2016 23:02
A simple Service Locator pattern for Swift and Obj-c
#import <Foundation/Foundation.h>
#define dep(p) [[Registry shared] resolve: (@protocol(p))]
#define reg(c, p) [[Registry shared] register:[c class] forProtocol:@protocol(p)]
@interface Registry : NSObject
@property (nonatomic, strong) NSMutableDictionary * _Nonnull classRegister;
@property (nonatomic, strong) NSMutableDictionary * _Nonnull classInstances;
+(instancetype _Nonnull)shared;
- (id _Nullable)resolve:(Protocol * _Null_unspecified)aProtocol;