Skip to content

Instantly share code, notes, and snippets.

View CassiusPacheco's full-sized avatar

Cassius Pacheco CassiusPacheco

View GitHub Profile
@CassiusPacheco
CassiusPacheco / XCTAssertThrowsErrorAsync.swift
Last active September 14, 2022 04:49
Helper for easily asserting an async method threw an error
import Foundation
import XCTest
extension XCTestCase {
/// Expects that the async code will throw
@discardableResult
func XCTAssertThrowsErrorAsync(
testName: String = #function,
file: StaticString = #filePath,
line: UInt = #line,
@CassiusPacheco
CassiusPacheco / DataResult.swift
Created February 23, 2021 23:00
This abstraction contains either a success data of generic type `T` or an error of type `ErrorCode` as its result.
enum ErrorCode: Equatable, Error {
case unknown
// Top level server error
case serverIssue
// Network
case noNetworkConnection
}
@CassiusPacheco
CassiusPacheco / data_result.dart
Last active December 13, 2023 03:14
Dart's DataResult<S> inspired by my Swift enum implementation and Avdosev's Dart Either implementation
// The code below was inspired by my swift implementation https://gist.github.com/CassiusPacheco/4378d30d69316e4a6ba28a0c3af72628
// and Avdosev's Dart Either https://github.com/avdosev/either_dart/blob/master/lib/src/either.dart
import 'package:equatable/equatable.dart';
abstract class Failure extends Equatable implements Exception {
@override
String toString() => '$runtimeType Exception';
@override
@CassiusPacheco
CassiusPacheco / color_to_materialcolor.dart
Created February 12, 2021 01:09
[Dart/Flutter] Color to Material Color
import 'package:flutter/material.dart';
extension MaterialX on Color {
MaterialColor toMaterial() {
return _createMaterialColor(this);
}
// Taken from https://medium.com/@filipvk/creating-a-custom-color-swatch-in-flutter-554bcdcb27f3
MaterialColor _createMaterialColor(Color color) {
final List<double> strengths = [.05];
@CassiusPacheco
CassiusPacheco / service_locator.dart
Last active March 18, 2021 04:55
A dependency resolver container for Dart/Flutter
import 'package:logging/logging.dart';
typedef InstanceBuilderCallback<S> = S Function(ServiceLocator);
typedef InstanceBuilderCallback1<S, A> = S Function(ServiceLocator, A);
/// A service lookup class which allows factories of instances and singletons
/// to be registered and resolved as part of a dependency injection system.
class ServiceLocator {
final Map<String, InstanceBuilderCallback> _factories = {};
final Map<String, dynamic> _factories1 = {};
@CassiusPacheco
CassiusPacheco / EraseToAnyPublisherFailure.swift
Last active May 17, 2020 03:18
Wraps this publisher with a type eraser that updates the expected `Failure` from `Never` to the given type.
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Publisher where Self.Failure == Never {
/// Wraps this publisher with a type eraser that updates the expected `Failure` from `Never` to the given type.
///
/// Use `eraseToAnyPublisherFailure(to:)` to expose an instance of AnyPublisher to the downstream subscriber, rather than this publisher’s actual type.
/// Under the hood it updates the failure type by calling `setFailureType(to:)` applying the given type.
public func eraseToAnyPublisherFailure<E>(to failureType: E.Type) -> AnyPublisher<Self.Output, E> where E: Error {
return self
.setFailureType(to: failureType)
.eraseToAnyPublisher()
@CassiusPacheco
CassiusPacheco / NSMutableAttributedString+Extensions.swift
Last active May 6, 2020 04:17
Extensions to make attributed strings composable
import Foundation
public func + (lhs: NSAttributedString, rhs: NSAttributedString) -> NSMutableAttributedString {
let mutable = NSMutableAttributedString(attributedString: lhs)
mutable.append(rhs)
return mutable
}
extension String {
public var attributedString: NSMutableAttributedString {
@CassiusPacheco
CassiusPacheco / Assert.swift
Created April 29, 2020 06:57
Assert helper that provides a few functions for asserting while debugging
public final class Assert {
public static var shouldCrashOnDebug = true
fileprivate static var isUnitTesting = NSClassFromString("XCTest") != nil
}
public func debugAssertion(_ condition: @autoclosure () -> Bool, message: String = "") {
#if DEBUG || STAGE
if !condition() {
if Assert.shouldCrashOnDebug {
@CassiusPacheco
CassiusPacheco / SingleDisposer.swift
Created April 28, 2020 11:54
A dispose bag that keeps only one disposable in the bag at a time.
import Foundation
import RxSwift
extension Disposable {
/// Adds `self` to `single`
///
/// - parameter single: `SingleDisposer` to add `self` to.
/// - note: `SingleDisposer` is a wrapper of `DisposeBag` that is recreated
/// whenever a disposable object is inserted into it, cancelling any
/// previous subscriptions. It keeps at most one subscription at a
@CassiusPacheco
CassiusPacheco / WaitForCompletion.swift
Last active September 1, 2022 08:30
toBlocking equivalent for Combine's Publishers
extension Publisher {
func waitForCompletion(timeout: TimeInterval = 1.0, file: StaticString = #file, line: UInt = #line) throws -> [Output] {
let expectation = XCTestExpectation(description: "wait for completion")
var completion: Subscribers.Completion<Failure>?
var output = [Output]()
let subscription = self.collect()
.sink(receiveCompletion: { receiveCompletion in
completion = receiveCompletion
expectation.fulfill()