diff --git a/PadelClub/Views/Match/EditSharingView.swift b/PadelClub/Views/Match/EditSharingView.swift index b4d2bfa..12a57e2 100644 --- a/PadelClub/Views/Match/EditSharingView.swift +++ b/PadelClub/Views/Match/EditSharingView.swift @@ -5,10 +5,11 @@ // Created by Razmig Sarkissian on 03/02/2024. // -import SwiftUI -import TipKit import CoreTransferable import PadelClubData +import SwiftUI +import TipKit +import AVFoundation struct EditSharingView: View { var match: Match @@ -17,50 +18,61 @@ struct EditSharingView: View { @State private var showCamera: Bool = false @State private var newImage: UIImage? @State private var copied: Bool = false + @State private var cameraAuthorizationStatus: AVAuthorizationStatus = .notDetermined + @State private var showCameraAccessAlert: Bool = false var shareMessage: String { shareMessage(displayRank: displayRank, displayTeamName: displayTeamTitle) } - + func shareMessage(displayRank: Bool, displayTeamName: Bool) -> String { var messageData: [String] = [] - + if match.hasEnded() == false { var locAndTime: String? if let courtName = match.courtName() { locAndTime = "\(courtName)" } - + if let startDate = match.startDate { - locAndTime = [locAndTime, startDate.formattedAsHourMinute()].compactMap({ $0 }).joined(separator: " à ") + locAndTime = [locAndTime, startDate.formattedAsHourMinute()].compactMap({ $0 }) + .joined(separator: " à ") } - + if let locAndTime, locAndTime.isEmpty == false { messageData.append(locAndTime) } } - + if let tournament = match.currentTournament() { messageData.append(tournament.tournamentTitle()) } - - let message = [match.isLoserBracket ? "Classement" : nil, match.roundTitle(), match.isLoserBracket ? nil : ((match.index > 0 || match.isGroupStage()) ? match.matchTitle(.short) : nil)].compactMap({ $0 }).joined(separator: " ") + + let message = [ + match.isLoserBracket ? "Classement" : nil, match.roundTitle(), + match.isLoserBracket + ? nil + : ((match.index > 0 || match.isGroupStage()) ? match.matchTitle(.short) : nil), + ].compactMap({ $0 }).joined(separator: " ") messageData.append(message) - - guard let labelOne = match.team(.one)?.teamLabelRanked(displayRank: displayRank, displayTeamName: displayTeamName), let labelTwo = match.team(.two)?.teamLabelRanked(displayRank: displayRank, displayTeamName: displayTeamName) else { + + guard + let labelOne = match.team(.one)?.teamLabelRanked( + displayRank: displayRank, displayTeamName: displayTeamName), + let labelTwo = match.team(.two)?.teamLabelRanked( + displayRank: displayRank, displayTeamName: displayTeamName) + else { return messageData.joined(separator: "\n") } - + let players = "\(labelOne)\ncontre\n\(labelTwo)" messageData.append(players) - + messageData.append(match.scoreLabel()) return messageData.joined(separator: "\n") } - - var body: some View { List { @@ -70,7 +82,7 @@ struct EditSharingView: View { TipView(tip) .tipStyle(tint: .green) } - + Section { ZStack { Color.black @@ -93,16 +105,16 @@ struct EditSharingView: View { } else { Section { RowButtonView("Prendre une photo", systemImage: "camera") { - showCamera = true + checkCameraAuthorization() } - } + } } - + Section { Toggle(isOn: $displayRank) { Text("Afficher leurs rangs dans ce tournoi") } - + Toggle(isOn: $displayTeamTitle) { Text("Afficher plutôt le nom de l'équipe") } @@ -124,23 +136,40 @@ struct EditSharingView: View { .toolbar { ToolbarItem(placement: .topBarTrailing) { if let newImage { - let photo = Photo(image: Image(uiImage:newImage), caption: shareMessage) + let photo = Photo(image: Image(uiImage: newImage), caption: shareMessage) ShareLink( item: photo, preview: SharePreview( photo.caption, - image: photo.image)) { - Text("Partager") - } - .onAppear { - UIPasteboard.general.string = shareMessage - copied = true - } + image: photo.image) + ) { + Text("Partager") + } + .onAppear { + UIPasteboard.general.string = shareMessage + copied = true + } } else { ShareLink("Partager", item: shareMessage) } } } + .onChange(of: displayTeamTitle) { + copied = false + } + .alert("Accès à l'appareil photo requis", isPresented: $showCameraAccessAlert) { + Button("Annuler", role: .cancel) {} + Button("Paramètres") { + if let settingsURL = URL(string: UIApplication.openSettingsURLString) { + UIApplication.shared.open(settingsURL) + } + } + } message: { + Text( + "Pour prendre des photos, autorisez l'accès à l'appareil photo dans les paramètres de l'application." + ) + } + .navigationTitle("Préparation") .navigationBarTitleDisplayMode(.inline) .toolbarBackground(.visible, for: .navigationBar) @@ -155,6 +184,34 @@ struct EditSharingView: View { copied = false } } + + func checkCameraAuthorization() { + let status = AVCaptureDevice.authorizationStatus(for: .video) + self.cameraAuthorizationStatus = status + + switch status { + case .authorized: + // Camera access already granted, show camera + self.showCamera = true + case .notDetermined: + // Request camera access + AVCaptureDevice.requestAccess(for: .video) { granted in + DispatchQueue.main.async { + if granted { + self.showCamera = true + } else { + self.showCameraAccessAlert = true + } + } + } + case .denied, .restricted: + // Camera access was previously denied or restricted + self.showCameraAccessAlert = true + @unknown default: + // Handle future cases + self.showCameraAccessAlert = true + } + } } struct Photo: Transferable {