Created
January 18, 2023 17:21
-
-
Save SoundBlaster/0a4bd8c2c3a78f7da68d398bcba22d5b to your computer and use it in GitHub Desktop.
SafeRefreshable modifier for crossplatform SwiftUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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