Skip to content

Instantly share code, notes, and snippets.

View alexdrone's full-sized avatar

Alex Usbergo alexdrone

View GitHub Profile
@alexdrone
alexdrone / Locking.swift
Created May 4, 2021 10:26
Locks and Atomic Property wrappers.
import Foundation
// MARK: - Locking
public protocol Locking {
init()
/// Attempts to acquire a lock, blocking a thread’s execution until the lock can be acquired.
func lock()
@alexdrone
alexdrone / Fields.swift
Created March 7, 2021 12:58
SwiftUI MacOS Quick Forms
import SwiftUI
import AppKit
import Combine
extension View {
public func embedInLabel(label: String) -> some View {
FieldLabel(label: label) {
self
}
}
@alexdrone
alexdrone / modern_cxx_tutorials__array.h
Last active April 13, 2020 19:25
Notes for teaching Moden C++.
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <array>
struct Vertex {
float x, y, z;
};
std::ostream& operator<<(std::ostream& stream, const Vertex& vertex) {
@alexdrone
alexdrone / BridgeView.swift
Last active January 12, 2020 18:06
(UIKit >> SwiftUI) Inline bridge view using some @dynamicMemberLookup magic.
import Foundation
import UIKit
import SwiftUI
// MARK: - UIViewBridge
/// Usage:
///
/// var body: some View {
/// ...
@alexdrone
alexdrone / Proxy.swift
Last active July 23, 2019 09:03
Proxy and ProxyBuilder in Swift 5.1 using @dynamicMemberLookup
import Foundation
// MARK: - Proxy
public protocol ProxyProtocol {
associatedtype ProxyType
/// The wrapped proxied object.
var proxiedObject: ProxyType { get }
}
@alexdrone
alexdrone / Typography.swift
Last active December 29, 2018 09:26
Customizable template used to handle app-wide typography.
import UIKit
// Example usage:
// let label = UILabel()
// label.attributedText = Typography.current.style(.button).asAttributedString("Edit"),
class Typography {
/// The current application typography.
/// Override this static property to use a custom typography.
static var current: TypographyProtocol = BaseTypography()
@alexdrone
alexdrone / BaseView.swift
Created October 16, 2018 14:36
Vanilla View Swift
import UIKit
/// Represents the internal state of the view.
public protocol BaseViewState {
init()
}
/// Empty view state.
public final class BaseViewNullState: BaseViewState {
public init() {
@alexdrone
alexdrone / objcxx.h
Created October 3, 2018 18:37
ObjectiveC++ MACROS
#ifndef objcxx_h
#define objcxx_h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
// #pragma mark - Type inference and dynamic casts
// Type inference for local variables.
@alexdrone
alexdrone / objc_struct.h
Created June 14, 2018 08:19
ARC Struct Boxable Macros
#import <Foundation/Foundation.h>
#define objc_struct_check(__TYPE__, __ACTION__) \
[__ACTION__.identifier isEqualToString: @ #__TYPE__] \
#define objc_struct_define \
typedef struct __attribute__((objc_boxable)) \
#define _objc_boxed_struct_make(__TYPE__, __ARGS__) \
[[objc_boxed_struct alloc] initWithIdentifier:@ # __TYPE__ \
@alexdrone
alexdrone / KVO.swift
Last active December 29, 2018 09:27
(Deprecated) Pre-Swift 4 KVO Observation Proxy.
import Foundation
// MARK: KVO Change set
/// Represent a change for a KVO compliant property.
public struct KVOChange<O:AnyObject, T> {
/// The associated object for this change.
public let object: O?
/// The keyPath the triggered this change.