Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from teambition/develop
Browse files Browse the repository at this point in the history
Feature(Avatar):add avatar click handle event
  • Loading branch information
Harries Chen committed Mar 17, 2016
2 parents 615ae84 + 22e0d58 commit b5b167a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 54 deletions.
13 changes: 13 additions & 0 deletions MessageKit/BaseMessageCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public class BaseMessageCollectionViewCell<BubbleViewType where BubbleViewType:U

func createAvatarView() -> UIImageView! {
let imageView = UIImageView()
imageView.userInteractionEnabled = true
return imageView
}

Expand All @@ -129,6 +130,11 @@ public class BaseMessageCollectionViewCell<BubbleViewType where BubbleViewType:U
return tapGestureRecognizer
}()

public private(set) lazy var tapAvatarGestureRecognizer: UITapGestureRecognizer = {
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "avatarTapped:")
return tapGestureRecognizer
}()

public private (set) lazy var longPressGestureRecognizer: UILongPressGestureRecognizer = {
let longpressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "bubbleLongPressed:")
longpressGestureRecognizer.delegate = self
Expand All @@ -140,6 +146,7 @@ public class BaseMessageCollectionViewCell<BubbleViewType where BubbleViewType:U
self.bubbleView.addGestureRecognizer(self.tapGestureRecognizer)
self.bubbleView.addGestureRecognizer(self.longPressGestureRecognizer)
self.avatarView = createAvatarView()
self.avatarView.addGestureRecognizer(self.tapAvatarGestureRecognizer)
self.timestampLabel = createTimestampView()
self.contentView.addSubview(self.bubbleView)
self.contentView.addSubview(self.avatarView)
Expand Down Expand Up @@ -253,6 +260,12 @@ public class BaseMessageCollectionViewCell<BubbleViewType where BubbleViewType:U
func bubbleLongPressed() {
self.onBubbleLongPressed?(cell: self)
}

public var onAvatarTapped: ((cell: BaseMessageCollectionViewCell) -> Void)?
@objc
private func avatarTapped(tapGestureRecognizer: UITapGestureRecognizer) {
self.onAvatarTapped?(cell: self)
}
}

struct BaseMessageLayoutModel {
Expand Down
5 changes: 5 additions & 0 deletions MessageKit/BaseMessagePresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public protocol BaseMessageInteractionHandlerProtocol {
func userDidTapOnFailIcon(viewModel viewModel: ViewModelT)
func userDidTapOnBubble(viewModel viewModel: ViewModelT)
func userDidLongPressOnBubble(viewModel viewModel: ViewModelT)
func userDidTapOnAvatar(viewModel viewModel: ViewModelT)
}

public class BaseMessagePresenter<BubbleViewT, ViewModelBuilderT, InteractionHandlerT where
Expand Down Expand Up @@ -102,6 +103,10 @@ BubbleViewT: UIView, BubbleViewT:MaximumLayoutWidthSpecificable, BubbleViewT: Ba
guard let sSelf = self else { return }
sSelf.interactionHandler?.userDidTapOnFailIcon(viewModel: sSelf.messageViewModel)
}
cell.onAvatarTapped = { [weak self] (cell) in
guard let sSelf = self else { return }
sSelf.interactionHandler?.userDidTapOnAvatar(viewModel: sSelf.messageViewModel)
}
additionalConfiguration?()
}, animated: animated, completion: nil)
}
Expand Down
13 changes: 9 additions & 4 deletions MessageKit/CustomBubbleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,15 @@ public final class CustomBubbleView: UIView, MaximumLayoutWidthSpecificable, Bac
// MARK: Layout

public override func sizeThatFits(size: CGSize) -> CGSize {
return self.calculateCustomBubbleLayout(maximumWidth: self.preferredMaxLayoutWidth).size
return self.calculateCustomBubbleLayout(maximumWidth: self.preferredMaxLayoutWidth).frame.size
}

