From 0b964c92cfbc3a972143bdfb9d58fd3c8fb8083b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=A9tudiant?= Date: Tue, 28 May 2024 11:01:17 +0200 Subject: [PATCH] add players pop up is done :sparkles: --- .../DouShouQi_App.xcodeproj/project.pbxproj | 4 ++ .../Components/Player/AddPlayerView.swift | 63 +++++++++++++++++++ .../Components/Player/PlayerRow.swift | 6 +- .../Views/Player/PlayersView.swift | 25 +++++--- 4 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 DouShouQi_App/DouShouQi_App/Components/Player/AddPlayerView.swift diff --git a/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj b/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj index 13e4fcf..8f191fd 100644 --- a/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj +++ b/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj @@ -40,6 +40,7 @@ EC62C5062C045C1A0048CD0B /* TitleScreenButtonSound.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = EC62C5052C045C1A0048CD0B /* TitleScreenButtonSound.mp3 */; }; EC62C5092C0467240048CD0B /* SplashScreenView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC62C5082C0467240048CD0B /* SplashScreenView.swift */; }; EC62C50D2C046D9E0048CD0B /* SplashScreenSound.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = EC62C50C2C046D9E0048CD0B /* SplashScreenSound.mp3 */; }; + EC62C50F2C05D06A0048CD0B /* AddPlayerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC62C50E2C05D06A0048CD0B /* AddPlayerView.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -110,6 +111,7 @@ EC62C5052C045C1A0048CD0B /* TitleScreenButtonSound.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = TitleScreenButtonSound.mp3; sourceTree = ""; }; EC62C5082C0467240048CD0B /* SplashScreenView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SplashScreenView.swift; sourceTree = ""; }; EC62C50C2C046D9E0048CD0B /* SplashScreenSound.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = SplashScreenSound.mp3; sourceTree = ""; }; + EC62C50E2C05D06A0048CD0B /* AddPlayerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddPlayerView.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -230,6 +232,7 @@ children = ( 645B4C1F2BFCCA0500FD658A /* PlayerResumeFrame.swift */, EC62C4FC2C0391D30048CD0B /* PlayerRow.swift */, + EC62C50E2C05D06A0048CD0B /* AddPlayerView.swift */, ); path = Player; sourceTree = ""; @@ -478,6 +481,7 @@ buildActionMask = 2147483647; files = ( 643AB69B2BFCFB5C0018DA73 /* HistoricView.swift in Sources */, + EC62C50F2C05D06A0048CD0B /* AddPlayerView.swift in Sources */, 6458345E2BF5F92300E18321 /* ContentView.swift in Sources */, 649B59A92BF64C6A002BAE38 /* Colors.swift in Sources */, 649B59B42BF653E1002BAE38 /* ViewTitleTextStyle.swift in Sources */, diff --git a/DouShouQi_App/DouShouQi_App/Components/Player/AddPlayerView.swift b/DouShouQi_App/DouShouQi_App/Components/Player/AddPlayerView.swift new file mode 100644 index 0000000..80b25ba --- /dev/null +++ b/DouShouQi_App/DouShouQi_App/Components/Player/AddPlayerView.swift @@ -0,0 +1,63 @@ +// +// AddPlayerView.swift +// DouShouQi_App +// +// Created by étudiant on 28/05/2024. +// + +import SwiftUI + +struct AddPlayerView: View { + @Binding var isPresented: Bool + @Binding var players: [Player] + @State private var playerName: String = "" + @State private var showAlert = false + + var body: some View { + VStack(spacing: 20) { + Text("Add New Player") + .font(.headline) + + TextField("Player Name", text: $playerName) + .padding() + .background(Color(.systemGray6)) + .cornerRadius(10) + + HStack { + Button(action: { + if players.contains(where: { $0.name.lowercased() == playerName.lowercased() }) { + showAlert = true + } else { + let newPlayer = Player(name: playerName, wins: 0, losses: 0) + players.append(newPlayer) + isPresented = false + } + }) { + Text("Add") + .foregroundColor(.white) + .padding() + .background(Color.blue) + .cornerRadius(10) + } + + Button(action: { + isPresented = false + }) { + Text("Cancel") + .foregroundColor(.white) + .padding() + .background(Color.red) + .cornerRadius(10) + } + } + } + .padding() + .frame(maxWidth: 300) + .background(Color.white) + .cornerRadius(20) + .shadow(radius: 10) + .alert(isPresented: $showAlert) { + Alert(title: Text("Error"), message: Text("Player already exists."), dismissButton: .default(Text("OK"))) + } + } +} diff --git a/DouShouQi_App/DouShouQi_App/Components/Player/PlayerRow.swift b/DouShouQi_App/DouShouQi_App/Components/Player/PlayerRow.swift index b07bffe..950a364 100644 --- a/DouShouQi_App/DouShouQi_App/Components/Player/PlayerRow.swift +++ b/DouShouQi_App/DouShouQi_App/Components/Player/PlayerRow.swift @@ -10,9 +10,13 @@ import SwiftUI struct PlayerRow: View { var player: Player + @Binding var players: [Player] var body: some View { HStack { + Image(systemName: "custom.person") + .foregroundColor(.black) + VStack(alignment: .leading) { Text(player.name) .font(.headline) @@ -27,7 +31,7 @@ struct PlayerRow: View { .foregroundColor(.black) } Button(action: { - // Action pour supprimer le joueur + }) { Image(systemName: "trash") .foregroundColor(.red) diff --git a/DouShouQi_App/DouShouQi_App/Views/Player/PlayersView.swift b/DouShouQi_App/DouShouQi_App/Views/Player/PlayersView.swift index 8f27145..5dfea67 100644 --- a/DouShouQi_App/DouShouQi_App/Views/Player/PlayersView.swift +++ b/DouShouQi_App/DouShouQi_App/Views/Player/PlayersView.swift @@ -26,6 +26,8 @@ struct PlayersView: View { ] @State private var searchText = "" + @State private var showingPopup = false + @State private var newPlayerName = "" var filteredPlayers: [Player] { if searchText.isEmpty { @@ -50,14 +52,14 @@ struct PlayersView: View { ForEach(groupedPlayers.keys.sorted(), id: \.self) { key in Section(header: Text(key)) { ForEach(groupedPlayers[key]!) { player in - PlayerRow(player: player) + PlayerRow(player: player, players: $players) } } } } - + Button(action: { - + showingPopup = true }) { Text("Add a player") .font(.headline) @@ -69,6 +71,9 @@ struct PlayersView: View { .padding(.horizontal) } .padding(.bottom) + .sheet(isPresented: $showingPopup) { + AddPlayerView(isPresented: $showingPopup, players: $players) + } } } } @@ -80,38 +85,38 @@ struct PlayersView: View { } } - // Vue pour la barre de recherche struct SearchBar: UIViewRepresentable { @Binding var text: String - + class Coordinator: NSObject, UISearchBarDelegate { @Binding var text: String - + init(text: Binding) { _text = text } - + func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { text = searchText } } - + func makeCoordinator() -> Coordinator { return Coordinator(text: $text) } - + func makeUIView(context: Context) -> UISearchBar { let searchBar = UISearchBar(frame: .zero) searchBar.delegate = context.coordinator return searchBar } - + func updateUIView(_ uiView: UISearchBar, context: Context) { uiView.text = text } } + struct PlayersPageView_Previews: PreviewProvider { static var previews: some View { PlayersView()