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
@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()
@Czajnikowski
Czajnikowski / Example.swift
Last active September 23, 2021 13:48
Solution for propagating changes from nested ObservableObjects
class TrackViewModel {
private let nestedViewModel: PlaybackViewModel
private var propagation: Any?
init(nestedViewModel: PlaybackViewModel) {
self.nestedViewModel = nestedViewModel
propagation = nestedViewModel.propagateWeakly(to: self)
}
@markmals
markmals / Wiggle.swift
Last active February 10, 2024 03:44
The iOS Home Screen wiggle animation, in SwiftUI
import SwiftUI
extension View {
func wiggling() -> some View {
modifier(WiggleModifier())
}
}
struct WiggleModifier: ViewModifier {
@State private var isWiggling = false
@importRyan
importRyan / whenHovered.md
Last active April 6, 2024 06:54
Reliable SwiftUI mouse hover

Reliable mouseEnter/Exit for SwiftUI

Kapture 2021-03-01 at 14 43 39

On Mac, SwiftUI's .onHover closure is not always called on mouse exit, particularly with high cursor velocity. A grid of targets or with finer target shapes will often have multiple targets falsely active after the mouse has moved on.

It is easy to run back to AppKit's safety. Below is a SwiftUI-like modifier for reliable mouse-tracking. You can easily adapt it for other mouse tracking needs.

import SwiftUI
@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
}
@hishnash
hishnash / ExampleWindowReaderApp.swift
Created December 21, 2020 05:59
Access to the underlying UIWindow & NSWindow in swiftUI gives access to window methods and attributes.
//
// ExampleWindowReaderApp.swift
// Shared
//
// Created by Matthaus Woolard on 21/12/2020.
//
import SwiftUI
@main
@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 {
@mattadatta
mattadatta / CollectionView.swift
Last active January 8, 2024 23:18
UIKit's UICollectionView implemented in SwiftUI using UIViewControllerRepresentable
//
// (See usage below implementation)
//
// SwiftUI `CollectionView` type implemented with UIKit's UICollectionView under the hood.
// Requires `UIViewControllerRepresentable` over `UIViewRepresentable` as the type that allows
// for SwiftUI `View`s to be added as subviews of UIKit `UIView`s at all bridges this gap as
// the `UIHostingController`.
//
// Not battle-tested yet, but seems to be working well so far.
// Expect changes.
@available (iOS 14.0, *)
public struct MenuPicker<T, V: View>: View {
@Binding var selected: Int
var array: [T]
var title: String?
let mapping: (T) -> V
public init(selected: Binding<Int>, array: [T], title: String? = nil,
mapping: @escaping (T) -> V) {
@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
}