ソースを参照

use shared variable for singletons in swift instead of getInstance()

cyberta 5 年 前
コミット
98187d6b6a

+ 1 - 1
deltachat-ios/AppDelegate.swift

@@ -16,7 +16,7 @@ enum ApplicationState {
 
 
 @UIApplicationMain
 @UIApplicationMain
 class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
 class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
-    private let dcContext = DcContext.getInstance()
+    private let dcContext = DcContext.shared
     var appCoordinator: AppCoordinator!
     var appCoordinator: AppCoordinator!
     var relayHelper: RelayHelper!
     var relayHelper: RelayHelper!
     var locationManager: LocationManager!
     var locationManager: LocationManager!

+ 1 - 1
deltachat-ios/Controller/EditGroupViewController.swift

@@ -77,7 +77,7 @@ class EditGroupViewController: UITableViewController, MediaPickerDelegate {
         if let groupImage = groupImage, let dcContext = coordinator?.dcContext {
         if let groupImage = groupImage, let dcContext = coordinator?.dcContext {
             AvatarHelper.saveChatAvatar(dcContext: dcContext, image: groupImage, for: Int(chat.id))
             AvatarHelper.saveChatAvatar(dcContext: dcContext, image: groupImage, for: Int(chat.id))
         }
         }
-        _ = DcContext.getInstance().setChatName(chatId: chat.id, name: newName ?? "")
+        _ = DcContext.shared.setChatName(chatId: chat.id, name: newName ?? "")
         coordinator?.navigateBack()
         coordinator?.navigateBack()
     }
     }
 
 

+ 1 - 1
deltachat-ios/Controller/GroupMembersViewController.swift

@@ -324,7 +324,7 @@ class GroupMembersViewController: UITableViewController, UISearchResultsUpdating
     var selectedContactIds: Set<Int> = []
     var selectedContactIds: Set<Int> = []
 
 
     init() {
     init() {
-        self.dcContext = DcContext.getInstance()
+        self.dcContext = DcContext.shared
         super.init(style: .grouped)
         super.init(style: .grouped)
         hidesBottomBarWhenPushed = true
         hidesBottomBarWhenPushed = true
     }
     }

+ 1 - 1
deltachat-ios/Controller/NewChatViewController.swift

@@ -47,7 +47,7 @@ class NewChatViewController: UITableViewController {
     var hud: ProgressHud?
     var hud: ProgressHud?
 
 
     lazy var deviceContactHandler: DeviceContactsHandler = {
     lazy var deviceContactHandler: DeviceContactsHandler = {
-        let handler = DeviceContactsHandler(dcContext: DcContext.getInstance())
+        let handler = DeviceContactsHandler(dcContext: DcContext.shared)
         handler.contactListDelegate = self
         handler.contactListDelegate = self
         return handler
         return handler
     }()
     }()

+ 15 - 15
deltachat-ios/DC/Wrapper.swift

@@ -19,7 +19,7 @@ class DcContext {
         dc_context_unref(contextPointer)
         dc_context_unref(contextPointer)
     }
     }
 
 
-    static func getInstance() -> DcContext {
+    static var shared: DcContext {
         return .dcContext
         return .dcContext
     }
     }
 
 
