Skip to content

Instantly share code, notes, and snippets.

View lucaswkuipers's full-sized avatar
💻
iOS Development 

Lucas Werner Kuipers lucaswkuipers

💻
iOS Development 
View GitHub Profile
@lucaswkuipers
lucaswkuipers / ReusableViewControllerTests.swift
Created July 12, 2022 01:30
ReusableViewControllerTests
import XCTest
@testable import ReusingViewControllers
final class ReusableViewControllerTests: XCTestCase {
func test_lifeCycle_sendsEventsCorrectly() {
let (sut, delegateSpy) = makeSUT()
sut.viewDidLoad()
sut.viewWillAppear(true)
@lucaswkuipers
lucaswkuipers / SceneDelegate.swift
Created July 12, 2022 03:26
Scene delegate demonstrating the composition of the `ReusableViewController`
import UIKit
final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
let view = QuoteView()
let viewController = ReusableViewController(with: view)
-- Set initial variables
set isWiFiConnected to false
set maxAttempts to 10
set attemptCount to 0
set wifiOffDelay to 0.1 -- Set the delay duration (in seconds) after turning Wi-Fi off
set connectionCheckDelay to 5 -- Set the delay duration (in seconds) before checking Wi-Fi connection status after turning Wi-Fi on
-- Repeat the loop until Wi-Fi is connected or the maximum number of attempts is reached
repeat while not isWiFiConnected and attemptCount < maxAttempts
-- Increment the attempt count
import UIKit
final class UIKitSampleView: UIView {
private let containerView = UIView()
private let titleLabel: UILabel = {
let label = UILabel()
label.text = "Hello, world!"
label.font = .preferredFont(forTextStyle: .largeTitle)
return label
import ViewKit
final class ViewKitSampleView: ProgrammaticView {
var body: UIView {
VerticalStack(spacing: 40) {
UILabel("Hello, world!")
.font(.preferredFont(forTextStyle: .largeTitle))
UIButton("Tap me!") { _ in print("Tapped") }
@lucaswkuipers
lucaswkuipers / View+prefersHomeIndicatorAutoHidden.swift
Created October 11, 2023 17:49
View+prefersHomeIndicatorAutoHidden.swift
import SwiftUI
extension View {
func prefersHomeIndicatorAutoHidden() -> some View {
background(HiddenHomeIndicatorHostingController())
}
}
struct HiddenHomeIndicatorHostingController: UIViewControllerRepresentable {
typealias UIViewControllerType = UIViewController
extension Button {
func onTouchDown(perform action: @escaping () -> Void) -> some View {
onLongPressGesture(perform: {}) { isPressing in
if isPressing {
action()
}
}
}
}
@lucaswkuipers
lucaswkuipers / XCTestCase+trackForMemoryLeak.swift
Last active January 8, 2024 19:42
XCTestCase+trackForMemoryLeak
import XCTest
extension XCTestCase {
func assertDeallocated(
_ instance: AnyObject,
file: StaticString = #filePath,
line: UInt = #line
) {
addTeardownBlock { [weak instance] in
XCTAssertNil(
@lucaswkuipers
lucaswkuipers / View+onLoad.swift
Last active November 29, 2023 17:45
View+onLoad
import SwiftUI
private struct OnLoad: ViewModifier {
let action: () -> Void
@State private var loaded = false
func body(content: Content) -> some View {
content.onAppear {
if !loaded {
loaded = true
@lucaswkuipers
lucaswkuipers / MutableCollection+[safe].swift
Created November 28, 2023 22:20
MutableCollection+[safe]
extension MutableCollection {
subscript(safe index: Index) -> Element? {
get {
indices.contains(index) ? self[index] : nil
}
set {
if let newValue, indices.contains(index) {
self[index] = newValue
}