Skip to content

Commit 769ec24

Browse files
committed
Lint issues manual fixes.
1 parent 5fc407d commit 769ec24

File tree

3 files changed

+146
-98
lines changed

3 files changed

+146
-98
lines changed

Example/AblyChatExample/ContentView.swift

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ struct ContentView: View {
2424
@State private var occupancyInfo = "Connections: 0"
2525
@State private var statusInfo = ""
2626

27-
private func room() async -> Room {
28-
try! await chatClient.rooms.get(roomID: "Demo", options: .init())
27+
private func room() async throws -> Room {
28+
try await chatClient.rooms.get(roomID: "Demo", options: .init())
2929
}
3030

3131
private var sendTitle: String {
@@ -57,17 +57,7 @@ struct ContentView: View {
5757
#if !os(tvOS)
5858
.textFieldStyle(RoundedBorderTextFieldStyle())
5959
#endif
60-
Button(action: {
61-
if newMessage.isEmpty {
62-
Task {
63-
try await sendReaction(type: ReactionType.like.rawValue)
64-
}
65-
} else {
66-
Task {
67-
try await sendMessage()
68-
}
69-
}
70-
}) {
60+
Button(action: sendButtonAction) {
7161
#if os(iOS)
7262
Text(sendTitle)
7363
.foregroundColor(.white)
@@ -108,45 +98,57 @@ struct ContentView: View {
10898
}
10999
}
110100
}
111-
.task { await showMessages() }
112-
.task { await showReactions() }
113-
.task { await showPresence() }
114-
.task { await showTypings() }
115-
.task { await showOccupancy() }
116-
.task { await showRoomStatus() }
117-
.task { await setDefaultTitle() }
101+
.tryTask { try await setDefaultTitle() }
102+
.tryTask { try await showMessages() }
103+
.tryTask { try await showReactions() }
104+
.tryTask { try await showPresence() }
105+
.tryTask { try await showTypings() }
106+
.tryTask { try await showOccupancy() }
107+
.tryTask { try await showRoomStatus() }
108+
}
109+
110+
func sendButtonAction() {
111+
if newMessage.isEmpty {
112+
Task {
113+
try await sendReaction(type: ReactionType.like.rawValue)
114+
}
115+
} else {
116+
Task {
117+
try await sendMessage()
118+
}
119+
}
118120
}
119121

120-
func setDefaultTitle() async {
121-
title = await "\(room().roomID)"
122+
func setDefaultTitle() async throws {
123+
title = try await "\(room().roomID)"
122124
}
123125

124-
func showMessages() async {
125-
for await message in await room().messages.subscribe(bufferingPolicy: .unbounded) {
126+
func showMessages() async throws {
127+
for await message in try await room().messages.subscribe(bufferingPolicy: .unbounded) {
126128
withAnimation {
127129
messages.insert(BasicListItem(id: message.timeserial, title: message.clientID, text: message.text), at: 0)
128130
}
129131
}
130132
}
131133

132-
func showReactions() async {
133-
for await reaction in await room().reactions.subscribe(bufferingPolicy: .unbounded) {
134+
func showReactions() async throws {
135+
for await reaction in try await room().reactions.subscribe(bufferingPolicy: .unbounded) {
134136
withAnimation {
135137
showReaction(reaction.displayedText)
136138
}
137139
}
138140
}
139141

140-
func showPresence() async {
141-
for await event in await room().presence.subscribe(events: [.enter, .leave]) {
142+
func showPresence() async throws {
143+
for await event in try await room().presence.subscribe(events: [.enter, .leave]) {
142144
withAnimation {
143145
messages.insert(BasicListItem(id: UUID().uuidString, title: "System", text: event.clientID + " \(event.action.displayedText)"), at: 0)
144146
}
145147
}
146148
}
147149

148-
func showTypings() async {
149-
for await typing in await room().typing.subscribe(bufferingPolicy: .unbounded) {
150+
func showTypings() async throws {
151+
for await typing in try await room().typing.subscribe(bufferingPolicy: .unbounded) {
150152
withAnimation {
151153
typingInfo = "Typing: \(typing.currentlyTyping.joined(separator: ", "))..."
152154
Task {
@@ -159,16 +161,16 @@ struct ContentView: View {
159161
}
160162
}
161163

162-
func showOccupancy() async {
163-
for await event in await room().occupancy.subscribe(bufferingPolicy: .unbounded) {
164+
func showOccupancy() async throws {
165+
for await event in try await room().occupancy.subscribe(bufferingPolicy: .unbounded) {
164166
withAnimation {
165167
occupancyInfo = "Connections: \(event.presenceMembers) (\(event.connections))"
166168
}
167169
}
168170
}
169171

170-
func showRoomStatus() async {
171-
for await status in await room().status.onChange(bufferingPolicy: .unbounded) {
172+
func showRoomStatus() async throws {
173+
for await status in try await room().status.onChange(bufferingPolicy: .unbounded) {
172174
withAnimation {
173175
if status.current == .attaching {
174176
statusInfo = "\(status.current)...".capitalized
@@ -188,7 +190,9 @@ struct ContentView: View {
188190
}
189191

190192
func sendMessage() async throws {
191-
guard !newMessage.isEmpty else { return }
193+
guard !newMessage.isEmpty else {
194+
return
195+
}
192196
_ = try await room().messages.send(params: .init(text: newMessage))
193197
newMessage = ""
194198
}
@@ -311,3 +315,15 @@ extension PresenceEventType {
311315
}
312316
}
313317
}
318+
319+
extension View {
320+
nonisolated func tryTask(priority: TaskPriority = .userInitiated, _ action: @escaping @Sendable () async throws -> Void) -> some View {
321+
task(priority: priority) {
322+
do {
323+
try await action()
324+
} catch {
325+
print("Action can't be performed: \(error)") // TODO: replace with logger (+ message to the user?)
326+
}
327+
}
328+
}
329+
}

Example/AblyChatExample/Mocks/Misc.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ final class MockMessagesPaginatedResult: PaginatedResult {
1010

1111
var items: [T] {
1212
Array(repeating: 0, count: numberOfMockMessages).map { _ in
13-
Message(timeserial: "\(Date().timeIntervalSince1970)",
14-
clientID: self.clientID,
15-
roomID: self.roomID,
16-
text: MockStrings.randomPhrase(),
17-
createdAt: Date(),
18-
metadata: [:],
19-
headers: [:])
13+
Message(
14+
timeserial: "\(Date().timeIntervalSince1970)",
15+
clientID: self.clientID,
16+
roomID: self.roomID,
17+
text: MockStrings.randomPhrase(),
18+
createdAt: Date(),
19+
metadata: [:],
20+
headers: [:]
21+
)
2022
}
2123
}
2224

Example/AblyChatExample/Mocks/MockClients.swift

Lines changed: 86 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,15 @@ actor MockMessages: Messages {
9090

9191
private func createSubscription() {
9292
mockSubscription = MockSubscription<Message>(randomElement: {
93-
Message(timeserial: "\(Date().timeIntervalSince1970)",
94-
clientID: MockStrings.names.randomElement()!,
95-
roomID: self.roomID,
96-
text: MockStrings.randomPhrase(),
97-
createdAt: Date(),
98-
metadata: [:],
99-
headers: [:])
93+
Message(
94+
timeserial: "\(Date().timeIntervalSince1970)",
95+
clientID: MockStrings.names.randomElement()!,
96+
roomID: self.roomID,
97+
text: MockStrings.randomPhrase(),
98+
createdAt: Date(),
99+
metadata: [:],
100+
headers: [:]
101+
)
100102
}, interval: 3)
101103
}
102104

@@ -115,13 +117,15 @@ actor MockMessages: Messages {
115117
guard let mockSubscription else {
116118
fatalError("Call `subscribe` first.")
117119
}
118-
let message = Message(timeserial: "\(Date().timeIntervalSince1970)",
119-
clientID: clientID,
120-
roomID: roomID,
121-
text: params.text,
122-
createdAt: Date(),
123-
metadata: params.metadata ?? [:],
124-
headers: params.headers ?? [:])
120+
let message = Message(
121+
timeserial: "\(Date().timeIntervalSince1970)",
122+
clientID: clientID,
123+
roomID: roomID,
124+
text: params.text,
125+
createdAt: Date(),
126+
metadata: params.metadata ?? [:],
127+
headers: params.headers ?? [:]
128+
)
125129
mockSubscription.emit(message)
126130
return message
127131
}
@@ -146,25 +150,29 @@ actor MockRoomReactions: RoomReactions {
146150

147151
private func createSubscription() {
148152
mockSubscription = MockSubscription<Reaction>(randomElement: {
149-
Reaction(type: ReactionType.allCases.randomElement()!.rawValue,
150-
metadata: [:],
151-
headers: [:],
152-
createdAt: Date(),
153-
clientID: self.clientID,
154-
isSelf: false)
153+
Reaction(
154+
type: ReactionType.allCases.randomElement()!.rawValue,
155+
metadata: [:],
156+
headers: [:],
157+
createdAt: Date(),
158+
clientID: self.clientID,
159+
isSelf: false
160+
)
155161
}, interval: Double.random(in: 0.1 ... 0.5))
156162
}
157163

158164
func send(params: SendReactionParams) async throws {
159165
guard let mockSubscription else {
160166
fatalError("Call `subscribe` first.")
161167
}
162-
let reaction = Reaction(type: params.type,
163-
metadata: [:],
164-
headers: [:],
165-
createdAt: Date(),
166-
clientID: clientID,
167-
isSelf: false)
168+
let reaction = Reaction(
169+
type: params.type,
170+
metadata: [:],
171+
headers: [:],
172+
createdAt: Date(),
173+
clientID: clientID,
174+
isSelf: false
175+
)
168176
mockSubscription.emit(reaction)
169177
}
170178

@@ -241,30 +249,36 @@ actor MockPresence: Presence {
241249

242250
private func createSubscription() {
243251
mockSubscription = MockSubscription<PresenceEvent>(randomElement: {
244-
PresenceEvent(action: [.enter, .leave].randomElement()!,
245-
clientID: MockStrings.names.randomElement()!,
246-
timestamp: Date(),
247-
data: nil)
252+
PresenceEvent(
253+
action: [.enter, .leave].randomElement()!,
254+
clientID: MockStrings.names.randomElement()!,
255+
timestamp: Date(),
256+
data: nil
257+
)
248258
}, interval: 5)
249259
}
250260

251261
func get() async throws -> [PresenceMember] {
252262
MockStrings.names.map { name in
253-
PresenceMember(clientID: name,
254-
data: ["foo": "bar"],
255-
action: .present,
256-
extras: nil,
257-
updatedAt: Date())
263+
PresenceMember(
264+
clientID: name,
265+
data: ["foo": "bar"],
266+
action: .present,
267+
extras: nil,
268+
updatedAt: Date()
269+
)
258270
}
259271
}
260272

261273
func get(params _: ARTRealtimePresenceQuery?) async throws -> [PresenceMember] {
262274
MockStrings.names.map { name in
263-
PresenceMember(clientID: name,
264-
data: ["foo": "bar"],
265-
action: .present,
266-
extras: nil,
267-
updatedAt: Date())
275+
PresenceMember(
276+
clientID: name,
277+
data: ["foo": "bar"],
278+
action: .present,
279+
extras: nil,
280+
updatedAt: Date()
281+
)
268282
}
269283
}
270284

@@ -276,20 +290,28 @@ actor MockPresence: Presence {
276290
guard let mockSubscription else {
277291
fatalError("Call `subscribe` first.")
278292
}
279-
mockSubscription.emit(PresenceEvent(action: .enter,
280-
clientID: clientID,
281-
timestamp: Date(),
282-
data: nil))
293+
mockSubscription.emit(
294+
PresenceEvent(
295+
action: .enter,
296+
clientID: clientID,
297+
timestamp: Date(),
298+
data: nil
299+
)
300+
)
283301
}
284302

285303
func enter(data: PresenceData) async throws {
286304
guard let mockSubscription else {
287305
fatalError("Call `subscribe` first.")
288306
}
289-
mockSubscription.emit(PresenceEvent(action: .enter,
290-
clientID: clientID,
291-
timestamp: Date(),
292-
data: data))
307+
mockSubscription.emit(
308+
PresenceEvent(
309+
action: .enter,
310+
clientID: clientID,
311+
timestamp: Date(),
312+
data: data
313+
)
314+
)
293315
}
294316

295317
func update() async throws {
@@ -304,20 +326,28 @@ actor MockPresence: Presence {
304326
guard let mockSubscription else {
305327
fatalError("Call `subscribe` first.")
306328
}
307-
mockSubscription.emit(PresenceEvent(action: .leave,
308-
clientID: clientID,
309-
timestamp: Date(),
310-
data: nil))
329+
mockSubscription.emit(
330+
PresenceEvent(
331+
action: .leave,
332+
clientID: clientID,
333+
timestamp: Date(),
334+
data: nil
335+
)
336+
)
311337
}
312338

313339
func leave(data: PresenceData) async throws {
314340
guard let mockSubscription else {
315341
fatalError("Call `subscribe` first.")
316342
}
317-
mockSubscription.emit(PresenceEvent(action: .leave,
318-
clientID: clientID,
319-
timestamp: Date(),
320-
data: data))
343+
mockSubscription.emit(
344+
PresenceEvent(
345+
action: .leave,
346+
clientID: clientID,
347+
timestamp: Date(),
348+
data: data
349+
)
350+
)
321351
}
322352

323353
func subscribe(event _: PresenceEventType) -> Subscription<PresenceEvent> {

0 commit comments

Comments
 (0)