浏览代码

Add final into classes

Sergey 2 年之前
父节点
当前提交
c99ee0f065

+ 0 - 6
Chat.xcodeproj/project.pbxproj

@@ -23,8 +23,6 @@
 		2F6045A6288E19ED008F005E /* KeyboardReadable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F6045A4288E19ED008F005E /* KeyboardReadable.swift */; };
 		2F6045A9288E9B2C008F005E /* XMPPFramework in Frameworks */ = {isa = PBXBuildFile; productRef = 2F6045A8288E9B2C008F005E /* XMPPFramework */; };
 		2F6045AB288E9B2C008F005E /* XMPPFrameworkSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 2F6045AA288E9B2C008F005E /* XMPPFrameworkSwift */; };
-		2F6045AD288E9B73008F005E /* XMPPController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F6045AC288E9B73008F005E /* XMPPController.swift */; };
-		2F6045AE288E9B73008F005E /* XMPPController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F6045AC288E9B73008F005E /* XMPPController.swift */; };
 		2F6045B0288E9D3E008F005E /* ChatListViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F6045AF288E9D3E008F005E /* ChatListViewModel.swift */; };
 		2F6045B1288E9D3E008F005E /* ChatListViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F6045AF288E9D3E008F005E /* ChatListViewModel.swift */; };
 		2F6045B3288EA0E6008F005E /* LoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F6045B2288EA0E6008F005E /* LoginView.swift */; };
