Format code

This commit is contained in:
David Ullmer 2022-07-10 18:47:52 +02:00
parent 9250dc650d
commit fcc022ea22
No known key found for this signature in database
GPG Key ID: 4AEABE3359D5883C
3 changed files with 90 additions and 91 deletions

View File

@ -8,17 +8,17 @@
import CoreStore import CoreStore
import Foundation import Foundation
import Stinsen
import JellyfinAPI import JellyfinAPI
import Stinsen
final class UserSignInViewModel: ViewModel { final class UserSignInViewModel: ViewModel {
@RouterObject @RouterObject
var router: UserSignInCoordinator.Router? var router: UserSignInCoordinator.Router?
let server: SwiftfinStore.State.Server let server: SwiftfinStore.State.Server
@Published @Published
var users: [UserDto] = [] var users: [UserDto] = []
init(server: SwiftfinStore.State.Server) { init(server: SwiftfinStore.State.Server) {
self.server = server self.server = server
@ -52,20 +52,20 @@ final class UserSignInViewModel: ViewModel {
self.isLoading = false self.isLoading = false
} }
func loadUsers() { func loadUsers() {
// TODO: this is a hack // TODO: this is a hack
JellyfinAPIAPI.basePath = server.currentURI JellyfinAPIAPI.basePath = server.currentURI
UserAPI.getPublicUsers() UserAPI.getPublicUsers()
.sink(receiveCompletion: { completion in .sink(receiveCompletion: { completion in
switch completion { switch completion {
case .finished: () case .finished: ()
case .failure: case .failure:
self.users = [] self.users = []
} }
}, receiveValue: { response in }, receiveValue: { response in
self.users = response self.users = response
}) })
.store(in: &cancellables) .store(in: &cancellables)
} }
} }

View File

@ -6,65 +6,64 @@
// Copyright (c) 2022 Jellyfin & Jellyfin Contributors // Copyright (c) 2022 Jellyfin & Jellyfin Contributors
// //
import SwiftUI
import JellyfinAPI import JellyfinAPI
import SwiftUI
struct UserLoginCellView: View { struct UserLoginCellView: View {
@State
private var expanded = false;
@State
private var enteredPassword: String = ""
var user: UserDto
var baseURL: String?
var loginTapped : (String, String) -> Void
var cancelTapped: () -> Void
var body: some View {
DisclosureGroup() {
VStack(alignment: .leading, spacing: 16) {
SecureField(L10n.password, text: $enteredPassword)
Button {
loginTapped(user.name ?? "", enteredPassword)
} label: {
L10n.signIn.text
}
}
.padding(.leading, -16)
} label: {
HStack(spacing: 4.0) {
AsyncImage(
url: getProfileImageUrl(),
content: { image in
image.resizable()
.aspectRatio(contentMode: .fit)
.frame(maxWidth: 50, maxHeight: 50)
.clipShape(Circle())
},
placeholder: {
Image(systemName: "person.circle")
.resizable()
.font(.system(size: 40))
.scaledToFit()
.frame(maxWidth: 50, maxHeight: 50)
})
.padding(.vertical, 4.0)
Text(user.name ?? "") @State
.padding(.leading, 4.0) private var expanded = false
Spacer()
} @State
} private var enteredPassword: String = ""
}
var user: UserDto
func getProfileImageUrl() -> URL? { var baseURL: String?
if let userId = user.id, let imageTag = user.primaryImageTag, let server = baseURL { var loginTapped: (String, String) -> Void
let url = URL(string: "\(server)/Users/\(userId)/Images/Primary?width=200&tag=\(imageTag)&quality=90") var cancelTapped: () -> Void
LogManager.log.debug(url?.absoluteString ?? "")
return url var body: some View {
} DisclosureGroup {
return nil VStack(alignment: .leading, spacing: 16) {
} SecureField(L10n.password, text: $enteredPassword)
Button {
loginTapped(user.name ?? "", enteredPassword)
} label: {
L10n.signIn.text
}
}
.padding(.leading, -16)
} label: {
HStack(spacing: 4.0) {
AsyncImage(url: getProfileImageUrl(),
content: { image in
image.resizable()
.aspectRatio(contentMode: .fit)
.frame(maxWidth: 50, maxHeight: 50)
.clipShape(Circle())
},
placeholder: {
Image(systemName: "person.circle")
.resizable()
.font(.system(size: 40))
.scaledToFit()
.frame(maxWidth: 50, maxHeight: 50)
})
.padding(.vertical, 4.0)
Text(user.name ?? "")
.padding(.leading, 4.0)
Spacer()
}
}
}
func getProfileImageUrl() -> URL? {
if let userId = user.id, let imageTag = user.primaryImageTag, let server = baseURL {
let url = URL(string: "\(server)/Users/\(userId)/Images/Primary?width=200&tag=\(imageTag)&quality=90")
LogManager.log.debug(url?.absoluteString ?? "")
return url
}
return nil
}
} }

View File

@ -19,19 +19,19 @@ struct UserSignInView: View {
private var password: String = "" private var password: String = ""
var body: some View { var body: some View {
List { List {
#if !os(tvOS) #if !os(tvOS)
// DisclosureGroup not available on tvOS // DisclosureGroup not available on tvOS
if (viewModel.users.count > 0) { if !viewModel.users.isEmpty {
Section(header: L10n.knownUsers.text) { Section(header: L10n.knownUsers.text) {
ForEach(viewModel.users, id: \.id) { user in ForEach(viewModel.users, id: \.id) { user in
UserLoginCellView(user: user, baseURL: viewModel.server.currentURI, loginTapped: viewModel.login, cancelTapped: viewModel.cancelSignIn) UserLoginCellView(user: user, baseURL: viewModel.server.currentURI, loginTapped: viewModel.login,
.disabled(viewModel.isLoading) cancelTapped: viewModel.cancelSignIn)
} .disabled(viewModel.isLoading)
} }
} }
#endif }
#endif
Section { Section {
TextField(L10n.username, text: $username) TextField(L10n.username, text: $username)
@ -67,6 +67,6 @@ struct UserSignInView: View {
} }
.navigationTitle(L10n.signIn) .navigationTitle(L10n.signIn)
.navigationBarBackButtonHidden(viewModel.isLoading) .navigationBarBackButtonHidden(viewModel.isLoading)
.onAppear(perform: viewModel.loadUsers) .onAppear(perform: viewModel.loadUsers)
} }
} }