Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Made Link in Chat Clickable #518

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Susi.xcworkspace/xcshareddata/xcschemes/Susi.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@
</EnvironmentVariable>
</EnvironmentVariables>
<AdditionalOptions>
<AdditionalOption
key = "MallocStackLogging"
value = ""
isEnabled = "YES">
</AdditionalOption>
<AdditionalOption
key = "PrefersMallocStackLoggingLite"
value = ""
isEnabled = "YES">
</AdditionalOption>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ extension ChatViewController: AVAudioRecorderDelegate {
Used to initialise the snowboy wrapper
**/
func initSnowboy() {
wrapper = SnowboyWrapper(resources: RESOURCE, modelStr: MODEL)
wrapper.setSensitivity("0.5")
wrapper.setAudioGain(1.0)
wrapper?.setSensitivity("0.5")
wrapper?.setAudioGain(1.0)
// print("Sample rate: \(wrapper?.sampleRate()); channels: \(wrapper?.numChannels()); bits: \(wrapper?.bitsPerSample())")
}

Expand Down Expand Up @@ -52,7 +51,7 @@ extension ChatViewController: AVAudioRecorderDelegate {
// print("Frame capacity: \(AVAudioFrameCount(file.length))")
// print("Buffer frame length: \(buffer.frameLength)")

let result = wrapper.runDetection(array, length: Int32(buffer!.frameLength))
let result = wrapper?.runDetection(array, length: Int32(buffer!.frameLength))
// print("Result: \(result)")

if result == 1 {
Expand Down
21 changes: 18 additions & 3 deletions Susi/Controllers/ChatViewController/ChatVCMethods.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import AVFoundation
import RealmSwift
import Material

protocol ChatViewControllerProtocol: class {
func searchWith(text: String?)
}
extension ChatViewController {

// MARK: - Keyboard Notifications
Expand Down Expand Up @@ -317,17 +320,21 @@ extension ChatViewController {
}
}
}

func shouldOpenSkillListingVC() {
if(self.shouldOpenSkillListing) {
self.shouldOpenSkillListing = false
presentSkillListingController()
}
}
// present skill listing controller
@objc func presentSkillListingController() {
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
if let vc = mainStoryboard.instantiateViewController(withIdentifier: "SkillListingController") as? SkillListingViewController {
vc.chatViewController = self
vc.chatViewControllerDelegate = self
let nvc = AppNavigationController(rootViewController: vc)
present(nvc, animated: true, completion: nil)
}
}

// checks if personal trained model exists
func checkAndAssignIfModelExists() {
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
Expand Down Expand Up @@ -377,3 +384,11 @@ extension ChatViewController: PresentControllerDelegate {
}

}

extension ChatViewController: ChatViewControllerProtocol {
func searchWith(text: String?) {
guard let text = text else { return }
self.inputTextField.text = text
self.handleSend()
}
}
20 changes: 17 additions & 3 deletions Susi/Controllers/ChatViewController/ChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,21 @@ import Speech
import NVActivityIndicatorView
import Realm
import Reachability
import BouncyLayout

class ChatViewController: UICollectionViewController {

init(collectionViewLayout layout: UICollectionViewLayout = BouncyLayout(), shouldOpenSkillListing: Bool = false) {
super.init(collectionViewLayout: layout)
self.shouldOpenSkillListing = shouldOpenSkillListing
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Variable Declarations

let reachability = Reachability()!

var shouldOpenSkillListing: Bool = false
lazy var susiSkillListingButton: IconButton = {
let ib = IconButton()
ib.image = ControllerConstants.Images.susiSymbol
Expand Down Expand Up @@ -99,7 +108,10 @@ class ChatViewController: UICollectionViewController {
var MODEL: String = Bundle.main.path(forResource: "susi", ofType: "pmdl")!

// snowboy wrapper
var wrapper: SnowboyWrapper! = nil
lazy var wrapper: SnowboyWrapper? = {
var wrapper = SnowboyWrapper(resources: RESOURCE, modelStr: MODEL)
return wrapper
}()

// records audio
var audioRecorder: AVAudioRecorder!
Expand Down Expand Up @@ -163,6 +175,8 @@ class ChatViewController: UICollectionViewController {
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
shouldOpenSkillListingVC()
NotificationCenter.default.addObserver(self, selector: #selector(internetConnection), name: Notification.Name.reachabilityChanged, object: reachability)

do {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,10 @@ extension SkillDetailViewController {

@objc func trySkillFromExample() {
let query = selectedExample ?? self.skill?.examples.first
weak var weakSelf = self
navigationController?.dismiss(animated: true, completion: {
self.chatViewController?.inputTextField.text = query
self.chatViewController?.handleSend()
weakSelf?.chatViewControllerDelegate?.searchWith(text: query)
})
// In-case of 3D-touch home action
if let chatVC = self.chatViewController, isOpenThroughShortcut {
present(chatVC, animated: true, completion: {
chatVC.inputTextField.text = query
chatVC.handleSend()
})
}
}

func addSkillDescription() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ class SkillDetailViewController: GeneralViewController {
return cellConfig
}()

var isOpenThroughShortcut = false
var skill: Skill?
var chatViewController: ChatViewController?
weak var chatViewControllerDelegate: ChatViewControllerProtocol?
var selectedExample: String?
var submitRatingParams: [String: AnyObject] = [:]
var getRatingParam: [String: AnyObject] = [:]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ extension SkillListingViewController {
// dismiss controller
@objc func dismissController() {
navigationController?.dismiss(animated: true, completion: nil)
// In-case of 3D-touch home action
if let chatVC = self.chatViewController, isOpenThroughShortcut {
present(chatVC, animated: true, completion: nil)
}
}

// setup activity indicator
Expand Down Expand Up @@ -142,16 +138,16 @@ extension SkillListingViewController {
}

func checkReachability() {
weak var weakSelf = self
reachability.whenUnreachable = { reachability in
DispatchQueue.main.async {
let noConnection = noConnectionViewController()
let view = UINavigationController(rootViewController: noConnection)
self.present(view, animated: true, completion: {
noConnection.skillListingInstance = self
weakSelf?.present(view, animated: true, completion: {
noConnection.skillListingInstance = weakSelf
})
}
self.dismissingTheController()
weakSelf?.dismissingTheController()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class SkillListingViewController: UITableViewController {

let reachability = Reachability()!
var dismissChecker: Bool?
var isOpenThroughShortcut = false
lazy var settingsButton: IconButton = {
let ib = IconButton()
ib.image = Icon.moreVertical
Expand Down Expand Up @@ -51,8 +50,8 @@ class SkillListingViewController: UITableViewController {
}()

var groups: [String]?

var chatViewController: ChatViewController?
weak var chatViewControllerDelegate: ChatViewControllerProtocol?

var shouldShowShimmerLoading: Bool = true
// stores how many group's data fetched
Expand All @@ -77,13 +76,7 @@ class SkillListingViewController: UITableViewController {
return indicator
}()

var selectedSkill: Skill? {
didSet {
if(selectedSkill != nil ) {
performSegue(withIdentifier: ControllerConstants.skillDetailControllerIdentifier, sender: self)
}
}
}
var selectedSkill: Skill?

override func viewDidLoad() {
super.viewDidLoad()
Expand Down Expand Up @@ -153,9 +146,18 @@ class SkillListingViewController: UITableViewController {

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let skillDetailVC = segue.destination as? SkillDetailViewController, segue.identifier == ControllerConstants.skillDetailControllerIdentifier {
skillDetailVC.chatViewController = chatViewController
skillDetailVC.isOpenThroughShortcut = isOpenThroughShortcut
skillDetailVC.chatViewControllerDelegate = chatViewControllerDelegate
skillDetailVC.skill = selectedSkill
}
}
}
extension SkillListingViewController: SkillSelectionProtocol {
//MARK: - SkillSelectionProtocol method
func didSelectSkill(skill: Skill?) {
guard let skill = skill else {
return
}
selectedSkill = skill
performSegue(withIdentifier: ControllerConstants.skillDetailControllerIdentifier, sender: self)
}
}
24 changes: 24 additions & 0 deletions Susi/Custom Views/LongPressToCopyTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import UIKit

class LongPressToCopyTextView: UITextView {

var url: String?

override var canBecomeFirstResponder: Bool {
return true
}
Expand All @@ -30,7 +32,29 @@ class LongPressToCopyTextView: UITextView {
self.addGestureRecognizer(gestureRecognizer)
}

func addTapGesture() {
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(sender:)))
self.addGestureRecognizer(tapGestureRecognizer)
}


// MARK: - UIGestureRecognizer
@objc func handleTapGesture(sender: UITapGestureRecognizer) {
guard let strUrl = url else { return }
let textView = UITextView(frame: self.frame)
textView.text = self.text
textView.attributedText = self.attributedText
if let linkedRange = self.attributedText.string.range(of: strUrl) {
let index = textView.layoutManager.characterIndex(for: sender.location(in: self),
in: textView.textContainer,
fractionOfDistanceBetweenInsertionPoints: nil)
if linkedRange.lowerBound.encodedOffset <= index && linkedRange.upperBound.encodedOffset >= index {
if let url = URL(string: strUrl) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
}
}

@objc func handleLongPressGesture(recognizer: UIGestureRecognizer) {
guard recognizer.state == .recognized else { return }
Expand Down
15 changes: 3 additions & 12 deletions Susi/Custom Views/Message Cells/ChatMessageCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ChatMessageCell: BaseCell, UITextViewDelegate {
textView.isScrollEnabled = false
textView.isUserInteractionEnabled = true
textView.addLongPressGesture()
textView.addTapGesture()
return textView
}()

Expand All @@ -40,20 +41,10 @@ class ChatMessageCell: BaseCell, UITextViewDelegate {

override func setupViews() {
super.setupViews()

addSubview(textBubbleView)
addSubview(messageTextView)
self.contentView.addSubview(textBubbleView)
self.contentView.addSubview(messageTextView)
accessibilityIdentifier = ControllerConstants.TestKeys.chatCells

backgroundColor = .clear
}

func textView(_ textView: UITextView, shouldInteractWith url: URL, in characterRange: NSRange) -> Bool {
return true
}

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
return true
}

}
12 changes: 12 additions & 0 deletions Susi/Custom Views/Message Cells/IncomingChatCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import Material
class IncomingBubbleCell: ChatMessageCell, MKMapViewDelegate {

var message: Message?

var updatedHeight: CGFloat = 40.0

lazy var thumbUpIcon: IconButton = {
let button = IconButton()
Expand All @@ -36,8 +38,15 @@ class IncomingBubbleCell: ChatMessageCell, MKMapViewDelegate {
setupTheme()
}

override func layoutSubviews() {
super.layoutSubviews()
self.contentView.frame.size.height = updatedHeight + 40
self.frame.size.height = updatedHeight + 38
}

func setupCell(_ estimatedFrame: CGRect, _ viewFrame: CGRect) {
if let message = message {
updatedHeight = estimatedFrame.height
if message.actionType == ActionType.answer.rawValue {
messageTextView.frame = CGRect(x: 12, y: 4, width: max(estimatedFrame.width + 26, viewFrame.width / 3), height: estimatedFrame.height + 20)
textBubbleView.frame = CGRect(x: 8, y: 0, width: max(estimatedFrame.width + 40, viewFrame.width / 3), height: estimatedFrame.height + 36)
Expand All @@ -46,6 +55,8 @@ class IncomingBubbleCell: ChatMessageCell, MKMapViewDelegate {
if message.message.containsURL() {
_ = attributedString.setAsLink(textToFind: message.message.extractFirstURL(),
linkURL: message.message.extractFirstURL(), text: message.message)
messageTextView.url = message.message.extractFirstURL()

} else {
attributedString.addAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16.0)],
range: NSRange(location: 0, length: message.message.count))
Expand Down Expand Up @@ -85,6 +96,7 @@ class IncomingBubbleCell: ChatMessageCell, MKMapViewDelegate {
textBubbleView.addConstraintsWithFormat(format: "V:[v0(16)]-2-|", views: thumbUpIcon)
textBubbleView.addConstraintsWithFormat(format: "V:[v0(16)]-2-|", views: thumbDownIcon)
textBubbleView.addConstraintsWithFormat(format: "V:[v0]-4-|", views: timeLabel)
self.layoutIfNeeded()
}

@objc func sendFeedback(sender: IconButton) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import UIKit
import Material

protocol SkillSelectionProtocol: class {
func didSelectSkill(skill: Skill?)
}

class SkillListingCollectionView: UICollectionView, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

let cellId = "cellId"
Expand All @@ -20,10 +24,9 @@ class SkillListingCollectionView: UICollectionView, UICollectionViewDelegateFlow
self.reloadData()
}
}
weak var selectionDelegate: SkillSelectionProtocol?

var isLoading: Bool = false

var skillListController: SkillListingViewController?

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return groupSkills?.count ?? (isLoading ? 3 : 0)
Expand All @@ -49,7 +52,7 @@ class SkillListingCollectionView: UICollectionView, UICollectionViewDelegateFlow
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
skillListController?.selectedSkill = groupSkills?[indexPath.row]
selectionDelegate?.didSelectSkill(skill: groupSkills?[indexPath.row])
}

}
Loading