diff --git a/DJSystemiOS/Info.plist b/DJSystemiOS/Info.plist index ad5b139..fa815e3 100644 --- a/DJSystemiOS/Info.plist +++ b/DJSystemiOS/Info.plist @@ -24,6 +24,8 @@ $(ENVIRONMENT) LSApplicationCategoryType public.app-category.entertainment + NSCameraUsageDescription + このアプリはカメラへのアクセスを求めています NSHumanReadableCopyright LiT Kansai Mentors UIApplicationSceneManifest diff --git a/DJSystemiOS/Pages/QRReaderPage/QRReaderViewController.swift b/DJSystemiOS/Pages/QRReaderPage/QRReaderViewController.swift index d031914..1865464 100644 --- a/DJSystemiOS/Pages/QRReaderPage/QRReaderViewController.swift +++ b/DJSystemiOS/Pages/QRReaderPage/QRReaderViewController.swift @@ -1,3 +1,4 @@ +import AVFoundation import QRScanner import UIKit @@ -5,8 +6,70 @@ final class QRReaderViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() + setupQRScanner() } -// private func extractRoomID() { + private func setupQRScanner() { + switch AVCaptureDevice.authorizationStatus(for: .video) { + case .authorized: + setupQRScannerView() + case .notDetermined: + AVCaptureDevice.requestAccess(for: .video) { [weak self] granted in + if granted { + DispatchQueue.main.async { [weak self] in + self?.setupQRScannerView() + } + } + } + default: + showAlert() + } + } + + private func setupQRScannerView() { + let qrScannerView = QRScannerView(frame: view.bounds) + view.addSubview(qrScannerView) + qrScannerView.configure(delegate: self, input: .init(isBlurEffectEnabled: true)) + qrScannerView.startRunning() + } + private func showAlert() { + DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in + let alert = UIAlertController(title: "エラー", message: "このアプリを使うにはカメラへのアクセスを許可する必要があります。", preferredStyle: .alert) + alert.addAction(.init(title: "OK", style: .default)) + self?.present(alert, animated: true) + } + } +} + +extension QRReaderViewController: QRScannerViewDelegate { + func qrScannerView(_ qrScannerView: QRScannerView, didFailure error: QRScannerError) { + let alert = UIAlertController(title: "エラー", message: "このQRコードは無効です", preferredStyle: .alert) + alert.addAction(.init(title: "OK", style: .default)) + self.present(alert, animated: true) + } + + func qrScannerView(_ qrScannerView: QRScannerView, didSuccess code: String) { + let extractedRoomID = URL.extractRoomID(inputURL: code) + switch extractedRoomID { + case .success(let roomID): + Task{ + let client = APIClient(baseURL: AppConfig().BaseAPIURL) + let result = await client.get(from: .getRoom(roomId: roomID), dataType: Room.API.GetRoomResponse.self) + switch result { + case .success(let response): + let roomOverViewPageView = RoomOverviewViewController(roomAPI: Room.API(), roomOverview: RoomOverview(id: response.id, name: response.name, description: response.description)) + navigationController?.pushViewController(roomOverViewPageView, animated: true) + case .failure: + let alert = UIAlertController(title: "エラー", message: "ルームが見つかりませんでした。", preferredStyle: .alert) + alert.addAction(.init(title: "OK", style: .default)) + self.present(alert, animated: true) + } + } + case .failure: + let alert = UIAlertController(title: "エラー", message: "このURLに対応するルームが見つかりません。", preferredStyle: .alert) + alert.addAction(.init(title: "OK", style: .default)) + self.present(alert, animated: true) + } + } }