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

Suggestions from PR #2 #4

Merged
merged 1 commit into from
Jun 15, 2018
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import Foundation

/// AttachmentManagerDataSource is a protocol to passes data to the AttachmentManager
public protocol AttachmentManagerDataSource: class {
public protocol AttachmentManagerDataSource: AnyObject {

/// The AttachmentCell for the attachment that is to be inserted into the AttachmentView
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import UIKit

/// AttachmentManagerDelegate is a protocol that can recieve notifications from the AttachmentManager
public protocol AttachmentManagerDelegate: class {
public protocol AttachmentManagerDelegate: AnyObject {

/// Can be used to determine if the AttachmentManager should be inserted into an InputStackView
///
Expand Down
41 changes: 17 additions & 24 deletions Plugins/AutocompleteManager/AutocompleteManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import UIKit

open class AutocompleteManager: NSObject, InputPlugin {
open class AutocompleteManager: NSObject, InputPlugin, UITextViewDelegate, UITableViewDelegate, UITableViewDataSource {

// MARK: - Properties [Public]

Expand All @@ -34,8 +34,8 @@ open class AutocompleteManager: NSObject, InputPlugin {
/// A protocol that more precisely defines `AutocompleteManager` logic
open weak var delegate: AutocompleteManagerDelegate?

/// A reference to the `InputTextView` that the `AutocompleteManager` is using
private(set) public weak var inputTextView: InputTextView?
/// A reference to the `UITextView` that the `AutocompleteManager` is using
private(set) public weak var textView: UITextView?

/// An ongoing session reference that holds the prefix, range and text to complete with
private(set) public var currentSession: AutocompleteSession? { didSet { layoutIfNeeded() } }
Expand Down Expand Up @@ -108,16 +108,16 @@ open class AutocompleteManager: NSObject, InputPlugin {

public init(for textView: InputTextView) {
super.init()
self.inputTextView = textView
self.inputTextView?.delegate = self
self.textView = textView
self.textView?.delegate = self
}

// MARK: - InputPlugin

/// Reloads the InputPlugin's session
open func reloadData() {

guard let result = inputTextView?.find(prefixes: autocompletePrefixes) else {
guard let result = textView?.find(prefixes: autocompletePrefixes) else {
invalidate()
return
}
Expand All @@ -135,7 +135,7 @@ open class AutocompleteManager: NSObject, InputPlugin {
///
/// - Parameter object: A string to append
open func handleInput(of object: AnyObject) -> Bool {
guard let newText = object as? String, let textView = inputTextView else { return false }
guard let newText = object as? String, let textView = textView else { return false }
let attributedString = NSMutableAttributedString(attributedString: textView.attributedText)
let newAttributedString = NSAttributedString(string: newText, attributes: typingTextAttributes)
attributedString.append(newAttributedString)
Expand Down Expand Up @@ -163,7 +163,7 @@ open class AutocompleteManager: NSObject, InputPlugin {
/// - text: The replacement text
open func autocomplete(with session: AutocompleteSession) {

guard let textView = inputTextView else { return }
guard let textView = textView else { return }
guard delegate?.autocompleteManager(self, shouldComplete: session.prefix, with: session.filter) != false else { return }

// Create a range that overlaps the prefix
Expand Down Expand Up @@ -199,7 +199,7 @@ open class AutocompleteManager: NSObject, InputPlugin {

var typingAttributes = [String: Any]()
typingTextAttributes.forEach { typingAttributes[$0.key.rawValue] = $0.value }
inputTextView?.typingAttributes = typingAttributes
textView?.typingAttributes = typingAttributes
}


Expand All @@ -211,7 +211,7 @@ open class AutocompleteManager: NSObject, InputPlugin {
/// - range: The 'NSRange' to insert over
private func insertAutocomplete(_ autocomplete: String, at session: AutocompleteSession, for range: NSRange) {

guard let textView = inputTextView else { return }
guard let textView = textView else { return }

// Apply the autocomplete attributes
var attrs = autocompleteTextAttributes[session.prefix] ?? defaultTextAttributes
Expand Down Expand Up @@ -263,17 +263,13 @@ open class AutocompleteManager: NSObject, InputPlugin {
tableView.superview?.layoutIfNeeded()
}

}

extension AutocompleteManager: UITextViewDelegate {

// MARK: - UITextViewDelegate

public func textViewDidChange(_ textView: UITextView) {
open func textViewDidChange(_ textView: UITextView) {
reloadData()
}

public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
open func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {

// Ensure that the text to be inserted is not using previous attributes
preserveTypingAttributes()
Expand Down Expand Up @@ -308,31 +304,28 @@ extension AutocompleteManager: UITextViewDelegate {
return true
}

}

extension AutocompleteManager: UITableViewDelegate, UITableViewDataSource {

// MARK: - UITableViewDataSource

final public func numberOfSections(in tableView: UITableView) -> Int {
open func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

final public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return currentAutocompleteOptions.count
}

final public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

guard var session = currentSession else { return UITableViewCell() }
session.completion = currentAutocompleteOptions[indexPath.row]
currentSession = session
let cell = dataSource?.autocompleteManager(self, tableView: tableView, cellForRowAt: indexPath, for: session) ?? UITableViewCell()
return cell
}

// MARK: - UITableViewDelegate

final public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

guard var session = currentSession else { return }
session.completion = currentAutocompleteOptions[indexPath.row]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import UIKit

/// AutocompleteManagerDataSource is a protocol that passes data to the AutocompleteManager
public protocol AutocompleteManagerDataSource: class {
public protocol AutocompleteManagerDataSource: AnyObject {

/// The autocomplete options for the registered prefix.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import UIKit

/// AutocompleteManagerDelegate is a protocol that more precisely define AutocompleteManager logic
public protocol AutocompleteManagerDelegate: class {
public protocol AutocompleteManagerDelegate: AnyObject {

/// Can be used to determine if the AutocompleteManager should be inserted into an InputStackView
///
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Add your app to the list of apps using this library and make a pull request.
## MessageKit Core Team

- [@SD10](https://github.com/sd10), Steven Deutsch
- [@nathantannar4](https://github.com/nathantannar4), Nathan Tannar (MessageInputBar Owner)
- [@nathantannar4](https://github.com/nathantannar4), Nathan Tannar (MessageInputBar Maintainer)
- [@zhongwuzw](https://github.com/zhongwuzw), Wu Zhong

## Thanks
Expand Down
6 changes: 3 additions & 3 deletions Sources/Extensions/NSMutableAttributedString+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ extension NSMutableAttributedString {

extension NSAttributedString {

func replacingCharacters(in range: NSRange, with attributedString: NSAttributedString) -> NSMutableAttributedString {
internal func replacingCharacters(in range: NSRange, with attributedString: NSAttributedString) -> NSMutableAttributedString {
let ns = NSMutableAttributedString(attributedString: self)
ns.replaceCharacters(in: range, with: attributedString)
return ns
}

static func += (lhs: inout NSAttributedString, rhs: NSAttributedString) {
internal static func += (lhs: inout NSAttributedString, rhs: NSAttributedString) {
let ns = NSMutableAttributedString(attributedString: lhs)
ns.append(rhs)
lhs = ns
}

static func + (lhs: NSAttributedString, rhs: NSAttributedString) -> NSAttributedString {
internal static func + (lhs: NSAttributedString, rhs: NSAttributedString) -> NSAttributedString {
let ns = NSMutableAttributedString(attributedString: lhs)
ns.append(rhs)
return NSAttributedString(attributedString: ns)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Protocols/InputItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import UIKit

/// InputItem is a protocol that links elements to the MessageInputBar to make them reactive
public protocol InputItem: class {
public protocol InputItem: AnyObject {

/// A reference to the MessageInputBar. Set automatically when inserted into an InputStackView
var messageInputBar: MessageInputBar? { get set }
Expand Down
2 changes: 1 addition & 1 deletion Sources/Protocols/InputPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import UIKit

/// `InputPlugin` is a protocol that makes integrating plugins to the `MessageInputBar` easy.
public protocol InputPlugin: class {
public protocol InputPlugin: AnyObject {

/// Should reload the state if the `InputPlugin`
func reloadData()
Expand Down