@@ -586,7 +586,7 @@ class DcChat {
 
 
     // use DcContext.getChat() instead of calling the constructor directly
     // use DcContext.getChat() instead of calling the constructor directly
     init(id: Int) {
     init(id: Int) {
-        if let p = dc_get_chat(DcContext.getInstance().contextPointer, UInt32(id)) {
+        if let p = dc_get_chat(DcContext.shared.contextPointer, UInt32(id)) {
             chatPointer = p
             chatPointer = p
         } else {
         } else {
             fatalError("Invalid chatID opened \(id)")
             fatalError("Invalid chatID opened \(id)")
@@ -654,7 +654,7 @@ class DcChat {
     }
     }
 
 
     var contactIds: [Int] {
     var contactIds: [Int] {
-        return Utils.copyAndFreeArray(inputArray: dc_get_chat_contacts(DcContext.getInstance().contextPointer, UInt32(id)))
+        return Utils.copyAndFreeArray(inputArray: dc_get_chat_contacts(DcContext.shared.contextPointer, UInt32(id)))
     }
     }
 
 
     lazy var profileImage: UIImage? = { [unowned self] in
     lazy var profileImage: UIImage? = { [unowned self] in
@@ -713,15 +713,15 @@ class DcMsg: MessageType {
             DC_MSG_FILE
             DC_MSG_FILE
      */
      */
     init(viewType: Int32) {
     init(viewType: Int32) {
-        messagePointer = dc_msg_new(DcContext.getInstance().contextPointer, viewType)
+        messagePointer = dc_msg_new(DcContext.shared.contextPointer, viewType)
     }
     }
 
 
     init(id: Int) {
     init(id: Int) {
-        messagePointer = dc_get_msg(DcContext.getInstance().contextPointer, UInt32(id))
+        messagePointer = dc_get_msg(DcContext.shared.contextPointer, UInt32(id))
     }
     }
 
 
     init(type: Int32) {
     init(type: Int32) {
-        messagePointer = dc_msg_new(DcContext.getInstance().contextPointer, type)
+        messagePointer = dc_msg_new(DcContext.shared.contextPointer, type)
     }
     }
 
 
     deinit {
     deinit {
@@ -1009,31 +1009,31 @@ class DcMsg: MessageType {
     }
     }
 
 
     func sendInChat(id: Int) {
     func sendInChat(id: Int) {
-        dc_send_msg(DcContext.getInstance().contextPointer, UInt32(id), messagePointer)
+        dc_send_msg(DcContext.shared.contextPointer, UInt32(id), messagePointer)
     }
     }
 
 
     func previousMediaURLs() -> [URL] {
     func previousMediaURLs() -> [URL] {
         var urls: [URL] = []
         var urls: [URL] = []
-        var prev: Int = Int(dc_get_next_media(DcContext.getInstance().contextPointer, UInt32(id), -1, Int32(type), 0, 0))
+        var prev: Int = Int(dc_get_next_media(DcContext.shared.contextPointer, UInt32(id), -1, Int32(type), 0, 0))
         while prev != 0 {
         while prev != 0 {
             let prevMessage = DcMsg(id: prev)
             let prevMessage = DcMsg(id: prev)
             if let url = prevMessage.fileURL {
             if let url = prevMessage.fileURL {
                 urls.insert(url, at: 0)
                 urls.insert(url, at: 0)
             }
             }
-            prev = Int(dc_get_next_media(DcContext.getInstance().contextPointer, UInt32(prevMessage.id), -1, Int32(prevMessage.type), 0, 0))
+            prev = Int(dc_get_next_media(DcContext.shared.contextPointer, UInt32(prevMessage.id), -1, Int32(prevMessage.type), 0, 0))
         }
         }
         return urls
         return urls
     }
     }
 
 
     func nextMediaURLs() -> [URL] {
     func nextMediaURLs() -> [URL] {
         var urls: [URL] = []
         var urls: [URL] = []
-        var next: Int = Int(dc_get_next_media(DcContext.getInstance().contextPointer, UInt32(id), 1, Int32(type), 0, 0))
+        var next: Int = Int(dc_get_next_media(DcContext.shared.contextPointer, UInt32(id), 1, Int32(type), 0, 0))
         while next != 0 {
         while next != 0 {
             let nextMessage = DcMsg(id: next)
             let nextMessage = DcMsg(id: next)
             if let url = nextMessage.fileURL {
             if let url = nextMessage.fileURL {
                 urls.append(url)
                 urls.append(url)
             }
             }
-            next = Int(dc_get_next_media(DcContext.getInstance().contextPointer, UInt32(nextMessage.id), 1, Int32(nextMessage.type), 0, 0))
+            next = Int(dc_get_next_media(DcContext.shared.contextPointer, UInt32(nextMessage.id), 1, Int32(nextMessage.type), 0, 0))
         }
         }
         return urls
         return urls
     }
     }
@@ -1043,7 +1043,7 @@ class DcContact {
     private var contactPointer: OpaquePointer?
     private var contactPointer: OpaquePointer?
 
 
     init(id: Int) {
     init(id: Int) {
-        contactPointer = dc_get_contact(DcContext.getInstance().contextPointer, UInt32(id))
+        contactPointer = dc_get_contact(DcContext.shared.contextPointer, UInt32(id))
     }
     }
 
 
     deinit {
     deinit {
@@ -1112,15 +1112,15 @@ class DcContact {
     }
     }
 
 
     func block() {
     func block() {
-        dc_block_contact(DcContext.getInstance().contextPointer, UInt32(id), 1)
+        dc_block_contact(DcContext.shared.contextPointer, UInt32(id), 1)
     }
     }
 
 
     func unblock() {
     func unblock() {
-        dc_block_contact(DcContext.getInstance().contextPointer, UInt32(id), 0)
+        dc_block_contact(DcContext.shared.contextPointer, UInt32(id), 0)
     }
     }
 
 
     func marknoticed() {
     func marknoticed() {
-        dc_marknoticed_contact(DcContext.getInstance().contextPointer, UInt32(id))
+        dc_marknoticed_contact(DcContext.shared.contextPointer, UInt32(id))
     }
     }
 }
 }
 
 

+ 1 - 1
deltachat-ios/DC/events.swift

@@ -131,7 +131,7 @@ public func callbackSwift(event: CInt, data1: CUnsignedLong, data2: CUnsignedLon
                 logger.info("notifications: added \(content)")
                 logger.info("notifications: added \(content)")
             }
             }
 
 
-            let array = DcContext.getInstance().getFreshMessages()
+            let array = DcContext.shared.getFreshMessages()
             UIApplication.shared.applicationIconBadgeNumber = array.count
             UIApplication.shared.applicationIconBadgeNumber = array.count
         }
         }
 
 

+ 1 - 1
deltachat-ios/View/ContactCell.swift

@@ -252,7 +252,7 @@ class ContactCell: UITableViewCell {
             titleLabel.attributedText = cellViewModel.title.boldAt(indexes: cellViewModel.titleHighlightIndexes, fontSize: titleLabel.font.pointSize)
             titleLabel.attributedText = cellViewModel.title.boldAt(indexes: cellViewModel.titleHighlightIndexes, fontSize: titleLabel.font.pointSize)
 
 
         case .chat(let chatData):
         case .chat(let chatData):
-            let chat = DcContext.getInstance().getChat(chatId: chatData.chatId)
+            let chat = DcContext.shared.getChat(chatId: chatData.chatId)
 
 
             // text bold if chat contains unread messages - otherwise hightlight search results if needed
             // text bold if chat contains unread messages - otherwise hightlight search results if needed
             if chatData.unreadMessages > 0 {
             if chatData.unreadMessages > 0 {