This playground demonstrates key SwiftUI and Combine concepts including:
- Network requests using async/await and Combine
- MVVM architecture
- SwiftUI views and navigation
- Protocol-based networking
- JSON decoding
struct SwiftUIView: View { @State private var range: ClosedRange = 0…5 @State var shakeNUmber: Int = 10 var body: some View { NavigationView { VStack { Button { withAnimation(.easeInOut(duration: 0.5)) { // Use a known animation let random = Int.random(in: 10…20) range = 0…random } } label: { Text(“Randomize”) } .padding() .buttonStyle(FIPrimaryButtonStyle())
.navigationTitle("Hello")
Button("Shake Me") { shakeNUmber += 1 }
.shake(shakes: CGFloat(shakeNUmber))
.animation(.default, value: shakeNUmber)
ForEach(range, id: \.self) { item in
Text("\(item)") // Use correct string interpolation
}
}
.padding()
}
}
}
struct FIPrimaryButtonStyle: ButtonStyle { func makeBody(configuration: Configuration) -> some View { configuration.label .modifier(fullWidthModifier()) .cornerRadius() .opacity(configuration.isPressed ? 0.5: 1) .scaleEffect(configuration.isPressed ? 1.03 : 1) .modifier(FIShadowModifier()) .animation(.easeInOut, value: configuration.isPressed) } }
struct fullWidthModifier: ViewModifier { func body(content: Content) -> some View { content .frame(maxWidth: .infinity) .padding() .background { Color.green }
}
}
struct FIShadowModifier: ViewModifier { func body(content: Content) -> some View { content .shadow(radius: 5) } }
import SwiftUI
struct ShakeAnimation: AnimatableModifier { var shakes: CGFloat
var animatableData: CGFloat {
get { shakes }
set { shakes = newValue }
}
func body(content: Content) -> some View {
content
.offset(x: -sin(shakes * .pi * 2) * 50) // Corrected multiplication
}
}
extension View { func shake(shakes: CGFloat) -> some View { self.modifier(ShakeAnimation(shakes: shakes)) } }
#Preview { SwiftUIView() }