Skip to content

Instantly share code, notes, and snippets.

View amirdew's full-sized avatar
⚒️
Building something new

Amir Khorsandi amirdew

⚒️
Building something new
View GitHub Profile
@amirdew
amirdew / View+MinimumPadding.swift
Created December 22, 2022 13:20
Minimum paddings for SwiftUI views
extension View {
func minimumPadding(edges: Edge.Set = .all, _ length: CGFloat = 8) -> some View {
GeometryReader { geo in
padding(.bottom, edges.contains(.bottom) ? max(length, geo.safeAreaInsets.bottom) : 0)
.padding(.top, edges.contains(.top) ? max(length, geo.safeAreaInsets.top) : 0)
.padding(.leading, edges.contains(.leading) ? max(length, geo.safeAreaInsets.leading) : 0)
.padding(.trailing, edges.contains(.trailing) ? max(length, geo.safeAreaInsets.trailing) : 0)
.ignoresSafeArea(edges: edges)
}
}
@amirdew
amirdew / ModifyCodable.swift
Last active May 29, 2024 06:05
Modifying private and immutable properties (let) in Codable instances
import Foundation
extension Decodable where Self: Encodable {
/// Creates a new instance and changes the value for the provided key.
///
/// - Parameters:
/// - key: The key path to the property that you want to modify.
/// Use period to separate levels and [] for indexes.
/// Examples: "id", "name.firstName", "children[2].name.firstName"
///
@amirdew
amirdew / View+Previews.swift
Created August 21, 2020 09:48
An extention for making previews easier
// How to use:
struct YourView_Previews: PreviewProvider {
static var previews: some View {
YourView()
.makePreviews()
}
}
extension Optional {
func `let`<T>(_ transform: (Wrapped) -> T?) -> T? {
if case .some(let value) = self {
return transform(value)
}
return nil
}
}
import RxSwift
extension Observable where Element: OptionalType {
func ignoreNil() -> Observable<Element.T> {
filter { $0.optional != nil }.map { $0.optional! }
}
}