public override func layoutSubviews() {
super.layoutSubviews()
let _ = self.calculateCustomBubbleLayout(maximumWidth: self.preferredMaxLayoutWidth)
let layout = self.calculateCustomBubbleLayout(maximumWidth: self.preferredMaxLayoutWidth)

self.addSubview(self.customMessageViewModel.customView)
customMessageViewModel.customView.frame = layout.frame
}

private func calculateCustomBubbleLayout(maximumWidth maximumWidth: CGFloat) -> CustomBubbleLayoutModel {
Expand All @@ -122,7 +123,7 @@ public final class CustomBubbleView: UIView, MaximumLayoutWidthSpecificable, Bac
}

private class CustomBubbleLayoutModel {
var size: CGSize = CGSize.zero
var frame: CGRect = CGRect.zero

struct LayoutContext {
let customViewSize: CGSize
Expand All @@ -140,6 +141,10 @@ private class CustomBubbleLayoutModel {
}

func calculateLayout() {
size = layoutContext.customViewSize
//adjust X
var currentX: CGFloat = 0.0
currentX += 8
frame.origin.x = currentX
frame.size = layoutContext.customViewSize
}
}
50 changes: 0 additions & 50 deletions MessageKitDemo/MessageKitDemo/TableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,66 +26,16 @@ class TableViewController: UITableViewController, UISplitViewControllerDelegate
// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 3
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
cell.textLabel?.text = "你好"
// Configure the cell...

return cell
}

/*
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
*/

/*
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/

/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

}
*/

/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}
15 changes: 15 additions & 0 deletions MessageKitDemo/MessageKitDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class TextMessageTestHandler: BaseMessageInteractionHandlerProtocol {
func userDidLongPressOnBubble(viewModel viewModel: ViewModelT) {
print("长按bubble")
}

func userDidTapOnAvatar(viewModel viewModel: ViewModelT) {
print("点击头像")
}
}

class PhotoMessageTestHandler: BaseMessageInteractionHandlerProtocol {
Expand All @@ -40,6 +44,9 @@ class PhotoMessageTestHandler: BaseMessageInteractionHandlerProtocol {
func userDidLongPressOnBubble(viewModel viewModel: ViewModelT) {
print("长按图片")
}
func userDidTapOnAvatar(viewModel viewModel: ViewModelT) {
print("点击头像")
}
}

class FileMessageTestHandler: BaseMessageInteractionHandlerProtocol {
Expand All @@ -56,6 +63,9 @@ class FileMessageTestHandler: BaseMessageInteractionHandlerProtocol {
func userDidLongPressOnBubble(viewModel viewModel: ViewModelT) {

}
func userDidTapOnAvatar(viewModel viewModel: ViewModelT) {
print("点击头像")
}
}

class CustomMessageTestHandler: BaseMessageInteractionHandlerProtocol {
Expand All @@ -72,6 +82,9 @@ class CustomMessageTestHandler: BaseMessageInteractionHandlerProtocol {
func userDidLongPressOnBubble(viewModel viewModel: ViewModelT) {

}
func userDidTapOnAvatar(viewModel viewModel: ViewModelT) {
print("点击头像")
}
}

class FakeDataSource: MessageDataSourceProtocol {
Expand Down Expand Up @@ -170,6 +183,8 @@ class ViewController: MessageViewController {
testModel(uid:"19", sid: "dd", type: .File, coming: true, text: "标题位置可以分别设置为上下左右,4个位置", isSuccess: true),
testModel(uid:"19", sid: "dd", type: .File, coming: true, text: "标题位置可以分别设置为上下左右,4个位置", isSuccess: true),
testModel(uid:"18", sid: "dd", type: .Text, coming: false, text: "dsfsd", isSuccess: true),
testModel(uid:"19", sid: "dd", type: .Text, coming: true, text: "标题位置可以分别设置为上下左右,4个位置", isSuccess: true),
testModel(uid:"18", sid: "dd", type: .Text, coming: true, text: "dsfsd", isSuccess: true),
testModel(uid:"19", sid: "dd", type: .Custom, coming: true, text: "标题位置可以分别设置为上下左右,4个位置", isSuccess: true)
]

Expand Down

0 comments on commit b5b167a

Please sign in to comment.