class ReportMessageHandler {
private var flagReasons: [FlagReason] = []
// Load flag reasons (call this on app init or when needed)
func loadFlagReasons(completion: @escaping ([FlagReason]) -> Void) {
CometChat.getFlagReasons { [weak self] reasons in
self?.flagReasons = reasons
completion(reasons)
} onError: { error in
print("Failed to load flag reasons: \(error?.errorDescription ?? "")")
completion([])
}
}
// Get reasons for UI display
func getReasons() -> [FlagReason] {
return flagReasons
}
// Flag a message with selected reason
func flagMessage(
messageId: Int,
reasonId: String,
remark: String? = nil,
completion: @escaping (Bool, String?) -> Void
) {
let flagDetail = FlagDetail(
messageId: messageId,
reasonId: reasonId,
remark: remark ?? ""
)
CometChat.flagMessage(messageId: messageId, detail: flagDetail) { response in
completion(true, response)
} onError: { error in
completion(false, error?.errorDescription)
}
}
}
// Usage
let reportHandler = ReportMessageHandler()
// Load reasons when app initializes
reportHandler.loadFlagReasons { reasons in
// Display reasons in UI for user to select
}
// When user submits the report
reportHandler.flagMessage(messageId: 123, reasonId: "spam", remark: "User is sending promotional links") { success, message in
if success {
showToast("Message reported successfully")
}
}