Mixonomer-iOS/Mixonomer/Views/Admin/UsersList.swift

89 lines
2.6 KiB
Swift
Raw Normal View History

2022-08-14 13:14:21 +01:00
//
// UsersList.swift
// Mixonomer
//
// Created by Andy Pack on 13/08/2022.
// Copyright © 2022 Sarsoo. All rights reserved.
//
import SwiftUI
import SwiftyJSON
2022-11-23 23:39:58 +00:00
import OSLog
2022-08-14 13:14:21 +01:00
struct UsersList: View {
@EnvironmentObject var liveUser: LiveUser
@State private var users: [User] = []
@State var isLoading = true
2022-08-14 13:14:21 +01:00
var body: some View {
if isLoading {
LoadingScreen()
.onAppear {
self.get_users()
}
}
else {
List{
Section { // Weird? added empty header as list renders with space for header then jumps up, not nice
if self.users.count > 0 {
ForEach(self.users.indices, id: \.self){ userIdx in
NavigationLink(destination: UserView(user: self.$users[userIdx])) {
Text(self.users[userIdx].username)
}
}
}else {
HStack {
Text("No Users")
.multilineTextAlignment(.center)
Spacer()
2022-08-14 13:14:21 +01:00
}
}
}
}
// .id(UUID())
.navigationBarTitle("Users")
2022-08-14 13:14:21 +01:00
}
}
func get_users() {
let api = AdminApi.getUsers
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
if self.liveUser.check_network_response(response: response) {
guard let data = response.data else {
2022-11-23 23:39:58 +00:00
Logger.net.error("failed to get users")
return
2022-08-14 13:14:21 +01:00
}
guard let json = try? JSON(data: data) else {
2022-11-23 23:39:58 +00:00
Logger.parse.error("failed to get users")
return
2022-08-14 13:14:21 +01:00
}
// update state
self.users = UserApi.fromJSON(user: json["accounts"].arrayValue)
.sorted(by: { (user1, user2) in
return user1.username < user2.username
})
self.isLoading = false
2022-08-14 13:14:21 +01:00
} else {
2022-11-23 23:39:58 +00:00
Logger.net.error("failed to get users from view")
2022-08-14 13:14:21 +01:00
}
}
}
}
struct UsersList_Previews: PreviewProvider {
static var previews: some View {
UsersList(isLoading: false)
2022-12-09 08:51:42 +00:00
.environmentObject(LiveUser.get_preview_user())
2022-08-14 13:14:21 +01:00
}
}