From 7c6f78630233b4d63d8cc0674a909f8dafb3d740 Mon Sep 17 00:00:00 2001 From: yostane <1958676+yostane@users.noreply.github.com> Date: Thu, 27 Jun 2024 15:20:48 +0200 Subject: [PATCH] chore: Update SwiftUI custom input view and ContentView --- corrections/SwiftUI-00.swiftpm/MyApp.swift | 2 +- .../SwiftUI-01.swiftpm/ContentView.swift | 43 +++++++++++++++ .../SwiftUI-01.swiftpm/CustomInputView.swift | 54 +++++++++++++++++++ corrections/SwiftUI-01.swiftpm/MyApp.swift | 15 ++++++ corrections/SwiftUI-01.swiftpm/Package.swift | 42 +++++++++++++++ 5 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 corrections/SwiftUI-01.swiftpm/ContentView.swift create mode 100644 corrections/SwiftUI-01.swiftpm/CustomInputView.swift create mode 100644 corrections/SwiftUI-01.swiftpm/MyApp.swift create mode 100644 corrections/SwiftUI-01.swiftpm/Package.swift diff --git a/corrections/SwiftUI-00.swiftpm/MyApp.swift b/corrections/SwiftUI-00.swiftpm/MyApp.swift index 7cb2ea61..6dce1000 100644 --- a/corrections/SwiftUI-00.swiftpm/MyApp.swift +++ b/corrections/SwiftUI-00.swiftpm/MyApp.swift @@ -1,7 +1,7 @@ import SwiftUI @main -struct MyApp: App { +struct MyApp00: App { var body: some Scene { WindowGroup { ContentView() diff --git a/corrections/SwiftUI-01.swiftpm/ContentView.swift b/corrections/SwiftUI-01.swiftpm/ContentView.swift new file mode 100644 index 00000000..928ad482 --- /dev/null +++ b/corrections/SwiftUI-01.swiftpm/ContentView.swift @@ -0,0 +1,43 @@ +import SwiftUI + +struct ContentView: View { + @EnvironmentObject var globalState: GlobalState + @State var toggle = false + @State var textFieldContent = "" + @State var isEmailValid = false + var body: some View { + VStack { + CustomInputView(text: "Custom Component", isValidEmail: $isEmailValid) { value in + print(value) + } + TextField("Please enter something", + text: $textFieldContent) + .border(.secondary) + .padding() + .onSubmit { + print("submitted", textFieldContent) + } + Text("text field content: \(textFieldContent)") + Text("Hello SwiftUI") + .font(.largeTitle) + .foregroundColor(isEmailValid ? .red : .green) + .padding() + Button(action: { + toggle = !toggle + print(isEmailValid) + globalState.score = 20 + }) { + HStack { + Image(systemName: "suit.heart.fill") + .foregroundColor(.red) + Text("I am a button") + .font(.headline) + .foregroundColor(.white) + } + .padding(12) + .background(toggle ? Color.orange : Color.blue) + .cornerRadius(8) + } + } + } +} diff --git a/corrections/SwiftUI-01.swiftpm/CustomInputView.swift b/corrections/SwiftUI-01.swiftpm/CustomInputView.swift new file mode 100644 index 00000000..d6c13f5e --- /dev/null +++ b/corrections/SwiftUI-01.swiftpm/CustomInputView.swift @@ -0,0 +1,54 @@ +// +// CustomInputView.swift +// SwiftUI-00 +// +// Created by Yassine Benabbas on 27/06/2024. +// + +import SwiftUI + +struct CustomInputView: View { + @EnvironmentObject var globalState: GlobalState + var text = "Hello World" + @State private var textFieldContent = "" + @Binding var isValidEmail: Bool + let onSpecialEvent : (String) -> Void + + var body: some View { + VStack { + Text(text) + TextField("Please enter something", + text: $textFieldContent) + .border(.secondary) + .padding() + .onSubmit { + isValidEmail = textFieldContent.contains("@") + } + .onChange(of: textFieldContent, perform: { newValue in + if textFieldContent.hasSuffix(".com") { + onSpecialEvent(textFieldContent) + } + }) + Text("text field content: \(textFieldContent) - \(globalState.score)") + } + .border(.secondary) + .padding() + } +} + + +//struct MyView_Previews: PreviewProvider { +// static var previews: some View { +// @State var isValidEmail = false +// CustomInputView(isValidEmail: $isValidEmail) +// } +//} +// +//#Preview { +// MyView_Previews() +//} + +//#Preview("Custom view preiew 2") { +// @State var isValidEmail = false +// _ = CustomInputView(text: "Other preview", isValidEmail: $isValidEmail) +//} diff --git a/corrections/SwiftUI-01.swiftpm/MyApp.swift b/corrections/SwiftUI-01.swiftpm/MyApp.swift new file mode 100644 index 00000000..781854aa --- /dev/null +++ b/corrections/SwiftUI-01.swiftpm/MyApp.swift @@ -0,0 +1,15 @@ +import SwiftUI + +class GlobalState: ObservableObject { + @Published var score = 0 +} + +@main +struct MyApp01: App { + @StateObject var globalState = GlobalState() + var body: some Scene { + WindowGroup { + ContentView() + } + } +} diff --git a/corrections/SwiftUI-01.swiftpm/Package.swift b/corrections/SwiftUI-01.swiftpm/Package.swift new file mode 100644 index 00000000..e264175a --- /dev/null +++ b/corrections/SwiftUI-01.swiftpm/Package.swift @@ -0,0 +1,42 @@ +// swift-tools-version: 5.8 + +// WARNING: +// This file is automatically generated. +// Do not edit it by hand because the contents will be replaced. + +import PackageDescription +import AppleProductTypes + +let package = Package( + name: "SwiftUI-01", + platforms: [ + .iOS("16.0") + ], + products: [ + .iOSApplication( + name: "SwiftUI-01", + targets: ["AppModule"], + bundleIdentifier: "wl.iostraining.SwiftUI-01", + displayVersion: "1.0", + bundleVersion: "1", + appIcon: .placeholder(icon: .calculator), + accentColor: .presetColor(.yellow), + supportedDeviceFamilies: [ + .pad, + .phone + ], + supportedInterfaceOrientations: [ + .portrait, + .landscapeRight, + .landscapeLeft, + .portraitUpsideDown(.when(deviceFamilies: [.pad])) + ] + ) + ], + targets: [ + .executableTarget( + name: "AppModule", + path: "." + ) + ] +) \ No newline at end of file