Browse Source

Create xmpp controller singleton and message model

Sergey 2 years ago
parent
commit
57da73c150
3 changed files with 87 additions and 0 deletions
  1. 8 0
      Chat.xcodeproj/project.pbxproj
  2. 65 0
      Chat/Main/XMPPController.swift
  3. 14 0
      Chat/Models/Message.swift

+ 8 - 0
Chat.xcodeproj/project.pbxproj

@@ -20,6 +20,7 @@
 		2F6045B0288E9D3E008F005E /* ChatListViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F6045AF288E9D3E008F005E /* ChatListViewModel.swift */; };
 		2F6045B3288EA0E6008F005E /* LoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F6045B2288EA0E6008F005E /* LoginView.swift */; };
 		2F6045B6288EA10B008F005E /* LoginViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F6045B5288EA10B008F005E /* LoginViewModel.swift */; };
+		2F6E936828F8714600FAF0E5 /* Message.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F6E936728F8714600FAF0E5 /* Message.swift */; };
 		2F81974128ACD9A6008D8C45 /* StreamManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F81974028ACD9A6008D8C45 /* StreamManager.swift */; };
 		2F81974328ACDBC4008D8C45 /* RoomManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F81974228ACDBC4008D8C45 /* RoomManager.swift */; };
 		2F81974528ACDBCF008D8C45 /* RosterManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F81974428ACDBCF008D8C45 /* RosterManager.swift */; };
@@ -29,6 +30,7 @@
 		2FCB75E728A425580097BD1D /* RoomsListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2FCB75E628A425580097BD1D /* RoomsListView.swift */; };
 		2FCB75EA28A425900097BD1D /* RoomsListViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2FCB75E928A425900097BD1D /* RoomsListViewModel.swift */; };
 		2FCB75ED28A42B1E0097BD1D /* ChatViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2FCB75EC28A42B1E0097BD1D /* ChatViewModel.swift */; };
+		2FFCDFBC28F6EA1800D9FA9D /* XMPPController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2FFCDFBB28F6EA1800D9FA9D /* XMPPController.swift */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXFileReference section */
@@ -45,6 +47,7 @@
 		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>"; };
+		2F6E936728F8714600FAF0E5 /* Message.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Message.swift; sourceTree = "<group>"; };
 		2F81974028ACD9A6008D8C45 /* StreamManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StreamManager.swift; sourceTree = "<group>"; };
 		2F81974228ACDBC4008D8C45 /* RoomManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoomManager.swift; sourceTree = "<group>"; };
 		2F81974428ACDBCF008D8C45 /* RosterManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RosterManager.swift; sourceTree = "<group>"; };
@@ -55,6 +58,7 @@
 		2FCB75E928A425900097BD1D /* RoomsListViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoomsListViewModel.swift; sourceTree = "<group>"; };
 		2FCB75EC28A42B1E0097BD1D /* ChatViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatViewModel.swift; sourceTree = "<group>"; };
 		2FF4F6B328B3BF4900BB62E1 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
+		2FFCDFBB28F6EA1800D9FA9D /* XMPPController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XMPPController.swift; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -75,6 +79,7 @@
 			children = (
 				2F60457A288D787E008F005E /* ChatApp.swift */,
 				2F24E94728F2FC8A007ECE97 /* AppDelegate.swift */,
+				2FFCDFBB28F6EA1800D9FA9D /* XMPPController.swift */,
 				2F60457C288D787F008F005E /* Assets.xcassets */,
 				2F24E94228F2F9C6007ECE97 /* Info.plist */,
 			);
@@ -154,6 +159,7 @@
 			children = (
 				2F81974728ACEDC8008D8C45 /* User.swift */,
 				2F81974B28ACFDEA008D8C45 /* Room.swift */,
+				2F6E936728F8714600FAF0E5 /* Message.swift */,
 			);
 			path = Models;
 			sourceTree = "<group>";
@@ -263,11 +269,13 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				2FFCDFBC28F6EA1800D9FA9D /* XMPPController.swift in Sources */,
 				2FCB75ED28A42B1E0097BD1D /* ChatViewModel.swift in Sources */,
 				2F81974328ACDBC4008D8C45 /* RoomManager.swift in Sources */,
 				2F24E94828F2FC8A007ECE97 /* AppDelegate.swift in Sources */,
 				2F81974C28ACFDEA008D8C45 /* Room.swift in Sources */,
 				2F60459C288D8000008F005E /* MessageView.swift in Sources */,
+				2F6E936828F8714600FAF0E5 /* Message.swift in Sources */,
 				2F81974128ACD9A6008D8C45 /* StreamManager.swift in Sources */,
 				2F6045B0288E9D3E008F005E /* ChatListViewModel.swift in Sources */,
 				2FCB75EA28A425900097BD1D /* RoomsListViewModel.swift in Sources */,

+ 65 - 0
Chat/Main/XMPPController.swift

@@ -0,0 +1,65 @@
+//
+//  XMPPController.swift
+//  Chat (iOS)
+//
+//  Created by Sergey Tarasov on 12.10.2022.
+//
+
+import XMPPFramework
+import XMPPFrameworkSwift
+
+final class XMPPController {
+	static let shared: XMPPController = XMPPController()
+
+	let xmppStream: XMPPStream
+	let xmppReconnect: XMPPReconnect
+	let xmppRoster: XMPPRoster
+	let xmppRosterStorage: XMPPRosterMemoryStorage
+
+	var xmppAuth: XMPPPlainAuthentication
+
+	private init() {
+		self.xmppStream = XMPPStream()
+
+		self.xmppRosterStorage = XMPPRosterMemoryStorage()
+		self.xmppRoster = XMPPRoster(rosterStorage: xmppRosterStorage)
+
+		self.xmppReconnect = XMPPReconnect()
+
+		self.xmppReconnect.activate(xmppStream)
+		self.xmppRoster.activate(xmppStream)
+
+		self.xmppAuth = XMPPPlainAuthentication()
+
+		self.xmppReconnect.addDelegate(self, delegateQueue: .main)
+	}
+
+	func setupStream() {
+		self.xmppStream.hostName = "msg.sharix-app.org"
+		self.xmppStream.hostPort = 5222
+		self.xmppStream.startTLSPolicy = XMPPStreamStartTLSPolicy.allowed
+	}
+
+	func login(with login: String, and password: String) {
+		self.xmppStream.myJID = XMPPJID(string: login)
+		self.xmppAuth = XMPPPlainAuthentication(stream: xmppStream, password: password)
+		connect()
+	}
+
+	func connect() {
+		if self.xmppStream.isDisconnected {
+			do {
+				try self.xmppStream.authenticate(xmppAuth)
+			} catch {
+
+			}
+		}
+	}
+}
+
+extension XMPPController: XMPPReconnectDelegate {
+	func xmppReconnect(_ sender: XMPPReconnect, shouldAttemptAutoReconnect connectionFlags: SCNetworkConnectionFlags) -> Bool {
+		print("---------- xmppReconnect:shouldAttemptAutoReconnect: ----------")
+		return true
+	}
+}

+ 14 - 0
Chat/Models/Message.swift

@@ -0,0 +1,14 @@
+//
+//  Message.swift
+//  Chat (iOS)
+//
+//  Created by Sergey Tarasov on 13.10.2022.
+//
+
+import Foundation
+
+struct Message {
+	let messageID: String
+	let body: String
+	let fromID: String
+}