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

채팅 기능 및 UI 개선 #450

Merged
merged 5 commits into from
Dec 14, 2022
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 @@ -63,7 +63,6 @@ final class ChatDataSource: ChatDataSourceProtocol {
return Observable.create { emitter in
let query = Constant.chatRoom.document(chatRoomID).collection("chat")
query.getDocuments(completion: { snapshot, _ in
if snapshot?.isEmpty == true { query.addDocument(data: [:]) }
self.listener = query.order(by: "date").limit(toLast: 1).addSnapshotListener({ snapshot, _ in
if let snapshot = snapshot,
let change = snapshot.documentChanges.last,
Expand Down
2 changes: 1 addition & 1 deletion Mogakco/Sources/Presentation/Chat/View/ChatCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ final class ChatCell: UICollectionViewCell, Identifiable {
}

timeLabel.snp.remakeConstraints {
$0.right.equalTo(bubbleContainer.snp.left).offset(-4)
$0.right.equalTo(bubbleContainer.snp.left).offset(-8)
$0.bottom.equalTo(bubbleContainer)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ final class ChatViewController: UIViewController {
private let viewModel: ChatViewModel
private let selectedUser = PublishSubject<User>()
private let chatMenuSelected = PublishSubject<ChatMenu>()
private var keyboardHeight = 0.0

init(viewModel: ChatViewModel) {
self.viewModel = viewModel
Expand Down Expand Up @@ -119,6 +120,7 @@ final class ChatViewController: UIViewController {
let output = viewModel.transform(input: input)
bindChatCollection(output: output)
bindSideBar(output: output)
bindTextView()
bindKeyboard()
}

Expand Down Expand Up @@ -147,7 +149,6 @@ final class ChatViewController: UIViewController {
for: IndexPath(row: index, section: 0)) as? ChatCell else {
return UICollectionViewCell()
}

cell.layoutChat(chat: chat)

cell.profileImageButton.rx.tap
Expand Down Expand Up @@ -215,6 +216,35 @@ final class ChatViewController: UIViewController {
.disposed(by: disposeBag)
}

func bindTextView() {
messageInputView.messageInputTextView.rx
.didChange
.subscribe(onNext: { [weak self] in
guard let self else { return }
let size = CGSize(width: self.messageInputView.messageInputTextView.frame.width, height: .infinity)
let estimatedSize = self.messageInputView.messageInputTextView.sizeThatFits(size)
let isMaxHeight = estimatedSize.height >= Constant.messageInputViewHeight
if isMaxHeight == self.messageInputView.messageInputTextView.isScrollEnabled { return }
self.messageInputView.messageInputTextView.isScrollEnabled = isMaxHeight
self.messageInputView.messageInputTextView.reloadInputViews()
print("DEBUG @@ : \(isMaxHeight)")
self.setNeedsUpdateConstraints(isScrollEnabled: isMaxHeight)
})
.disposed(by: disposeBag)
}

func setNeedsUpdateConstraints(isScrollEnabled: Bool) {
messageInputView.snp.remakeConstraints {
$0.left.right.equalTo(self.view.safeAreaLayoutGuide)
$0.bottom.equalToSuperview().inset(keyboardHeight)
if isScrollEnabled {
$0.top.lessThanOrEqualTo(self.view.snp.centerY)
} else {
$0.height.equalTo(Constant.messageInputViewHeight)
}
}
}

func bindKeyboard() {
RxKeyboard.instance.visibleHeight
.skip(1)
Expand Down Expand Up @@ -303,6 +333,7 @@ final class ChatViewController: UIViewController {
} else {
UIView.animate(withDuration: 0.5) { [weak self] in
guard let self else { return }
self.keyboardHeight = height
self.collectionView.snp.remakeConstraints {
$0.top.left.right.equalToSuperview()
$0.bottom.equalTo(self.messageInputView.snp.top)
Expand Down
11 changes: 2 additions & 9 deletions Mogakco/Sources/Presentation/Chat/ViewModel/ChatViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ final class ChatViewModel: ViewModel {
let alert: Signal<Alert>
}

private var isFirst = true
var chatRoomID: String = ""
var chatUseCase: ChatUseCaseProtocol?
var leaveStudyUseCase: LeaveStudyUseCaseProtocol?
Expand Down Expand Up @@ -141,10 +140,8 @@ final class ChatViewModel: ViewModel {
switch result {
case .success(let chat):
let chatID = try? newChats.value().last?.id
if viewModel.isFirst == false && chat.id != chatID {
if chat.id != chatID {
newChat.onNext(chat)
} else {
viewModel.isFirst = false
}
case .failure:
let alert = Alert(title: "메세지 로드 실패", message: "메세지 로드에 실패했어요! 다시 시도해주세요", observer: nil)
Expand Down Expand Up @@ -262,9 +259,7 @@ final class ChatViewModel: ViewModel {
)
.withUnretained(self)
.flatMap { $0.0.unsubscribePushNotificationUseCase?.excute(topic: $0.0.chatRoomID) ?? .empty() }
.subscribe(onNext: { [weak self] _ in
self?.isFirst = true
})
.subscribe(onNext: { _ in })
.disposed(by: disposeBag)

// 채팅방 나갈 시 -> 푸쉬 알림 구독
Expand All @@ -275,8 +270,6 @@ final class ChatViewModel: ViewModel {
.withUnretained(self)
.flatMap { $0.0.subscribePushNotificationUseCase?.excute(topic: $0.0.chatRoomID) ?? .empty() }
.subscribe(onNext: { [weak self] _ in
print("DEBUG : OBSERVE OUT")
self?.isFirst = true
self?.chatUseCase?.stopObserving()
})
.disposed(by: disposeBag)
Expand Down