Skip to content

Instantly share code, notes, and snippets.

View andr3a88's full-sized avatar

Andrea Stevanato andr3a88

View GitHub Profile
@andr3a88
andr3a88 / AppSyncRequestBodyCreator.swift
Last active August 30, 2024 09:59
Apollo iOS with AppSync custom body request: AppSyncRequestBodyCreator
/// Creates an AWS AppSync-compatible RequestBody `payload` for subscriptions.
/// This is necessary due to the different payload used by Apollo
/// Credit to:
/// - https://community.apollographql.com/t/using-apollo-with-appsync-directly-on-ios/324/4
/// - https://docs.aws.amazon.com/appsync/latest/devguide/real-time-websocket-client.html#header-parameter-format-based-on-appsync-api-authorization-mode
/// - https://github.com/lingfengmarskey/NetworkSample
///
/// AppSync payload:
/// {
/// "id": "subscriptionId",
@andr3a88
andr3a88 / AnyEncodable.swift
Created February 27, 2024 17:51
Using array of mixed Encodable types (in Swift)
import UIKit
/// A type eraser to erase the concrete types of your encodable objects
struct AnyEncodable: Encodable {
private let encodeClosure: (Encoder) throws -> Void
init<T: Encodable>(_ value: T) {
encodeClosure = { encoder in
try value.encode(to: encoder)
}
@andr3a88
andr3a88 / two-way-binding.swift
Created August 8, 2023 10:57
SwiftUI: Two way binding with @State and @binding
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
@State private var sliderValue: CGFloat = 0
private var minSliderValue: CGFloat = 10
private var maxSliderValue: CGFloat = 100
@andr3a88
andr3a88 / CountdownTests.swift
Created November 30, 2022 11:58 — forked from JonnyBeeGod/CountdownTests.swift
This code allows for testing UNNotificationCenter and UNNotificationSettings
func testNotifications() {
// map all authorizationStatus with expected Result
let authorizationStatusMap: [UNAuthorizationStatus: Int] = [.authorized: 1, .denied: 0, .notDetermined: 0, .provisional: 1]
UNNotificationSettings.swizzleAuthorizationStatus()
authorizationStatusMap.forEach { (key: UNAuthorizationStatus, value: Int) in
UNNotificationSettings.fakeAuthorizationStatus = key
let mockCenter = UserNotificationCenterMock()
let mockCoder = MockNSCoder()
@andr3a88
andr3a88 / Publisher+isPrimeInteger.swift
Created June 4, 2021 15:09
Combine A custom publisher
import Foundation
import Combine
extension Publisher{
func isPrimeInteger<T: BinaryInteger>() -> Publishers
.CompactMap<Self, T> where Output == T {
compactMap{self.isPrime($0)}
}
@andr3a88
andr3a88 / Urlsession-dataTaskPublisher.swift
Last active June 4, 2021 14:35
API call with combine
import Foundation
import Combine
struct Post: Codable{
let userId: Int
let id: Int
let title: String
let body: String
}
@andr3a88
andr3a88 / Throttler.swift
Created January 20, 2021 15:25
Throttler
import Foundation
public class Throttler {
private var workItem = DispatchWorkItem(block: {})
private var previousRun = Date.distantPast
private let queue: DispatchQueue
private let minimumDelay: TimeInterval
public init(minimumDelay: TimeInterval, queue: DispatchQueue = DispatchQueue.main) {
@andr3a88
andr3a88 / DiffDateComponents.swift
Created March 25, 2020 09:21
Difference between 2 date with DateComponents
let date1 = Date()
let date2 = date1.addingTimeInterval(12000)
// Use the date components .year .month .day .hour
let diffComponents = Calendar.current.dateComponents([.hour, .minute], from: date1, to: date2)
let hours = diffComponents.hour
let minutes = diffComponents.minute
print("Hours \(hours) Minutes \(minutes)")
@andr3a88
andr3a88 / LocalizedView.swift
Last active December 13, 2019 14:26
LocalizedView
import Foundation
protocol LocalizedView {
/// Localized type
var localizer: Localizer { get }
}
extension LocalizedView where Self: UIViewController {
import Foundation
/// A protocol to present an alert controller
protocol AlertPresentable where Self: UIViewController {
/// Present an alert controller
///
/// - Parameters:
/// - title: The alert title
/// - message: The alert message