@@ -56,7 +54,6 @@
 		2F60459B288D8000008F005E /* MessageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageView.swift; sourceTree = "<group>"; };
 		2F60459E288D869B008F005E /* ChatListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatListView.swift; sourceTree = "<group>"; };
 		2F6045A4288E19ED008F005E /* KeyboardReadable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyboardReadable.swift; sourceTree = "<group>"; };
-		2F6045AC288E9B73008F005E /* XMPPController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XMPPController.swift; sourceTree = "<group>"; };
 		2F6045AF288E9D3E008F005E /* ChatListViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatListViewModel.swift; sourceTree = "<group>"; };
 		2F6045B2288EA0E6008F005E /* LoginView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginView.swift; sourceTree = "<group>"; };
 		2F6045B5288EA10B008F005E /* LoginViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginViewModel.swift; sourceTree = "<group>"; };
@@ -177,7 +174,6 @@
 		2FCB762528AA59620097BD1D /* Managers */ = {
 			isa = PBXGroup;
 			children = (
-				2F6045AC288E9B73008F005E /* XMPPController.swift */,
 				2F81974028ACD9A6008D8C45 /* StreamManager.swift */,
 				2F81974428ACDBCF008D8C45 /* RosterManager.swift */,
 				2F81974928ACF91E008D8C45 /* MUCManager.swift */,
@@ -295,7 +291,6 @@
 				2F81974328ACDBC4008D8C45 /* RoomManager.swift in Sources */,
 				2F81974C28ACFDEA008D8C45 /* Room.swift in Sources */,
 				2F60459C288D8000008F005E /* MessageView.swift in Sources */,
-				2F6045AD288E9B73008F005E /* XMPPController.swift in Sources */,
 				2F81974128ACD9A6008D8C45 /* StreamManager.swift in Sources */,
 				2F6045B0288E9D3E008F005E /* ChatListViewModel.swift in Sources */,
 				2FCB75EA28A425900097BD1D /* RoomsListViewModel.swift in Sources */,
@@ -319,7 +314,6 @@
 			files = (
 				2FCB75EE28A42B1E0097BD1D /* ChatViewModel.swift in Sources */,
 				2F60459D288D8000008F005E /* MessageView.swift in Sources */,
-				2F6045AE288E9B73008F005E /* XMPPController.swift in Sources */,
 				2F6045B1288E9D3E008F005E /* ChatListViewModel.swift in Sources */,
 				2FCB75EB28A425900097BD1D /* RoomsListViewModel.swift in Sources */,
 				2FCB75E828A425580097BD1D /* RoomsListView.swift in Sources */,

+ 1 - 1
Shared/Auth/LoginViewModel.swift

@@ -7,7 +7,7 @@
 
 import Foundation
 
-class LoginViewModel: ObservableObject {
+final class LoginViewModel: ObservableObject {
     let manager = StreamManager.shared
 
     @Published var login: String = "test11@msg.sharix-app.org"

+ 2 - 2
Shared/ContentView.swift

@@ -41,7 +41,7 @@ struct ContentView: View {
                             }
                             .tag(Tab.settings)
                     }
-                    .navigationBarTitle(returnNaviBarTitle(tabSelection: self.selectedTab))
+                    .navigationBarTitle(returnNaviBarTitle(self.selectedTab))
                 }
             } else {
                 LoginView(viewModel: loginViewModel, authStatus: $authStatus)
@@ -49,7 +49,7 @@ struct ContentView: View {
         }
     }
 
-    func returnNaviBarTitle(tabSelection: Tab) -> String {
+    func returnNaviBarTitle(_ tabSelection: Tab) -> String {
         switch tabSelection {
         case .contacts: return "Контакты"
         case .rooms: return "Чаты"

+ 1 - 1
Shared/Core/ChatListViewModel.swift

@@ -9,7 +9,7 @@ import Foundation
 import XMPPFramework
 import XMPPFrameworkSwift
 
-class ChatListViewModel: ObservableObject {
+final class ChatListViewModel: ObservableObject {
     let manager = RosterManager()
 
     @Published var users: [User] = []

+ 1 - 1
Shared/Core/ChatView.swift

@@ -65,7 +65,7 @@ struct ChatView: View, KeyboardReadable {
                     .padding(.horizontal, 12)
                     .background(.background)
                 Button {
-                    viewModel.messageText = ""
+                    viewModel.sendMessage()
                 } label: {
                     Image(systemName: "arrow.up.circle.fill")
                         .font(.title)

+ 4 - 3
Shared/Core/ChatViewModel.swift

@@ -9,11 +9,11 @@ import Foundation
 import XMPPFramework
 import XMPPFrameworkSwift
 
-class ChatViewModelExample {
+final class ChatViewModelExample {
     static let messages: [String] = ["Display a short text message, called an alert, that draws attention to something new in your app.", "Play a notification sound.", "Set a badge number on the app’s icon to let the user know there are new items.", "Provide actions the user can take without opening the app.", "Show a media attachment.", "Be silent, allowing the app to perform a task in the background.", "No", "Group notifications into threads.", "Yes", "Edit or remove delivered notifications.", "Run code to change your notification before displaying it.", "Display a custom, interactive UI for your notification.", "And probably more.", "Yes", "Edit or remove delivered notifications.", "Run code to change your notification before displaying it.", "Display a custom, interactive UI for your notification.", "And probably more."]
 }
 
-class ChatViewModel: NSObject, ObservableObject {
+final class ChatViewModel: NSObject, ObservableObject {
     let manager: RoomManager
 
     @Published var messageText: String = ""
@@ -37,7 +37,8 @@ class ChatViewModel: NSObject, ObservableObject {
 
     func sendMessage() {
         if !self.messageText.isEmpty {
-            self.manager.room.sendMessage(withBody: self.messageText)
+            manager.sendMessage(self.messageText)
+            self.messageText = ""
         }
     }
 }

+ 1 - 1
Shared/Core/RoomsListViewModel.swift

@@ -9,7 +9,7 @@ import Foundation
 import XMPPFramework
 import XMPPFrameworkSwift
 
-class RoomsListViewModel: ObservableObject {
+final class RoomsListViewModel: ObservableObject {
     let manager = MUCManager()
 
     @Published var rooms: [Room] = []

+ 1 - 1
Shared/Managers/MUCManager.swift

@@ -9,7 +9,7 @@ import Foundation
 import XMPPFramework
 import XMPPFrameworkSwift
 
-class MUCManager: NSObject {
+final class MUCManager: NSObject {
     var muc: XMPPMUCLight
 
     var rooms: [Room] = []

+ 22 - 6
Shared/Managers/RoomManager.swift

@@ -9,11 +9,13 @@ import Foundation
 import XMPPFramework
 import XMPPFrameworkSwift
 
-class RoomManager: NSObject {
+final class RoomManager: NSObject {
     var room: XMPPRoom
     var storage: XMPPRoomMemoryStorage
     var mam: XMPPMessageArchiveManagement
 
+    var jid: XMPPJID?
+
     var messages: [XMPPMessage] = []
 
     init(with jidString: String) {
@@ -34,11 +36,25 @@ class RoomManager: NSObject {
     func fetchMessages() {
         self.messages.removeAll()
 
-        let field = DDXMLElement(name: "field")
-        field.addAttribute(withName: "var", stringValue: "with")
-        field.addChild(DDXMLElement(name: "value", stringValue: self.room.roomJID.bare))
+        if self.room.isJoined {
+            self.mam.retrieveMessageArchive(at: self.room.roomJID.bareJID, withFields: nil, with: nil)
+        } else {
+            let field = DDXMLElement(name: "field")
+            field.addAttribute(withName: "var", stringValue: "with")
+            field.addChild(DDXMLElement(name: "value", stringValue: self.room.roomJID.bare))
 
-        self.mam.retrieveMessageArchive(at: nil, withFields: [field], with: nil)
+            self.mam.retrieveMessageArchive(at: nil, withFields: [field], with: nil)
+        }
+    }
+
+    func sendMessage(_ text: String) {
+        let xmppMessage = XMPPMessage(messageType: room.isJoined ? .chat : .normal, to: self.room.roomJID.bareJID)
+        xmppMessage.addBody(text)
+        if self.room.isJoined {
+            self.room.send(xmppMessage)
+        } else {
+            StreamManager.shared.stream.send(xmppMessage)
+        }
     }
 }
 
@@ -76,6 +92,6 @@ extension RoomManager: XMPPMessageArchiveManagementDelegate {
     }
 
     func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didFinishReceivingMessagesWith resultSet: XMPPResultSet) {
-        
+        debugPrint("FINISH MAM")
     }
 }

+ 1 - 1
Shared/Managers/RosterManager.swift

@@ -9,7 +9,7 @@ import Foundation
 import XMPPFramework
 import XMPPFrameworkSwift
 
-class RosterManager {
+final class RosterManager {
     var roster: XMPPRoster
     var storage: XMPPRosterMemoryStorage
 

+ 1 - 1
Shared/Managers/StreamManager.swift

@@ -17,7 +17,7 @@ final class StreamManager: NSObject {
 
     var password: String
 
-    override init() {
+    private override init() {
         self.stream = XMPPStream()
 
         self.password = ""

+ 0 - 171
Shared/Managers/XMPPController.swift

@@ -1,171 +0,0 @@
-//
-//  XMPPController.swift
-//  Chat
-//
-//  Created by Sergey Tarasov on 25.07.2022.
-//
-
-import Foundation
-import XMPPFramework
-import XMPPFrameworkSwift
-
-/*
-final class XMPPController: NSObject {
-    static let shared = XMPPController()
-
-    var xmppStream: XMPPStream
-    var xmppReconnect: XMPPReconnect
-    var xmppRoster: XMPPRoster
-    var xmppRosterStorage: XMPPRosterMemoryStorage
-    var xmppMUC: XMPPMUCLight
-    var xmppMAM: XMPPMessageArchiveManagement
-    var xmppCapabilities: XMPPCapabilities
-    var xmppCapabilitiesStorage: XMPPCapabilitiesStorage
-    var xmppPing: XMPPPing
-    var xmppTime: XMPPTime
-
-    var users: [XMPPJID] = []
-    var rooms: [XMPPRoom] = []
-    var messages: [String] = []
-
-    var password: String?
-
-    override init() {
-        self.xmppStream = XMPPStream()
-        self.xmppReconnect = XMPPReconnect()
-        self.xmppRosterStorage = XMPPRosterMemoryStorage()
-        self.xmppRoster = XMPPRoster(rosterStorage: self.xmppRosterStorage, dispatchQueue: DispatchQueue.main)
-        self.xmppMUC = XMPPMUCLight(dispatchQueue: DispatchQueue.main)
-        self.xmppMAM = XMPPMessageArchiveManagement(dispatchQueue: DispatchQueue.main)
-        self.xmppCapabilitiesStorage = XMPPCapabilitiesCoreDataStorage.sharedInstance()
-        self.xmppCapabilities = XMPPCapabilities(capabilitiesStorage: self.xmppCapabilitiesStorage)
-        self.xmppCapabilities.autoFetchHashedCapabilities = true
-        self.xmppCapabilities.autoFetchNonHashedCapabilities = false
-        self.xmppPing = XMPPPing()
-        self.xmppTime = XMPPTime()
-
-        self.xmppReconnect.activate(self.xmppStream)
-        self.xmppRoster.activate(self.xmppStream)
-        self.xmppMUC.activate(self.xmppStream)
-        self.xmppMAM.activate(self.xmppStream)
-        self.xmppCapabilities.activate(self.xmppStream)
-        self.xmppPing.activate(self.xmppStream)
-        self.xmppTime.activate(self.xmppStream)
-
-        super.init()
-
-        self.xmppStream.addDelegate(self, delegateQueue: DispatchQueue.main)
-        self.xmppReconnect.addDelegate(self, delegateQueue: DispatchQueue.main)
-        self.xmppMUC.addDelegate(self, delegateQueue: DispatchQueue.main)
-        self.xmppMAM.addDelegate(self, delegateQueue: DispatchQueue.main)
-        self.xmppCapabilities.addDelegate(self, delegateQueue: DispatchQueue.main)
-    }
-
-    func setupStream(with login: String?, password: String?) {
-        self.password = password ?? ""
-        self.xmppStream.hostName = "msg.sharix-app.org"
-        self.xmppStream.hostPort = 5222
-        self.xmppStream.myJID = XMPPJID(string: login ?? "test11@msg.sharix-app.org")
-        self.xmppStream.startTLSPolicy = XMPPStreamStartTLSPolicy.allowed
-    }
-
-    func connect() {
-        if !self.xmppStream.isDisconnected { return }
-        try! self.xmppStream.connect(withTimeout: XMPPStreamTimeoutNone)
-    }
-
-    func getUserList() {
-        self.users = self.xmppRosterStorage.jids(for: self.xmppStream)
-    }
-
-    func getRoomList() {
-        self.xmppMUC.discoverRooms(forServiceNamed: "chat.msg.sharix-app.org")
-    }
-
-    func getMessages(from jid: XMPPJID) {
-        self.messages.removeAll()
-
-        let field = DDXMLElement(name: "field")
-        field.addAttribute(withName: "var", stringValue: "with")
-        let fieldValue = DDXMLElement(name: "value", stringValue: jid.bare)
-
-        field.addChild(fieldValue)
-
-        self.xmppMAM.retrieveMessageArchive(withFields: nil, with: nil)
-    }
-}
-
-extension XMPPController: XMPPMessageArchiveManagementDelegate {
-    func xmppMessageArchiveManagement(
-        _ xmppMessageArchiveManagement: XMPPMessageArchiveManagement,
-        didReceiveFormFields iq: XMPPIQ
-    ) {
-        print(iq.debugDescription)
-    }
-
-    func xmppMessageArchiveManagement(
-        _ xmppMessageArchiveManagement: XMPPMessageArchiveManagement,
-        didReceiveMAMMessage message: XMPPMessage
-    ) {
-        print(message.debugDescription)
-        guard let messageText = message.body else { return }
-        self.messages.append(messageText)
-    }
-
-    func xmppMessageArchiveManagement(
-        _ xmppMessageArchiveManagement: XMPPMessageArchiveManagement,
-        didFinishReceivingMessagesWith resultSet: XMPPResultSet
-    ) {
-        print(resultSet.debugDescription)
-    }
-}
-
-extension XMPPController: XMPPReconnectDelegate {
-    func xmppReconnect(
-        _ sender: XMPPReconnect,
-        shouldAttemptAutoReconnect connectionFlags: SCNetworkConnectionFlags
-    ) -> Bool {
-        return true
-    }
-}
-
-extension XMPPController: XMPPStreamDelegate {
-    func xmppStreamDidConnect(_ stream: XMPPStream) {
-        print("Stream: Connected")
-        try! stream.authenticate(withPassword: self.password ?? "test11_-")
-    }
-
-    func xmppStreamDidAuthenticate(_ sender: XMPPStream) {
-        self.xmppStream.send(XMPPPresence())
-        print("Stream: Authenticated")
-    }
-
-    func xmppStream(_ sender: XMPPStream, willReceive message: XMPPMessage) -> XMPPMessage? {
-        guard let body = message.body else {
-            return nil
-        }
-        debugPrint(body)
-        return message
-    }
-}
-
-extension XMPPController: XMPPMUCLightDelegate {
-//    func xmppMUC(_ sender: XMPPMUC, didDiscoverRooms rooms: [Any], forServiceNamed serviceName: String) {
-//        let elements = rooms.map({ try! DDXMLElement(xmlString: String(describing: $0)) })
-//        let jids = elements.compactMap({ XMPPJID(string: $0.attributeStringValue(forName: "jid") ?? "") })
-//        self.rooms = jids
-//        print(self.rooms)
-//    }
-
-    func xmppMUCLight(
-        _ sender: XMPPMUCLight,
-        didDiscoverRooms rooms: [DDXMLElement],
-        forServiceNamed serviceName: String
-    ) {
-        let jids = rooms.compactMap({ XMPPJID(string: $0.attributeStringValue(forName: "jid") ?? "") })
-        let rooms = jids.map { XMPPRoom(roomStorage: XMPPRoomMemoryStorage(), jid: $0) }
-        self.rooms = rooms
-        print(self.rooms)
-    }
-}
-*/