fix empty toast issue and improve the duration of toast

pull/1061/head
Ryan ZHAO 9 months ago
parent be95cb181f
commit 7daa8835a3

@ -14,10 +14,11 @@ extension ContextMenuVC {
struct Action { struct Action {
let icon: UIImage? let icon: UIImage?
let title: String let title: String
let feedback: String let feedback: String?
let expirationInfo: ExpirationInfo? let expirationInfo: ExpirationInfo?
let themeColor: ThemeValue let themeColor: ThemeValue
let actionType: ActionType let actionType: ActionType
let shouldDismissInfoScreen: Bool
let accessibilityLabel: String? let accessibilityLabel: String?
let work: () -> Void let work: () -> Void
@ -33,10 +34,11 @@ extension ContextMenuVC {
init( init(
icon: UIImage? = nil, icon: UIImage? = nil,
title: String = "", title: String = "",
feedback: String = "", feedback: String? = nil,
expirationInfo: ExpirationInfo? = nil, expirationInfo: ExpirationInfo? = nil,
themeColor: ThemeValue = .textPrimary, themeColor: ThemeValue = .textPrimary,
actionType: ActionType = .generic, actionType: ActionType = .generic,
shouldDismissInfoScreen: Bool = false,
accessibilityLabel: String? = nil, accessibilityLabel: String? = nil,
work: @escaping () -> Void work: @escaping () -> Void
) { ) {
@ -46,6 +48,7 @@ extension ContextMenuVC {
self.expirationInfo = expirationInfo self.expirationInfo = expirationInfo
self.themeColor = themeColor self.themeColor = themeColor
self.actionType = actionType self.actionType = actionType
self.shouldDismissInfoScreen = shouldDismissInfoScreen
self.accessibilityLabel = accessibilityLabel self.accessibilityLabel = accessibilityLabel
self.work = work self.work = work
} }
@ -75,6 +78,7 @@ extension ContextMenuVC {
return Action( return Action(
icon: UIImage(named: "ic_reply"), icon: UIImage(named: "ic_reply"),
title: "reply".localized(), title: "reply".localized(),
shouldDismissInfoScreen: true,
accessibilityLabel: "Reply to message" accessibilityLabel: "Reply to message"
) { delegate?.reply(cellViewModel, using: dependencies) } ) { delegate?.reply(cellViewModel, using: dependencies) }
} }
@ -107,6 +111,7 @@ extension ContextMenuVC {
expiresInSeconds: cellViewModel.expiresInSeconds expiresInSeconds: cellViewModel.expiresInSeconds
), ),
themeColor: .danger, themeColor: .danger,
shouldDismissInfoScreen: true,
accessibilityLabel: "Delete message" accessibilityLabel: "Delete message"
) { delegate?.delete(cellViewModel, using: dependencies) } ) { delegate?.delete(cellViewModel, using: dependencies) }
} }
@ -134,6 +139,7 @@ extension ContextMenuVC {
icon: UIImage(named: "ic_block"), icon: UIImage(named: "ic_block"),
title: "banDeleteAll".localized(), title: "banDeleteAll".localized(),
themeColor: .danger, themeColor: .danger,
shouldDismissInfoScreen: true,
accessibilityLabel: "Ban user and delete" accessibilityLabel: "Ban user and delete"
) { delegate?.banAndDeleteAllMessages(cellViewModel, using: dependencies) } ) { delegate?.banAndDeleteAllMessages(cellViewModel, using: dependencies) }
} }

@ -307,9 +307,12 @@ struct MessageInfoScreen: View {
action: { action: {
actions[index].work() actions[index].work()
feedbackMessage = actions[index].feedback feedbackMessage = actions[index].feedback
DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: { if actions[index].shouldDismissInfoScreen {
let deadline: DispatchTime = .now() + (feedbackMessage?.isEmpty == false ? 2 : 0)
DispatchQueue.main.asyncAfter(deadline: deadline, execute: {
dismiss() dismiss()
}) })
}
}, },
label: { label: {
HStack(spacing: Values.largeSpacing) { HStack(spacing: Values.largeSpacing) {

@ -2,6 +2,7 @@
import SwiftUI import SwiftUI
import Combine import Combine
import NaturalLanguage
public struct ToastModifier: ViewModifier { public struct ToastModifier: ViewModifier {
@Binding var message: String? @Binding var message: String?
@ -34,7 +35,17 @@ public struct ToastModifier: ViewModifier {
} }
workItem = task workItem = task
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5, execute: task)
let duration: TimeInterval = {
guard let message: String = message else { return 1.5 }
let tokenizer = NLTokenizer(unit: .word)
tokenizer.string = message
let wordCount = tokenizer.tokens(for: message.startIndex..<message.endIndex).count
return min(1.5 + Double(wordCount - 1) * 0.1 , 5)
}()
DispatchQueue.main.asyncAfter(deadline: .now() + duration, execute: task)
} }
private func dismissToast() { private func dismissToast() {

Loading…
Cancel
Save