Open5

SwiftUI

haseyuyhaseyuy

addCurve(to:controlPoint1:controlPoint2:)

Appends a cubic Bézier curve to the path.

func addCurve(to endPoint: CGPoint, 
controlPoint1: CGPoint, 
controlPoint2: CGPoint)
haseyuyhaseyuy

Extension for adding rounded corners in specific corners

// Extension for adding rounded corners in specific corners
extension View {
    func cornerRadisu(_ radius: CGFloat, corners: UIRectCorner) -> some View {
        clipShape(RoundedCorner(radius: radius, corners: corners))
    }
}

// Custom RoundedCorner shape used for cornerRadius extension above
struct RoundedCorner: Shape {
    var radius: CGFloat = .infinity
    var corners: UIRectCorner = .allCorners

    func path(in rect: CGRect) -> Path {
        let path = UIBezierPath(roundedRect: rect, byRoundingCorners:  corners, cornerRadii: CGSize(width: radius, height: radius))
        return Path(path.cgPath)
    }
}
haseyuyhaseyuy

ScrollViewReader

The scroll view reader’s content view builder receives a ScrollViewProxy instance; you use the proxy’s scrollTo(_:anchor:) to perform scrolling.

https://developer.apple.com/documentation/swiftui/scrollviewreader

@Namespace var topID
@Namespace var bottomID

var body: some View {
    ScrollViewReader { proxy in
        ScrollView {
            Button("Scroll to Bottom") {
                withAnimation {
                    proxy.scrollTo(bottomID)
                }
            }
            .id(topID)

            VStack(spacing: 0) {
                ForEach(0..<100) { i in
                    color(fraction: Double(i) / 100)
                        .frame(height: 32)
                }
            }

            Button("Top") {
                withAnimation {
                    proxy.scrollTo(topID)
                }
            }
            .id(bottomID)
        }
    }
}

func color(fraction: Double) -> Color {
    Color(red: fraction, green: 1 - fraction, blue: 0.5)
}
haseyuyhaseyuy

Codable

let menuList = documents.compactMap { doc in

    let result = Result {
        try doc.data(as: Menu.self)
    }

    switch result {
    case .success(let _menu):
        if let menu = _menu {
            return menu
        } else {
            print("Document does not exist")
            return nil
        }
    case .failure(let error):
        print("Error decoding menu: \(error.localizedDescription)")
        return nil
    }
}