Skip to content

Commit

Permalink
[refactor] enterState 로직 UseCase에서 Repository로 이동
Browse files Browse the repository at this point in the history
  • Loading branch information
12251145 committed Dec 17, 2022
1 parent 953bbcb commit f69413c
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 42 deletions.
24 changes: 6 additions & 18 deletions Soulmate/Soulmate/Data/Repository/DefaultChattingRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ final class DefaultChattingRepository: ChattingRepository {
var lastDocument: QueryDocumentSnapshot?
var othersMessages = PassthroughSubject<[Chat], Never>()

private var listenerRegistration: ListenerRegistration?
private var chattinListenerRegistration: ListenerRegistration?

init(authRepository: AuthRepository, networkDatabaseApi: NetworkDatabaseApi) {
self.authRepository = authRepository
self.networkDatabaseApi = networkDatabaseApi
}

func removeListen() {
listenerRegistration?.remove()
listenerRegistration = nil
func removeChattingListen() {
chattinListenerRegistration?.remove()
chattinListenerRegistration = nil
}

func setStartDocument(_ doc: QueryDocumentSnapshot?) {
Expand Down Expand Up @@ -197,7 +197,7 @@ final class DefaultChattingRepository: ChattingRepository {

let query = networkDatabaseApi.query(path: path, constraints: constraints)

listenerRegistration = query
chattinListenerRegistration = query
.addSnapshotListener { [weak self] snapshot, err in
guard let snapshot, err == nil, !snapshot.documentChanges.isEmpty else {
return
Expand Down Expand Up @@ -233,20 +233,8 @@ final class DefaultChattingRepository: ChattingRepository {
self?.addMeToReadUsers(of: snapshot)
self?.othersMessages.send(chats)
self?.setLastDocument(snapshot.documents.last)
self?.listenerRegistration?.remove()
self?.listenerRegistration = nil
self?.removeChattingListen()
self?.listenOthersChattings(from: chatRoomId, uid: uid)
}
}

func listenOtherIsReading(from chatRoomId: String, userId: String) -> Query {
let path = "ChatRooms/\(chatRoomId)/LastRead"
var constraints = [
QueryEntity(field: "userId", value: userId, comparator: .isEqualTo)
]

let query = networkDatabaseApi.query(path: path, constraints: constraints)

return query
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,40 @@ final class DefaultEnterStateRepository: EnterStateRepository {

private let networkDatabaseApi: NetworkDatabaseApi

private var enterStateListenerRegistration: ListenerRegistration?

init(authRepository: AuthRepository, networkDatabaseApi: NetworkDatabaseApi) {
self.networkDatabaseApi = networkDatabaseApi
}

var othersEnterState: Bool = false
var otherIsEntered = PassthroughSubject<String, Never>()

func removeListen() {
enterStateListenerRegistration?.remove()
enterStateListenerRegistration = nil
}

func set(state: Bool, in chatRoomId: String, uid: String) {
let path = "ChatRooms/\(chatRoomId)/EnterState"

networkDatabaseApi.update(path: path, documentId: uid, with: ["state": state])
}

func listenOtherEnterStateDocRef(in chatRoomId: String, othersId: String) -> DocumentReference {
func listenOtherEnterState(in chatRoomId: String, othersId: String) {
let path = "ChatRooms/\(chatRoomId)/EnterState"

let docRef = networkDatabaseApi.documentRef(path: path, documentId: othersId)

return docRef
enterStateListenerRegistration = docRef.addSnapshotListener { [weak self] snapshot, err in
guard let document = snapshot, err == nil else { return }

if let state = document.data()?["state"] as? Bool {
self?.othersEnterState = state
if state {
self?.otherIsEntered.send(othersId)
}
}
}
}
}
5 changes: 2 additions & 3 deletions Soulmate/Soulmate/Domain/Interface/ChattingRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protocol ChattingRepository {
func setStartDocument(_ doc: QueryDocumentSnapshot?)
func setLastDocument(_ doc: QueryDocumentSnapshot?)

func removeListen()
func removeChattingListen()

func loadReadChattings(from chatRoomId: String) async -> [MessageInfoDTO]
func loadUnReadChattings(from chatRoomId: String) async -> [MessageInfoDTO]
Expand All @@ -26,6 +26,5 @@ protocol ChattingRepository {
func updateUnreadCountToZero(of chatRoomId: String, othersId: String)
func increaseUnreadCount(of id: String, in chatRoomId: String) async
func addMessage(_ message: MessageToSendDTO, to chatRoomId: String) async -> Bool
func listenOthersChattings(from chatRoomId: String, uid: String)
func listenOtherIsReading(from chatRoomId: String, userId: String) -> Query
func listenOthersChattings(from chatRoomId: String, uid: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
// Created by Hoen on 2022/12/08.
//

import Combine
import FirebaseFirestore

protocol EnterStateRepository {
var othersEnterState: Bool { get set }
var otherIsEntered: PassthroughSubject<String, Never> { get }

func removeListen()
func set(state: Bool, in chatRoomId: String, uid: String)
func listenOtherEnterStateDocRef(in chatRoomId: String, othersId: String) -> DocumentReference
func listenOtherEnterState(in chatRoomId: String, othersId: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final class DefaultListenOthersChattingUseCase: ListenOthersChattingUseCase {
}

func removeListen() {
chattingRepository.removeListen()
chattingRepository.removeChattingListen()
}

func listenOthersChattings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ import FirebaseFirestoreSwift


final class DefaultListenOthersEnterStateUseCase: ListenOthersEnterStateUseCase {

private let info: ChatRoomInfo
private var enterStateRepository: EnterStateRepository
private let authRepository: AuthRepository
private var listenerRegistration: ListenerRegistration?

var otherIsEntered = PassthroughSubject<String, Never>()
var othersEnterState = false
var otherIsEntered: PassthroughSubject<String, Never>

init(
with info: ChatRoomInfo,
Expand All @@ -28,32 +27,22 @@ final class DefaultListenOthersEnterStateUseCase: ListenOthersEnterStateUseCase
self.info = info
self.enterStateRepository = enterStateRepository
self.authRepository = authRepository

self.otherIsEntered = enterStateRepository.otherIsEntered
}

func removeListen() {
listenerRegistration?.remove()
listenerRegistration = nil
enterStateRepository.removeListen()
}

func listenOthersEnterState() {
guard let chatRoomId = info.documentId,
let uid = try? authRepository.currentUid(),
let othersId = info.userIds.first(where: { $0 != uid }) else { return }

let docRef = enterStateRepository.listenOtherEnterStateDocRef(
enterStateRepository.listenOtherEnterState(
in: chatRoomId,
othersId: othersId
)

listenerRegistration = docRef.addSnapshotListener { [weak self] snapshot, err in
guard let document = snapshot, err == nil else { return }

if let state = document.data()?["state"] as? Bool {
self?.enterStateRepository.othersEnterState = state
if state {
self?.otherIsEntered.send(othersId)
}
}
}
}
}

0 comments on commit f69413c

Please sign in to comment.