Skip to content

Commit 3ce9fb1

Browse files
Moved channel buttons to a separate file
Attempted more performance optimisations in Gateway
1 parent 7b19b11 commit 3ce9fb1

File tree

6 files changed

+101
-99
lines changed

6 files changed

+101
-99
lines changed

Swiftcord.xcodeproj/project.pbxproj

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
DA520AE227D76BEB009FD740 /* BoolToString.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA520AE127D76BEB009FD740 /* BoolToString.swift */; };
119119
DA520AE727D8ECA6009FD740 /* DecodableThrowable.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA520AE627D8ECA6009FD740 /* DecodableThrowable.swift */; };
120120
DA57F44428056718001DC46E /* ChannelList.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA57F44328056718001DC46E /* ChannelList.swift */; };
121+
DA57F44628065209001DC46E /* ChannelButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA57F44528065209001DC46E /* ChannelButton.swift */; };
121122
DA585C9927E1F6AC00FA4EE0 /* CursorOnViewHover.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA585C9827E1F6AC00FA4EE0 /* CursorOnViewHover.swift */; };
122123
/* End PBXBuildFile section */
123124

@@ -236,6 +237,7 @@
236237
DA520AE127D76BEB009FD740 /* BoolToString.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BoolToString.swift; sourceTree = "<group>"; };
237238
DA520AE627D8ECA6009FD740 /* DecodableThrowable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DecodableThrowable.swift; sourceTree = "<group>"; };
238239
DA57F44328056718001DC46E /* ChannelList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelList.swift; sourceTree = "<group>"; };
240+
DA57F44528065209001DC46E /* ChannelButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelButton.swift; sourceTree = "<group>"; };
239241
DA585C9827E1F6AC00FA4EE0 /* CursorOnViewHover.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CursorOnViewHover.swift; sourceTree = "<group>"; };
240242
/* End PBXFileReference section */
241243

@@ -555,6 +557,7 @@
555557
DA4A892327C5D6C500720909 /* ServerView.swift */,
556558
DA4A891327C49B1000720909 /* ServerButton.swift */,
557559
DA57F44328056718001DC46E /* ChannelList.swift */,
560+
DA57F44528065209001DC46E /* ChannelButton.swift */,
558561
);
559562
path = Server;
560563
sourceTree = "<group>";
@@ -683,6 +686,7 @@
683686
DA4A891027C48A7B00720909 /* GatewaySend.swift in Sources */,
684687
DA4A88F127C3543A00720909 /* Voice.swift in Sources */,
685688
DA4A88C827C1EB8F00720909 /* DiscordGateway.swift in Sources */,
689+
DA57F44628065209001DC46E /* ChannelButton.swift in Sources */,
686690
DA32EF6627CB772300A9ED72 /* CacheModel.xcdatamodeld in Sources */,
687691
DA2384C827CD9418009E15E0 /* Logger.swift in Sources */,
688692
DA520AE227D76BEB009FD740 /* BoolToString.swift in Sources */,
@@ -876,7 +880,7 @@
876880
CODE_SIGN_ENTITLEMENTS = Swiftcord/Swiftcord.entitlements;
877881
CODE_SIGN_STYLE = Automatic;
878882
COMBINE_HIDPI_IMAGES = YES;
879-
CURRENT_PROJECT_VERSION = 1;
883+
CURRENT_PROJECT_VERSION = 2;
880884
DEVELOPMENT_ASSET_PATHS = "\"Swiftcord/Preview Content\"";
881885
DEVELOPMENT_TEAM = Q382QLKDG3;
882886
ENABLE_HARDENED_RUNTIME = YES;
@@ -888,7 +892,7 @@
888892
"$(inherited)",
889893
"@executable_path/../Frameworks",
890894
);
891-
MARKETING_VERSION = 0.1.0;
895+
MARKETING_VERSION = 0.2.1;
892896
PRODUCT_BUNDLE_IDENTIFIER = com.cryptoalgo.swiftcord;
893897
PRODUCT_NAME = "$(TARGET_NAME)";
894898
SWIFT_EMIT_LOC_STRINGS = YES;
@@ -904,7 +908,7 @@
904908
CODE_SIGN_ENTITLEMENTS = Swiftcord/Swiftcord.entitlements;
905909
CODE_SIGN_STYLE = Automatic;
906910
COMBINE_HIDPI_IMAGES = YES;
907-
CURRENT_PROJECT_VERSION = 1;
911+
CURRENT_PROJECT_VERSION = 2;
908912
DEVELOPMENT_ASSET_PATHS = "\"Swiftcord/Preview Content\"";
909913
DEVELOPMENT_TEAM = Q382QLKDG3;
910914
ENABLE_HARDENED_RUNTIME = YES;
@@ -916,7 +920,7 @@
916920
"$(inherited)",
917921
"@executable_path/../Frameworks",
918922
);
919-
MARKETING_VERSION = 0.1.0;
923+
MARKETING_VERSION = 0.2.1;
920924
PRODUCT_BUNDLE_IDENTIFIER = com.cryptoalgo.swiftcord;
921925
PRODUCT_NAME = "$(TARGET_NAME)";
922926
SWIFT_EMIT_LOC_STRINGS = YES;

