Skip to content

Instantly share code, notes, and snippets.

// Introducing a 'combine' function for applying a value to
// any function or closure:
func combine<A, B>(
_ value: A,
with closure: @escaping (A) -> B
) -> () -> B {
return { closure(value) }
}
// Example
@dfrobison
dfrobison / Result.swift
Created February 4, 2020 03:40
[Result’s convenience APIs] from John Sundell (https://www.swiftbysundell.com/tips/result-type-convenience-apis/)
let data: Data = ...
let decoder = JSONDecoder()
// This will create a Result<Model, Error> instance, which will contain
// 'success' if its expression succeeded, and 'failure' if it ended
// up throwing an error:
let result = Result {
try decoder.decode(Model.self, from: data)
}
@dfrobison
dfrobison / dropDown.swift
Last active February 4, 2020 19:53
[Dropdown menu for SwiftUI] from Kavsoft (https://www.youtube.com/watch?v=CwD4cScGCq8)
struct DropDown: View {
@State var expand = false
var body: some View {
VStack(alignment: .leading, spacing: 18, content: {
HStack {
Text("Expand").fontWeight(.heavy).foregroundColor(.white)
Image(systemName: expand ? "chevron.up" : "chevron.down").resizable().frame(width: 13, height: 6).foregroundColor(.white)
}
.onTapGesture {
@dfrobison
dfrobison / RemoveDividers.swift
Last active February 8, 2020 17:42
[Remove dividers] Current way of removing the dividing lines on List, Form, etc.
struct Container: View {
init() {
// To remove only extra separators below the list:
UITableView.appearance().tableFooterView = UIView()
// To remove all separators including the actual ones:
UITableView.appearance().separatorStyle = .none
}
@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
@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]
}
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 / 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()
//
// 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,
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
}