Skip to content

Instantly share code, notes, and snippets.

View ethanhuang13's full-sized avatar

Ethan Huang ethanhuang13

View GitHub Profile
@ethanhuang13
ethanhuang13 / FacebookAuth.swift
Last active March 28, 2024 08:24
FacebookAuth is for iOS app developers who need to support Facebook login but don't want to use the official SDK
//
// FacebookAuth.swift
// GitHub: ethanhuang13
// Twitter: @ethanhuang13
import AuthenticationServices
import SafariServices
/*
Updated:
@ethanhuang13
ethanhuang13 / VirtualKeyboard.swift
Last active February 7, 2024 05:58
MacBook Air M1 Zhuyin Keyboard written with SwiftUI ~=300 LOC, < 4hrs
//
// VirtualKeyboard.swift
// MacBook Air M1 Zhuyin Keyboard
// Written with SwiftUI ~=300 LOC, < 4hrs
// Created by Ethan Huang on 2021/1/13.
// Twitter: @ethanhuang13
import SwiftUI
struct VirtualKeyboard: View {
// iOS 17's JSONEncoder is rarely producing a different key sorting order(< 5%), which is ok for JSON itself, but will cause flaky tests.
// It wasn't happened pre-iOS 17.
// https://mastodon.social/@ethanhuang13/110904915670262949
import XCTest
final class JSONEncoderTests: XCTestCase {
func testEncode() {
struct Model: Codable {
var a: String = ""
@ViewBuilder
private func build<#name#>(_ viewStore: ViewStoreType) -> some View {
}
@ethanhuang13
ethanhuang13 / xcode-beta-slink.sh
Last active February 3, 2023 20:21
Symbolic link from Xcode.app to Xcode-beta.app (Xcode 11.2)
@ethanhuang13
ethanhuang13 / UIView+isPossiblyVisible.swift
Last active December 20, 2022 06:00
[WIP] UIView to check is visible on screen. Not perfect solution
extension UIView {
func isPossiblyVisible() -> Bool {
guard isHidden == false,
alpha > 0,
bounds != .zero,
let window = window, // In a window's view hierarchy
window.isKeyWindow, // Does not consider cases covered by another transparent window
window.hitTest(convert(center, to: nil), with: nil) != self
else { return false }
@ethanhuang13
ethanhuang13 / ErrorAlerts.swift
Created June 4, 2022 07:07
SwiftUI .alert(item:) for multiple types of Error
//
// ContentView.swift
// ErrorAlert
//
// Created by Ethan Huang on 2022/6/4.
//
import SwiftUI
enum AError: Error {
@ethanhuang13
ethanhuang13 / detect-string-language.swift
Created March 23, 2017 03:46
Detect string language with Swift
//: Playground - noun: a place where people can play
//
// The result is not guaranteed to be accurate. Typically, the function requires 200-400 characters to reliably guess the language of a string.
// Reference: [CFStringTokenizerCopyBestStringLanguage(_:_:)](https://developer.apple.com/reference/corefoundation/1542136-cfstringtokenizercopybeststringl)
//
import Foundation
extension String {
func guessLanguage() -> String {
// A challenge from https://twitter.com/hanyu_chen_ios/status/1395533041989079045
// Screenshots of performance tests https://twitter.com/ethanhuang13/status/1395577548151541760?s=21
import XCTest
let maxValue = 100
let minValue = 0
final class ClampTests: XCTestCase {
func testMeasureNaiveCompare() throws {
@ethanhuang13
ethanhuang13 / HexColor.swift
Created January 2, 2019 06:44
@dynamicMemberLookup example
// An (useless) example of Swift 4.2 @dynamicMemberLookup attributes.
// let color1 = HexColor().cbcbcb
// let color2 = HexColor().000000 // This can also be built. Will be treat as "000000"
import Foundation
@dynamicMemberLookup
struct HexColor {
subscript(dynamicMember member: String) -> UIColor? {
do {