From 0142a9146bfdec4595ce154031001f413beaafdf Mon Sep 17 00:00:00 2001 From: Benoit Martins Date: Mon, 20 Nov 2023 17:00:19 +0100 Subject: [PATCH] Display toast when user copy adress or delete call logs in History views --- Linphone.xcodeproj/project.pbxproj | 4 ++ Linphone/Core/CoreContext.swift | 9 ++-- Linphone/LinphoneApp.swift | 2 - .../Fragments/QrCodeScannerFragment.swift | 4 +- .../UI/Assistant/Viewmodel/QRScanner.swift | 6 ++- .../Fragments/ContactListBottomSheet.swift | 3 +- Linphone/UI/Main/ContentView.swift | 9 ++++ Linphone/UI/Main/Fragments/ToastView.swift | 51 ++++++++----------- .../Fragments/HistoryContactFragment.swift | 4 +- .../Fragments/HistoryListBottomSheet.swift | 3 +- Linphone/UI/Main/History/HistoryView.swift | 1 - .../Main/Viewmodel/SharedMainViewModel.swift | 2 - .../UI/Main/Viewmodel/ToastViewModel.swift | 19 +++++++ 13 files changed, 73 insertions(+), 44 deletions(-) create mode 100644 Linphone/UI/Main/Viewmodel/ToastViewModel.swift diff --git a/Linphone.xcodeproj/project.pbxproj b/Linphone.xcodeproj/project.pbxproj index 71f3fce2e..64e88979f 100644 --- a/Linphone.xcodeproj/project.pbxproj +++ b/Linphone.xcodeproj/project.pbxproj @@ -46,6 +46,7 @@ D777DBB32AE12C5900565A99 /* ContactsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D777DBB22AE12C5900565A99 /* ContactsManager.swift */; }; D78290B82ADD3910004AA85C /* ContactsFragment.swift in Sources */ = {isa = PBXBuildFile; fileRef = D78290B72ADD3910004AA85C /* ContactsFragment.swift */; }; D78290BB2ADD40B2004AA85C /* ContactViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D78290BA2ADD40B2004AA85C /* ContactViewModel.swift */; }; + D796F2002B0BB61A0041115F /* ToastViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D796F1FF2B0BB61A0041115F /* ToastViewModel.swift */; }; D7A03FBD2ACC2DB60081A588 /* ContactsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7A03FBC2ACC2DB60081A588 /* ContactsView.swift */; }; D7A03FC02ACC2E390081A588 /* HistoryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7A03FBF2ACC2E390081A588 /* HistoryView.swift */; }; D7A03FC62ACC458A0081A588 /* SplashScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7A03FC52ACC458A0081A588 /* SplashScreen.swift */; }; @@ -117,6 +118,7 @@ D777DBB22AE12C5900565A99 /* ContactsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactsManager.swift; sourceTree = ""; }; D78290B72ADD3910004AA85C /* ContactsFragment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactsFragment.swift; sourceTree = ""; }; D78290BA2ADD40B2004AA85C /* ContactViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactViewModel.swift; sourceTree = ""; }; + D796F1FF2B0BB61A0041115F /* ToastViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToastViewModel.swift; sourceTree = ""; }; D7A03FBC2ACC2DB60081A588 /* ContactsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactsView.swift; sourceTree = ""; }; D7A03FBF2ACC2E390081A588 /* HistoryView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoryView.swift; sourceTree = ""; }; D7A03FC52ACC458A0081A588 /* SplashScreen.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SplashScreen.swift; sourceTree = ""; }; @@ -386,6 +388,7 @@ isa = PBXGroup; children = ( D7A2EDD52AC18115005D90FC /* SharedMainViewModel.swift */, + D796F1FF2B0BB61A0041115F /* ToastViewModel.swift */, ); path = Viewmodel; sourceTree = ""; @@ -529,6 +532,7 @@ D719ABC92ABC6FD700B41C10 /* CoreContext.swift in Sources */, D7EAACCF2AD6ED8000AA6A8A /* PermissionsFragment.swift in Sources */, D777DBB32AE12C5900565A99 /* ContactsManager.swift in Sources */, + D796F2002B0BB61A0041115F /* ToastViewModel.swift in Sources */, D7C3650A2AF001C300FE6142 /* EditContactFragment.swift in Sources */, D7A03FBD2ACC2DB60081A588 /* ContactsView.swift in Sources */, D719ABCF2ABC779A00B41C10 /* AccountLoginViewModel.swift in Sources */, diff --git a/Linphone/Core/CoreContext.swift b/Linphone/Core/CoreContext.swift index 386b9f07b..4a76095bc 100644 --- a/Linphone/Core/CoreContext.swift +++ b/Linphone/Core/CoreContext.swift @@ -71,9 +71,11 @@ final class CoreContext: ObservableObject { self.mCore.publisher?.onConfiguringStatus?.postOnMainQueue { (cbVal: (core: Core, status: Config.ConfiguringState, message: String)) in NSLog("New configuration state is \(cbVal.status) = \(cbVal.message)\n") if cbVal.status == Config.ConfiguringState.Successful { - self.sharedMainViewModel.toastMessage = "Successful" + ToastViewModel.shared.toastMessage = "Successful" + ToastViewModel.shared.displayToast.toggle() } else { - self.sharedMainViewModel.toastMessage = "Failed" + ToastViewModel.shared.toastMessage = "Failed" + ToastViewModel.shared.displayToast.toggle() } } @@ -90,7 +92,8 @@ final class CoreContext: ObservableObject { } else if cbVal.state == .Progress { self.loggingInProgress = true } else { - self.sharedMainViewModel.toastMessage = "Registration failed" + ToastViewModel.shared.toastMessage = "Registration failed" + ToastViewModel.shared.displayToast.toggle() self.loggingInProgress = false self.loggedIn = false } diff --git a/Linphone/LinphoneApp.swift b/Linphone/LinphoneApp.swift index 52b399973..6371e5bee 100644 --- a/Linphone/LinphoneApp.swift +++ b/Linphone/LinphoneApp.swift @@ -34,7 +34,6 @@ struct LinphoneApp: App { WelcomeView() } else if coreContext.defaultAccount == nil || sharedMainViewModel.displayProfileMode { AssistantView() - .toast(isShowing: $sharedMainViewModel.toastMessage) } else if coreContext.defaultAccount != nil { ContentView( contactViewModel: ContactViewModel(), @@ -42,7 +41,6 @@ struct LinphoneApp: App { historyViewModel: HistoryViewModel(), historyListViewModel: HistoryListViewModel() ) - .toast(isShowing: $sharedMainViewModel.toastMessage) } } else { SplashScreen(isActive: $isActive) diff --git a/Linphone/UI/Assistant/Fragments/QrCodeScannerFragment.swift b/Linphone/UI/Assistant/Fragments/QrCodeScannerFragment.swift index 31045592c..a12a98419 100644 --- a/Linphone/UI/Assistant/Fragments/QrCodeScannerFragment.swift +++ b/Linphone/UI/Assistant/Fragments/QrCodeScannerFragment.swift @@ -55,13 +55,15 @@ struct QrCodeScannerFragment: View { .edgesIgnoringSafeArea(.all) .navigationBarHidden(true) - if sharedMainViewModel.toastMessage == "Successful" { + /* + if $isShowToast { ZStack { }.onAppear { dismiss() } } + */ } } diff --git a/Linphone/UI/Assistant/Viewmodel/QRScanner.swift b/Linphone/UI/Assistant/Viewmodel/QRScanner.swift index a7f3d2eeb..014cf08f3 100644 --- a/Linphone/UI/Assistant/Viewmodel/QRScanner.swift +++ b/Linphone/UI/Assistant/Viewmodel/QRScanner.swift @@ -77,10 +77,12 @@ class Coordinator: NSObject, AVCaptureMetadataOutputObjectsDelegate { try? core.start() } } else { - sharedMainViewModel.toastMessage = "Invalide URI" + ToastViewModel.shared.toastMessage = "Invalide URI" + ToastViewModel.shared.displayToast.toggle() } } else { - sharedMainViewModel.toastMessage = "Invalide URI" + ToastViewModel.shared.toastMessage = "Invalide URI" + ToastViewModel.shared.displayToast.toggle() } } } diff --git a/Linphone/UI/Main/Contacts/Fragments/ContactListBottomSheet.swift b/Linphone/UI/Main/Contacts/Fragments/ContactListBottomSheet.swift index a35158ec4..0a883e3de 100644 --- a/Linphone/UI/Main/Contacts/Fragments/ContactListBottomSheet.swift +++ b/Linphone/UI/Main/Contacts/Fragments/ContactListBottomSheet.swift @@ -70,7 +70,8 @@ struct ContactListBottomSheet: View { dismiss() } - sharedMainViewModel.toastMessage = "Success_copied_into_clipboard" + ToastViewModel.shared.toastMessage = "Success_copied_into_clipboard" + ToastViewModel.shared.displayToast.toggle() } label: { HStack { diff --git a/Linphone/UI/Main/ContentView.swift b/Linphone/UI/Main/ContentView.swift index 8b7ea36b9..cdffd8cd0 100644 --- a/Linphone/UI/Main/ContentView.swift +++ b/Linphone/UI/Main/ContentView.swift @@ -26,6 +26,7 @@ struct ContentView: View { @Environment(\.scenePhase) var scenePhase @ObservedObject private var coreContext = CoreContext.shared + @ObservedObject private var sharedMainViewModel = SharedMainViewModel.shared var contactManager = ContactsManager.shared var magicSearch = MagicSearchSingleton.shared @@ -543,6 +544,9 @@ struct ContentView: View { historyListViewModel.removeCallLogs() self.isShowDeleteAllHistoryPopup.toggle() historyViewModel.displayedCall = nil + + ToastViewModel.shared.toastMessage = "Success_remove_call_logs" + ToastViewModel.shared.displayToast.toggle() }) .background(.black.opacity(0.65)) .zIndex(3) @@ -580,6 +584,11 @@ struct ContentView: View { self.isShowDismissPopup.toggle() } } + + //if sharedMainViewModel.displayToast { + ToastView() + .zIndex(3) + //} } } .overlay { diff --git a/Linphone/UI/Main/Fragments/ToastView.swift b/Linphone/UI/Main/Fragments/ToastView.swift index 4caf6acab..40d5e7e16 100644 --- a/Linphone/UI/Main/Fragments/ToastView.swift +++ b/Linphone/UI/Main/Fragments/ToastView.swift @@ -19,31 +19,21 @@ import SwiftUI -struct ToastView: ViewModifier { +struct ToastView: View { - @ObservedObject private var sharedMainViewModel = SharedMainViewModel.shared + @ObservedObject private var toastViewModel = ToastViewModel.shared - @Binding var isShowing: String - - func body(content: Content) -> some View { - ZStack { - content - toastView - .padding(.top, 60) - } - } - - private var toastView: some View { + var body: some View { VStack { - if !isShowing.isEmpty { + if toastViewModel.displayToast { HStack { - Image(isShowing.contains("Success") ? "check" : "warning-circle") + Image(toastViewModel.toastMessage.contains("Success") ? "check" : "warning-circle") .resizable() .renderingMode(.template) .frame(width: 25, height: 25, alignment: .leading) - .foregroundStyle(isShowing.contains("Success") ? Color.greenSuccess500 : Color.redDanger500) + .foregroundStyle(toastViewModel.toastMessage.contains("Success") ? Color.greenSuccess500 : Color.redDanger500) - switch isShowing { + switch toastViewModel.toastMessage { case "Successful": Text("QR code validated!") .multilineTextAlignment(.center) @@ -100,29 +90,30 @@ struct ToastView: ViewModifier { .overlay( RoundedRectangle(cornerRadius: 50) .inset(by: 0.5) - .stroke(isShowing.contains("Success") ? Color.greenSuccess500 : Color.redDanger500, lineWidth: 1) + .stroke(toastViewModel.toastMessage.contains("Success") ? Color.greenSuccess500 : Color.redDanger500, lineWidth: 1) ) .onTapGesture { - isShowing = "" + withAnimation { + toastViewModel.toastMessage = "" + toastViewModel.displayToast = false + } } .onAppear { DispatchQueue.main.asyncAfter(deadline: .now() + 2) { - isShowing = "" + withAnimation { + toastViewModel.toastMessage = "" + toastViewModel.displayToast = false + } } } + + Spacer() } - Spacer() } - .frame(maxWidth: sharedMainViewModel.maxWidth) + .frame(maxWidth: SharedMainViewModel.shared.maxWidth) .padding(.horizontal, 16) .padding(.bottom, 18) - .animation(.linear(duration: 0.3), value: isShowing) - .transition(.opacity) - } -} - -extension View { - func toast(isShowing: Binding) -> some View { - self.modifier(ToastView(isShowing: isShowing)) + .transition(.move(edge: .top)) + .padding(.top, 60) } } diff --git a/Linphone/UI/Main/History/Fragments/HistoryContactFragment.swift b/Linphone/UI/Main/History/Fragments/HistoryContactFragment.swift index cb4916f6f..07cc6a2a5 100644 --- a/Linphone/UI/Main/History/Fragments/HistoryContactFragment.swift +++ b/Linphone/UI/Main/History/Fragments/HistoryContactFragment.swift @@ -132,7 +132,9 @@ struct HistoryContactFragment: View { ) } - sharedMainViewModel.toastMessage = "Success_copied_into_clipboard" + ToastViewModel.shared.toastMessage = "Success_copied_into_clipboard" + ToastViewModel.shared.displayToast.toggle() + } label: { HStack { Text("Copy SIP address") diff --git a/Linphone/UI/Main/History/Fragments/HistoryListBottomSheet.swift b/Linphone/UI/Main/History/Fragments/HistoryListBottomSheet.swift index 233c18f57..e5a58b79d 100644 --- a/Linphone/UI/Main/History/Fragments/HistoryListBottomSheet.swift +++ b/Linphone/UI/Main/History/Fragments/HistoryListBottomSheet.swift @@ -164,7 +164,8 @@ struct HistoryListBottomSheet: View { dismiss() } - sharedMainViewModel.toastMessage = "Success_copied_into_clipboard" + ToastViewModel.shared.toastMessage = "Success_copied_into_clipboard" + ToastViewModel.shared.displayToast.toggle() } label: { HStack { diff --git a/Linphone/UI/Main/History/HistoryView.swift b/Linphone/UI/Main/History/HistoryView.swift index 8b4977b97..13c857b14 100644 --- a/Linphone/UI/Main/History/HistoryView.swift +++ b/Linphone/UI/Main/History/HistoryView.swift @@ -42,7 +42,6 @@ struct HistoryView: View { ) Button { - SharedMainViewModel.shared.toastMessage = "Success_remove_call_logs" } label: { Image("phone-plus") .padding() diff --git a/Linphone/UI/Main/Viewmodel/SharedMainViewModel.swift b/Linphone/UI/Main/Viewmodel/SharedMainViewModel.swift index 189962e19..abb48824b 100644 --- a/Linphone/UI/Main/Viewmodel/SharedMainViewModel.swift +++ b/Linphone/UI/Main/Viewmodel/SharedMainViewModel.swift @@ -23,8 +23,6 @@ class SharedMainViewModel: ObservableObject { static let shared = SharedMainViewModel() - @Published var toastMessage: String = "" - @Published var welcomeViewDisplayed = false @Published var generalTermsAccepted = false @Published var displayProfileMode = false diff --git a/Linphone/UI/Main/Viewmodel/ToastViewModel.swift b/Linphone/UI/Main/Viewmodel/ToastViewModel.swift new file mode 100644 index 000000000..6fb3287a9 --- /dev/null +++ b/Linphone/UI/Main/Viewmodel/ToastViewModel.swift @@ -0,0 +1,19 @@ +// +// ToastViewModel.swift +// Linphone +// +// Created by BenoƮt Martins on 20/11/2023. +// + +import Foundation + +class ToastViewModel: ObservableObject { + + static let shared = ToastViewModel() + + var toastMessage: String = "" + @Published var displayToast = false + + private init() { + } +}