Skip to content

Instantly share code, notes, and snippets.

View couchdeveloper's full-sized avatar

Andreas Grosam couchdeveloper

View GitHub Profile
@couchdeveloper
couchdeveloper / PageView.swift
Created January 6, 2023 13:37
SwiftUI PageView - a UIPageViewController with dynamic content
import SwiftUI
import Foundation
struct PageView<Page, PageContent: View>: View where
Page: Identifiable,
PageContent: Identifiable,
PageContent.ID == Page.ID
{
let uiViewControllerRepresentable: PageViewController<Page, PageContent>
@Binding var currentPage: Page
@couchdeveloper
couchdeveloper / TagsView.swift
Last active September 22, 2021 09:47
TagsView
// 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 August 16, 2021 12:33
Elm-like Store using Combine
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!
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`.
//
// TaskQueue.swift
//
// Copyright © 2017 Andreas Grosam. All rights reserved.
//
import Dispatch
/**
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 July 29, 2019 06:55
A timer based on dispatch_source_create() Objective-C
//
// 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 December 29, 2015 12:09
A RXStreamToStreamCopier asynchronously copies the content of a source stream into a destination stream.
//
// 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 October 30, 2016 09:50
Asynchronous Pattern with GCD Part I
/**
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 December 18, 2015 09:58
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.
//
// SimpleGetHTTPRequest.h
//
#import <Foundation/Foundation.h>
typedef void (^completionHandler_t) (id result);