Skip to content

Commit

Permalink
chore: Update SwiftUI custom input view and ContentView
Browse files Browse the repository at this point in the history
  • Loading branch information
yostane committed Jun 27, 2024
1 parent d2ed81c commit 7c6f786
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 1 deletion.
2 changes: 1 addition & 1 deletion corrections/SwiftUI-00.swiftpm/MyApp.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SwiftUI

@main
struct MyApp: App {
struct MyApp00: App {
var body: some Scene {
WindowGroup {
ContentView()
Expand Down
43 changes: 43 additions & 0 deletions corrections/SwiftUI-01.swiftpm/ContentView.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}
54 changes: 54 additions & 0 deletions corrections/SwiftUI-01.swiftpm/CustomInputView.swift
Original file line number Diff line number Diff line change
@@ -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)
//}
15 changes: 15 additions & 0 deletions corrections/SwiftUI-01.swiftpm/MyApp.swift
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
42 changes: 42 additions & 0 deletions corrections/SwiftUI-01.swiftpm/Package.swift
Original file line number Diff line number Diff line change
@@ -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: "."
)
]
)

0 comments on commit 7c6f786

Please sign in to comment.