Skip to content

Instantly share code, notes, and snippets.

@hlung
hlung / Privacy Policy - No data collected.txt
Last active January 27, 2025 16:05
Privacy Policy - No data collected
No personal data shared with us will be given to any third party, under any circumstances. Your data will also never be used by us for any purpose without specific permission.
The app engages in no ad targeting, data mining, or other activities that may compromise your privacy, and we do not affiliate ourselves with any third parties that do so.
@hlung
hlung / CIFilter-colorAbsoluteDifference-demo.swift
Created July 21, 2023 11:22
Demonstrate how to use CIFilter.colorAbsoluteDifference() on macOS
import Cocoa
import CoreImage
import CoreImage.CIFilterBuiltins
let nextButtonImage = NSImage(named: "next button")!
let startGameButtonImage = NSImage(named: "start game button")!
computeImageDifference(inputImage: nextButtonImage, inputImage2: startGameButtonImage)
// MARK: -
@hlung
hlung / RecursiveLazyDependency.swift
Created September 25, 2022 15:15
for medium blog
import UIKit
class MyViewController: UIViewController {
var name: String = "" {
didSet {
print("name = \(name)")
testLabel.text = name // 2
}
}
// Put this in a UITableViewCell or UICollectionViewCell subclass.
// Returns nil if point is not within the specified subviews.
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let subviews = [imageView, titleLabel, detailLabel, downloadButton]
for view in subviews {
if view.convert(view.bounds, to: self).contains(point) {
return super.hitTest(point, with: event)
}
}
return nil
let layout = UICollectionViewCompositionalLayout(sectionProvider: {
(sectionIndex: Int, layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(30))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(30))
let layout = UICollectionViewCompositionalLayout(sectionProvider: {
(sectionIndex: Int, layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .fractionalHeight(1.0))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
// (1) setting widthDimension to half of collectionView size
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.5),
heightDimension: .absolute(50))
@hlung
hlung / APIClient.swift
Last active February 21, 2022 01:56
APIClient startDecodableRequest
import Foundation
class APIClient {
// 1 - Error enum
enum APIClientError: Error {
case dataTaskFailed(Error)
case noHTTPURLResponse
case badHTTPStatusCode(HTTPURLResponse)
case noData
@hlung
hlung / LowestCommonAncestor.swift
Last active October 25, 2020 10:23
Find the least common ancestor of 2 views.
import Foundation
import UIKit
// From an example view hierarchy...
// 1 -> 2 -> 3 -> 4 -> 5
// |
// ---> 6
// Find the least common ancestor of 2 views.
// Input
@hlung
hlung / chown-applications.sh
Last active July 23, 2020 02:48
Change ownership of all apps in Applications folder to current user
find /Applications -name "*.app" -user old-user -maxdepth 1 | tr \\n \\0 | xargs -0 sudo chown -R $USER
@hlung
hlung / DynamicCodingKey.swift
Last active June 27, 2020 06:39
Decodes multiple layers of nested containers using a String array for key path. (It's actually cleaner to declare another struct for the nested data. But this is to show how this can be done.)
import Foundation
// Allows defining CodingKey from String
struct DynamicCodingKey: CodingKey {
var intValue: Int?
var stringValue: String
init?(intValue: Int) {
assertionFailure("Not implemented")
return nil