Skip to content

Instantly share code, notes, and snippets.

View Edudjr's full-sized avatar
🏠
Working from home

Eduardo Domene Junior Edudjr

🏠
Working from home
  • Mercado Livre
  • Sao Paulo
View GitHub Profile
@Edudjr
Edudjr / ThreadSafe.swift
Last active July 15, 2024 15:09
Swift implementation of a thread-safe, generic, Property Wrapper
@propertyWrapper
final class ThreadSafe<T> {
private let queue = DispatchQueue(label: "ThreadSafeTypeQueue",
attributes: .concurrent)
private var content: T
var wrappedValue: T {
get {
queue.sync {
struct AnimationSample: View {
@State var displaySheet = false
@State var shake = false
var body: some View {
Text("Shake Me")
.font(.title)
.onTapGesture {
shake = true
}
struct Shake<Content: View>: View {
/// Set to true in order to animate
@Binding var shake: Bool
/// How many times the content will animate back and forth
var repeatCount = 3
/// Duration in seconds
var duration = 0.8
/// Range in pixels to go back and forth
var offsetRange = 10.0
struct AnimationSample: View {
@State var isAnimated = false
@State var displaySheet = false
var body: some View {
Text("Testing")
.scaleEffect(isAnimated ? 2 : 1)
.onTapGesture {
Task {
await animate(duration: 0.5) {
extension View {
func animate(duration: CGFloat, _ execute: @escaping () -> Void) async {
await withCheckedContinuation { continuation in
withAnimation(.linear(duration: duration)) {
execute()
}
DispatchQueue.main.asyncAfter(deadline: .now() + duration) {
continuation.resume()
}
Text("Testing")
.scaleEffect(firstAnimation ? 2 : 1)
.foregroundColor(secondAnimation ? .red : .green)
.onTapGesture {
Task {
let animationTime = 0.5
await animate(duration: animationTime) {
firstAnimation = true
}
Text("Testing")
.scaleEffect(firstAnimation ? 2 : 1)
.foregroundColor(secondAnimation ? .red : .green)
.onTapGesture {
let animationTime = 0.5
withAnimation(.linear(duration: animationTime)) {
firstAnimation = true
}
struct AnimationSample: View {
@State var firstAnimation = false
@State var secondAnimation = false
@State var displaySheet = false
var body: some View {
Text("Testing")
.scaleEffect(firstAnimation ? 2 : 1)
.foregroundColor(secondAnimation ? .red : .green)
.onTapGesture {
struct AnimationSample: View {
@State var isAnimating = false
var body: some View {
Text("Testing")
.scaleEffect(isAnimating ? 2 : 1)
.foregroundColor(isAnimating ? .red : .green)
.onTapGesture {
withAnimation {
isAnimating = true
struct AnimationSample: View {
@State var firstAnimation = false
@State var secondAnimation = false
var body: some View {
Text("Testing")
.scaleEffect(firstAnimation ? 2 : 1)
.foregroundColor(secondAnimation ? .red : .green)
.onTapGesture {
withAnimation(.linear(duration: 0.5)) {