Skip to content

Instantly share code, notes, and snippets.

View griffin-stewie's full-sized avatar

griffin-stewie griffin-stewie

View GitHub Profile
import LinkPresentation
import Combine
extension LPMetadataProvider {
func startFetchingMetadataPublisher(for url: URL) -> AnyPublisher<LPLinkMetadata?, Error> {
Future<LPLinkMetadata?, Error> { completion in
self.startFetchingMetadata(for: url) { meta, error in
guard let error = error else {
return completion(.success(meta))
}
/*
Copyright © 2020 Apple 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 all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR O
import UIKit
class BaseImageView: UIView{
var roundedShape = CAShapeLayer()
var curvedPath: UIBezierPath!
var shapeColor: UIColor!
var circular: Bool!
var shadow: Bool!
@hirohitokato
hirohitokato / SomeViewController.swift
Created April 28, 2020 02:02
iPadでも画面の向きによってAutoLayoutを適用したいときのコード
/*
iPadは縦横どちらの向きでもUIUserInterfaceSizeClassが.regularなので、AutoLayoutのOrientationによる表示変更ができない。
以下のコードを必要なViewControllerに書いておくと`.regular × .compact`と判断するようになりiPhoneと同じルールでレイアウトできる。
*/
// MARK: - Trick for iPad device orientation
override public var traitCollection: UITraitCollection {
let device = UIDevice.current
let result: UITraitCollection
@griffin-stewie
griffin-stewie / date_formatter_with_argumentparser.swift
Created April 7, 2020 13:35
ArgumentParser で Date を扱うときの例
// https://forums.swift.org/t/support-for-date/34797/2?u=griffin-stewie
func parseDate(_ formatter: DateFormatter) -> (String) throws -> Date {
{ arg in
guard let date = formatter.date(from: arg) else {
throw ValidationError("Invalid date")
}
return date
}
}
@griffin-stewie
griffin-stewie / Array+UniqueExtensions.swift
Last active September 2, 2021 14:03
Swift Command Line Tool Helpers
import Foundation
// O(n^2)
public extension Array where Element: Equatable {
/// Remove duplicated elements
/// Complexity: O(n^2), where n is the length of the sequence.
/// - Returns: A new Array containing those elements from self that are not duplicates
func unique() -> Array<Element> {
return reduce(into: []) { (array, r) in
@myobie
myobie / timer.swift
Last active August 21, 2020 04:43
GCD Timer using DispatchSourceTimer
import Cocoa
import Combine
class Timer: Cancellable, Publisher {
enum Error: Swift.Error {
case cancelled
}
typealias Output = Never
typealias Failure = Error
@AliSoftware
AliSoftware / Demo.swift
Last active October 31, 2023 12:25
NestableCodingKey: Nice way to define nested coding keys for properties
struct Contact: Decodable, CustomStringConvertible {
var id: String
@NestedKey
var firstname: String
@NestedKey
var lastname: String
@NestedKey
var address: String
enum CodingKeys: String, NestableCodingKey {
@chockenberry
chockenberry / tot.sh
Last active March 9, 2025 22:28
A shell script for Tot
#!/bin/sh
basename=`basename $0`
if [ -z "$*" ]; then
echo "usage: ${basename} <dot> [ -o | -r | <file> | - ]"
echo ""
echo "options:"
echo " -o open dot in window with keyboard focus"
echo " -r read contents of dot"
@propertyWrapper
class YORO<T> {
private var value: T?
var wrappedValue: T? {
get {
let result = value
value = .none
return result
}
set {