SimpleX-Chat/apps/ios/Shared/Views/UserSettings/UserProfilesView.swift
Stanislav Dmitrenko ad6aa10cd2
ios: Multiusers feature continue (#1793)
* ios: Multiusers feature continue

* Logging of user in responses

* UserId

Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com>

* Undo ugly user inclusion into functions.  Now it's in backend

* Do not set active user if it's unchanged

* Blank line

* if

* Change active user function

* refactor

* refactor

Co-authored-by: JRoberts <8711996+jr-simplex@users.noreply.github.com>

* Alert

Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com>
Co-authored-by: JRoberts <8711996+jr-simplex@users.noreply.github.com>
2023-01-19 16:22:56 +00:00

70 lines
2.4 KiB
Swift

//
// Created by Avently on 17.01.2023.
// Copyright (c) 2023 SimpleX Chat. All rights reserved.
//
import SwiftUI
import SimpleXChat
struct UserProfilesView: View {
@EnvironmentObject private var m: ChatModel
@Environment(\.editMode) private var editMode
@State private var selectedUser: Int? = nil
@State private var showAddUser: Bool? = false
var body: some View {
List {
Section("Your profiles") {
ForEach(Array(m.users.enumerated()), id: \.0) { i, userInfo in
userProfileView(userInfo, index: i)
.deleteDisabled(userInfo.user.activeUser)
}
.onDelete { indexSet in
AlertManager.shared.showAlert(
Alert(
title: Text("Delete profile?"),
message: Text("All chats and messages will be deleted - this cannot be undone!"),
primaryButton: .destructive(Text("Delete")) {
removeUser(index: indexSet.first!)
},
secondaryButton: .cancel()
))
}
NavigationLink(destination: CreateProfile(), tag: true, selection: $showAddUser) {
Text("Add profile…")
}
}
}
.toolbar { EditButton() }
}
private func removeUser(index: Int) {
do {
try apiDeleteUser(m.users[index].user.userId)
var users = m.users
users.remove(at: index)
m.updateUsers(users)
} catch {
AlertManager.shared.showAlertMsg(
title: "Failed to delete the user",
message: "Error: \(responseError(error))"
)
}
}
private func userProfileView(_ userBinding: UserInfo, index: Int) -> some View {
let user = userBinding.user
return NavigationLink(tag: index, selection: $selectedUser) {
// UserPrefs(user: userBinding, index: index)
// .navigationBarTitle(user.chatViewName)
// .navigationBarTitleDisplayMode(.large)
} label: {
Text(user.chatViewName)
}
}
}
struct UserProfilesView_Previews: PreviewProvider {
static var previews: some View {
UserProfilesView()
}
}