Skip to content

Instantly share code, notes, and snippets.

View thecoolwinter's full-sized avatar

Khan Winter thecoolwinter

View GitHub Profile
func isUpdateAvailable(completion: @escaping (Bool?, Error?) -> Void) throws -> URLSessionDataTask {
print("\(String(describing: type(of: self))) \(#function)", level: .info)
guard let info = Bundle.main.infoDictionary,
let currentVersion = info["CFBundleShortVersionString"] as? String,
let identifier = info["CFBundleIdentifier"] as? String,
let url = URL(string: "http://itunes.apple.com/lookup?bundleId=\(identifier)") else {
throw VersionError.invalidBundleInfo
}
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
do {
/**
* Check a password in the [haveibeenpwned](https://haveibeenpwned.com/Passwords) database.
*
* Uses a secure mechanism for sending passwords according to the API spec. First, the password is hashed using the
* SHA-1 secure hash algorithm. Then, the first 5 characters are sent to the API. The API returns a list of possible
* matches and the calculated hash is used to check if the password exists in the returned list.
*
* @async
* @author Khan Winter (https://github.com/thecoolwinter)
* @license MIT
@thecoolwinter
thecoolwinter / NSLabel.swift
Last active June 4, 2024 15:07
Quick dirty NSLabel class for drawing text.
//
// NSLabel.swift
//
//
// Created by Khan Winter on 4/8/22.
//
import AppKit
class NSLabel: NSView {
@thecoolwinter
thecoolwinter / DelayedTask.swift
Last active May 23, 2022 16:21
DelayedTask.swift
/// The `DelayedTask` waits to execute an action, and will only call it's `onEnd` completion if the
/// `action` has been performed.
///
/// This allows for situations where you may `await` for a cache item that could take a small amount of time,
/// but it could also require a network call. An example of such a situation is below:
/// ```swift
/// Task {
/// self.showLoadingIndicator()
/// let data = await ResourceManager.getData()
/// self.removeLoadingIndicator()
@thecoolwinter
thecoolwinter / SchemaBuilder+addTimestamps.swift
Created June 23, 2021 23:02
A small extension for easily adding timestamps to models with one line in a migration. You can customize the fieldnames as well. By default adds `createdAt`, `updatedAt` and `deletedAt`.
import Fluent
extension SchemaBuilder {
enum TimestampTypes: Equatable {
case createdAt(_ fieldName: String = "createdAt")
case updatedAt(_ fieldName: String = "updatedAt")
case deletedAt(_ fieldName: String = "deletedAt")
}
@thecoolwinter
thecoolwinter / hide_desktop.sh
Created May 20, 2021 18:35
Quickly toggle your desktop icons with a simple script. Can also be used in an Automator script as an application.
#!/usr/bin/env zsh
previous_val=$(defaults read com.apple.finder CreateDesktop)
if [ "$previous_val" = "true" ]
then
previous_val=$(echo 'false');
else
previous_val=$(echo 'true')
fi
import UIKit
final class ContentSizedTableView: UITableView {
override var contentSize:CGSize {
didSet {
invalidateIntrinsicContentSize()
}
}
@thecoolwinter
thecoolwinter / ExtraCodable.swift
Created March 16, 2021 21:28
Helper methods for working with Codable objects and JSON Data and dictionaries. Provides two initializers and two methods for creating a Codable object using a dictionary or a Data object. And two methods for retreiving a dictionary or Data from the Codable instance.
//
// ExtraCodable.swift
//
// Created by Khan Winter on 3/16/21.
// Copyright © 2021 WindChillMedia. All rights reserved.
//
// 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
@thecoolwinter
thecoolwinter / PercentageField.swift
Last active February 10, 2022 18:28
UITextField that automatically formats a percentage as a user types. It limits the input, and can return a value without any string formatting.
//
// PercentageField.swift
//
// Created by Khan Winter on 8/16/20.
//
import UIKit
class PercentageField: UITextField {
import Foundation
public class CodableStorage {
fileprivate init() { }
enum Directory {
case documents
case caches
case shared // Use for sharing files between containers. Eg, with an app extension.