/* * Copyright (c) 2010-2023 Belledonne Communications SARL. * * This file is part of linphone-iphone * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ import SwiftUI import linphonesw // swiftlint:disable type_body_length struct StartCallFragment: View { private var idiom: UIUserInterfaceIdiom { UIDevice.current.userInterfaceIdiom } @ObservedObject var contactsManager = ContactsManager.shared @ObservedObject var magicSearch = MagicSearchSingleton.shared @ObservedObject private var telecomManager = TelecomManager.shared @EnvironmentObject var callViewModel: CallViewModel @StateObject private var startCallViewModel = StartCallViewModel() @State private var transferAddress: Address? = nil @State private var isShowTransferPopup: Bool = false @Binding var isShowStartCallFragment: Bool @Binding var showingDialer: Bool @FocusState var isSearchFieldFocused: Bool @State private var delayedColor = Color.white var resetCallView: () -> Void var body: some View { NavigationView { if #available(iOS 16.4, *), idiom != .pad { startCall .sheet(isPresented: $showingDialer) { DialerBottomSheet( startCallViewModel: startCallViewModel, callViewModel: callViewModel, isShowStartCallFragment: $isShowStartCallFragment, showingDialer: $showingDialer, currentCall: nil ) .presentationDetents([.medium]) .presentationBackgroundInteraction(.enabled(upThrough: .medium)) } } else { startCall .halfSheet(showSheet: $showingDialer) { DialerBottomSheet( startCallViewModel: startCallViewModel, callViewModel: callViewModel, isShowStartCallFragment: $isShowStartCallFragment, showingDialer: $showingDialer, currentCall: nil ) } onDismiss: {} } } .navigationViewStyle(StackNavigationViewStyle()) } var startCall: some View { ZStack { VStack(spacing: 1) { Rectangle() .foregroundColor(delayedColor) .edgesIgnoringSafeArea(.top) .frame(height: 0) .task(delayColor) HStack { Image("caret-left") .renderingMode(.template) .resizable() .foregroundStyle(Color.orangeMain500) .frame(width: 25, height: 25, alignment: .leading) .padding(.all, 10) .padding(.top, 2) .padding(.leading, -10) .onTapGesture { startCallViewModel.searchField = "" magicSearch.currentFilter = "" magicSearch.searchForContacts() if callViewModel.isTransferInsteadCall == true { callViewModel.isTransferInsteadCall = false } resetCallView() delayColorDismiss() withAnimation { isShowStartCallFragment.toggle() } } let address = callViewModel.currentCall?.remoteAddress let nameTmp = address?.displayName ?? address?.username ?? String(address?.asStringUriOnly().dropFirst(4) ?? "") Text( !callViewModel.isTransferInsteadCall ? String(localized: "history_call_start_title") : String( format: String(localized: "call_transfer_current_call_title"), nameTmp ) ) .multilineTextAlignment(.leading) .default_text_style_orange_800(styleSize: 16) Spacer() } .frame(maxWidth: .infinity) .frame(height: 50) .padding(.horizontal) .padding(.bottom, 4) .background(.white) VStack(spacing: 0) { if callViewModel.isTransferInsteadCall { HStack(alignment: .center) { Text("call_transfer_active_calls_label") .default_text_style_800(styleSize: 16) Spacer() } .padding(.vertical, 10) .padding(.horizontal, 16) if let remoteAddress = callViewModel.currentCall?.remoteAddress, !callViewModel.calls.filter({ $0.remoteAddress?.equal(address2: remoteAddress) == false }).isEmpty { callsList(remoteAddress: remoteAddress) .padding(.bottom, 10) } else { HStack(alignment: .center) { Text("call_transfer_no_active_call_label") .default_text_style_700(styleSize: 16) } .padding(.bottom, 10) } } ZStack(alignment: .trailing) { TextField("history_call_start_search_bar_filter_hint", text: $startCallViewModel.searchField) .default_text_style(styleSize: 15) .frame(height: 25) .focused($isSearchFieldFocused) .padding(.horizontal, 30) .onChange(of: startCallViewModel.searchField) { newValue in magicSearch.currentFilter = newValue magicSearch.searchForContacts() } .simultaneousGesture(TapGesture().onEnded { showingDialer = false }) HStack { Button(action: { }, label: { Image("magnifying-glass") .renderingMode(.template) .resizable() .foregroundStyle(Color.grayMain2c500) .frame(width: 25, height: 25) }) Spacer() if startCallViewModel.searchField.isEmpty { Button(action: { if !showingDialer { isSearchFieldFocused = false DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { showingDialer = true } } else { showingDialer = false DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { isSearchFieldFocused = true } } }, label: { Image(!showingDialer ? "dialer" : "keyboard") .renderingMode(.template) .resizable() .foregroundStyle(Color.grayMain2c500) .frame(width: 25, height: 25) }) } else { Button(action: { startCallViewModel.searchField = "" magicSearch.currentFilter = "" magicSearch.searchForContacts() }, label: { Image("x") .renderingMode(.template) .resizable() .foregroundStyle(Color.grayMain2c500) .frame(width: 25, height: 25) }) } } } .padding(.horizontal, 15) .padding(.vertical, 10) .cornerRadius(60) .overlay( RoundedRectangle(cornerRadius: 60) .inset(by: 0.5) .stroke(isSearchFieldFocused ? Color.orangeMain500 : Color.gray200, lineWidth: 1) ) .padding(.vertical) .padding(.horizontal) if !startCallViewModel.hideGroupCallButton { NavigationLink(destination: { StartGroupCallFragment(isShowStartCallFragment: $isShowStartCallFragment) .environmentObject(startCallViewModel) }, label: { HStack { HStack(alignment: .center) { Image("video-conference") .renderingMode(.template) .resizable() .foregroundStyle(.white) .frame(width: 28, height: 28) } .padding(10) .background(Color.orangeMain500) .cornerRadius(40) Text("history_call_start_create_group_call") .foregroundStyle(.black) .default_text_style_800(styleSize: 16) Spacer() Image("caret-right") .renderingMode(.template) .resizable() .foregroundStyle(Color.grayMain2c500) .frame(width: 25, height: 25, alignment: .leading) } }) .padding(.vertical, 10) .padding(.horizontal, 20) .background( LinearGradient(gradient: Gradient(colors: [.grayMain2c100, .white]), startPoint: .leading, endPoint: .trailing) .padding(.vertical, 10) .padding(.horizontal, 40) ) } ZStack { ScrollView { if !ContactsManager.shared.lastSearch.isEmpty { HStack(alignment: .center) { Text("contacts_list_all_contacts_title") .default_text_style_800(styleSize: 16) Spacer() } .padding(.vertical, 10) .padding(.horizontal, 16) } ContactsListFragment(showingSheet: .constant(false) , startCallFunc: { addr in if callViewModel.isTransferInsteadCall { self.transferAddress = addr self.isShowTransferPopup = true } else { showingDialer = false startCallViewModel.searchField = "" magicSearch.currentFilter = "" magicSearch.searchForContacts() if callViewModel.isTransferInsteadCall == true { callViewModel.isTransferInsteadCall = false } resetCallView() delayColorDismiss() withAnimation { isShowStartCallFragment.toggle() telecomManager.doCallOrJoinConf(address: addr) } } }) .padding(.horizontal, 16) if !contactsManager.lastSearchSuggestions.isEmpty { HStack(alignment: .center) { Text("generic_address_picker_suggestions_list_title") .default_text_style_800(styleSize: 16) Spacer() } .padding(.vertical, 10) .padding(.horizontal, 16) suggestionsList } } if magicSearch.isLoading { ProgressView() .controlSize(.large) .progressViewStyle(CircularProgressViewStyle(tint: .orangeMain500)) } } } .frame(maxWidth: .infinity) } .background(.white) if isShowTransferPopup { PopupView( isShowPopup: $isShowTransferPopup, title: Text("history_dialog_delete_all_call_logs_title"), content: Text("history_dialog_delete_all_call_logs_message"), titleFirstButton: nil, actionFirstButton: {}, titleSecondButton: Text("dialog_confirm"), actionSecondButton: { showingDialer = false startCallViewModel.searchField = "" magicSearch.currentFilter = "" magicSearch.searchForContacts() if callViewModel.isTransferInsteadCall == true { callViewModel.isTransferInsteadCall = false } resetCallView() delayColorDismiss() withAnimation { isShowStartCallFragment.toggle() if let transferAddress = self.transferAddress { callViewModel.blindTransferCallTo(toAddress: transferAddress) self.transferAddress = nil } } self.isShowTransferPopup.toggle() }, titleThirdButton: Text("dialog_cancel"), actionThirdButton: { self.isShowTransferPopup.toggle() } ) .background(.black.opacity(0.65)) .zIndex(3) .onTapGesture { self.isShowTransferPopup.toggle() } } } .navigationTitle("") .navigationBarHidden(true) .onAppear { if !magicSearch.currentFilter.isEmpty || (self.contactsManager.lastSearch.isEmpty && self.contactsManager.lastSearchSuggestions.isEmpty) { magicSearch.currentFilter = "" magicSearch.searchForContacts() } } } @Sendable private func delayColor() async { try? await Task.sleep(nanoseconds: 250_000_000) delayedColor = Color.orangeMain500 } func delayColorDismiss() { Task { try? await Task.sleep(nanoseconds: 80_000_000) delayedColor = .white } } func callsList(remoteAddress: Address) -> some View { ForEach(0..