Skip to content

Instantly share code, notes, and snippets.

View Codelaby's full-sized avatar

Codelaby Codelaby

View GitHub Profile
//
// CoverFlowCarousel.swift
// ClockSample
//
// Created by Codelaby on 25/7/24.
//
import SwiftUI
//RandomAccessCollection & MutableCollection & RangeReplaceableCollection & Equatable & Hashable
struct CoverFlowCarousel<Content: View, Data: RandomAccessCollection>: View where Data.Element: Identifiable {
import SwiftUI
//RandomAccessCollection & MutableCollection & RangeReplaceableCollection & Equatable & Hashable
struct CoverFlowCarousel<Content: View, Data: RandomAccessCollection>: View where Data.Element: Identifiable {
@Environment(\.layoutDirection) var direction
private var config: Config
private let data: Data
@Binding var selection: Data.Element.ID?
private let content: (Data.Element) -> Content
@Codelaby
Codelaby / BinaryClock.swift
Last active May 4, 2024 09:13
Binary Clock System in Swift
import Foundation
struct BinaryClock {
private(set) var hours24: [Int] = Array(repeating: 0, count: 6)
private(set) var hours12: [Int] = Array(repeating: 0, count: 4)
private(set) var minutes: [Int] = Array(repeating: 0, count: 6)
private(set) var seconds: [Int] = Array(repeating: 0, count: 6)
mutating func update(withSeconds seconds: TimeInterval) {
let totalSeconds = Int(seconds)
@Codelaby
Codelaby / validate_fields.swift
Last active May 4, 2024 09:04
validate_field.swift
//https://betterprogramming.pub/a-data-validation-solution-utilizing-swift-property-wrappers-and-swiftui-view-extensions-ae2db2209a32
import SwiftUI
public protocol ValidationRule {
associatedtype Value: Equatable
associatedtype Failure: Error
typealias ValidationResult = Result<Value, Failure>
init()
@Codelaby
Codelaby / inspector.swift
Created January 25, 2024 10:27
Inspector SwiftUI
import SwiftUI
//https://dimillian.medium.com/how-to-use-the-new-inspector-swiftui-view-modifier-9cefb8353beb
struct InspectorPanel: View {
@State var isShowingInspector = false
var body: some View {
NavigationStack {
VStack {
@Codelaby
Codelaby / Grouped_dates.swift
Last active December 18, 2023 18:37
Grouped dates
import Foundation
// Array de fechas como strings
let dateStrings = [
"2023-01-01 08:00:00",
"2023-01-01 10:30:00",
"2023-01-01 12:45:00",
"2023-01-01 08:00:00",
"2023-01-01 18:00:00",
"2023-01-01 10:30:00",
@Codelaby
Codelaby / Demo_LicenseAgree.swift
Created December 14, 2023 20:03
Demo_LicenseAgree
import SwiftUI
struct ViewOffsetKey: PreferenceKey {
typealias Value = CGFloat
static var defaultValue = CGFloat.zero
static func reduce(value: inout Value, nextValue: () -> Value) {
value += nextValue()
}
}
@Codelaby
Codelaby / MetaphoneSpanish.swift
Created November 22, 2023 15:38
Metaphone Spanish
import Foundation
class MetaphoneSpanish {
private class func stringAt(_ string: String, _ start: Int, _ length: Int, _ list: [String]) -> Bool {
if start < 0 || start >= string.count {
return false
}
let substring = string[string.index(string.startIndex, offsetBy: start)..<string.index(string.startIndex, offsetBy: start+length)]
@Codelaby
Codelaby / RomanConverter.swift
Last active October 16, 2023 15:52
Roman number conversor
import Foundation
class RomanConverter {
func romanToInt(_ s: String) -> Int {
let romanValues: [Character: Int] = [
"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000
]
@Codelaby
Codelaby / applebuyguide.swift
Last active October 30, 2023 10:42
Apple buy guide
import Foundation
extension Calendar {
func numberOfDaysBetween(_ from: Date, and to: Date) -> Int {
let fromDate = startOfDay(for: from) // <1>
let toDate = startOfDay(for: to) // <2>
let numberOfDays = dateComponents([.day], from: fromDate, to: toDate) // <3>
return numberOfDays.day!
}