Skip to content

Instantly share code, notes, and snippets.

View marcpalmer's full-sized avatar
💭
Busy working on Concepts app, and Captionista

Marc Palmer marcpalmer

💭
Busy working on Concepts app, and Captionista
View GitHub Profile
@marcpalmer
marcpalmer / Floating.swift
Created January 14, 2024 19:48
Modifiers for animating views from one place to another in the view hierarchy, even if the clipping changes
//
// Floating.swift
// Captionista
//
// Created by Marc Palmer on 24/02/2023.
//
import SwiftUI
/// Set to true for debug prints.
private var debug = false
@marcpalmer
marcpalmer / FrameCapture.swift
Created March 6, 2023 10:42
Modifiers to easily capture and store view geometry in state.
//
// FrameCapture.swift
// FrameCapture
//
// Created by Marc Palmer on 31/03/2020.
//
// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
@marcpalmer
marcpalmer / Table.swift
Created March 2, 2023 23:32
Simple Table/Row/Column views
//
// Table.swift
// Captionista
//
// Created by Marc Palmer on 20/03/2022.
// Copyright © 2022 Montana Floss Co. Ltd. All rights reserved.
//
import SwiftUI
import Foundation
import Combine
extension Publishers {
/// A publisher that publishers any values that are immediately available on the current thread/queue,
/// but anything received after the subscription process completes is published on a different scheduler.
///
/// This allows you do perform immediate state updates at the point of subscribing without having
/// `.receive(on:)` force every single result to require a scheduler hop.
@marcpalmer
marcpalmer / example.txt
Last active September 5, 2022 15:20
Possible structured data in Markdown
| userID | name | age | permissions | address |
|--------|------|-----|-------------|--------------------|
|1 |peter | 32 |read |[object](#address-1)|
|2 |admin | 100 |read,write |[object](#address-2)|
## Address 1 // <--- linkify this when loading e.g. address-1
| street | town |
|--------|-------|
|High St.|Tetbury|
@marcpalmer
marcpalmer / ReceiveAsyncResultsOn.swift
Last active August 29, 2022 02:30
A Combine operator that works like receive(on:) but only hops on the scheduler after the initial subscribe process completes
import Foundation
import Combine
/// Usage example:
///
/// ```
/// source
/// .receiveAsyncResults(on: DispatchQueue.main)
/// .sink {
/// print("Value: \($0)")
@marcpalmer
marcpalmer / SizeClassDependentValue.swift
Last active May 12, 2021 12:09
Example of adaptive values in SwiftUI where size classes determine the value you want to use
import Foundation
import SwiftUI
/// A value that can have different values depending on compact or regular size class in a single axis.
///
/// Usage example:
///
/// ```
/// static let textPadding = SizeClassDependentValue(compact: 20, regular: 50)
///
@marcpalmer
marcpalmer / CombineExportProgressSample.swift
Last active April 10, 2023 02:09
Pulling down a video asset from Photos and exporting it, with progress and cancellation with Combine
/// Get the payload of a video asset and export it to a local temp file.
/// The resulting publsher will emit values until completed or cancelled. The `Double` is the progress,
/// which is a combination of the download progress and the export progress, so it will range from 0 to 1 but the
/// last export part is probably a lot quicker than the download part.
///
/// Calling cancel() on the publisher will cancel both the image request and the export as appropriate
func exportAVAsset(forPHAsset phAsset: PHAsset) -> (AnyPublisher<(URL?, Double), MediaError>) {
let avAssetOptions = PHVideoRequestOptions()
avAssetOptions.isNetworkAccessAllowed = true
avAssetOptions.deliveryMode = .highQualityFormat
@marcpalmer
marcpalmer / FrameCaptureModifier.swift
Last active November 26, 2022 15:32
Code to capture frames of views for use elsewhere in the SwiftUI hierarchy
//
// FrameCaptureModifier.swift
// FrameCaptureModifier
//
// Created by Marc Palmer on 31/03/2020.
//
// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
@marcpalmer
marcpalmer / BookmarkDataIssueTests.swift
Created September 7, 2020 15:27
Example of infinite recursion crash when asking for URL.bookmarkData in a unit test
///
/// Example of infinite recursion crash when asking for URL.bookmarkData in a unit test,
/// from a non-main queue.
///
import XCTest
class BookmarkDataIssueTests: XCTestCase {
func testExample() throws {