Skip to content

Commit

Permalink
update for 3.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Sergey-K committed Jul 29, 2022
1 parent 74ad50e commit 486c578
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 90 deletions.
2 changes: 0 additions & 2 deletions Core/UDMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public class UDMessage: NSObject, Codable {
@objc public var avatar = ""
@objc public var file = UDFile()

var attributedString: NSMutableAttributedString? = nil

var outgoing: Bool {
return !incoming
}
Expand Down
41 changes: 11 additions & 30 deletions Core/UDSocketResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
// UseDesk_SDK_Swift
//
//

import Foundation
import Down
import MarkdownKit

class UDSocketResponse {

Expand Down Expand Up @@ -164,7 +163,6 @@ class UDSocketResponse {

public class func actionAddMessage(_ data: [Any]?, newMessageBlock: UDNewMessageBlock?, feedbackMessageBlock: UDFeedbackMessageBlock?, sendAdditionalFieldsBlock: () -> Void, isSendedAdditionalField: Bool, model: UseDeskModel) {
let dicServer = data?[0] as? [AnyHashable : Any]

let type = dicServer?["type"] as? String
if type == nil {
return
Expand All @@ -184,7 +182,7 @@ class UDSocketResponse {
var textWithoutLinkImage: String? = nil
var linksImage: [String] = []
if var text = message!["text"] as? String {
(linksImage, textWithoutLinkImage, mutableAttributedString) = parseText(text)
(linksImage, textWithoutLinkImage) = parseText(text)
for link in linksImage {
text = text.replacingOccurrences(of: link, with: "")
if let messageImageLink = UDSocketResponse.parseFileMessageDic(message, withImageUrl: link) {
Expand All @@ -195,7 +193,7 @@ class UDSocketResponse {
if (message!["file"] as? [AnyHashable : Any] ) != nil {
messageFile = parseFileMessageDic(message)
}
m = parseMessageDic(message, textWithoutLinkImage: textWithoutLinkImage, attributedString: mutableAttributedString, model: model)
m = parseMessageDic(message, textWithoutLinkImage: textWithoutLinkImage, model: model)

var isAddMessage = false
if m != nil {
Expand Down Expand Up @@ -240,7 +238,7 @@ class UDSocketResponse {
var textWithoutLinkImage: String? = nil
var linksImage: [String] = []
if var text = message["text"] as? String {
(linksImage, textWithoutLinkImage, mutableAttributedString) = parseText(text)
(linksImage, textWithoutLinkImage) = parseText(text)
for link in linksImage {
text = text.replacingOccurrences(of: link, with: "")
if let messageImageLink = UDSocketResponse.parseFileMessageDic(message, withImageUrl: link) {
Expand All @@ -251,7 +249,7 @@ class UDSocketResponse {
if (message["file"] as? [AnyHashable : Any] ) != nil {
messageFile = UDSocketResponse.parseFileMessageDic(message)
}
m = UDSocketResponse.parseMessageDic(message, textWithoutLinkImage: textWithoutLinkImage, attributedString: mutableAttributedString, model: model)
m = UDSocketResponse.parseMessageDic(message, textWithoutLinkImage: textWithoutLinkImage, model: model)
}
if m != nil && m!.text != "" {
messages.append(m!)
Expand Down Expand Up @@ -349,9 +347,8 @@ class UDSocketResponse {
return m
}

class func parseMessageDic(_ mess: [AnyHashable : Any]?, textWithoutLinkImage: String? = nil, attributedString: NSMutableAttributedString? = nil, model: UseDeskModel) -> UDMessage? {
class func parseMessageDic(_ mess: [AnyHashable : Any]?, textWithoutLinkImage: String? = nil, model: UseDeskModel) -> UDMessage? {
let m = UDMessage(text: "", incoming: false)

let createdAt = mess?["createdAt"] as? String ?? ""
let dateFormatter = DateFormatter()
dateFormatter.locale = .current
Expand Down Expand Up @@ -383,9 +380,8 @@ class UDSocketResponse {
}
}
m.text = textWithoutLinkImage != nil ? textWithoutLinkImage! : mess?["text"] as? String ?? ""
m.attributedString = attributedString
if m.incoming {
let stringsFromButtons = parseMessageFromButtons(text: m.attributedString != nil ? m.attributedString!.string : m.text)
let stringsFromButtons = parseMessageFromButtons(text: m.text)
for stringFromButton in stringsFromButtons {
let button = buttonFromString(stringButton: stringFromButton)
var textButton = ""
Expand All @@ -396,11 +392,7 @@ class UDSocketResponse {
textButton += button!.title
}
}
if m.attributedString != nil {
m.attributedString!.mutableString.replaceOccurrences(of: stringFromButton, with: textButton, options: .caseInsensitive, range: NSRange(location: 0, length: m.attributedString!.length))
} else {
m.text = m.text.replacingOccurrences(of: stringFromButton, with: textButton)
}
m.text = m.text.replacingOccurrences(of: stringFromButton, with: textButton)
}
for index in 0..<m.buttons.count {
let invertIndex = (m.buttons.count - 1) - index
Expand Down Expand Up @@ -468,31 +460,20 @@ class UDSocketResponse {
return stringsFromButton
}

private class func parseText(_ textPars: String) -> ([String], String?, NSMutableAttributedString?) {
private class func parseText(_ textPars: String) -> ([String], String?) {
var text = textPars
text = text.udRemoveFirstSymbol(with: "\u{200b}")
text = text.udRemoveFirstSymbol(with: "\n")
text = text.udRemoveLastSymbol(with: "\u{200b}")
text = text.udRemoveLastSymbol(with: "\n")
let textBeforeRemoveMarkdownUrls = text
var mutableAttributedString: NSMutableAttributedString? = nil
var textWithoutLinkImage: String? = nil
let linksImage = text.udRemoveMarkdownUrlsAndReturnLinks()
textWithoutLinkImage = text
if text.count > 0 {
text = text.replacingOccurrences(of: "\n", with: "<№;%br>")
let down = Down(markdownString: text)
if let attributedString = try? down.toAttributedString() {
let paragraphStyle = NSMutableParagraphStyle()
mutableAttributedString = NSMutableAttributedString(attributedString: attributedString)
mutableAttributedString!.addAttributes([.paragraphStyle : paragraphStyle], range: NSRange(location: 0, length: mutableAttributedString!.length))
mutableAttributedString!.mutableString.replaceCharacters(in: NSRange(location: mutableAttributedString!.length - 1, length: 1), with: "")
mutableAttributedString!.mutableString.replaceOccurrences(of: "<№;%br>", with: "\n", options: .caseInsensitive, range: NSRange(location: 0, length: mutableAttributedString!.length))
}
} else if linksImage.count == 0 {
if linksImage.count == 0 {
textWithoutLinkImage = textBeforeRemoveMarkdownUrls
}
return (linksImage, textWithoutLinkImage, mutableAttributedString)
return (linksImage, textWithoutLinkImage)
}

private class func buttonFromString(stringButton: String) -> UDMessageButton? {
Expand Down
2 changes: 1 addition & 1 deletion Core/UseDeskSDKHelp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class UseDeskSDKHelp {
let payload: [String : Any] = [
"sdk" : "iOS",
"type" : "sdk",
"version" : "3.1.2",
"version" : "3.1.3",
"message_limit" : countMessagesOnInit
]
var dic = [
Expand Down
12 changes: 6 additions & 6 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PODS:
- Alamofire (5.6.1)
- Down (0.11.0)
- IQKeyboardManagerSwift (6.5.10)
- MarkdownKit (1.7.1)
- PINCache (3.0.3):
- PINCache/Arc-exception-safe (= 3.0.3)
- PINCache/Core (= 3.0.3)
Expand Down Expand Up @@ -41,9 +41,9 @@ PODS:
- Texture/Core
- Texture/Video (3.1.0):
- Texture/Core
- UseDesk_SDK_Swift (3.0.0):
- UseDesk_SDK_Swift (3.1.2):
- Alamofire (~> 5)
- Down
- MarkdownKit
- ReachabilitySwift
- Socket.IO-Client-Swift (~> 16.0)
- Texture
Expand All @@ -55,8 +55,8 @@ DEPENDENCIES:
SPEC REPOS:
trunk:
- Alamofire
- Down
- IQKeyboardManagerSwift
- MarkdownKit
- PINCache
- PINOperation
- PINRemoteImage
Expand All @@ -71,16 +71,16 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
Alamofire: 87bd8c952f9a4454320fce00d9cc3de57bcadaf5
Down: b6ba1bc985c9d2f4e15e3b293d2207766fa12612
IQKeyboardManagerSwift: 52962c76ab33532f15ad9f3ff4e5715eda5335bb
MarkdownKit: 0b15195c63cf3e39387949c4bdeccde0055e252a
PINCache: 7a8fc1a691173d21dbddbf86cd515de6efa55086
PINOperation: 00c935935f1e8cf0d1e2d6b542e75b88fc3e5e20
PINRemoteImage: f1295b29f8c5e640e25335a1b2bd9d805171bd01
ReachabilitySwift: 985039c6f7b23a1da463388634119492ff86c825
Socket.IO-Client-Swift: c116d6dc9fd6be9c259bacfe143f8725bce7d79e
Starscream: 5178aed56b316f13fa3bc55694e583d35dd414d9
Texture: 2e8ab2519452515f7f5a520f5a8f7e0a413abfa3
UseDesk_SDK_Swift: 9ead4597515f85131d9a053cd960753fa1be55f0
UseDesk_SDK_Swift: 3d595a2e16ba98c438957e5241f59822960a850a

PODFILE CHECKSUM: 38ca86b4f5802b67eb523063e3dd15ab9ab41411

Expand Down
8 changes: 4 additions & 4 deletions Example/UseDesk.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-UseDesk_Example/Pods-UseDesk_Example-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/Alamofire/Alamofire.framework",
"${BUILT_PRODUCTS_DIR}/Down/Down.framework",
"${BUILT_PRODUCTS_DIR}/IQKeyboardManagerSwift/IQKeyboardManagerSwift.framework",
"${BUILT_PRODUCTS_DIR}/MarkdownKit/MarkdownKit.framework",
"${BUILT_PRODUCTS_DIR}/PINCache/PINCache.framework",
"${BUILT_PRODUCTS_DIR}/PINOperation/PINOperation.framework",
"${BUILT_PRODUCTS_DIR}/PINRemoteImage/PINRemoteImage.framework",
Expand All @@ -349,8 +349,8 @@
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Alamofire.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Down.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/IQKeyboardManagerSwift.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MarkdownKit.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/PINCache.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/PINOperation.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/PINRemoteImage.framework",
Expand Down Expand Up @@ -587,7 +587,7 @@
INFOPLIST_FILE = "UseDesk/UseDesk-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MARKETING_VERSION = 3.1.2;
MARKETING_VERSION = 3.1.3;
MODULE_NAME = ExampleApp;
ONLY_ACTIVE_ARCH = NO;
PRODUCT_BUNDLE_IDENTIFIER = UseDeskExampleSDK.ru;
Expand Down Expand Up @@ -619,7 +619,7 @@
INFOPLIST_FILE = "UseDesk/UseDesk-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MARKETING_VERSION = 3.1.2;
MARKETING_VERSION = 3.1.3;
MODULE_NAME = ExampleApp;
ONLY_ACTIVE_ARCH = NO;
PRODUCT_BUNDLE_IDENTIFIER = UseDeskExampleSDK.ru;
Expand Down
16 changes: 11 additions & 5 deletions UseDesk/Classes/DialogflowView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class DialogflowView: UDMessagesView {
wSelf.addMessage(message)
}
wSelf.sendOtherMessages()
wSelf.isScrollChatToBottom = true
})
}

Expand Down Expand Up @@ -193,12 +194,14 @@ class DialogflowView: UDMessagesView {
}

// MARK: - Message methods
func addMessage(_ message: UDMessage) {
func addMessage(_ message: UDMessage, incoming: Bool = false) {
DispatchQueue.main.async(execute: { [weak self] in
guard let wSelf = self else {return}
wSelf.allMessages.append(message)
wSelf.newMessagesIds.append(message.id)
wSelf.updateCountNewMessagesView()
if !incoming {
wSelf.newMessagesIds.append(message.id)
wSelf.updateCountNewMessagesView()
}
var isNewSection = true
if wSelf.messagesWithSection.count > 0 {
if wSelf.messagesWithSection[0].count > 0 {
Expand Down Expand Up @@ -257,6 +260,9 @@ class DialogflowView: UDMessagesView {
}
}
}
if incoming {
wSelf.scrollChatToStart()
}
})
}

Expand Down Expand Up @@ -463,7 +469,7 @@ class DialogflowView: UDMessagesView {
if let id = usedesk?.networkManager?.newIdLoadingMessages() {
firstMessage.loadingMessageId = id
}
addMessage(firstMessage)
addMessage(firstMessage, incoming: true)
sendMessage(firstMessage)
draftMessages.removeFirst()
}
Expand All @@ -472,7 +478,7 @@ class DialogflowView: UDMessagesView {
if let id = usedesk?.networkManager?.newIdLoadingMessages() {
message.loadingMessageId = id
}
addMessage(message)
addMessage(message, incoming: true)
queueOfSendMessages.append(message)
}
}
Expand Down
9 changes: 4 additions & 5 deletions UseDesk/Classes/UDFeedbackMessageCellNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import UIKit
import AsyncDisplayKit
import MarkdownKit

protocol UDFeedbackMessageCellNodeDelegate: AnyObject {
func feedbackAction(indexPath: IndexPath, feedback: Bool)
Expand Down Expand Up @@ -31,11 +32,9 @@ class UDFeedbackMessageCellNode: UDMessageCellNode {

var attributedString = NSMutableAttributedString()
let messageStyle = configurationStyle.messageStyle
if message.attributedString != nil {
attributedString = message.attributedString!
} else {
attributedString = NSMutableAttributedString(string: message.text)
}
let markdownParser = MarkdownParser(font: messageStyle.font, color: message.outgoing ? messageStyle.textOutgoingColor : messageStyle.textIncomingColor)
markdownParser.link.color = message.outgoing ? messageStyle.linkOutgoingColor : messageStyle.linkIncomingColor
attributedString = NSMutableAttributedString(attributedString: markdownParser.parse(message.text))
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
paragraphStyle.lineBreakMode = .byWordWrapping
Expand Down
Loading

0 comments on commit 486c578

Please sign in to comment.