2022-07-26 10:55:58 +04:00
|
|
|
//
|
|
|
|
// AddGroupMembersView.swift
|
|
|
|
// SimpleX (iOS)
|
|
|
|
//
|
|
|
|
// Created by JRoberts on 22.07.2022.
|
|
|
|
// Copyright © 2022 SimpleX Chat. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
import SimpleXChat
|
|
|
|
|
|
|
|
struct AddGroupMembersView: View {
|
|
|
|
@EnvironmentObject var chatModel: ChatModel
|
|
|
|
var chat: Chat
|
2022-07-27 11:16:07 +04:00
|
|
|
var groupInfo: GroupInfo
|
2022-07-26 12:33:10 +04:00
|
|
|
@Binding var showSheet: Bool
|
2022-07-26 10:55:58 +04:00
|
|
|
@State private var contactsToAdd: [Contact] = []
|
|
|
|
@State private var selectedContacts = Set<Int64>()
|
2022-07-27 11:16:07 +04:00
|
|
|
@State private var selectedRole: GroupMemberRole = .admin
|
2022-07-26 10:55:58 +04:00
|
|
|
|
|
|
|
var body: some View {
|
2022-07-27 11:16:07 +04:00
|
|
|
NavigationView {
|
|
|
|
List {
|
|
|
|
ChatInfoToolbar(chat: chat, imageSize: 48)
|
|
|
|
.frame(maxWidth: .infinity, alignment: .center)
|
|
|
|
.listRowBackground(Color.clear)
|
|
|
|
.listRowSeparator(.hidden)
|
|
|
|
|
|
|
|
if (contactsToAdd.isEmpty) {
|
|
|
|
Text("No contacts to add")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
.padding()
|
|
|
|
.frame(maxWidth: .infinity, alignment: .center)
|
|
|
|
.listRowBackground(Color.clear)
|
|
|
|
} else {
|
2022-07-26 10:55:58 +04:00
|
|
|
let count = selectedContacts.count
|
2022-07-27 11:16:07 +04:00
|
|
|
Section {
|
|
|
|
rolePicker()
|
|
|
|
inviteMembersButton()
|
|
|
|
.disabled(count < 1)
|
|
|
|
} footer: {
|
|
|
|
if (count >= 1) {
|
|
|
|
HStack {
|
|
|
|
Button { selectedContacts.removeAll() } label: { Text("Clear") }
|
|
|
|
Spacer()
|
|
|
|
Text("\(count) contact(s) selected")
|
2022-07-26 10:55:58 +04:00
|
|
|
}
|
2022-07-27 11:16:07 +04:00
|
|
|
} else {
|
|
|
|
Text("No contacts selected")
|
|
|
|
.frame(maxWidth: .infinity, alignment: .trailing)
|
2022-07-26 10:55:58 +04:00
|
|
|
}
|
|
|
|
}
|
2022-07-27 11:16:07 +04:00
|
|
|
|
|
|
|
Section {
|
|
|
|
ForEach(contactsToAdd) { contact in
|
|
|
|
contactCheckView(contact)
|
2022-07-26 10:55:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-27 11:16:07 +04:00
|
|
|
.navigationBarHidden(true)
|
2022-07-26 10:55:58 +04:00
|
|
|
}
|
|
|
|
.frame(maxHeight: .infinity, alignment: .top)
|
|
|
|
.task {
|
|
|
|
contactsToAdd = await getContactsToAdd()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getContactsToAdd() async -> [Contact] {
|
2022-07-28 10:11:16 +01:00
|
|
|
let ms = await apiListMembers(chat.chatInfo.apiId)
|
|
|
|
let memberContactIds = ms.compactMap{ m in m.memberCurrent ? m.memberContactId : nil }
|
2022-07-26 10:55:58 +04:00
|
|
|
return chatModel.chats
|
|
|
|
.compactMap{ $0.chatInfo.contact }
|
|
|
|
.filter{ !memberContactIds.contains($0.apiId) }
|
|
|
|
.sorted{ $0.displayName.lowercased() < $1.displayName.lowercased() }
|
|
|
|
}
|
|
|
|
|
2022-07-27 11:16:07 +04:00
|
|
|
func inviteMembersButton() -> some View {
|
|
|
|
Button {
|
|
|
|
Task {
|
|
|
|
for contactId in selectedContacts {
|
|
|
|
await addMember(groupId: chat.chatInfo.apiId, contactId: contactId, memberRole: selectedRole)
|
|
|
|
}
|
|
|
|
showSheet = false
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
HStack {
|
|
|
|
Text("Invite to group")
|
|
|
|
Image(systemName: "checkmark")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.frame(maxWidth: .infinity, alignment: .trailing)
|
|
|
|
}
|
|
|
|
|
|
|
|
func rolePicker() -> some View {
|
|
|
|
Picker("New member role", selection: $selectedRole) {
|
|
|
|
ForEach(GroupMemberRole.allCases) { role in
|
|
|
|
if role <= groupInfo.membership.memberRole {
|
|
|
|
Text(role.text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 10:55:58 +04:00
|
|
|
func contactCheckView(_ contact: Contact) -> some View {
|
|
|
|
let checked = selectedContacts.contains(contact.apiId)
|
|
|
|
return Button {
|
|
|
|
if checked {
|
|
|
|
selectedContacts.remove(contact.apiId)
|
|
|
|
} else {
|
|
|
|
selectedContacts.insert(contact.apiId)
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
HStack{
|
|
|
|
ProfileImage(imageStr: contact.image)
|
|
|
|
.frame(width: 30, height: 30)
|
|
|
|
.padding(.trailing, 2)
|
|
|
|
Text(ChatInfo.direct(contact: contact).chatViewName)
|
2022-07-27 11:16:07 +04:00
|
|
|
.foregroundColor(.primary)
|
2022-07-26 10:55:58 +04:00
|
|
|
.lineLimit(1)
|
|
|
|
Spacer()
|
|
|
|
Image(systemName: checked ? "checkmark.circle.fill": "circle")
|
2022-07-27 11:16:07 +04:00
|
|
|
.foregroundColor(checked ? .accentColor : Color(uiColor: .tertiaryLabel))
|
2022-07-26 10:55:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AddGroupMembersView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2022-07-26 12:33:10 +04:00
|
|
|
@State var showSheet = true
|
2022-07-27 11:16:07 +04:00
|
|
|
return AddGroupMembersView(chat: Chat(chatInfo: ChatInfo.sampleData.group), groupInfo: GroupInfo.sampleData, showSheet: $showSheet)
|
2022-07-26 10:55:58 +04:00
|
|
|
}
|
|
|
|
}
|