Skip to content

Instantly share code, notes, and snippets.

@SoundBlaster
Created January 18, 2023 17:21
Show Gist options
  • Save SoundBlaster/0a4bd8c2c3a78f7da68d398bcba22d5b to your computer and use it in GitHub Desktop.
Save SoundBlaster/0a4bd8c2c3a78f7da68d398bcba22d5b to your computer and use it in GitHub Desktop.
SafeRefreshable modifier for crossplatform SwiftUI
//
// SafeRefreshable.swift
// BLNC
//
// Created by Egor Merkushev on 23.01.2022.
// Copyright © 2022 Egor Merkushev. All rights reserved.
//
import SwiftUI
extension View {
func safeRefreshable(action: @escaping @Sendable () async -> Void) -> ModifiedContent<Self, SafeRefreshable> {
return modifier(SafeRefreshable(action: action))
}
}
/// Usage:
/// ContentView()
/// .safeRefreshable { await refresh() }
struct SafeRefreshable: ViewModifier {
let action: @Sendable () async -> Void
func body(content: Content) -> some View {
if UIDevice.current.isCatalystMacIdiom {
content
} else {
content.refreshable(action: action)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment