Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if events must be on main #2253

Merged
merged 17 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 64 additions & 79 deletions DcCore/DcCore/DC/events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,118 +56,106 @@ public class DcEventHandler {

case DC_EVENT_CONFIGURE_PROGRESS:
logger.info("📡[\(accountId)] configure: \(Int(data1))")
DispatchQueue.main.async {
let done = Int(data1) == 1000

NotificationCenter.default.post(name: Event.configurationProgress, object: nil, userInfo: [
"progress": Int(data1),
"error": Int(data1) == 0,
"done": done,
"errorMessage": event.data2String,
])

if done {
UserDefaults.standard.set(true, forKey: Constants.Keys.deltachatUserProvidedCredentialsKey)
UserDefaults.standard.synchronize()
}
}


let done = Int(data1) == 1000

NotificationCenter.default.post(name: Event.configurationProgress, object: nil, userInfo: [
"progress": Int(data1),
"error": Int(data1) == 0,
"done": done,
"errorMessage": event.data2String,
])

if done {
UserDefaults.standard.set(true, forKey: Constants.Keys.deltachatUserProvidedCredentialsKey)
}

case DC_EVENT_IMEX_PROGRESS:
DispatchQueue.main.async {
NotificationCenter.default.post(name: Event.importExportProgress, object: nil, userInfo: [
"progress": Int(data1),
"error": Int(data1) == 0,
"done": Int(data1) == 1000,
"errorMessage": self.dcAccounts.get(id: accountId).lastErrorString,
])
}
NotificationCenter.default.post(name: Event.importExportProgress, object: nil, userInfo: [
"progress": Int(data1),
"error": Int(data1) == 0,
"done": Int(data1) == 1000,
"errorMessage": self.dcAccounts.get(id: accountId).lastErrorString,
])

case DC_EVENT_MSGS_CHANGED:
guard accountId == dcAccounts.getSelected().id else { return }

logger.info("📡[\(accountId)] msgs changed: \(data1), \(data2)")
DispatchQueue.main.async {
NotificationCenter.default.post(name: Event.messagesChanged, object: nil, userInfo: [
"message_id": Int(data2),
"chat_id": Int(data1),
])
}

NotificationCenter.default.post(name: Event.messagesChanged, object: nil, userInfo: [
"message_id": Int(data2),
"chat_id": Int(data1),
])

case DC_EVENT_REACTIONS_CHANGED, DC_EVENT_MSG_READ, DC_EVENT_MSG_DELIVERED, DC_EVENT_MSG_FAILED:
guard accountId == dcAccounts.getSelected().id else { return }

logger.info("📡[\(accountId)] msgs reaction/read/delivered/failed: \(data1), \(data2)")
DispatchQueue.main.async {
NotificationCenter.default.post(name: Event.messageReadDeliveredFailedReaction, object: nil, userInfo: [
"message_id": Int(data2),
"chat_id": Int(data1),
])
}

NotificationCenter.default.post(name: Event.messageReadDeliveredFailedReaction, object: nil, userInfo: [
"message_id": Int(data2),
"chat_id": Int(data1),
])

case DC_EVENT_MSGS_NOTICED:
if accountId != dcAccounts.getSelected().id {
return
}
DispatchQueue.main.async {
NotificationCenter.default.post(name: Event.messagesNoticed, object: nil, userInfo: [
"chat_id": Int(data1),
])
}

NotificationCenter.default.post(name: Event.messagesNoticed, object: nil, userInfo: [
"chat_id": Int(data1),
])

case DC_EVENT_CHAT_MODIFIED:
if accountId != dcAccounts.getSelected().id {
return
}
logger.info("📡[\(accountId)] chat modified: \(data1)")
DispatchQueue.main.async {
NotificationCenter.default.post(name: Event.chatModified, object: nil, userInfo: [
"chat_id": Int(data1),
])
}

NotificationCenter.default.post(name: Event.chatModified, object: nil, userInfo: [
"chat_id": Int(data1),
])

case DC_EVENT_CHAT_EPHEMERAL_TIMER_MODIFIED:
if accountId != dcAccounts.getSelected().id {
return
}
logger.info("📡[\(accountId)] ephemeral timer modified: \(data1)")
DispatchQueue.main.async {
NotificationCenter.default.post(name: Event.ephemeralTimerModified, object: nil, userInfo: [
"chat_id": Int(data1),
])
}

NotificationCenter.default.post(name: Event.ephemeralTimerModified, object: nil, userInfo: [
"chat_id": Int(data1),
])

case DC_EVENT_INCOMING_MSG:
DispatchQueue.main.async {
NotificationCenter.default.post(name: Event.incomingMessageOnAnyAccount, object: nil)
}

NotificationCenter.default.post(name: Event.incomingMessageOnAnyAccount, object: nil)
if accountId != dcAccounts.getSelected().id {
return
}
logger.info("📡[\(accountId)] incoming message \(data2)")
DispatchQueue.main.async {
NotificationCenter.default.post(name: Event.incomingMessage, object: nil, userInfo: [
"message_id": Int(data2),
"chat_id": Int(data1),
])
}

NotificationCenter.default.post(name: Event.incomingMessage, object: nil, userInfo: [
"message_id": Int(data2),
"chat_id": Int(data1),
])

case DC_EVENT_CONTACTS_CHANGED:
if accountId != dcAccounts.getSelected().id {
return
}
logger.info("📡[\(accountId)] contact changed: \(data1)")
DispatchQueue.main.async {
NotificationCenter.default.post(name: Event.contactsChanged, object: nil, userInfo: [
"contact_id": Int(data1)
])
}

NotificationCenter.default.post(name: Event.contactsChanged, object: nil, userInfo: [
"contact_id": Int(data1)
])

case DC_EVENT_CONNECTIVITY_CHANGED:
if accountId != dcAccounts.getSelected().id {
return
}
logger.info("📡[\(accountId)] connectivity changed")
DispatchQueue.main.async {
NotificationCenter.default.post(name: Event.connectivityChanged, object: nil)
}
NotificationCenter.default.post(name: Event.connectivityChanged, object: nil)

case DC_EVENT_ACCOUNTS_BACKGROUND_FETCH_DONE:
if let sem = dcAccounts.fetchSemaphore {
Expand All @@ -179,22 +167,19 @@ public class DcEventHandler {
return
}
logger.info("📡[\(accountId)] webxdc update")
DispatchQueue.main.async {
NotificationCenter.default.post(name: Event.webxdcStatusUpdate, object: nil, userInfo: [
"message_id": Int(data1),
])
}
NotificationCenter.default.post(name: Event.webxdcStatusUpdate, object: nil, userInfo: [
"message_id": Int(data1),
])

case DC_EVENT_WEBXDC_REALTIME_DATA:
if accountId != dcAccounts.getSelected().id {
return
}
DispatchQueue.main.async {
NotificationCenter.default.post(name: Event.webxdcRealtimeDataReceived, object: nil, userInfo: [
"message_id": Int(data1),
"data": event.data2Data,
])
}

NotificationCenter.default.post(name: Event.webxdcRealtimeDataReceived, object: nil, userInfo: [
"message_id": Int(data1),
"data": event.data2Data,
])

default:
break
Expand Down
11 changes: 5 additions & 6 deletions deltachat-ios/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
static func emitMsgsChangedIfShareExtensionWasUsed() {
if let userDefaults = UserDefaults.shared, userDefaults.bool(forKey: UserDefaults.hasExtensionAttemptedToSend) {
userDefaults.removeObject(forKey: UserDefaults.hasExtensionAttemptedToSend)
DispatchQueue.main.async {
NotificationCenter.default.post(name: Event.messagesChanged, object: nil, userInfo: [
"message_id": Int(0),
"chat_id": Int(0),
])
}

NotificationCenter.default.post(name: Event.messagesChanged, object: nil, userInfo: [
"message_id": Int(0),
"chat_id": Int(0),
])
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,11 @@ class InstantOnboardingViewController: UIViewController {
}

private func handleCreateSuccess() {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
appDelegate.registerForNotifications()
appDelegate.reloadDcContext()
DispatchQueue.main.async {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
appDelegate.registerForNotifications()
appDelegate.reloadDcContext()
}
zeitschlag marked this conversation as resolved.
Show resolved Hide resolved
}

private func storeImageAndName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class WelcomeViewController: UIViewController {
backupProgressObserver = NotificationCenter.default.addObserver(
forName: Event.importExportProgress,
object: nil,
queue: nil
queue: OperationQueue.main
) { [weak self] notification in
self?.handleImportExportProgress(notification, importByFile: importByFile)
}
Expand Down
69 changes: 36 additions & 33 deletions deltachat-ios/Controller/BackupTransferViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,43 +166,46 @@ class BackupTransferViewController: UIViewController {
@objc private func handleImportExportProgress(_ notification: Notification) {
guard let ui = notification.userInfo, let permille = ui["progress"] as? Int, isFinishing == false else { return }

var statusLineText: String?
var hideQrCode = false

if permille == 0 {
if self.transferState != TranferState.error {
self.transferState = TranferState.error
self.showLastErrorAlert("Error")
DispatchQueue.main.async { [weak self] in
guard let self else { return }

var statusLineText: String?
var hideQrCode = false

if permille == 0 {
if self.transferState != TranferState.error {
self.transferState = TranferState.error
self.showLastErrorAlert("Error")
}
hideQrCode = true
} else if permille <= 350 {
statusLineText = nil
} else if permille <= 400 {
statusLineText = nil
} else if permille <= 450 {
statusLineText = String.localized("receiver_connected")
hideQrCode = true
} else if permille < 1000 {
let percent = (permille-450)/5
statusLineText = String.localized("transferring") + " \(percent)%" // TODO: use a scrollbar, show precide percentage only for receiver
hideQrCode = true
} else if permille == 1000 {
self.transferState = TranferState.success
self.navigationItem.leftBarButtonItem = nil // "Cancel" no longer fits as things are done
statusLineText = String.localized("done") + " 😀"
hideQrCode = true
}
hideQrCode = true
} else if permille <= 350 {
statusLineText = nil
} else if permille <= 400 {
statusLineText = nil
} else if permille <= 450 {
statusLineText = String.localized("receiver_connected")
hideQrCode = true
} else if permille < 1000 {
let percent = (permille-450)/5
statusLineText = String.localized("transferring") + " \(percent)%" // TODO: use a scrollbar, show precide percentage only for receiver
hideQrCode = true
} else if permille == 1000 {
self.transferState = TranferState.success
self.navigationItem.leftBarButtonItem = nil // "Cancel" no longer fits as things are done
statusLineText = String.localized("done") + " 😀"
hideQrCode = true
}

if let statusLineText = statusLineText {
self.statusLine.text = statusLineText
}
if let statusLineText = statusLineText {
self.statusLine.text = statusLineText
}

if hideQrCode && !self.qrContentView.isHidden {
self.statusLine.textAlignment = .center
experimentalLine.isHidden = true
self.qrContentView.isHidden = true
if hideQrCode && !self.qrContentView.isHidden {
self.statusLine.textAlignment = .center
experimentalLine.isHidden = true
self.qrContentView.isHidden = true
}
}

}

// MARK: - setup
Expand Down
4 changes: 3 additions & 1 deletion deltachat-ios/Controller/ChatListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ class ChatListViewController: UITableViewController {
}

@objc private func handleIncomingMessageOnAnyAccount(_ notification: Notification) {
updateAccountButton()
DispatchQueue.main.async { [weak self] in
self?.updateAccountButton()
}
}

private func setupSubviews() {
Expand Down
3 changes: 0 additions & 3 deletions deltachat-ios/Controller/FullMessageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,13 @@ class FullMessageViewController: WebViewViewController {

@objc func alwaysActionPressed(_ action: UIAlertAction) {
UserDefaults.standard.set(true, forKey: "html_load_remote_content")
UserDefaults.standard.synchronize()
loadContentOnce = false
loadUnrestricedHtml()
}

@objc func onceActionPressed(_ action: UIAlertAction) {
if !isHalfBlocked {
UserDefaults.standard.set(false, forKey: "html_load_remote_content")
UserDefaults.standard.synchronize()
}
loadContentOnce = true
loadUnrestricedHtml()
Expand All @@ -112,7 +110,6 @@ class FullMessageViewController: WebViewViewController {
@objc func neverActionPressed(_ action: UIAlertAction) {
if !isHalfBlocked {
UserDefaults.standard.set(false, forKey: "html_load_remote_content")
UserDefaults.standard.synchronize()
}
loadContentOnce = false
loadRestrictedHtml()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ class BackgroundOptionsViewController: UIViewController, MediaPickerDelegate {
@objc private func onDefaultSelected() {
setDefault(backgroundContainer)
UserDefaults.standard.set(nil, forKey: Constants.Keys.backgroundImageName)
UserDefaults.standard.synchronize()
}

private func setDefault(_ imageView: UIImageView) {
Expand All @@ -156,7 +155,6 @@ class BackgroundOptionsViewController: UIViewController, MediaPickerDelegate {
func onImageSelected(image: UIImage) {
if let path = ImageFormat.saveImage(image: image, name: Constants.backgroundImageName) {
UserDefaults.standard.set(URL(fileURLWithPath: path).lastPathComponent, forKey: Constants.Keys.backgroundImageName)
UserDefaults.standard.synchronize()
backgroundContainer.sd_setImage(with: URL(fileURLWithPath: path), placeholderImage: nil, options: .refreshCached, completed: nil)
} else {
logger.error("failed to save background image")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ internal final class SettingsViewController: UITableViewController {
} else {
NotificationManager.removeAllNotifications()
}
UserDefaults.standard.synchronize()
zeitschlag marked this conversation as resolved.
Show resolved Hide resolved

NotificationManager.updateBadgeCounters()
NotificationCenter.default.post(name: Event.messagesChanged, object: nil, userInfo: ["message_id": Int(0), "chat_id": Int(0)])
}
Expand Down
Loading
Loading