Skip to content

Commit 8abeb02

Browse files
committed
shifted match to main screen
1 parent 8e49c48 commit 8abeb02

6 files changed

Lines changed: 34 additions & 30 deletions

File tree

Examples/GKMatchMaker/Shared/ViewModels/GKMatchMakerAppModel.swift

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,21 @@ class GKMatchMakerAppModel: ObservableObject {
1919
@Published public var showAuthentication = false
2020
@Published public var showInvite = false
2121
@Published public var showMatch = false
22-
@Published public var invite: Invite = Invite.zero
22+
@Published public var invite: Invite = Invite.zero {
23+
didSet {
24+
self.showInvite = invite.gkInvite != nil
25+
self.showAuthentication = invite.needsToAuthenticate ?? false
26+
}
27+
}
2328
@Published public var match: GKMatch? {
2429
didSet {
2530
self.showInvite = false
2631
self.showMatch = true
2732
}
2833
}
2934

30-
private var cancellable: AnyCancellable?
35+
private var cancellableInvite: AnyCancellable?
36+
private var cancellableMatch: AnyCancellable?
3137

3238
public init() {
3339
self.subscribe()
@@ -46,18 +52,23 @@ class GKMatchMakerAppModel: ObservableObject {
4652
}
4753

4854
func subscribe() {
49-
self.cancellable = GKMatchManager
55+
self.cancellableInvite = GKMatchManager
5056
.shared
5157
.invite
5258
.sink { (invite) in
5359
self.invite = invite
54-
self.showInvite = invite.gkInvite != nil
55-
self.showAuthentication = invite.needsToAuthenticate ?? false
60+
}
61+
self.cancellableMatch = GKMatchManager
62+
.shared
63+
.match
64+
.sink { (match) in
65+
self.match = match.gkMatch
5666
}
5767
}
5868

5969
func unsubscribe() {
60-
self.cancellable?.cancel()
70+
self.cancellableInvite?.cancel()
71+
self.cancellableMatch?.cancel()
6172
}
6273

6374
public func showAlert(title: String, message: String) {

Examples/GKMatchMaker/Shared/ViewModels/MultiPlayer/MatchMakingViewModel.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,9 @@ class MatchMakingViewModel: ObservableObject {
1313

1414
@Published public var showModal = false
1515
@Published public var showAlert = false
16-
@Published public var showMatch = false
1716
@Published public var alertTitle: String = ""
1817
@Published public var alertMessage: String = ""
1918
@Published public var currentState: String = "Loading GameKit..."
20-
@Published public var match: GKMatch? {
21-
didSet {
22-
self.showMatch = self.match != nil
23-
}
24-
}
2519

2620
public init() {
2721
}

Examples/GKMatchMaker/Shared/Views/MultiPlayer/MatchMakingView.swift

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,14 @@ struct MatchMakingView: View {
1515
ZStack {
1616
Color("BackgroundColor").ignoresSafeArea()
1717
VStack() {
18-
if self.viewModel.showMatch,
19-
let match = self.viewModel.match {
20-
MatchView(match)
21-
} else {
22-
Text(self.viewModel.currentState)
23-
.font(.body)
24-
.padding(8)
25-
Button() {
26-
self.viewModel.showMatchMakerModal()
27-
} label: {
28-
Text("Create Match")
29-
.primaryButtonStyle()
30-
}
18+
Text(self.viewModel.currentState)
19+
.font(.body)
20+
.padding(8)
21+
Button() {
22+
self.viewModel.showMatchMakerModal()
23+
} label: {
24+
Text("Create Match")
25+
.primaryButtonStyle()
3126
}
3227
}
3328
.navigationBarTitle(Text("GameKit Matchmaker"))
@@ -49,7 +44,6 @@ struct MatchMakingView: View {
4944
self.viewModel.showAlert(title: "Match Making Failed", message: error.localizedDescription)
5045
} started: { (match) in
5146
self.viewModel.showModal = false
52-
self.viewModel.match = match
5347
}
5448
}
5549
.alert(isPresented: self.$viewModel.showAlert) {

Sources/GameKitUI/InviteAndMatchManager/GKMatchManager.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public final class GKMatchManager: NSObject {
8080

8181
guard GKLocalPlayer.local.isAuthenticated,
8282
let matchmakerViewController = GKMatchmakerViewController(invite: invite) else {
83+
GKMatchmaker.shared().cancel()
8384
canceled()
8485
return nil
8586
}
@@ -91,6 +92,7 @@ public final class GKMatchManager: NSObject {
9192
public func createMatchmaker(invite: GKInvite) -> GKMatchmakerViewController? {
9293
guard GKLocalPlayer.local.isAuthenticated,
9394
let matchmakerViewController = GKMatchmakerViewController(invite: invite) else {
95+
GKMatchmaker.shared().cancel()
9496
return nil
9597
}
9698

@@ -105,16 +107,20 @@ public final class GKMatchManager: NSObject {
105107
self.canceled = canceled
106108
self.failed = failed
107109
self.started = started
108-
109110
guard GKLocalPlayer.local.isAuthenticated,
110111
let matchmakerViewController = GKMatchmakerViewController(matchRequest: request) else {
112+
GKMatchmaker.shared().cancel()
111113
canceled()
112114
return nil
113115
}
114116

115117
matchmakerViewController.matchmakerDelegate = self
116118
return matchmakerViewController
117119
}
120+
121+
public func cancel() {
122+
GKMatchmaker.shared().cancel()
123+
}
118124
}
119125

120126
extension GKMatchManager: GKMatchmakerViewControllerDelegate {
@@ -123,7 +129,7 @@ extension GKMatchManager: GKMatchmakerViewControllerDelegate {
123129
viewController.dismiss(
124130
animated: true,
125131
completion: {
126-
os_log("Found match!", log: OSLog.matchmaking, type: .info)
132+
os_log("Matchmaking successful!", log: OSLog.matchmaking, type: .info)
127133
self.match.send(Match(gkMatch: match))
128134
self.started(match)
129135
viewController.remove()

Sources/GameKitUI/InviteAndMatchManager/Match.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import GameKit
2727

2828
public struct Match {
29-
var gkMatch: GKMatch?
29+
public private(set) var gkMatch: GKMatch?
3030
}
3131

3232
extension Match {

Sources/GameKitUI/Matchmaker/MatchmakerViewController+iOS.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public class MatchmakerViewController: UIViewController, GKMatchDelegate, GKLoca
7979

8080
guard let invite = invite.gkInvite else { return }
8181

82-
// self.removeAll()
8382
if let viewController = GKMatchManager.shared.createInvite(invite: invite,
8483
canceled: self.canceled,
8584
failed: self.failed,

0 commit comments

Comments
 (0)