Skip to content

Instantly share code, notes, and snippets.

View gopalkrishnareddy's full-sized avatar

Gopal Krishna Reddy Thotli gopalkrishnareddy

  • Harman India
  • Bangalore
View GitHub Profile
@Amzd
Amzd / PreferenceUIHostingController.swift
Last active September 8, 2023 12:14
PreferenceUIHostingController. Adds hiding home indicator and deferring system edge gestures to SwiftUI. (Don't work at the same time but I think that's normal?)
extension View {
/// Controls the application's preferred home indicator auto-hiding when this view is shown.
func prefersHomeIndicatorAutoHidden(_ value: Bool) -> some View {
preference(key: PreferenceUIHostingController.PrefersHomeIndicatorAutoHiddenPreferenceKey.self, value: value)
}
/// Controls the application's preferred screen edges deferring system gestures when this view is shown. Default is UIRectEdgeNone.
func edgesDeferringSystemGestures(_ edge: UIRectEdge) -> some View {
preference(key: PreferenceUIHostingController.PreferredScreenEdgesDeferringSystemGesturesPreferenceKey.self, value: edge)
}
@ollieatkinson
ollieatkinson / Restorable.swift
Created June 16, 2020 08:43
Restorable - Undo/Redo management of values using Swift 5.1 property wrappers
@propertyWrapper
public struct Restorable<Value> {
public var wrappedValue: Value
public init(wrappedValue: Value, using undoManager: UndoManager = .init()) {
self.wrappedValue = wrappedValue
self.projectedValue = undoManager
}
@EnesKaraosman
EnesKaraosman / RandomColor.swift
Last active July 14, 2023 09:35
Generatin random color in SwiftUI & UIKit
#if canImport(UIKit)
import UIKit
extension UIColor {
static var random: UIColor {
return UIColor(
red: .random(in: 0...1),
green: .random(in: 0...1),
blue: .random(in: 0...1)
)
@Amzd
Amzd / DateRounding.swift
Created June 2, 2021 08:58 — forked from casperzandbergenyaacomm/DateRounding.swift
Rounding of date and time
import Foundation
extension Date {
/// Returns date where **-component** is rounded to its closest
/// multiple of **-amount**. Warning: month and day start at 1
/// so round(to: 6, .month) will either return month 1 or 7!
func round(to amount: Int, _ component: Calendar.Component) -> Date {
let cal = Calendar.current
var value = cal.component(component, from: self)
@Amzd
Amzd / AppendingClosures.swift
Last active May 16, 2023 19:50
Appending Closures, eg a shorthand to create one new closure that calls two closures.
// MARK: Appending closures `a + b`
/// Append closures, lhs first.
public func + <I>(lhs: @escaping (I) -> Void, rhs: @escaping (I) -> Void) -> ((I) -> Void) {
return {
lhs($0)
rhs($0)
}
}
@Amzd
Amzd / HierarchyDescription.swift
Last active May 16, 2023 19:48
Description that can be used with any type that has recursive children
public protocol HierarchyDescription {
associatedtype Child: HierarchyDescription
var children: [Child] { get }
var description: String { get }
}
extension HierarchyDescription {
public var hierarchyDescription: String {
var description = self.description
for child in children {
@ohayon
ohayon / DraggableView.swift
Last active April 5, 2023 16:24
Example of making a reusable `draggable()` modifier for SwiftUI Views
struct DraggablePita: View {
var body: some View {
Image(uiImage: UIImage(named: "pita.png")!)
.draggable() // Add the new, custom modifier to make this draggable
}
}
// Handle dragging
struct DraggableView: ViewModifier {
@State var offset = CGPoint(x: 0, y: 0)
@yannxou
yannxou / ForegroundTextColor.swift
Created December 23, 2020 09:55
Foreground text color based on background color #SwiftUI
// Taken from Apple's App Dev Training: https://developer.apple.com/tutorials/app-dev-training/
/// This color is either black or white, whichever is more accessible when viewed against the scrum color.
var accessibleFontColor: Color {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
UIColor(self).getRed(&red, green: &green, blue: &blue, alpha: nil)
return isLightColor(red: red, green: green, blue: blue) ? .black : .white
}
@ollieatkinson
ollieatkinson / AttributesOf.swift
Last active March 10, 2023 17:41
Composition of attributes and values for types in Swift
struct AttributesOf<Object>: CustomStringConvertible {
enum Error: Swift.Error { case message(String) }
public typealias Assignment = (keyPath: AnyKeyPath, set: (Object) -> Void)
private(set) var assignments: [Assignment]
init(@AttributesOfBuilder<Object> _ builder: () throws -> [Assignment]) rethrows {
assignments = try builder()
@chriseidhof
chriseidhof / script.swift
Last active February 21, 2023 05:12
SwiftUI
import SwiftSyntax
import SwiftSemantics
import Foundation
let source = try! String(contentsOf: URL(fileURLWithPath: "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk/System/Library/Frameworks/SwiftUI.framework/Modules/SwiftUI.swiftmodule/arm64e.swiftinterface"))
var collector = DeclarationCollector()
let tree = try SyntaxParser.parse(source: source)
tree.walk(&collector)