Browse Source

添加创建会话的功能

fancy 5 years ago
parent
commit
fbf9111cb8

+ 12 - 8
o2ios/O2Platform/App/IM-聊天/IMChatViewController.swift

@@ -61,16 +61,20 @@ class IMChatViewController: UIViewController {
         self.messageInputView.backgroundColor = base_gray_color
 
         //标题
-        if let c = self.conversation {
-            var person = ""
-            c.personList?.forEach({ (p) in
-                if  p != O2AuthSDK.shared.myInfo()?.distinguishedName {
-                    person = p
+        if self.conversation?.type == o2_im_conversation_type_single {
+            if let c = self.conversation {
+                var person = ""
+                c.personList?.forEach({ (p) in
+                    if  p != O2AuthSDK.shared.myInfo()?.distinguishedName {
+                        person = p
+                    }
+                })
+                if !person.isEmpty {
+                    self.title = person.split("@").first ?? ""
                 }
-            })
-            if !person.isEmpty {
-                self.title = person.split("@").first ?? ""
             }
+        }else {
+            self.title = self.conversation?.title
         }
         //获取聊天数据
         self.loadMsgList(page: page)

+ 58 - 1
o2ios/O2Platform/App/IM-聊天/IMConversationListViewController.swift

@@ -12,7 +12,7 @@ import CocoaLumberjack
 class IMConversationListViewController: UIViewController {
 
     fileprivate lazy var tableview: UITableView = {
-        var tableview = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.width, height: self.view.height))
+        var tableview = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.width, height: self.view.height - TAB_BAR_HEIGHT))
         tableview.delegate = self
         tableview.dataSource = self
         tableview.backgroundColor = UIColor(netHex: 0xe8edf3)
@@ -43,6 +43,9 @@ class IMConversationListViewController: UIViewController {
 
     override func viewDidLoad() {
         super.viewDidLoad()
+        
+        self.navigationItem.rightBarButtonItems = [UIBarButtonItem(image: UIImage(named: "add"), style: .plain, target: self, action: #selector(addConversation))]
+        
         view.addSubview(tableview)
         view.addSubview(emptyView)
 
@@ -127,7 +130,61 @@ class IMConversationListViewController: UIViewController {
             self.navigationController?.tabBarItem.badgeValue = nil
         }
     }
+    
+    
+    @objc private func addConversation() {
+        self.showSheetAction(title: nil, message: nil, actions: [
+            UIAlertAction(title: "创建单聊", style: .default, handler: { (action) in
+                self.createSingleConversation()
+            }),
+            UIAlertAction(title: "创建群聊", style: .default, handler: { (action) in
+                self.createGroupConversation()
+            })
+        ])
+    }
 
+    private func createSingleConversation() {
+        self.showContactPicker(modes: [.person], callback: { (result) in
+            if let users = result.users, users.count > 0 {
+                self.viewModel.createConversation(type: o2_im_conversation_type_single, users: [users[0].distinguishedName!]).then { (con) in
+                    self.createConversationSuccess(conv: con)
+                }.catch { (err) in
+                    self.showError(title: "创建单聊失败, \(err.localizedDescription)")
+                }
+                
+            }
+        }, multiple: false)
+    }
+    
+    private func createGroupConversation() {
+        self.showContactPicker(modes: [.person], callback: { (result) in
+            if let users = result.users, users.count > 0 {
+                let array = users.map { (item) -> String in
+                    item.distinguishedName!
+                }
+                self.viewModel.createConversation(type: o2_im_conversation_type_group, users: array).then { (conv) in
+                    self.createConversationSuccess(conv: conv)
+                }.catch { (err) in
+                    self.showError(title: "创建群聊失败, \(err.localizedDescription)")
+                }
+            }
+        })
+    }
+    
+    //创建会话成功 打开聊天界面
+    private func createConversationSuccess(conv: IMConversationInfo) {
+        if !self.conversationList.contains(where: { (info) -> Bool in
+            return info.id == conv.id
+        }) {
+            self.conversationList.append(conv)
+            DispatchQueue.main.async {
+                self.tableview.reloadData()
+            }
+        }
+        let chatView = IMChatViewController()
+        chatView.conversation = conv
+        self.navigationController?.pushViewController(chatView, animated: true)
+    }
 
 }
 

+ 22 - 0
o2ios/O2Platform/App/IM-聊天/IMViewModel.swift

@@ -20,6 +20,28 @@ class IMViewModel: NSObject {
 
 extension IMViewModel {
     
+    //创建会话 @param type: single group
+    func createConversation(type: String, users: [String]) -> Promise<IMConversationInfo> {
+        let conversation = IMConversationInfo()
+        conversation.type = type
+        conversation.personList = users
+        return Promise  { fulfill, reject in
+            self.communicateAPI.request(.createConversation(conversation), completion: { result in
+                let response = OOResult<BaseModelClass<IMConversationInfo>>(result)
+                if response.isResultSuccess() {
+                    if let info = response.model?.data {
+                        fulfill(info)
+                    } else {
+                        reject(OOAppError.apiEmptyResultError)
+                    }
+                } else {
+                    reject(response.error!)
+                }
+            })
+        }
+        
+    }
+    
     //阅读会话
     func readConversation(conversationId: String?) {
         guard let id = conversationId else {

+ 1 - 0
o2ios/O2Platform/App/IM-聊天/O2IM.swift

@@ -13,6 +13,7 @@ let o2_im_ws_heartbeat = "heartbeat"
 
 
 let o2_im_conversation_type_single = "single"
+let o2_im_conversation_type_group = "group"
 
 let o2_im_msg_type_text = "text"
 let o2_im_msg_type_emoji = "emoji"

+ 1 - 0
o2ios/O2Platform/App/IM-聊天/View/IMConversationItemCell.swift

@@ -70,6 +70,7 @@ class IMConversationItemCell: UITableViewCell {
             }
         }else {//todo 群组头像 ?
             self.nameLabel.text = conversation.title
+            self.avatarImg.image = UIImage(named: "group_default")
         }
         //time
         if let time = conversation.lastMessage?.createTime {

+ 8 - 4
o2ios/O2Platform/App/IM-聊天/View/IMConversationItemCell.xib

@@ -29,14 +29,17 @@
                             </userDefinedRuntimeAttribute>
                         </userDefinedRuntimeAttributes>
                     </imageView>
-                    <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" misplaced="YES" text="姓名" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="bOp-mm-qCN">
-                        <rect key="frame" x="66" y="10" width="154" height="21"/>
+                    <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="姓名" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="bOp-mm-qCN">
+                        <rect key="frame" x="66" y="10" width="153" height="21"/>
                         <fontDescription key="fontDescription" type="system" pointSize="17"/>
                         <nil key="textColor"/>
                         <nil key="highlightedColor"/>
                     </label>
-                    <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="几分钟前" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="URQ-HD-MDc">
-                        <rect key="frame" x="257" y="10" width="58" height="17"/>
+                    <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="2020-10-23" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="URQ-HD-MDc">
+                        <rect key="frame" x="231" y="10" width="84" height="17"/>
+                        <constraints>
+                            <constraint firstAttribute="width" constant="84" id="jM2-ye-nzP"/>
+                        </constraints>
                         <fontDescription key="fontDescription" type="system" pointSize="14"/>
                         <color key="textColor" white="0.66666666666666663" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                         <nil key="highlightedColor"/>
@@ -93,6 +96,7 @@
                     <constraint firstItem="9Ff-Wo-dWM" firstAttribute="leading" secondItem="H2p-sc-9uM" secondAttribute="leading" constant="34" id="cM0-LZ-1iA"/>
                     <constraint firstItem="bOp-mm-qCN" firstAttribute="top" secondItem="H2p-sc-9uM" secondAttribute="top" constant="10" id="iUq-ik-5sX"/>
                     <constraint firstAttribute="bottom" secondItem="ckc-YE-j5q" secondAttribute="bottom" id="nX2-Nw-uiy"/>
+                    <constraint firstItem="URQ-HD-MDc" firstAttribute="leading" secondItem="bOp-mm-qCN" secondAttribute="trailing" constant="12" id="obm-on-urU"/>
                     <constraint firstItem="ckc-YE-j5q" firstAttribute="leading" secondItem="H2p-sc-9uM" secondAttribute="leading" constant="66" id="t0M-PG-f03"/>
                     <constraint firstAttribute="bottom" secondItem="pgV-nP-wwV" secondAttribute="bottom" constant="8" id="tXP-Lu-4aH"/>
                     <constraint firstItem="i51-Sc-dds" firstAttribute="leading" secondItem="H2p-sc-9uM" secondAttribute="leading" constant="10" id="xep-37-rIl"/>

+ 21 - 0
o2ios/O2Platform/Assets.xcassets/首页/group_default.imageset/Contents.json

@@ -0,0 +1,21 @@
+{
+  "images" : [
+    {
+      "idiom" : "universal",
+      "scale" : "1x"
+    },
+    {
+      "filename" : "group_default@2x.png",
+      "idiom" : "universal",
+      "scale" : "2x"
+    },
+    {
+      "idiom" : "universal",
+      "scale" : "3x"
+    }
+  ],
+  "info" : {
+    "author" : "xcode",
+    "version" : 1
+  }
+}

BIN
o2ios/O2Platform/Assets.xcassets/首页/group_default.imageset/group_default@2x.png


+ 6 - 1
o2ios/O2Platform/Framework/O2API/Communicate/CommunicateAPI.swift

@@ -17,6 +17,7 @@ enum CommunicateAPI {
     case sendMsg(IMMessageInfo)
     case readConversation(String)
     case instantMessageList(Int)
+    case createConversation(IMConversationInfo)
     
     
 }
@@ -54,6 +55,8 @@ extension CommunicateAPI: TargetType {
             return "/jaxrs/im/conversation/\(conversationId)/read"
         case .instantMessageList(let count):
             return "/jaxrs/instant/list/currentperson/noim/count/\(count)/desc"
+        case .createConversation(_):
+            return "/jaxrs/im/conversation"
         }
     }
     
@@ -61,7 +64,7 @@ extension CommunicateAPI: TargetType {
         switch self {
         case .myConversationList, .instantMessageList(_):
             return .get
-        case .msgListByPaging(_, _, _), .sendMsg(_):
+        case .msgListByPaging(_, _, _), .sendMsg(_), .createConversation(_):
             return .post
         case .readConversation(_):
             return .put
@@ -82,6 +85,8 @@ extension CommunicateAPI: TargetType {
             return .requestParameters(parameters: form.toJSON()!, encoding: JSONEncoding.default)
         case .sendMsg(let msg):
             return .requestParameters(parameters: msg.toJSON()!, encoding: JSONEncoding.default)
+        case .createConversation(let conv):
+            return .requestParameters(parameters: conv.toJSON()!, encoding: JSONEncoding.default)
         }
     }
     

+ 2 - 0
o2ios/O2Platform/config/config.swift

@@ -36,6 +36,8 @@ let safeAreaTopHeight:CGFloat = (iPhoneX ? 88 : 64)
 let IOS11_TOP_STATUSBAR_HEIGHT = iPhoneX ? 44 : 20
 //底部安全高度
 let IPHONEX_BOTTOM_SAFE_HEIGHT: CGFloat = 34.0
+//uiTabBar 高度
+let TAB_BAR_HEIGHT: CGFloat  = iPhoneX ? (49 + 34) : 49