Skip to content

Instantly share code, notes, and snippets.

fileprivate extension DateFormatter {
static var month: DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = "MMMM"
return formatter
}
static var monthAndYear: DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = "MMMM yyyy"
@dfrobison
dfrobison / SimplePhoneFormatter.swift
Created April 19, 2020 16:31
[Simple phone number formatter] Phone number formatter based on a pattern
func formatPhone(phone: String) -> String {
let cleanPhoneNumber = phone.components(separatedBy: CharacterSet.decimalDigits.inverted).joined()
let mask = "(XXX) XXX-XXXX"
var result = ""
var index = cleanPhoneNumber.startIndex
for ch in mask where index < cleanPhoneNumber.endIndex {
if ch == "X" {
result.append(cleanPhoneNumber[index])
index = cleanPhoneNumber.index(after: index)
@dfrobison
dfrobison / openx
Created April 17, 2020 20:39
[openx] zsh script to open xcode workspace or project file
openx() {
fileToOpen='';
for file in `find . -maxdepth 1 -name *.xcworkspace`; do
fileToOpen=$file
done
if [ -n "$fileToOpen" ]
then
echo $fileToOpen
open $fileToOpen
@dfrobison
dfrobison / SimpleDependencyManager.swift
Last active April 14, 2020 14:12
[Simple dependency manager] A simple dependency manager that cleans itself up completely because of using NSMapTable (https://swiftrocks.com/weak-dictionary-values-in-swift)
import Foundation
final class DependencyStore {
var factories = [String: () -> Dependency]()
var cachedDependencies = NSMapTable<NSString, AnyObject>.init(
keyOptions: .copyIn,
valueOptions: .weakMemory
)
extension CGSize {
public static func + (lhs: CGSize, rhs: CGSize) -> CGSize {
CGSize(width: lhs.width+rhs.width,
height: lhs.height+rhs.height)
}
public static func += (lhs: inout CGSize, rhs: CGSize) {
lhs.width += rhs.width
lhs.height += rhs.height
}
//
// RelativeOffsetDemo.swift
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
@dfrobison
dfrobison / DraggingCircle.swift
Created February 11, 2020 04:01
[Dragging Circle]
import SwiftUI
struct ContentView: View {
// 1.
@State private var currentPosition: CGSize = .zero
@State private var newPosition: CGSize = .zero
var body: some View {
// 2.
Circle()
import SwiftUI
struct DayView: View {
@GestureState var dragState = DragState.inactive
@State var viewDragState = CGSize(width: 0, height: 120)
var translationOffset: CGSize {
return CGSize(width: 0, height: viewDragState.height + dragState.translation.height)
}
@dfrobison
dfrobison / DragView.swift
Created February 11, 2020 03:43
[How To Drag A SwiftUI View Along A Specific Path] from https://github.com/kieranb662/SwiftUI_Drag_View_Along_Path
import SwiftUI
import simd
/// Data structure used to store CGPath commands for easier manipulation of individual components
struct PathCommand {
let type: CGPathElementType
let point: CGPoint
let controlPoints: [CGPoint]
}
@dfrobison
dfrobison / DismissingKeyboard.swift
Created February 8, 2020 21:44
[Dismissing the keyboard] One way to dismiss the keyboard given that the tapGesture is not overwritten somewhere else
struct DismissingKeyboard: ViewModifier {
func body(content: Content) -> some View {
content
.onTapGesture {
let keyWindow = UIApplication.shared.connectedScenes
.filter({$0.activationState == .foregroundActive})
.map({$0 as? UIWindowScene})
.compactMap({$0})
.first?.windows
.filter({$0.isKeyWindow}).first