Skip to content

Instantly share code, notes, and snippets.

View kk-vv's full-sized avatar
👨‍💻

Felix.Hu kk-vv

👨‍💻
  • Alone
  • ON-CHAIN
View GitHub Profile
@kk-vv
kk-vv / MixinWrapper.swift
Created August 7, 2023 08:25
Swift namespace mixin
public struct MixinWrapper<Base> {
public var base: Base
init(_ base: Base) {
self.base = base
}
}
public protocol MixinWrapperCompatible {
associatedtype MixinWrapperBase
@kk-vv
kk-vv / BiometricsUtils.swift
Created October 12, 2023 08:54
Biometrics
import UIKit
import LocalAuthentication
class BiometricsUtils {
//Should not be singleton as always success after first time checked
//static let shared: BiometricsUtils = .init()
private lazy var context = LAContext()
@kk-vv
kk-vv / JSONDecodable.swift
Last active February 2, 2024 03:26
Extension SwiftyJSON with JSONDecodable
import Foundation
import SwiftyJSON
public protocol JSONDecodable {
static func decode(from json: JSON) throws -> Self
}
extension JSON: JSONDecodable {
@kk-vv
kk-vv / ShitcoinPriceFormat.swift
Last active May 16, 2024 07:18
Shitcoin price format eg. 0.0{6}4518
enum RegularType {
case scientificNotation
case shitcoinPrice
case url
case evmAddress
case btcAddress
case solanaAddress
case zeroAmount
case string(String)
case validAmountInput(_ decimals: Int)
@kk-vv
kk-vv / ValidCurrencyInput.swift
Last active November 3, 2023 15:44
Valid currency input
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if string.count > 0 {
if string.count == 1, textField.text.isNilOrEmpty {
if let single = string[0], (single == "." || single == "0") {// auto set . or 0 to 0.
textField.text = "0."
let newPosition = textField.endOfDocument
textField.selectedTextRange = textField.textRange(from: newPosition, to: newPosition)
textField.sendActions(for: .valueChanged)
return false
}
@kk-vv
kk-vv / BigIntToString.swift
Created November 3, 2023 10:48
BigInt to string
import Foundation
import BigInt
extension BigUInt {
func string(decimals: Int) -> String {
"\(self)".bigIntToString(decimals: decimals)
}
}
extension BigInt {
@kk-vv
kk-vv / TaskSyncQueue.swift
Created February 6, 2024 03:45
Convert multi async Tasks to sync queue
func fetchFromRemote(by ids: [MKAssetId]) {
let group = DispatchGroup()
let queue = DispatchQueue(label: "felix.hu.remote.assets")
let semaphore = DispatchSemaphore(value: 1)
for id in ids {
queue.async { // queue.async still sync but new thread, if queue.sync is main thread will cause dead lock
group.enter()
semaphore.wait()
Task {
do {
@kk-vv
kk-vv / Array++.swift
Created February 7, 2024 06:13
Convertto 2d array with raw cout or group by key
extension Array {
func convertTo2DArray(rawCount: Int) -> [[Element]] {
let array = stride(from: 0, to: self.count, by: rawCount).map { (index) -> [Element] in
if (index + rawCount) > self.count {
return Array(self[index...])
} else {
return Array(self[index..<index + rawCount])
}
}