Skip to content

Instantly share code, notes, and snippets.

View DanielCardonaRojas's full-sized avatar

Daniel Cardona Rojas DanielCardonaRojas

View GitHub Profile
/// A class tracking a sequence/sets of events
public class EventCompleter<Event: OptionSet> {
/// The set tracking all inserted events
public private(set) var eventSet: Event = []
/// The target set that will notify a completion
public private(set) var targetSet: Event = []
private var continuation: CheckedContinuation<Void, Error>?
@DanielCardonaRojas
DanielCardonaRojas / ArrangedViewContainer.swift
Last active April 5, 2023 17:48
An arranged view container that allow creating all sorts of layouts even UIStackView clones.
import Foundation
import UIKit
open class ArrangedViewContainer: UIView
{
public private(set) var hiddenViews = Set<UIView>()
private var observations: Set<NSKeyValueObservation> = Set()
open private(set) var arrangedSubviews = [UIView]()
private var invalidated = false
private var viewConstraints: [NSLayoutConstraint] = []
@DanielCardonaRojas
DanielCardonaRojas / Nested.swift
Created December 14, 2022 15:37
Reach deeply nested object without having to create all intermediate CodingKeys
class Nested<T: Decodable>: Decodable
{
let value: T
enum DecodeError: Error
{
case nestedKeyPathNotConfigure
case keyNotConfigured
}
@DanielCardonaRojas
DanielCardonaRojas / optional_assignement.swift
Created April 26, 2022 16:26
Optional assignment operator #swift
infix operator ??= : AdditionPrecedence
extension Optional {
static func ??= (_ rhs: inout Self, _ lhs: Self) {
if rhs == nil {
rhs = lhs
}
}
}
@DanielCardonaRojas
DanielCardonaRojas / commit-msg.sh
Last active November 8, 2021 12:16
githooks for flutter development
#!/bin/sh
if ! head -1 "$1" | grep -qE "^(feat|fix|chore|docs|test|style|refactor|perf|build|ci|revert)(\(.+?\))?: .{1,}$"; then
echo "Aborting commit. Your commit message is invalid." >&2
exit 1
fi
if ! head -1 "$1" | grep -qE "^.{1,88}$"; then
echo "Aborting commit. Your commit message is too long." >&2
exit 1
fi
# echo "$(cat $1)" | commitlint
@DanielCardonaRojas
DanielCardonaRojas / dialog_controller.dart
Last active September 2, 2021 23:07
Dialog Utilities for Flutter pages
import 'dart:async';
import 'package:flutter/material.dart';
typedef DialogShowHandler<T> = Future<T> Function();
class DialogController {
late DialogShowHandler _showDialog;
late Function _closeDialog;
Completer? _dialogCompleter;
@DanielCardonaRojas
DanielCardonaRojas / bloc_testing.dart
Created June 24, 2021 17:08
Bloc or Cubit test helper, without requiring bloc_test package
import 'package:mocktail/mocktail.dart';
import 'package:meta/meta.dart';
import 'package:bloc/bloc.dart';
import 'dart:async';
import 'package:test/test.dart';
abstract class Callable<T> {
void call([T? arg]) {}
}
@DanielCardonaRojas
DanielCardonaRojas / HTMLParser.swift
Last active May 4, 2021 18:56
HTMLParser #Swift
import Foundation
struct AttributeMatcher {
enum Comparison {
case equal, regex
}
let name: String
let value: String
let comparison: Comparison
@DanielCardonaRojas
DanielCardonaRojas / UIButton+Extensions.swift
Created April 7, 2021 22:37
UIButton extensions #swift #ios #button
extension UIButton {
func setInsets(
forContentPadding contentPadding: UIEdgeInsets,
imageTitlePadding: CGFloat
) {
let sign: CGFloat = semanticContentAttribute == .forceRightToLeft ? -1 : 1
let imageTitleSpacing = sign * imageTitlePadding
self.contentEdgeInsets = UIEdgeInsets(
@DanielCardonaRojas
DanielCardonaRojas / Resolver.swift
Last active September 29, 2023 01:01
Dependency injection for Swift projects
public class DependencyResolver
{
public typealias Factory<T> = () -> T
public static let shared = DependencyResolver()
var factoryDict: TypeDict<Factory<Any>> = [:]
public init() {}