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 / ScrollViewSnapIssue.swift
Last active October 22, 2023 06:19
Updated to correct problem with subscription being new every time state changes, fixed scrollTo so it works
import Combine
import SwiftUI
struct SnapPointPreferenceData {
let frame: Anchor<CGRect>
}
struct SnapPointPreferenceKey: PreferenceKey {
typealias Value = SnapPointPreferenceData?
@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 / 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 / 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 / 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 / TypealiasDefaultingInSubProtocol_TypeAliasNaturalApproach.swift
Created May 17, 2019 09:38
Example of sub-protocol typealias defaulting which results in compiler warning to use protocol constraints
import Foundation
// ****************************************************************************************
//
// Example of trying to supply a default type for an associated type in a sub-protocol
// and helper functions that use this default type in an extention on that sub-protocol.
// The natural way to achieve this by defining the typealias in the sub-protocol produces
// a compiler warning and fix-it suggesting use of protocol constraint.
//
// This approach: typealias