Swiftcord/DiscordAPI/Gateway/DiscordGateway.swift

Lines changed: 57 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class DiscordGateway: WebSocketDelegate, ObservableObject {
5353
request.timeoutInterval = connTimeout
5454
socket = WebSocket(request: request)
5555
socket.delegate = self
56+
socket.callbackQueue = queue
5657

5758
log.i("Attempting connection to Gateway: \(apiConfig.gateway)")
5859
socket.connect()
@@ -157,69 +158,67 @@ class DiscordGateway: WebSocketDelegate, ObservableObject {
157158
}
158159

159160
func handleIncoming(received: String) {
160-
queue.async { [self] in
161-
guard let decoded = try? JSONDecoder().decode(GatewayIncoming.self, from: received.data(using: .utf8)!)
162-
else { return }
163-
164-
DispatchQueue.main.async {
165-
if (decoded.s != nil) { seq = decoded.s } // Update sequence
161+
guard let decoded = try? JSONDecoder().decode(GatewayIncoming.self, from: received.data(using: .utf8)!)
162+
else { return }
163+
164+
DispatchQueue.main.async {
165+
if (decoded.s != nil) { self.seq = decoded.s } // Update sequence
166+
}
167+
168+
switch (decoded.op) {
169+
case .heartbeat:
170+
// Immediately send heartbeat as requested
171+
log.d("Send heartbeat by server request")
172+
sendToGateway(op: .heartbeat, d: GatewayHeartbeat())
173+
case .hello:
174+
// Start heartbeating and send identify
175+
guard let d = decoded.d as? GatewayHello else { return }
176+
initHeartbeat(interval: d.heartbeat_interval)
177+
178+
// Check if we're attempting to and can resume
179+
if isReconnecting && !doNotResume && sessionID != nil && seq != nil {
180+
log.i("Attempting resume")
181+
guard let resume = getResume(seq: seq!, sessionID: sessionID!)
182+
else { return }
183+
sendToGateway(op: .resume, d: resume)
166184
}
167-
168-
switch (decoded.op) {
169-
case .heartbeat:
170-
// Immediately send heartbeat as requested
171-
log.d("Send heartbeat by server request")
172-
sendToGateway(op: .heartbeat, d: GatewayHeartbeat())
173-
case .hello:
174-
// Start heartbeating and send identify
175-
guard let d = decoded.d as? GatewayHello else { return }
176-
initHeartbeat(interval: d.heartbeat_interval)
177-
178-
// Check if we're attempting to and can resume
179-
if isReconnecting && !doNotResume && sessionID != nil && seq != nil {
180-
log.i("Attempting resume")
181-
guard let resume = getResume(seq: seq!, sessionID: sessionID!)
182-
else { return }
183-
sendToGateway(op: .resume, d: resume)
185+
else {
186+
log.d("Sending identify:", isConnected, !doNotResume, sessionID ?? "No sessionID", seq ?? -1)
187+
// Send identify
188+
DispatchQueue.main.async {
189+
self.seq = nil // Clear sequence #
190+
self.isReconnecting = false // Resuming failed/not attempted
184191
}
185-
else {
186-
log.d("Sending identify:", isConnected, !doNotResume, sessionID ?? "No sessionID", seq ?? -1)
187-
// Send identify
188-
DispatchQueue.main.async {
189-
seq = nil // Clear sequence #
190-
isReconnecting = false // Resuming failed/not attempted
191-
}
192-
guard let identify = getIdentify() else {
193-
log.d("Token not in keychain")
194-
authFailed = true
195-
socket.disconnect(closeCode: 1000)
196-
return
197-
}
198-
sendToGateway(op: .identify, d: identify)
192+
guard let identify = getIdentify() else {
193+
log.d("Token not in keychain")
194+
authFailed = true
195+
socket.disconnect(closeCode: 1000)
196+
return
199197
}
200-
case .heartbeatAck: DispatchQueue.main.async { missedACK = 0 }
201-
case .dispatchEvent:
202-
guard let type = decoded.t else { return }
203-
guard let data = decoded.d else { return }
204-
switch (type) {
205-
case .ready:
206-
guard let d = data as? ReadyEvt else { return }
207-
DispatchQueue.main.async {
208-
doNotResume = false
209-
sessionID = d.session_id
210-
cache.guilds = d.guilds
211-
cache.user = d.user
212-
}
213-
log.i("Gateway ready")
214-
default: log.i("Dispatched event <\(type)>")
198+
sendToGateway(op: .identify, d: identify)
199+
}
200+
case .heartbeatAck: DispatchQueue.main.async { self.missedACK = 0 }
201+
case .dispatchEvent:
202+
guard let type = decoded.t else { return }
203+
guard let data = decoded.d else { return }
204+
switch (type) {
205+
case .ready:
206+
guard let d = data as? ReadyEvt else { return }
207+
DispatchQueue.main.async {
208+
self.doNotResume = false
209+
self.sessionID = d.session_id
210+
self.cache.guilds = d.guilds
211+
self.cache.user = d.user
215212
}
216-
onEvent.notify(event: (type, data))
217-
case .invalidSession:
218-
// Check if the session can be resumed
219-
let shouldResume = (decoded.primitiveData as? Bool) ?? false
220-
attemptReconnect(resume: shouldResume)
221-
default: log.w("Unimplemented opcode: \(decoded.op)")
213+
log.i("Gateway ready")
214+
default: log.i("Dispatched event <\(type)>")
222215
}
216+
onEvent.notify(event: (type, data))
217+
case .invalidSession:
218+
// Check if the session can be resumed
219+
let shouldResume = (decoded.primitiveData as? Bool) ?? false
220+
attemptReconnect(resume: shouldResume)
221+
default: log.w("Unimplemented opcode: \(decoded.op)")
223222
}
224223
}
225224
}

Swiftcord/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
<plist version="1.0">
44
<dict>
55
<key>ATSApplicationFontsPath</key>
6-
<string>.</string>
6+
<string>.</string>
77
</dict>
88
</plist>

Swiftcord/Utils/LossyCodableList.swift

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// ChannelButton.swift
3+
// Swiftcord
4+
//
5+
// Created by Vincent on 4/13/22.
6+
//
7+
8+
import SwiftUI
9+
10+
let chIcons = [
11+
ChannelType.voice: "speaker.wave.2.fill",
12+
.news: "megaphone.fill",
13+
]
14+
15+
struct ChannelButton: View {
16+
let channel: Channel
17+
let guild: Guild?
18+
@Binding var selectedCh: Channel?
19+
20+
var body: some View {
21+
Button {
22+
selectedCh = channel
23+
UserDefaults.standard.setValue(channel.id, forKey: "guildLastCh.\(guild!.id)")
24+
} label: {
25+
Label(
26+
channel.name ?? "",
27+
systemImage: (guild?.rules_channel_id != nil && guild?.rules_channel_id! == channel.id) ? "newspaper.fill" : (chIcons[channel.type] ?? "number")
28+
).frame(maxWidth: .infinity, alignment: .leading)
29+
}
30+
.buttonStyle(.borderedProminent)
31+
.accentColor(Color.gray)
32+
}
33+
}

Swiftcord/Views/Server/ChannelList.swift

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ struct ChannelList: View {
1212
@Binding var selCh: Channel?
1313
@Binding var guild: Guild?
1414

15-
let chIcons = [
16-
ChannelType.voice: "speaker.wave.2.fill",
17-
.news: "megaphone.fill",
18-
]
19-
2015
var body: some View {
2116
List {
2217
ForEach(
@@ -41,31 +36,10 @@ struct ChannelList: View {
4136
} else { return c2.id > c1.id }
4237
}),
4338
id: \.id
44-
) { channel in
45-
Button {
46-
selCh = channel
47-
} label: {
48-
Label(
49-
channel.name ?? "",
50-
systemImage: (guild?.rules_channel_id != nil && guild?.rules_channel_id! == channel.id) ? "newspaper.fill" : (chIcons[channel.type] ?? "number")
51-
).frame(maxWidth: .infinity, alignment: .leading)
52-
}
53-
.buttonStyle(.borderedProminent)
54-
.accentColor(Color.gray)
55-
}
56-
}
57-
}
58-
else if ch.parent_id == nil {
59-
Button {
60-
61-
} label: {
62-
Label(
63-
ch.name ?? "",
64-
systemImage: (guild?.rules_channel_id != nil && guild?.rules_channel_id! == ch.id) ? "newspaper.fill" : (chIcons[ch.type] ?? "number")
65-
)
39+
) { channel in ChannelButton(channel: channel, guild: guild, selectedCh: $selCh) }
6640
}
67-
.accentColor(Color.gray)
6841
}
42+
else if ch.parent_id == nil { ChannelButton(channel: ch, guild: guild, selectedCh: $selCh) }
6943
}
7044
}
7145
.frame(minWidth: 240, maxHeight: .infinity)

0 commit comments

Comments
 (0)