NewsBlur/clients/ios/Classes/SwiftUIUtilities.swift
David Sinclair 893c57d2aa #1720 (Grid view)
- Cards now include the feed bar, favicon, and name.
- Cards now include unread, saved, and shared indicators.
- Previews now support the none, left/right, and small/large options.
- Several layout tweaks.
- The Grid layout now supports the columns and length settings.
- The Grid layout now includes a bar above the story to close it.
- Tidied the code a bit.
2023-02-02 21:41:10 -06:00

56 lines
1.3 KiB
Swift

//
// SwiftUIUtilities.swift
// NewsBlur
//
// Created by David Sinclair on 2023-02-01.
// Copyright © 2023 NewsBlur. All rights reserved.
//
import SwiftUI
/// Some useful SwiftUI extensions.
extension View {
@ViewBuilder
func `if`<Content: View>(_ condition: Bool, transform: (Self) -> Content) -> some View {
if condition {
transform(self)
} else {
self
}
}
}
extension View {
@ViewBuilder
func modify<Content: View>(@ViewBuilder _ transform: (Self) -> Content?) -> some View {
if let view = transform(self), !(view is EmptyView) {
view
} else {
self
}
}
}
extension View {
func cornerRadius(_ radius: CGFloat, corners: UIRectCorner) -> some View {
clipShape(RoundedCorner(radius: radius, corners: corners))
}
}
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)
}
}
extension Color {
static var random: Color {
return Color(red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1)
)
}
}