Skip to content

Instantly share code, notes, and snippets.

View pietrobasso's full-sized avatar

Pietro Basso pietrobasso

View GitHub Profile
@pietrobasso
pietrobasso / Models.swift
Created January 11, 2022 20:49
Widget models
import Foundation
struct HeaderWidget: Decodable {
let title: String
let subtitle: String
}
struct CardWidget: Decodable {
let title: String
let image: URL
@pietrobasso
pietrobasso / Throwable.playground
Last active January 17, 2022 14:49
Throwable example usage!
import Foundation
// MARK: - Throwable
struct Throwable<T: Decodable>: Decodable {
private let result: Result<T, Error>
init(from decoder: Decoder) throws {
do {
result = .success(try T(from: decoder))
} catch let error {
@pietrobasso
pietrobasso / response_v2.json
Created January 11, 2022 13:23
Response v2
{
"items": [
{
"type": "HeaderWidget",
"title": "Hello world",
"subtitle": "Lorem ipsum"
},
{
"type": "ChartWidget",
"title": "Your expenses path through the month",
@pietrobasso
pietrobasso / ThrowableResponse.swift
Last active January 11, 2022 13:10
Throwable Response
struct Response {
let items: [Widget]
}
extension Response: Decodable {
enum CodingKeys: CodingKey {
case items
}
init(from decoder: Decoder) throws {
@pietrobasso
pietrobasso / ClassicResponse.swift
Created January 11, 2022 13:07
Classic Response
struct Response: Decodable {
let items: [Widget]
}
@pietrobasso
pietrobasso / Widget.swift
Last active January 17, 2022 14:49
Widget model
enum Widget {
case headerWidget(HeaderWidget)
case cardWidget(CardWidget)
case buttonWidget(ButtonWidget)
}
extension Widget: Decodable {
enum `Type`: String, Decodable {
case headerWidget = "HeaderWidget"
case cardWidget = "CardWidget"
@pietrobasso
pietrobasso / response.json
Created January 11, 2022 12:40
List of non-homogeneous JSON objects
{
"items": [
{
"type": "HeaderWidget",
"title": "Hello world",
"subtitle": "Lorem ipsum"
},
{
"type": "CardWidget",
"title": "Your top visits",
@pietrobasso
pietrobasso / Throwable.swift
Last active January 11, 2022 12:53
Throwable
struct Throwable<T: Decodable>: Decodable {
private let result: Result<T, Error>
init(from decoder: Decoder) throws {
do {
result = .success(try T(from: decoder))
} catch let error {
// additionally, logError(error)
result = .failure(error)
}
@pietrobasso
pietrobasso / SnapshotTestingDSL.swift
Created September 30, 2019 05:48
SnapshotTesting DSL
import UIKit
import XCTest
import SnapshotTesting
public protocol SnapshottableView {
var snapshotView: UIView { get }
}
extension UIView: SnapshottableView {
public var snapshotView: UIView {
@pietrobasso
pietrobasso / NotificationCenter+AddObserver.swift
Last active August 23, 2018 07:13
Wrapper around the Notification User Info Keys used to get values from the user information dictionary of keyboard notifications. - UIKonf 2017 Conference https://www.youtube.com/watch?v=GzZO5VB1xUg
//
// NotificationCenter+AddObserver.swift
//
// Created by Pietro Basso on 27/02/2018.
//
import Foundation
import UIKit
/**