-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Update SwiftUI custom input view and ContentView
- Loading branch information
Showing
5 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
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) | ||
} | ||
} | ||
} | ||
} |
This file contains 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
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) | ||
//} |
This file contains 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
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() | ||
} | ||
} | ||
} |
This file contains 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
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: "." | ||
) | ||
] | ||
) |