Skip to content

Commit

Permalink
Merge pull request #87 from lit-kansai/feature-readQRcode
Browse files Browse the repository at this point in the history
Feature read q rcode
  • Loading branch information
txxxxc authored Aug 13, 2023
2 parents 3716a30 + 78503b4 commit 9ad8b24
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
2 changes: 2 additions & 0 deletions DJSystemiOS/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<string>$(ENVIRONMENT)</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.entertainment</string>
<key>NSCameraUsageDescription</key>
<string>このアプリはカメラへのアクセスを求めています</string>
<key>NSHumanReadableCopyright</key>
<string>LiT Kansai Mentors</string>
<key>UIApplicationSceneManifest</key>
Expand Down
65 changes: 64 additions & 1 deletion DJSystemiOS/Pages/QRReaderPage/QRReaderViewController.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,75 @@
import AVFoundation
import QRScanner
import UIKit

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)
}
}
}

0 comments on commit 9ad8b24

Please sign in to comment.