ios: fix modal views not closing (#723)

This commit is contained in:
JRoberts
2022-06-03 13:19:41 +04:00
committed by GitHub
parent 800efb3a34
commit 3b708105a4
6 changed files with 36 additions and 33 deletions

View File

@@ -13,6 +13,7 @@ struct ContentView: View {
@ObservedObject var callController = CallController.shared
@Binding var doAuthenticate: Bool
@State private var userAuthorized: Bool?
@State private var showChatInfo: Bool = false // TODO comprehensively close modal views on authentication
@AppStorage(DEFAULT_SHOW_LA_NOTICE) private var prefShowLANotice = false
@AppStorage(DEFAULT_LA_NOTICE_SHOWN) private var prefLANoticeShown = false
@AppStorage(DEFAULT_PERFORM_LA) private var prefPerformLA = false
@@ -24,9 +25,9 @@ struct ContentView: View {
} else {
if let step = chatModel.onboardingStage {
if case .onboardingComplete = step,
let user = chatModel.currentUser {
chatModel.currentUser != nil {
ZStack(alignment: .top) {
ChatListView(user: user)
ChatListView(showChatInfo: $showChatInfo)
.onAppear {
NtfManager.shared.requestAuthorization(onDeny: {
alertManager.showAlert(notificationAlert())
@@ -58,20 +59,18 @@ struct ContentView: View {
if !prefPerformLA {
userAuthorized = true
} else {
chatModel.showChatInfo = false
DispatchQueue.main.async() {
userAuthorized = false
authenticate(reason: NSLocalizedString("Unlock", comment: "authentication reason")) { laResult in
switch (laResult) {
case .success:
userAuthorized = true
case .failed:
AlertManager.shared.showAlert(laFailedAlert())
case .unavailable:
userAuthorized = true
prefPerformLA = false
AlertManager.shared.showAlert(laUnavailableTurningOffAlert())
}
showChatInfo = false
userAuthorized = false
authenticate(reason: NSLocalizedString("Unlock", comment: "authentication reason")) { laResult in
switch (laResult) {
case .success:
userAuthorized = true
case .failed:
AlertManager.shared.showAlert(laFailedAlert())
case .unavailable:
userAuthorized = true
prefPerformLA = false
AlertManager.shared.showAlert(laUnavailableTurningOffAlert())
}
}
}

View File

@@ -14,7 +14,6 @@ import WebKit
final class ChatModel: ObservableObject {
@Published var onboardingStage: OnboardingStage?
@Published var currentUser: User?
@Published var showChatInfo: Bool = false // TODO comprehensively close modal views on authentication
// list of chat "previews"
@Published var chats: [Chat] = []
// current chat

View File

@@ -12,6 +12,7 @@ struct ChatInfoView: View {
@EnvironmentObject var chatModel: ChatModel
@ObservedObject var alertManager = AlertManager.shared
@ObservedObject var chat: Chat
@Binding var showChatInfo: Bool
@State var alert: ChatInfoViewAlert? = nil
@State var deletingContact: Contact?
@@ -98,7 +99,7 @@ struct ChatInfoView: View {
try await apiDeleteChat(type: .direct, id: contact.apiId)
DispatchQueue.main.async {
chatModel.removeChat(contact.id)
chatModel.showChatInfo = false
showChatInfo = false
}
} catch let error {
logger.error("ChatInfoView.deleteContactAlert apiDeleteChat error: \(error.localizedDescription)")
@@ -117,7 +118,7 @@ struct ChatInfoView: View {
Task {
await clearChat(chat)
DispatchQueue.main.async {
chatModel.showChatInfo = false
showChatInfo = false
}
}
},
@@ -129,6 +130,6 @@ struct ChatInfoView: View {
struct ChatInfoView_Previews: PreviewProvider {
static var previews: some View {
@State var showChatInfo = true
return ChatInfoView(chat: Chat(chatInfo: ChatInfo.sampleData.direct, chatItems: []))
return ChatInfoView(chat: Chat(chatInfo: ChatInfo.sampleData.direct, chatItems: []), showChatInfo: $showChatInfo)
}
}

View File

@@ -15,6 +15,7 @@ struct ChatView: View {
@Environment(\.colorScheme) var colorScheme
@AppStorage(DEFAULT_EXPERIMENTAL_CALLS) private var enableCalls = false
@ObservedObject var chat: Chat
@Binding var showChatInfo: Bool
@State private var composeState = ComposeState()
@State private var deletingItem: ChatItem? = nil
@FocusState private var keyboardVisible: Bool
@@ -97,12 +98,12 @@ struct ChatView: View {
}
ToolbarItem(placement: .principal) {
Button {
chatModel.showChatInfo = true
showChatInfo = true
} label: {
ChatInfoToolbar(chat: chat)
}
.sheet(isPresented: $chatModel.showChatInfo) {
ChatInfoView(chat: chat)
.sheet(isPresented: $showChatInfo) {
ChatInfoView(chat: chat, showChatInfo: $showChatInfo)
}
}
ToolbarItem(placement: .navigationBarTrailing) {
@@ -269,7 +270,8 @@ struct ChatView_Previews: PreviewProvider {
ChatItem.getSample(8, .directSnd, .now, "👍👍👍👍"),
ChatItem.getSample(9, .directSnd, .now, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
]
return ChatView(chat: Chat(chatInfo: ChatInfo.sampleData.direct, chatItems: []))
@State var showChatInfo = false
return ChatView(chat: Chat(chatInfo: ChatInfo.sampleData.direct, chatItems: []), showChatInfo: $showChatInfo)
.environmentObject(chatModel)
}
}

View File

@@ -11,6 +11,7 @@ import SwiftUI
struct ChatListNavLink: View {
@EnvironmentObject var chatModel: ChatModel
@State var chat: Chat
@Binding var showChatInfo: Bool
@State private var showContactRequestDialog = false
var body: some View {
@@ -27,7 +28,7 @@ struct ChatListNavLink: View {
}
private func chatView() -> some View {
ChatView(chat: chat)
ChatView(chat: chat, showChatInfo: $showChatInfo)
.onAppear {
do {
let cInfo = chat.chatInfo
@@ -278,19 +279,20 @@ struct ChatListNavLink: View {
struct ChatListNavLink_Previews: PreviewProvider {
static var previews: some View {
@State var chatId: String? = "@1"
@State var showChatInfo = false
return Group {
ChatListNavLink(chat: Chat(
chatInfo: ChatInfo.sampleData.direct,
chatItems: [ChatItem.getSample(1, .directSnd, .now, "hello")]
))
), showChatInfo: $showChatInfo)
ChatListNavLink(chat: Chat(
chatInfo: ChatInfo.sampleData.direct,
chatItems: [ChatItem.getSample(1, .directSnd, .now, "hello")]
))
), showChatInfo: $showChatInfo)
ChatListNavLink(chat: Chat(
chatInfo: ChatInfo.sampleData.contactRequest,
chatItems: []
))
), showChatInfo: $showChatInfo)
}
.previewLayout(.fixed(width: 360, height: 80))
}

View File

@@ -10,18 +10,17 @@ import SwiftUI
struct ChatListView: View {
@EnvironmentObject var chatModel: ChatModel
@Binding var showChatInfo: Bool
// not really used in this view
@State private var showSettings = false
@State private var searchText = ""
@AppStorage(DEFAULT_PENDING_CONNECTIONS) private var pendingConnections = true
var user: User
var body: some View {
let v = NavigationView {
List {
ForEach(filteredChats()) { chat in
ChatListNavLink(chat: chat)
ChatListNavLink(chat: chat, showChatInfo: $showChatInfo)
.padding(.trailing, -16)
}
}
@@ -92,10 +91,11 @@ struct ChatListView_Previews: PreviewProvider {
)
]
@State var showChatInfo = false
return Group {
ChatListView(user: User.sampleData)
ChatListView(showChatInfo: $showChatInfo)
.environmentObject(chatModel)
ChatListView(user: User.sampleData)
ChatListView(showChatInfo: $showChatInfo)
.environmentObject(ChatModel())
}
}