-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessagesViewController.swift
More file actions
150 lines (107 loc) · 5.17 KB
/
MessagesViewController.swift
File metadata and controls
150 lines (107 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// MessagesViewController.swift
// MessagesExtension
//
// Created by Thang Le Tan on 10/24/16.
// Copyright © 2016 Thang Le Tan. All rights reserved.
//
import UIKit
import Messages
import SpriteKit
import GameplayKit
class MessagesViewController: MSMessagesAppViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func playTapped(_ sender: AnyObject) {
requestPresentationStyle(.expanded)
}
// MARK: - Conversation Handling
override func willBecomeActive(with conversation: MSConversation) {
if presentationStyle == .expanded {
displayGameController(conversation: conversation, identifier: "CaroGame")
}
}
func displayGameController(conversation: MSConversation?, identifier: String) {
guard let vc = storyboard?.instantiateViewController(withIdentifier: identifier) as? GameViewController else {
return
}
vc.load(message: conversation?.selectedMessage)
vc.delegate = self
addChildViewController(vc)
// make child vc fill our view
vc.view.frame = view.bounds
vc.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(vc.view)
vc.view.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
vc.view.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
vc.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
vc.view.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
//tell the child it has moved to a new parent controller
vc.didMove(toParentViewController: self)
}
func createMessage(rowsString: String, player: Int, row: Int, col: Int) {
// return the extension to compact mode
requestPresentationStyle(.compact)
//make sure we have a conversation to work with
guard let conversation = activeConversation else { return }
var component = URLComponents()
var items = [URLQueryItem]()
var item = URLQueryItem(name: "rows", value: rowsString)
items.append(item)
item = URLQueryItem(name: "player", value: String(player))
items.append(item)
item = URLQueryItem(name: "row", value: String(row))
items.append(item)
item = URLQueryItem(name: "col", value: String(col))
items.append(item)
component.queryItems = items
//use existing session or create new one
let session = conversation.selectedMessage?.session ?? MSSession()
//create new message from session and url
let message = MSMessage(session: session)
message.url = component.url
//create a blank, default message layout
let layout = MSMessageTemplateLayout()
message.layout = layout
conversation.insert(message) { (error) in
if let error = error {
print("There was an error inserting message into conversation: \(error)")
}
}
}
override func didResignActive(with conversation: MSConversation) {
// Called when the extension is about to move from the active to inactive state.
// This will happen when the user dissmises the extension, changes to a different
// conversation or quits Messages.
// Use this method to release shared resources, save user data, invalidate timers,
// and store enough state information to restore your extension to its current state
// in case it is terminated later.
}
override func didReceive(_ message: MSMessage, conversation: MSConversation) {
// Called when a message arrives that was generated by another instance of this
// extension on a remote device.
// Use this method to trigger UI updates in response to the message.
}
override func didStartSending(_ message: MSMessage, conversation: MSConversation) {
// Called when the user taps the send button.
}
override func didCancelSending(_ message: MSMessage, conversation: MSConversation) {
// Called when the user deletes the message without sending it.
// Use this to clean up state related to the deleted message.
}
override func willTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
for child in childViewControllers {
child.willMove(toParentViewController: nil)
child.view.removeFromSuperview()
child.removeFromParentViewController()
}
if presentationStyle == .expanded {
displayGameController(conversation: activeConversation, identifier: "CaroGame")
}
}
override func didTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
// Called after the extension transitions to a new presentation style.
// Use this method to finalize any behaviors associated with the change in presentation style.
}
}