|
| 1 | +/// |
| 2 | +/// GKTurnBasedMatchmakerView.swift |
| 3 | +/// VocaBurn |
| 4 | +/// |
| 5 | +/// Created by Sascha Müllner on 22.11.20. |
| 6 | +/// Unauthorized copying or usage of this file, via any medium is strictly prohibited. |
| 7 | +/// Proprietary and confidential. |
| 8 | +/// Copyright © 2020 Webblazer EG. All rights reserved. |
| 9 | + |
| 10 | +import Foundation |
| 11 | +import GameKit |
| 12 | +import SwiftUI |
| 13 | + |
| 14 | +public struct GKTurnBasedMatchmakerView: UIViewControllerRepresentable { |
| 15 | + |
| 16 | + private let matchRequest: GKMatchRequest |
| 17 | + private let canceled: () -> Void |
| 18 | + private let failed: (Error) -> Void |
| 19 | + private let started: (GKTurnBasedMatch) -> Void |
| 20 | + |
| 21 | + public init(matchRequest: GKMatchRequest, |
| 22 | + canceled: @escaping () -> Void, |
| 23 | + failed: @escaping (Error) -> Void, |
| 24 | + started: @escaping (GKTurnBasedMatch) -> Void) { |
| 25 | + self.matchRequest = matchRequest |
| 26 | + self.canceled = canceled |
| 27 | + self.failed = failed |
| 28 | + self.started = started |
| 29 | + } |
| 30 | + |
| 31 | + public init(minPlayers: Int, |
| 32 | + maxPlayers: Int, |
| 33 | + inviteMessage: String, |
| 34 | + canceled: @escaping () -> Void, |
| 35 | + failed: @escaping (Error) -> Void, |
| 36 | + started: @escaping (GKTurnBasedMatch) -> Void) { |
| 37 | + let matchRequest = GKMatchRequest() |
| 38 | + matchRequest.minPlayers = minPlayers |
| 39 | + matchRequest.maxPlayers = maxPlayers |
| 40 | + matchRequest.inviteMessage = inviteMessage |
| 41 | + self.matchRequest = matchRequest |
| 42 | + self.canceled = canceled |
| 43 | + self.failed = failed |
| 44 | + self.started = started |
| 45 | + } |
| 46 | + |
| 47 | + public func makeUIViewController( |
| 48 | + context: UIViewControllerRepresentableContext<GKTurnBasedMatchmakerView>) -> TurnBasedMatchmakerViewController { |
| 49 | + return TurnBasedMatchmakerViewController( |
| 50 | + matchRequest: self.matchRequest) { |
| 51 | + self.canceled() |
| 52 | + } failed: { (error) in |
| 53 | + self.failed(error) |
| 54 | + } started: { (match) in |
| 55 | + self.started(match) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public func updateUIViewController( |
| 60 | + _ uiViewController: TurnBasedMatchmakerViewController, |
| 61 | + context: UIViewControllerRepresentableContext<GKTurnBasedMatchmakerView>) { |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +public class TurnBasedMatchmakerViewController: UIViewController, GKTurnBasedMatchmakerViewControllerDelegate, GKMatchDelegate { |
| 66 | + |
| 67 | + private let matchRequest: GKMatchRequest |
| 68 | + private let canceled: () -> Void |
| 69 | + private let failed: (Error) -> Void |
| 70 | + private let started: (GKTurnBasedMatch) -> Void |
| 71 | + |
| 72 | + public init(matchRequest: GKMatchRequest, |
| 73 | + canceled: @escaping () -> Void, |
| 74 | + failed: @escaping (Error) -> Void, |
| 75 | + started: @escaping (GKTurnBasedMatch) -> Void) { |
| 76 | + self.matchRequest = matchRequest |
| 77 | + self.canceled = canceled |
| 78 | + self.failed = failed |
| 79 | + self.started = started |
| 80 | + super.init(nibName: nil, bundle: nil) |
| 81 | + } |
| 82 | + |
| 83 | + required init?(coder: NSCoder) { |
| 84 | + fatalError("init(coder:) has not been implemented") |
| 85 | + } |
| 86 | + |
| 87 | + public override func viewWillAppear(_ animated: Bool) { |
| 88 | + super.viewWillAppear(animated) |
| 89 | + let viewController = GKTurnBasedMatchmakerViewController(matchRequest: self.matchRequest) |
| 90 | + viewController.turnBasedMatchmakerDelegate = self |
| 91 | + self.addChild(viewController) |
| 92 | + viewController.view.translatesAutoresizingMaskIntoConstraints = false |
| 93 | + self.view.addSubview(viewController.view) |
| 94 | + NSLayoutConstraint.activate([ |
| 95 | + viewController.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 0), |
| 96 | + viewController.view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: 0), |
| 97 | + viewController.view.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0), |
| 98 | + viewController.view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0) |
| 99 | + ]) |
| 100 | + } |
| 101 | + |
| 102 | + public func turnBasedMatchmakerViewControllerWasCancelled(_ viewController: GKTurnBasedMatchmakerViewController) { |
| 103 | + viewController.dismiss(animated: true, completion: nil) |
| 104 | + self.canceled() |
| 105 | + } |
| 106 | + |
| 107 | + public func turnBasedMatchmakerViewController(_ viewController: GKTurnBasedMatchmakerViewController, didFailWithError error: Error) { |
| 108 | + viewController.dismiss(animated: true, completion: nil) |
| 109 | + self.failed(error) |
| 110 | + } |
| 111 | + |
| 112 | + public func turnBasedMatchmakerViewController(_ viewController: GKTurnBasedMatchmakerViewController, didFind match: GKTurnBasedMatch) { |
| 113 | + viewController.dismiss(animated: true, completion: nil) |
| 114 | + self.started(match) |
| 115 | + } |
| 116 | +} |
0 commit comments