-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathContentView.swift
240 lines (220 loc) · 8.99 KB
/
ContentView.swift
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//
// ContentView.swift
// Swiftcord
//
// Created by Vincent Kwok on 19/2/22.
//
import SwiftUI
import CoreData
import os
import DiscordKit
import DiscordKitCore
struct ContentView: View {
@State private var loadingGuildID: Snowflake?
@State private var presentingOnboarding = false
@State private var presentingAddServer = false
@State private var skipWhatsNew = false
@State private var whatsNewMarkdown: String?
@StateObject private var audioManager = AudioCenterManager()
@EnvironmentObject var gateway: DiscordGateway
@EnvironmentObject var state: UIState
@EnvironmentObject var accountsManager: AccountSwitcher
@AppStorage("local.seenOnboarding") private var seenOnboarding = false
@AppStorage("local.previousBuild") private var prevBuild: String?
private let log = Logger(category: "ContentView")
private func makeDMGuild() -> PreloadedGuild {
PreloadedGuild(
channels: gateway.cache.dms,
properties: Guild(
id: "@me",
name: "DMs",
owner_id: "",
afk_timeout: 0,
verification_level: .none,
default_message_notifications: .all,
explicit_content_filter: .disabled,
roles: [], emojis: [], features: [],
mfa_level: .none,
system_channel_flags: 0,
channels: gateway.cache.dms,
premium_tier: .none,
preferred_locale: .englishUS,
nsfw_level: .default,
premium_progress_bar_enabled: false
)
)
}
private func loadLastSelectedGuild() {
if let lGID = UserDefaults.standard.string(forKey: "lastSelectedGuild"),
gateway.cache.guilds[lGID] != nil || lGID == "@me" {
state.selectedGuildID = lGID
} else {
state.selectedGuildID = "@me"
}
}
private var serverListItems: [ServerListItem] {
let unsortedGuilds = gateway.cache.guilds.values.filter { guild in
!gateway.guildFolders.contains { folder in
folder.guild_ids.contains(guild.id)
}
}
.sorted { lhs, rhs in lhs.joined_at > rhs.joined_at }
.map { ServerListItem.guild($0) }
return unsortedGuilds + gateway.guildFolders.compactMap { folder -> ServerListItem? in
if folder.id != nil {
let guilds = folder.guild_ids.compactMap {
gateway.cache.guilds[$0]
}
let name = folder.name ?? String(guilds.map { $0.properties.name }.joined(separator: ", "))
return .guildFolder(ServerFolder.GuildFolder(
name: name, guilds: guilds, color: folder.color.flatMap { Color(hex: $0) } ?? Color.accentColor
))
} else {
guard let guild = gateway.cache.guilds[folder.guild_ids.first ?? ""] else {
return nil
}
return .guild(guild)
}
}
}
var body: some View {
HStack(spacing: 0) {
// MARK: Server List
ScrollView(showsIndicators: false) {
LazyVStack(spacing: 8) {
ServerButton(
selected: state.selectedGuildID == "@me",
name: "Home",
assetIconName: "DiscordIcon"
) {
state.selectedGuildID = "@me"
}
.padding(.top, 4)
HorizontalDividerView().frame(width: 32)
ForEach(self.serverListItems) { item in
switch item {
case .guild(let guild):
ServerButton(
selected: state.selectedGuildID == guild.id || loadingGuildID == guild.id,
guild: guild,
name: guild.properties.name,
serverIconURL: guild.properties.iconURL(),
isLoading: loadingGuildID == guild.id
) {
state.selectedGuildID = guild.id
}
case .guildFolder(let folder):
ServerFolder(
folder: folder,
selectedGuildID: $state.selectedGuildID,
loadingGuildID: loadingGuildID
)
}
}
ServerButton(
selected: false,
name: "Add a Server",
systemIconName: "plus",
bgColor: .green,
noIndicator: true
) {
presentingAddServer = true
}.padding(.bottom, 4)
}
.padding(.bottom, 8)
.frame(width: 72)
}
.frame(maxHeight: .infinity, alignment: .top)
.background(VisualEffect()
.overlay(Color(nsColor: NSColor.controlBackgroundColor).opacity(0.5))
)
ServerView(
guild: state.selectedGuildID == nil
? nil
: (state.selectedGuildID == "@me" ? makeDMGuild() : gateway.cache.guilds[state.selectedGuildID!]), serverCtx: state.serverCtx
)
}
.environmentObject(audioManager)
.onChange(of: state.selectedGuildID) { id in
guard let id = id else { return }
UserDefaults.standard.set(id.description, forKey: "lastSelectedGuild")
}
.onChange(of: state.loadingState) { state in
if state == .gatewayConn { loadLastSelectedGuild() }
if state == .messageLoad,
!seenOnboarding || prevBuild != Bundle.main.infoDictionary?["CFBundleVersion"] as? String { // swiftlint:disable:this indentation_width
// If the user hasn't seen the onboarding (first page), present onboarding immediately
if !seenOnboarding { presentingOnboarding = true }
Task {
do {
whatsNewMarkdown = try await GitHubAPI
.getReleaseByTag(org: "SwiftcordApp", repo: "Swiftcord", tag: "v\(Bundle.main.infoDictionary!["CFBundleShortVersionString"] ?? "")")
.body
} catch {
skipWhatsNew = true
return
}
// If the user has already seen the onboarding, present the onboarding sheet only after loading the changelog
presentingOnboarding = true
}
}
}
.onAppear {
if state.loadingState == .messageLoad { loadLastSelectedGuild() }
_ = gateway.onEvent.addHandler { evt in
switch evt {
case .userReady(let payload):
state.loadingState = .gatewayConn
accountsManager.onSignedIn(with: payload.user)
fallthrough
case .resumed:
gateway.send(.voiceStateUpdate, data: GatewayVoiceStateUpdate(
guild_id: nil,
channel_id: nil,
self_mute: state.selfMute,
self_deaf: state.selfDeaf,
self_video: false
))
default: break
}
}
_ = gateway.socket?.onSessionInvalid.addHandler { state.loadingState = .initial }
}
.sheet(isPresented: $presentingOnboarding) {
seenOnboarding = true
prevBuild = Bundle.main.infoDictionary?["CFBundleVersion"] as? String
} content: {
OnboardingView(
skipOnboarding: seenOnboarding,
skipWhatsNew: $skipWhatsNew,
newMarkdown: $whatsNewMarkdown,
presenting: $presentingOnboarding
)
}
.sheet(isPresented: $presentingAddServer) {
ServerJoinView(presented: $presentingAddServer)
}
}
private enum ServerListItem: Identifiable {
case guild(PreloadedGuild), guildFolder(ServerFolder.GuildFolder)
var id: String {
switch self {
case .guild(let guild):
return guild.id
case .guildFolder(let folder):
return folder.id
}
}
}
}
private let itemFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .medium
return formatter
}()
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView() // .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}
}