Skip to content

Instantly share code, notes, and snippets.

Avatar

Andreas Grosam couchdeveloper

View GitHub Profile
View TagsView.swift
// Shows how to implement a view rendering a colleciton of "tags"
// which get wrapped to new rows as required.
import SwiftUI
struct WrappingHStack<Content: View, T: Identifiable & Hashable>: View {
typealias Row = [T]
typealias Rows = [Row]
struct Layout: Equatable {
@couchdeveloper
couchdeveloper / Oak.Store.swift
Created Aug 16, 2021
Elm-like Store using Combine
View Oak.Store.swift
import Combine
/// A `Store` is a non-failing Publisher whose output carries values of type `State`.
public final class Store<State, Event, Scheduler: Combine.Scheduler>: Publisher {
public typealias Output = State
public typealias Failure = Never
public typealias Events = AnyPublisher<Event, Never>
private let state: CurrentValueSubject<State, Never>
private var machine: Cancellable!
View gist:ebaff1befa9ad2b0210366c4889393b1
import SwiftUI
// Demonstrates issue when assigning `@State` values in the initialiser
struct ContentView: View {
@State var string: String = "Initial" // default initialise the value
init(string: String) {
self.string = string // assign it again with value `string`
// We expect that `self.string` has been assigned the value `string`.
View TaskQueue
//
// TaskQueue.swift
//
// Copyright © 2017 Andreas Grosam. All rights reserved.
//
import Dispatch
/**
View DataRaceDemo.swift
import Foundation
func test(completion: (Int) -> ()) {
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
//let queue = dispatch_queue_create("serial queue", DISPATCH_QUEUE_SERIAL)
var counter = 0
let grp = dispatch_group_create()
@couchdeveloper
couchdeveloper / RXTimer.h
Last active Jul 29, 2019
A timer based on dispatch_source_create() Objective-C
View RXTimer.h
//
// RXTimer.h
//
// Copyright 2013 Andreas Grosam
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
@couchdeveloper
couchdeveloper / RXAsynchronousOperation.h
Last active Dec 29, 2015
A RXStreamToStreamCopier asynchronously copies the content of a source stream into a destination stream.
View RXAsynchronousOperation.h
//
// RXAsynchronousOperation.h
//
// Copyright 2013 Andreas Grosam
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
@couchdeveloper
couchdeveloper / transform_each.m
Last active Oct 30, 2016
Asynchronous Pattern with GCD Part I
View transform_each.m
/**
Objective:
Asynchronously transform or process an array of items - one after the
other and return the result of each transform in an array.
Synopsis:
void transform_each(NSArray* inArray, unary_async_t task, completion_t completion);
@couchdeveloper
couchdeveloper / SimpleGetHTTPRequest.h
Last active Dec 18, 2015
SimpleGetHTTPRequest This is a simple Objective-C class which wraps a `NSURLConnection` and relevant state information. It's meant to give an idea how one can implement a more "real" and more versatile connection class. It's deliberately kept simple.
View SimpleGetHTTPRequest.h
//
// SimpleGetHTTPRequest.h
//
#import <Foundation/Foundation.h>
typedef void (^completionHandler_t) (id result);
@couchdeveloper
couchdeveloper / NSOperationQueue
Created May 10, 2013
Play around with NSOperationQueue and NSOperation.
View NSOperationQueue
#import <Foundation/Foundation.h>
#include <dispatch/dispatch.h>
@interface Operation : NSOperation
@property (nonatomic, readwrite) BOOL isExecuting;
@property (nonatomic, readwrite) BOOL isFinished;
@property (nonatomic, readwrite) BOOL terminating;
@end
@implementation Operation {