Skip to content

Instantly share code, notes, and snippets.

extension NSNotification.Name {
public static let deviceDidShakeNotification = NSNotification.Name("MyDeviceDidShakeNotification")
}
extension UIWindow {
open override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
super.motionEnded(motion, with: event)
NotificationCenter.default.post(name: .deviceDidShakeNotification, object: event)
}
}
extension View {
import SwiftUI
extension CGPoint {
static func *(lhs: Self, rhs: CGFloat) -> Self {
.init(x: lhs.x * rhs, y: lhs.y * rhs)
}
}
// Idea: https://www.framer.com/showcase/project/lo2Qka8jtPXrjzZaPZdB/
enum AsyncError: Error {
case finishedWithoutValue
}
extension AnyPublisher {
func async() async throws -> Output {
try await withCheckedThrowingContinuation { continuation in
var cancellable: AnyCancellable?
var finishedWithoutValue = true
cancellable = first()
//
// UDPListener.swift
//
// Created by Michael Robert Ellis on 12/16/21.
//
import Foundation
import Network
import Combine
// Author: The SwiftUI-Lab
// This code is part of the tutorial: https://swiftui-lab.com/swiftui-animations-part4/
import SwiftUI
// Sample usage
struct ContentView: View {
var body: some View {
VStack {
GifImage(url: URL(string: "https://media.giphy.com/media/YAlhwn67KT76E/giphy.gif?cid=790b7611b26260b2ad23535a70e343e67443ff80ef623844&rid=giphy.gif&ct=g")!)
.padding(10)
@amalmurali47
amalmurali47 / edit_commit_history.md
Last active April 30, 2024 09:50
Change ownership of selected older commits in Git
  1. Clone the repo.
  2. Use git rebase -i --root
  3. vim will open. Select the commits you want to modify by changing pick to edit. If you would like to change all the commits, perform the following replace: :%s/^pick/edit/g. This command changes all instances of "pick" at the start of lines to "edit".
  4. You will now be shown all the selected commits one by one. Each commit message will be displayed. You have two options:
    • If you would like to keep the commit author details the same, do a git rebase --continue.
    • If you would like to change it to a different name/email, do git commit --amend --reset-author. If --reset-author is specified, it will use the details from your git config. (If you need to specify an alternate name/email, you can do so with --author="John Doe <john@example.com>". If you would like to change the time to a previous date, you can do so with --date "2 days ago".)
  5. Do the same for all the commits and finish the rebase.
  6. Perform git push -f origin master to
@IanKeen
IanKeen / AnyPublisher+Extension.swift
Created March 17, 2021 17:02
Extension to create an AnyPublisher to easily 'lift' async code into Combine
extension AnyPublisher where Failure: Error {
struct Subscriber {
fileprivate let send: (Output) -> Void
fileprivate let complete: (Subscribers.Completion<Failure>) -> Void
func send(_ value: Output) { self.send(value) }
func send(completion: Subscribers.Completion<Failure>) { self.complete(completion) }
}
init(_ closure: (Subscriber) -> AnyCancellable) {
@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
@izakpavel
izakpavel / KaleidoscopeView.swift
Created January 21, 2021 05:33
An animation with a kaleidoscope effect
import SwiftUI
struct HalfShape: Shape {
let left: Bool
func path(in rect: CGRect) -> Path {
return Path { path in
let width = rect.width
let height = rect.height
@sharplet
sharplet / Always.swift
Last active February 15, 2024 18:50
A Combine publisher that repeatedly emits the same value as long as there is demand
// Copyright (c) 2019–20 Adam Sharp and thoughtbot, inc.
//
// 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 above copyright notice and this permission notice shall be included in