Skip to content

Commit 5661701

Browse files
Fix ordering for DMs without last_message_id
Also removed force unwrap in sorting function
1 parent 85a4d8b commit 5661701

File tree

6 files changed

+44
-45
lines changed

6 files changed

+44
-45
lines changed

Swiftcord.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@
830830
isa = XCRemoteSwiftPackageReference;
831831
repositoryURL = "https://github.com/SwiftcordApp/DiscordKit";
832832
requirement = {
833-
branch = main;
833+
branch = "dm-ordering";
834834
kind = branch;
835835
};
836836
};

Swiftcord.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Swiftcord/Utils/Extensions/DiscordAPI/Array.Channel+.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ extension Array where Element == Channel {
1111
func discordSorted() -> Self {
1212
sorted { c1, c2 in
1313
// This is a DM/Group DM channel
14-
if c1.type == .dm || c1.type == .groupDM,
15-
let c1m = c1.last_message_id,
16-
let c2m = c2.last_message_id {
17-
return c1m > c2m
18-
}
14+
if c1.type == .dm || c1.type == .groupDM {
15+
return c1.last_message_id ?? c1.id > c2.last_message_id ?? c2.id
16+
}
1917

2018
if c1.type == .voice, c2.type != .voice { return false }
2119
if c1.type != .voice, c2.type == .voice { return true }
22-
if c1.position != nil, c2.position != nil { return c2.position! > c1.position! }
20+
if let p1 = c1.position, let p2 = c2.position { return p2 > p1 }
2321
return c2.id > c1.id
2422
}
2523
}

Swiftcord/Views/LoadingView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ struct LoadingView: View {
7878
.frame(maxWidth: .infinity, maxHeight: .infinity)
7979
.allowsHitTesting(loadingNum != loadingStrings.count - 1)
8080
.background(Color(NSColor.windowBackgroundColor))
81-
.opacity(loadingNum != loadingStrings.count - 1 ? 0.2 : 0)
81+
.opacity(loadingNum != loadingStrings.count - 1 ? 1 : 0)
8282
.scaleEffect(loadingNum != loadingStrings.count - 1 ? 1 : 2)
8383
.animation(.interpolatingSpring(stiffness: 200, damping: 120), value: loadingNum != loadingStrings.count - 1)
8484
}

Swiftcord/Views/Server/ChannelButton.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ struct DMButton: View {
7878
VStack(alignment: .leading, spacing: 2) {
7979
Text(dm.label(gateway.cache.users) ?? "nil")
8080
if dm.type == .groupDM {
81-
Text("\(dm.recipient_ids?.count ?? 2) Members").font(.caption)
81+
Text("\((dm.recipient_ids?.count ?? 2) + 1) Members").font(.caption)
8282
}
8383
}
8484
Spacer()

Swiftcord/Views/User/UserAvatarView.swift

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@ struct UserAvatarView: View {
3232
guard !clickDisabled else { return }
3333
// Get user profile for a fuller User object
3434
if profile == nil && webhookID == nil { Task {
35-
profile = await DiscordAPI.getProfile(user: user.id, guildID: guildID)
35+
profile = await DiscordAPI.getProfile(
36+
user: user.id,
37+
guildID: guildID == "@me" ? nil : guildID
38+
)
3639
guard profile != nil else { // Profile is still nil: fetching failed
3740
loadFullFailed = true
3841
return
3942
}
4043
}}
41-
if guildRoles == nil && webhookID == nil { Task {
44+
if guildRoles == nil, webhookID == nil, guildID != "@me" { Task {
4245
guildRoles = await DiscordAPI.getGuildRoles(id: guildID)
4346
// print(guildRoles)
4447
}}
@@ -130,39 +133,37 @@ struct UserAvatarView: View {
130133
Text("NO ABOUT").font(.headline)
131134
}
132135

133-
if let profile = profile {
134-
if let guildRoles = guildRoles {
135-
let roles = guildRoles.filter({ r in
136-
profile.guild_member!.roles.contains(r.id)
137-
})
138-
139-
Text(roles.isEmpty
140-
? "NO ROLES"
141-
: (roles.count == 1 ? "ROLE" : "ROLES")
142-
).font(.headline).padding(.top, 8)
143-
if !roles.isEmpty {
144-
TagCloudView(content: roles.map({ role in
145-
HStack(spacing: 6) {
146-
Circle()
147-
.fill(Color(hex: role.color))
148-
.frame(width: 14, height: 14)
149-
.padding(.leading, 6)
150-
Text(role.name)
151-
.font(.system(size: 12))
152-
.padding(.trailing, 8)
153-
}
154-
.frame(height: 24)
155-
.background(Color.gray.opacity(0.2))
156-
.cornerRadius(7)
157-
})).padding(-2)
158-
}
159-
} else {
160-
ProgressView("Loading roles...")
161-
.progressViewStyle(.linear)
162-
.frame(maxWidth: .infinity)
163-
.tint(.blue)
136+
if let profile = profile, let guildRoles = guildRoles {
137+
let roles = guildRoles.filter({ r in
138+
profile.guild_member!.roles.contains(r.id)
139+
})
140+
141+
Text(roles.isEmpty
142+
? "NO ROLES"
143+
: (roles.count == 1 ? "ROLE" : "ROLES")
144+
).font(.headline).padding(.top, 8)
145+
if !roles.isEmpty {
146+
TagCloudView(content: roles.map({ role in
147+
HStack(spacing: 6) {
148+
Circle()
149+
.fill(Color(hex: role.color))
150+
.frame(width: 14, height: 14)
151+
.padding(.leading, 6)
152+
Text(role.name)
153+
.font(.system(size: 12))
154+
.padding(.trailing, 8)
155+
}
156+
.frame(height: 24)
157+
.background(Color.gray.opacity(0.2))
158+
.cornerRadius(7)
159+
})).padding(-2)
164160
}
165-
}
161+
} else {
162+
ProgressView("Loading roles...")
163+
.progressViewStyle(.linear)
164+
.frame(maxWidth: .infinity)
165+
.tint(.blue)
166+
}
166167

167168
Text("NOTE").font(.headline).padding(.top, 8)
168169
// Notes are stored locally for now, but eventually will be synced with the Discord API

0 commit comments

Comments
 (0)