-
-
Notifications
You must be signed in to change notification settings - Fork 534
Expand file tree
/
Copy pathUserSignInView.swift
More file actions
358 lines (324 loc) · 11.6 KB
/
Copy pathUserSignInView.swift
File metadata and controls
358 lines (324 loc) · 11.6 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//
// Swiftfin is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at https://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2026 Jellyfin & Jellyfin Contributors
//
import CollectionVGrid
import Defaults
import Factory
import JellyfinAPI
import Logging
import SwiftUI
struct UserSignInView: View {
private enum Field: Hashable {
case username
case password
}
@Environment(\.localUserAuthenticationAction)
private var authenticationAction
@FocusState
private var focusedTextField: Field?
@Router
private var router
@State
private var accessPolicy: LocalUserAccessPolicy = .none
@State
private var existingUser: UserSignInViewModel.UserStateDataPair? = nil
@State
private var isPresentingExistingUser: Bool = false
@State
private var password: String = ""
@State
private var pinHint: String = ""
@State
private var username: String = ""
@StateObject
private var viewModel: UserSignInViewModel
private let logger = Logger.swiftfin()
init(server: ServerState) {
self._viewModel = StateObject(wrappedValue: UserSignInViewModel(server: server))
}
private func handleEvent(_ event: UserSignInViewModel._Event) {
switch event {
case let .connected(user):
guard let authenticationAction else {
return
}
viewModel.save(
user: user,
authenticationAction: (
authenticationAction,
accessPolicy,
accessPolicy.createReason(
user: user.state.state
)
),
evaluatedPolicyMap: .init(action: processEvaluatedPolicy)
)
case let .existingUser(existingUser):
self.existingUser = existingUser
self.isPresentingExistingUser = true
case let .saved(user):
UIDevice.feedback(.success)
router.dismiss()
Container.shared.userSessionManager().signIn(userID: user.id)
}
}
private func processEvaluatedPolicy(
_ evaluatedPolicy: any EvaluatedLocalUserAccessPolicy
) -> any EvaluatedLocalUserAccessPolicy {
if let pinPolicy = evaluatedPolicy as? PinEvaluatedUserAccessPolicy {
return PinEvaluatedUserAccessPolicy(
pin: pinPolicy.pin,
pinHint: pinHint
)
}
return evaluatedPolicy
}
private func disclaimerText(_ disclaimer: String) -> Text {
let options = AttributedString.MarkdownParsingOptions(
interpretedSyntax: .inlineOnlyPreservingWhitespace
)
if let attributedString = try? AttributedString(
markdown: disclaimer,
options: options
) {
return Text(attributedString)
} else {
return Text(disclaimer)
}
}
// MARK: - Sign In Section
@ViewBuilder
private var signInSection: some View {
Section {
TextField(L10n.username, text: $username)
.autocorrectionDisabled()
.textContentType(.username)
.textInputAutocapitalization(.never)
.focused($focusedTextField, equals: .username)
.onSubmit {
focusedTextField = .password
}
SecureField(
L10n.password,
text: $password,
maskToggle: .enabled
)
.onSubmit {
focusedTextField = nil
viewModel.signIn(
username: username,
password: password
)
}
.autocorrectionDisabled()
.textContentType(.password)
.textInputAutocapitalization(.never)
.focused($focusedTextField, equals: .password)
} header: {
Text(L10n.signInToServer(viewModel.server.name))
} footer: {
switch accessPolicy {
case .requireDeviceAuthentication:
Label(L10n.userDeviceAuthRequiredDescription, systemImage: "exclamationmark.circle.fill")
.labelStyle(.sectionFooterWithImage(imageStyle: .orange))
case .requirePin:
Label(L10n.userPinRequiredDescription, systemImage: "exclamationmark.circle.fill")
.labelStyle(.sectionFooterWithImage(imageStyle: .orange))
case .none:
EmptyView()
}
}
if case .signingIn = viewModel.state {
Button(L10n.cancel, role: .cancel) {
viewModel.cancel()
}
.buttonStyle(.primary)
.frame(maxHeight: 75)
} else {
Button(L10n.signIn) {
viewModel.signIn(
username: username,
password: password
)
}
.buttonStyle(.primary)
.frame(maxHeight: 75)
.disabled(username.isEmpty)
.foregroundStyle(
Color.jellyfinPurple.overlayColor,
Color.jellyfinPurple
)
.opacity(username.isEmpty ? 0.5 : 1)
}
if viewModel.isQuickConnectEnabled {
Section {
Button(L10n.quickConnect) {
router.route(
to: .quickConnect(
client: viewModel.server.client
) { secret in
await viewModel.signInQuickConnect(secret: secret)
}
)
}
.buttonStyle(.primary)
.frame(maxHeight: 75)
.disabled(viewModel.state == .signingIn)
.foregroundStyle(
Color.jellyfinPurple.overlayColor,
Color.jellyfinPurple
)
}
}
if let disclaimer = viewModel.serverDisclaimer {
Section(L10n.disclaimer) {
disclaimerText(disclaimer)
.font(.callout)
}
}
}
// MARK: - Public Users Section
@ViewBuilder
private var publicUsersSection: some View {
Section(L10n.publicUsers) {
if viewModel.publicUsers.isEmpty {
Text(L10n.noPublicUsers)
.font(.callout)
.foregroundStyle(.secondary)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
} else {
#if os(iOS)
ForEach(viewModel.publicUsers) { user in
ChevronButton {
username = user.name ?? ""
password = ""
focusedTextField = .password
} label: {
HStack {
UserProfileImage(
userID: user.id,
source: user.profileImageSource(
client: viewModel.server.client,
maxWidth: 50
)
)
.frame(width: 50, height: 50)
Text(user.name ?? .emptyDash)
.fontWeight(.semibold)
.lineLimit(1)
}
}
}
#else
LazyVGrid(
columns: Array(repeating: GridItem(.flexible()), count: 4),
spacing: 30
) {
ForEach(viewModel.publicUsers) { user in
UserButton(
user: user,
client: viewModel.server.client
) {
username = user.name ?? ""
password = ""
focusedTextField = .password
}
.environment(\.isOverComplexContent, true)
}
}
#endif
}
}
.disabled(viewModel.state == .signingIn)
}
@ViewBuilder
private var contentView: some View {
#if os(iOS)
List {
signInSection
publicUsersSection
}
.navigationBarTitleDisplayMode(.inline)
.navigationBarCloseButton(disabled: viewModel.state == .signingIn) {
router.dismiss()
}
.topBarTrailing {
if viewModel.state == .signingIn || viewModel.background.is(.gettingPublicData) {
ProgressView()
}
Button(L10n.security, systemImage: "gearshape.fill") {
router.route(
to: .userSecurity(
pinHint: $pinHint,
accessPolicy: $accessPolicy
)
)
}
}
#else
SplitLoginWindowView(
isLoading: viewModel.state == .signingIn,
backgroundImageSource: viewModel.server.splashScreenImageSource
) {
signInSection
} trailingContentView: {
publicUsersSection
}
#endif
}
// MARK: - Body
var body: some View {
contentView
.navigationTitle(L10n.signIn.localizedCapitalized)
.interactiveDismissDisabled(viewModel.state == .signingIn)
.onReceive(viewModel.events, perform: handleEvent)
.onFirstAppear {
focusedTextField = .username
viewModel.getPublicData()
}
.alert(
L10n.duplicateUser,
isPresented: $isPresentingExistingUser,
presenting: existingUser
) { existingUser in
let userState = existingUser.state.state
let existingUserAccessPolicy = userState.accessPolicy
Button(L10n.signIn) {
viewModel.saveExisting(
user: existingUser,
replaceForAccessToken: false,
authenticationAction: (
authenticationAction!,
existingUserAccessPolicy,
existingUserAccessPolicy.authenticateReason(
user: userState
)
),
evaluatedPolicyMap: .init(action: processEvaluatedPolicy)
)
}
Button(L10n.replace) {
viewModel.saveExisting(
user: existingUser,
replaceForAccessToken: true,
authenticationAction: (
authenticationAction!,
existingUserAccessPolicy,
existingUserAccessPolicy.authenticateReason(
user: userState
)
),
evaluatedPolicyMap: .init(action: processEvaluatedPolicy)
)
}
Button(L10n.dismiss, role: .cancel) {}
} message: { existingUser in
Text(L10n.duplicateUserSaved(existingUser.state.state.username))
}
.errorMessage($viewModel.error)
}
}