Skip to content

Commit

Permalink
Add multichats logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sbobruskina committed Sep 30, 2024
1 parent 5c32616 commit a30a8a2
Show file tree
Hide file tree
Showing 19 changed files with 448 additions and 257 deletions.
12 changes: 12 additions & 0 deletions PyrusServiceDeskIOS/PyrusServiceDesk.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
1E1275C12CAA8DDB0080CB19 /* PSDChatsTableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E1275C02CAA8DDB0080CB19 /* PSDChatsTableView.swift */; };
1E1275C32CAA8DEC0080CB19 /* PSDChatsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E1275C22CAA8DEC0080CB19 /* PSDChatsViewController.swift */; };
1E1275C52CAA8E100080CB19 /* PSDCreateChat.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E1275C42CAA8E100080CB19 /* PSDCreateChat.swift */; };
4B060F2A2732AD8C002E20A7 /* StringHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B060F292732AD8C002E20A7 /* StringHelper.swift */; };
4B062A832518A8D100B317D3 /* PSDPlaceholderMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B062A822518A8D100B317D3 /* PSDPlaceholderMessage.swift */; };
4B062A882518AC9500B317D3 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 4B062A872518AC9500B317D3 /* [email protected] */; };
Expand Down Expand Up @@ -293,6 +296,9 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
1E1275C02CAA8DDB0080CB19 /* PSDChatsTableView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PSDChatsTableView.swift; sourceTree = "<group>"; };
1E1275C22CAA8DEC0080CB19 /* PSDChatsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PSDChatsViewController.swift; sourceTree = "<group>"; };
1E1275C42CAA8E100080CB19 /* PSDCreateChat.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PSDCreateChat.swift; sourceTree = "<group>"; };
4B060F292732AD8C002E20A7 /* StringHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringHelper.swift; sourceTree = "<group>"; };
4B062A822518A8D100B317D3 /* PSDPlaceholderMessage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PSDPlaceholderMessage.swift; sourceTree = "<group>"; };
4B062A872518AC9500B317D3 /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1120,6 +1126,7 @@
4BB8559E2208559200F7CCE5 /* Requests */ = {
isa = PBXGroup;
children = (
1E1275C42CAA8E100080CB19 /* PSDCreateChat.swift */,
4B5533722264C07B00353941 /* PSDDownloader.swift */,
4B56BE482216CC0E004E6685 /* PSDUploader.swift */,
4BB8559F2208593200F7CCE5 /* URLCreator.swift */,
Expand All @@ -1139,6 +1146,8 @@
4BB855A1220865D500F7CCE5 /* chats */ = {
isa = PBXGroup;
children = (
1E1275C22CAA8DEC0080CB19 /* PSDChatsViewController.swift */,
1E1275C02CAA8DDB0080CB19 /* PSDChatsTableView.swift */,
4BB855B02208741600F7CCE5 /* PSDChatInfoTableViewCell.swift */,
);
path = chats;
Expand Down Expand Up @@ -1455,6 +1464,7 @@
4B9C205D2451904E004DEF7A /* PSDRowMessage.swift in Sources */,
4B873BB6224BCA8100A96E93 /* PSDPushToken.swift in Sources */,
4B7F139022155F62007C6358 /* String+Date.swift in Sources */,
1E1275C32CAA8DEC0080CB19 /* PSDChatsViewController.swift in Sources */,
4B66011D223948CE00AA45E6 /* PyrusServiceDeskCustomization.swift in Sources */,
4B9BF878224CBCBC006AD05C /* PSDCopyLabel.swift in Sources */,
4B88C6F722145ACA00BDB304 /* PSDGetChats.swift in Sources */,
Expand Down Expand Up @@ -1491,6 +1501,7 @@
7B1E8D58250665F800C1E5AC /* PSDInfoView.swift in Sources */,
4BB6CDC325597E4C004C680A /* PyrusLogger.swift in Sources */,
4B7F350A21F8900A00977C6D /* PSDChatViewController.swift in Sources */,
1E1275C52CAA8E100080CB19 /* PSDCreateChat.swift in Sources */,
4B6D4A39244E1B6A007C6082 /* AttachmentCollectionView.swift in Sources */,
4B659AFE223FA9E400D79489 /* PSDSpinerTableView.swift in Sources */,
4B82A49A2268B57B00654CE0 /* PSDAttachmentLoadViewControllerInterface.swift in Sources */,
Expand Down Expand Up @@ -1524,6 +1535,7 @@
4B7AD4D6250A06C9004E9C48 /* PSDFilesManager.swift in Sources */,
4B7EA884221EB01B009C9B41 /* PSDErrorParser.swift in Sources */,
4BA3FF4022116C1100CE497A /* AttachmentHandler.swift in Sources */,
1E1275C12CAA8DDB0080CB19 /* PSDChatsTableView.swift in Sources */,
4B56BE4B2216CCC0004E6685 /* PSDAttachment.swift in Sources */,
4B060F2A2732AD8C002E20A7 /* StringHelper.swift in Sources */,
4B6E9DA3225F3B570037462F /* PSDPreviewSetter.swift in Sources */,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>PyrusServiceDesk-Universal.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
</dict>
<key>PyrusServiceDesk.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
<key>PyrusServiceDeskIOS.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>2</integer>
</dict>
<key>PyrusServiceDeskTests.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>3</integer>
</dict>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ protocol PSDChatTableViewDelegate: NSObjectProtocol {
class PSDChatTableView: PSDDetailTableView{
///The id of chat that is shown in table view
weak var chatDelegate: PSDChatTableViewDelegate?
var ticketId = 0
private let footerHeight : CGFloat = 10.0
private let BOTTOM_INFELICITY : CGFloat = 10.0
private var needShowRating : Bool = false
Expand Down Expand Up @@ -107,6 +108,38 @@ class PSDChatTableView: PSDDetailTableView{
self.keyboardDismissMode = .interactive
self.separatorColor = .clear
}

func reload() {
tableMatrix = [[PSDRowMessage]()]
let ticketId = ticketId
DispatchQueue.global().async {
[weak self] in
PSDGetChat.get(needShowError: true, delegate: self, ticketId: ticketId) {
chat in
DispatchQueue.main.async {
if chat != nil {
UnreadMessageManager.removeLastComment()
}
guard let self = self else {
return
}
self.gotData = true
if let chat = chat {
self.storeChat = chat
if !(self.loadingTimer?.isValid ?? false) {
self.drawTableWithData()
}
} else {
self.needShowRating = chat?.showRating ?? false
self.showRateIfNeed()
self.isLoading = false
self.buttonsView.updateWithButtons(nil, width: self.frame.size.width)
}
}
}
}
}

/**
Reloads ChatTableView. Creates the new tableMatrix.
*/
Expand All @@ -120,9 +153,10 @@ class PSDChatTableView: PSDDetailTableView{
self.reloadData()
self.isLoading = true
}
let ticketId = ticketId
DispatchQueue.global().async {
[weak self] in
PSDGetChat.get(needShowError: true, delegate: self) {
PSDGetChat.get(needShowError: true, delegate: self, ticketId: ticketId) {
chat in
DispatchQueue.main.async {
if chat != nil {
Expand Down Expand Up @@ -223,93 +257,93 @@ class PSDChatTableView: PSDDetailTableView{
///update taable matrix
///- parameter needProgress: Determines whether the view should respond to updating(need to show error)
func updateChat(needProgress:Bool) {
PSDGetChat.get(needShowError: needProgress, delegate: nil) { [weak self]
(chat : PSDChat?) in
DispatchQueue.main.async {
if let chat = chat{
UnreadMessageManager.removeLastComment()
self?.needShowRating = chat.showRating
self?.showRateIfNeed()
//compare number of messages it two last sections
guard let self = self else{
return
PSDGetChat.get(needShowError: needProgress, delegate: nil, ticketId: ticketId) { [weak self]
(chat : PSDChat?) in
DispatchQueue.main.async {
if let chat = chat{
UnreadMessageManager.removeLastComment()
self?.needShowRating = chat.showRating
self?.showRateIfNeed()
//compare number of messages it two last sections
guard let self = self else{
return
}
var hasChanges = false
let (removeIndexPaths, removeSections) = self.tableMatrix.removeFakeMessages()
if removeIndexPaths.count > 0
|| removeSections.count > 0,
self.tableMatrix.count > 0
{
if !hasChanges {
hasChanges = true
PyrusLogger.shared.logEvent("Колличество ячеек до удаления: \(self.tableMatrix[self.tableMatrix.count-1].count)")
PyrusLogger.shared.logEvent("Колличество ячеек после удаления: \(self.tableMatrix[self.tableMatrix.count-1].count)")
}
var hasChanges = false
let (removeIndexPaths, removeSections) = self.tableMatrix.removeFakeMessages()
if removeIndexPaths.count > 0
|| removeSections.count > 0,
self.tableMatrix.count > 0
{
if !hasChanges {
hasChanges = true
PyrusLogger.shared.logEvent("Колличество ячеек до удаления: \(self.tableMatrix[self.tableMatrix.count-1].count)")
PyrusLogger.shared.logEvent("Колличество ячеек после удаления: \(self.tableMatrix[self.tableMatrix.count-1].count)")
}
self.tableMatrix.complete(from: chat, startMessage:self.lastMessageFromServer){
(indexPaths: [IndexPath], sections:IndexSet, _) in
DispatchQueue.main.async {
if indexPaths.count > 0 ||
sections.count > 0,
self.tableMatrix.count > 0
{
if !hasChanges {
hasChanges = true
PyrusLogger.shared.logEvent("Колличество ячеек до удаления: \(self.tableMatrix[self.tableMatrix.count-1].count)")
}
PyrusLogger.shared.logEvent("Колличество ячеек после добавления: \(self.tableMatrix[self.tableMatrix.count-1].count)")
PyrusLogger.shared.logEvent("При удалении фейка: ячейки = \(removeIndexPaths), секции \(removeSections)")
PyrusLogger.shared.logEvent("При добавленни нового сообщения: ячейки = \(indexPaths), секции \(sections)")
}
}
self.tableMatrix.complete(from: chat, startMessage:self.lastMessageFromServer){
(indexPaths: [IndexPath], sections:IndexSet, _) in
DispatchQueue.main.async {
if indexPaths.count > 0 ||
sections.count > 0,
self.tableMatrix.count > 0
{
if !hasChanges {
hasChanges = true
PyrusLogger.shared.logEvent("Колличество ячеек до удаления: \(self.tableMatrix[self.tableMatrix.count-1].count)")
}
PyrusLogger.shared.logEvent("Колличество ячеек после добавления: \(self.tableMatrix[self.tableMatrix.count-1].count)")
PyrusLogger.shared.logEvent("При удалении фейка: ячейки = \(removeIndexPaths), секции \(removeSections)")
PyrusLogger.shared.logEvent("При добавленни нового сообщения: ячейки = \(indexPaths), секции \(sections)")
let oldContentOffset = self.contentOffset
let oldContentSize = self.contentSize
self.removeNoConnectionView()
self.lastMessageFromServer = chat.messages.last
self.setLastActivityDate()
if indexPaths.count > 0
|| sections.count > 0
|| removeIndexPaths.count > 0
|| removeSections.count > 0
{
let (newRemoveIndexPaths, addIndexPaths, reloadIndexPaths, newRemoveSections, addSections, reloadSections) = PSDTableView.compareAddAndRemoveRows(removeIndexPaths: removeIndexPaths, addIndexPaths: indexPaths, removeSections: removeSections, addSections: sections)
PyrusLogger.shared.logEvent("Результат после сопоставления: удалять = \(newRemoveIndexPaths), \(newRemoveSections); \n добавлять = \(addIndexPaths), \(addSections); \n Обновлять = \(reloadIndexPaths), \(reloadSections)")
self.beginUpdates()
if newRemoveIndexPaths.count > 0 {
self.deleteRows(at: newRemoveIndexPaths, with: .none)
}
if newRemoveSections.count > 0 {
self.deleteSections(newRemoveSections, with: .none)
}
if addIndexPaths.count > 0{
self.insertRows(at: addIndexPaths, with: .none)
}
if addSections.count > 0 {
self.insertSections(addSections, with: .none)
}
if reloadIndexPaths.count > 0 {
self.reloadRows(at: reloadIndexPaths, with: .none)
}
let oldContentOffset = self.contentOffset
let oldContentSize = self.contentSize
self.removeNoConnectionView()
self.lastMessageFromServer = chat.messages.last
self.setLastActivityDate()
if indexPaths.count > 0
|| sections.count > 0
|| removeIndexPaths.count > 0
|| removeSections.count > 0
{
let (newRemoveIndexPaths, addIndexPaths, reloadIndexPaths, newRemoveSections, addSections, reloadSections) = PSDTableView.compareAddAndRemoveRows(removeIndexPaths: removeIndexPaths, addIndexPaths: indexPaths, removeSections: removeSections, addSections: sections)
PyrusLogger.shared.logEvent("Результат после сопоставления: удалять = \(newRemoveIndexPaths), \(newRemoveSections); \n добавлять = \(addIndexPaths), \(addSections); \n Обновлять = \(reloadIndexPaths), \(reloadSections)")
self.beginUpdates()
if newRemoveIndexPaths.count > 0 {
self.deleteRows(at: newRemoveIndexPaths, with: .none)
}
if newRemoveSections.count > 0 {
self.deleteSections(newRemoveSections, with: .none)
}
if addIndexPaths.count > 0{
self.insertRows(at: addIndexPaths, with: .none)
}
if addSections.count > 0 {
self.insertSections(addSections, with: .none)
}
if reloadIndexPaths.count > 0 {
self.reloadRows(at: reloadIndexPaths, with: .none)
}
if reloadSections.count > 0 {
self.reloadSections(reloadSections, with: .none)
}
self.endUpdates()
self.buttonsView.updateWithButtons(PSDChat.draftAnswers(self.tableMatrix), width: self.frame.size.width)
self.buttonsView.collectionView.collectionViewLayout.invalidateLayout()
self.scrollToBottomAfterRefresh(with: oldContentOffset, oldContentSize: oldContentSize)
if reloadSections.count > 0 {
self.reloadSections(reloadSections, with: .none)
}
self.endUpdates()
self.buttonsView.updateWithButtons(PSDChat.draftAnswers(self.tableMatrix), width: self.frame.size.width)
self.buttonsView.collectionView.collectionViewLayout.invalidateLayout()
self.scrollToBottomAfterRefresh(with: oldContentOffset, oldContentSize: oldContentSize)
}
}

}

}
DispatchQueue.main.async {
if let self = self {
self.buttonsView.updateWithButtons(PSDChat.draftAnswers(self.tableMatrix), width: self.frame.size.width)
self.customRefresh.endRefreshing()
self.bottomRefresh.endRefreshing()
}
}
DispatchQueue.main.async {
if let self = self {
self.buttonsView.updateWithButtons(PSDChat.draftAnswers(self.tableMatrix), width: self.frame.size.width)
self.customRefresh.endRefreshing()
self.bottomRefresh.endRefreshing()
}
}
}
}
///Scrolls table to bottom after refresh, if table view was in bottom scroll position and new messages received
private func scrollToBottomAfterRefresh(with oldOffset: CGPoint?, oldContentSize: CGSize?) {
Expand Down Expand Up @@ -633,7 +667,7 @@ extension PSDChatTableView : PSDChatMessageCellDelegate{
}
if let message = self.getMessage(at: indexPath){
message.state = .sending
PSDMessageSend.pass(message, delegate: self)
PSDMessageSend.pass(message, delegate: self, ticketId: ticketId)
}
}
else{
Expand Down
Loading

0 comments on commit a30a8a2

Please sign in to comment.