forked from mirrors/linphone-iphone
Fix cell generation of UICollectionView
This commit is contained in:
parent
70671d973d
commit
2dd4750ba1
2 changed files with 100 additions and 27 deletions
|
|
@ -18,10 +18,13 @@ class ChatConversationTableViewModel: ControlsViewModel {
|
|||
|
||||
var chatRoom: ChatRoom? = nil
|
||||
|
||||
var nbEventDisplayed = MutableLiveData<Int>(20)
|
||||
|
||||
override init() {
|
||||
super.init()
|
||||
}
|
||||
|
||||
/*
|
||||
func updateData() {
|
||||
if (chatRoom == nil) {
|
||||
return
|
||||
|
|
@ -29,8 +32,10 @@ class ChatConversationTableViewModel: ControlsViewModel {
|
|||
|
||||
let oneToOne = chatRoom!.hasCapability(mask: Int(LinphoneChatRoomCapabilitiesOneToOne.rawValue))
|
||||
let chatRoomEvents = chatRoom?.getHistoryEvents(nbEvents: 20)
|
||||
if(nbEventDisplayed.value != 20){
|
||||
nbEventDisplayed.value = 20
|
||||
}
|
||||
messageListHistory.removeAll()
|
||||
messageListHistory = []
|
||||
chatRoomEvents?.forEach({ eventLog in
|
||||
let event = eventLog
|
||||
let eventType = event.type
|
||||
|
|
@ -38,7 +43,9 @@ class ChatConversationTableViewModel: ControlsViewModel {
|
|||
if oneToOne && !eventTypeIsOfInterestForOne(toOneRoom: eventType) {
|
||||
} else {
|
||||
if let chat = event.chatMessage {
|
||||
messageListHistory.append(chat)
|
||||
//messageListHistory.append(chat)
|
||||
messageListHistory.insert(chat, at: 0)
|
||||
print("MultilineMessageCell configure ChatMessage cell \(messageListHistory.count)")
|
||||
} //linphone_event_log_get_chat_message(event)
|
||||
/*
|
||||
if auto_download is available and file transfer in progress, not add event now
|
||||
|
|
@ -56,6 +63,62 @@ class ChatConversationTableViewModel: ControlsViewModel {
|
|||
})
|
||||
}
|
||||
|
||||
func addData() {
|
||||
if (chatRoom == nil) {
|
||||
return
|
||||
}
|
||||
|
||||
let oneToOne = chatRoom!.hasCapability(mask: Int(LinphoneChatRoomCapabilitiesOneToOne.rawValue))
|
||||
let chatRoomEvents = chatRoom?.getHistoryRangeEvents(begin: nbEventDisplayed.value!, end: nbEventDisplayed.value! + 20)
|
||||
chatRoomEvents?.reversed().forEach({ eventLog in
|
||||
let event = eventLog
|
||||
let eventType = event.type
|
||||
|
||||
if oneToOne && !eventTypeIsOfInterestForOne(toOneRoom: eventType) {
|
||||
} else {
|
||||
if let chat = event.chatMessage {
|
||||
//messageListHistory.insert(chat, at: 0)
|
||||
messageListHistory.append(chat)
|
||||
print("MultilineMessageCell configure ChatMessage cell added \(messageListHistory.count)")
|
||||
} //linphone_event_log_get_chat_message(event)
|
||||
/*
|
||||
if auto_download is available and file transfer in progress, not add event now
|
||||
if !(autoDownload && chat != nil && linphone_chat_message_is_file_transfer_in_progress(chat)) {
|
||||
totalEventList.append(NSValue(pointer: linphone_event_log_ref(event)))
|
||||
if listSize <= BASIC_EVENT_LIST {
|
||||
eventList.append(NSValue(pointer: linphone_event_log_ref(event)))
|
||||
}
|
||||
}
|
||||
|
||||
listSize -= 1*/
|
||||
|
||||
|
||||
}
|
||||
})
|
||||
if(chatRoomEvents!.count > 0){
|
||||
nbEventDisplayed.value! += 20
|
||||
}
|
||||
}
|
||||
*/
|
||||
func getMessage(index: Int) -> ChatMessage? {
|
||||
if (chatRoom == nil) {
|
||||
print("MultilineMessageCell configure ChatMessage cell empty")
|
||||
return nil
|
||||
}
|
||||
|
||||
let oneToOne = chatRoom!.hasCapability(mask: Int(LinphoneChatRoomCapabilitiesOneToOne.rawValue))
|
||||
let chatRoomEvents = chatRoom?.getHistoryRangeEvents(begin: index, end: index+1)
|
||||
print("MultilineMessageCell configure ChatMessage cell message is \(chatRoomEvents?.first?.chatMessage?.contents.first?.utf8Text)")
|
||||
return chatRoomEvents?.first?.chatMessage
|
||||
}
|
||||
|
||||
func getNBMessages() -> Int {
|
||||
if (chatRoom == nil) {
|
||||
return 0
|
||||
}
|
||||
return chatRoom!.historySize
|
||||
}
|
||||
|
||||
func eventTypeIsOfInterestForOne(toOneRoom type: EventLogType) -> Bool {
|
||||
return type.rawValue == LinphoneEventLogTypeConferenceChatMessage.rawValue || type.rawValue == LinphoneEventLogTypeConferenceEphemeralMessageEnabled.rawValue || type.rawValue == LinphoneEventLogTypeConferenceEphemeralMessageDisabled.rawValue || type.rawValue == LinphoneEventLogTypeConferenceEphemeralMessageLifetimeChanged.rawValue
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,31 +24,25 @@ class ChatConversationTableViewSwift: UIViewController, UICollectionViewDataSour
|
|||
return collectionView
|
||||
}()
|
||||
|
||||
/*
|
||||
// Initializers
|
||||
init() {
|
||||
// Create new `UICollectionView` and set `UICollectionViewFlowLayout` as its layout
|
||||
collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
|
||||
super.init(nibName: nil, bundle: nil)
|
||||
}
|
||||
var isLoaded = false
|
||||
|
||||
required init?(coder aDecoder: NSCoder) {
|
||||
// Create new `UICollectionView` and set `UICollectionViewFlowLayout` as its layout
|
||||
collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
|
||||
super.init(coder: aDecoder)
|
||||
}
|
||||
*/
|
||||
var basic :Bool = false
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
|
||||
self.initView()
|
||||
|
||||
UIDeviceBridge.displayModeSwitched.readCurrentAndObserve { _ in
|
||||
self.collectionView.backgroundColor = VoipTheme.backgroundWhiteBlack.get()
|
||||
}
|
||||
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(self.rotated), name: UIDevice.orientationDidChangeNotification, object: nil)
|
||||
|
||||
ChatConversationTableViewModel.sharedModel.nbEventDisplayed.observe { index in
|
||||
self.collectionView.reloadData()
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
|
|
@ -60,6 +54,8 @@ class ChatConversationTableViewSwift: UIViewController, UICollectionViewDataSour
|
|||
}
|
||||
|
||||
func initView(){
|
||||
basic = isBasicChatRoom(ChatConversationTableViewModel.sharedModel.chatRoom?.getCobject)
|
||||
|
||||
view.addSubview(collectionView)
|
||||
collectionView.backgroundColor = VoipTheme.backgroundWhiteBlack.get()
|
||||
collectionView.contentInsetAdjustmentBehavior = .always
|
||||
|
|
@ -77,36 +73,50 @@ class ChatConversationTableViewSwift: UIViewController, UICollectionViewDataSour
|
|||
|
||||
(collectionView.collectionViewLayout as! UICollectionViewFlowLayout).estimatedItemSize = UICollectionViewFlowLayout.automaticSize
|
||||
(collectionView.collectionViewLayout as! UICollectionViewFlowLayout).minimumLineSpacing = 2
|
||||
|
||||
collectionView.transform = CGAffineTransform(scaleX: 1, y: -1)
|
||||
}
|
||||
|
||||
override func viewWillAppear(_ animated: Bool) {
|
||||
ChatConversationTableViewModel.sharedModel.updateData()
|
||||
//ChatConversationTableViewModel.sharedModel.updateData()
|
||||
collectionView.reloadData()
|
||||
}
|
||||
|
||||
|
||||
override func viewDidAppear(_ animated: Bool) {
|
||||
let indexPath = IndexPath(item: ChatConversationTableViewModel.sharedModel.messageListHistory.count - 1, section: 0)
|
||||
self.collectionView.scrollToItem(at: indexPath, at: .bottom, animated: false)
|
||||
self.collectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: .top, animated: false)
|
||||
//print("MultilineMessageCell configure ChatMessage cell loaded")
|
||||
isLoaded = true
|
||||
}
|
||||
|
||||
|
||||
override func viewDidLayoutSubviews() {
|
||||
super.viewDidLayoutSubviews()
|
||||
self.collectionView.scrollToItem(at: IndexPath(row: ChatConversationTableViewModel.sharedModel.messageListHistory.count-1, section: 0), at: .bottom, animated: false)
|
||||
}
|
||||
|
||||
// MARK: - UICollectionViewDataSource -
|
||||
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
||||
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MultilineMessageCell.reuseId, for: indexPath) as! MultilineMessageCell
|
||||
let basic = isBasicChatRoom(ChatConversationTableViewModel.sharedModel.chatRoom?.getCobject)
|
||||
cell.configure(message: ChatConversationTableViewModel.sharedModel.messageListHistory[indexPath.row], isBasic: basic)
|
||||
|
||||
//cell.configure(message: ChatConversationTableViewModel.sharedModel.messageListHistory[indexPath.row], isBasic: basic)
|
||||
|
||||
print("MultilineMessageCell configure ChatMessage cell \(indexPath.row)")
|
||||
|
||||
if let message = ChatConversationTableViewModel.sharedModel.getMessage(index: indexPath.row){
|
||||
print("MultilineMessageCell configure ChatMessage cell inininni\(indexPath.row)")
|
||||
cell.configure(message: message, isBasic: basic)
|
||||
}
|
||||
|
||||
/*
|
||||
if(indexPath.row >= ChatConversationTableViewModel.sharedModel.messageListHistory.count-5 && indexPath.row < ChatConversationTableViewModel.sharedModel.messageListHistory.count-4 && isLoaded){
|
||||
|
||||
ChatConversationTableViewModel.sharedModel.addData()
|
||||
|
||||
//print("MultilineMessageCell configure ChatMessage cell iiiiinnnnn\(indexPath.row)")
|
||||
}*/
|
||||
//print("MultilineMessageCell configure ChatMessage cell \(indexPath.row)")
|
||||
|
||||
cell.contentView.transform = CGAffineTransform(scaleX: 1, y: -1)
|
||||
return cell
|
||||
}
|
||||
|
||||
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
||||
return ChatConversationTableViewModel.sharedModel.messageListHistory.count
|
||||
return ChatConversationTableViewModel.sharedModel.getNBMessages()
|
||||
}
|
||||
|
||||
// MARK: - UICollectionViewDelegateFlowLayout -
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue