Skip to content

Commit

Permalink
style: ktlint 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
gaeun5744 committed Oct 18, 2024
1 parent a5fdea1 commit 6df6833
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.happy.friendogly.data.repository

import android.util.Log
import com.happy.friendogly.data.mapper.toData
import com.happy.friendogly.data.mapper.toDomain
import com.happy.friendogly.data.mapper.toLocalData
Expand Down Expand Up @@ -46,7 +45,7 @@ class ChatMessageRepositoryImpl
chatRoomId = chatRoomId,
limit = limit,
offset = offset,
).map { it.toDomain(myMemberId) }
).map { it.toDomain(myMemberId) },
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sealed interface Message : ChatComponent {
val content: String

class Mine(
content: String,
content: String,
val member: ChatMember,
override val createdAt: LocalDateTime,
) : Message {
Expand All @@ -17,7 +17,7 @@ sealed interface Message : ChatComponent {
content: String,
val member: ChatMember,
override val createdAt: LocalDateTime,
) : Message{
) : Message {
override val content: String = content.trim()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.happy.friendogly.presentation.ui.chatlist

import android.app.Activity
import android.content.Intent
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,90 +25,90 @@ import javax.inject.Inject

@HiltViewModel
class ChatViewModel
@Inject
constructor(
private val getChatRoomClubUseCase: GetChatRoomClubUseCase,
private val getChatMessagesUseCase: GetChatMessagesUseCase,
private val connectWebsocketUseCase: ConnectWebsocketUseCase,
private val disconnectWebsocketUseCase: DisconnectWebsocketUseCase,
private val subScribeMessageUseCase: SubScribeMessageUseCase,
private val publishSendMessageUseCase: PublishSendMessageUseCase,
private val saveChatMessageUseCase: SaveChatMessageUseCase,
) : BaseViewModel() {
private val _chats: MutableStateFlow<List<ChatUiModel>> =
MutableStateFlow(emptyList())
val chats: StateFlow<List<ChatUiModel>> get() = _chats.asStateFlow()
@Inject
constructor(
private val getChatRoomClubUseCase: GetChatRoomClubUseCase,
private val getChatMessagesUseCase: GetChatMessagesUseCase,
private val connectWebsocketUseCase: ConnectWebsocketUseCase,
private val disconnectWebsocketUseCase: DisconnectWebsocketUseCase,
private val subScribeMessageUseCase: SubScribeMessageUseCase,
private val publishSendMessageUseCase: PublishSendMessageUseCase,
private val saveChatMessageUseCase: SaveChatMessageUseCase,
) : BaseViewModel() {
private val _chats: MutableStateFlow<List<ChatUiModel>> =
MutableStateFlow(emptyList())
val chats: StateFlow<List<ChatUiModel>> get() = _chats.asStateFlow()

val sendMessage = MutableLiveData("")
val sendMessage = MutableLiveData("")

private val _newChatEvent: MutableLiveData<Event<Unit>> = MutableLiveData()
val newChatEvent: LiveData<Event<Unit>> get() = _newChatEvent
private val _newChatEvent: MutableLiveData<Event<Unit>> = MutableLiveData()
val newChatEvent: LiveData<Event<Unit>> get() = _newChatEvent

private var newChatMessageCount: Int = 0
private var newChatMessageCount: Int = 0

private var myMemberId: Long = DEFAULT_MEMBER_ID
private var myMemberId: Long = DEFAULT_MEMBER_ID

val isCanSend =
MediatorLiveData<Boolean>().apply {
addSource(sendMessage) {
value = it.isNotBlank()
val isCanSend =
MediatorLiveData<Boolean>().apply {
addSource(sendMessage) {
value = it.isNotBlank()
}
}
}

fun subscribeMessage(chatRoomId: Long) {
launch {
connectWebsocketUseCase()
myMemberId = getChatRoomClubUseCase(chatRoomId).getOrThrow().myMemberId
getChatMessage(chatRoomId, INITIAL_POSITION)
fun subscribeMessage(chatRoomId: Long) {
launch {
connectWebsocketUseCase()
myMemberId = getChatRoomClubUseCase(chatRoomId).getOrThrow().myMemberId
getChatMessage(chatRoomId, INITIAL_POSITION)

subScribeMessageUseCase(chatRoomId, myMemberId).collect { newMessage ->
_chats.value = listOf(newMessage.toUiModel()) + _chats.value
_newChatEvent.emit()
launch {
newChatMessageCount += 1
saveChatMessageUseCase(chatRoomId, newMessage)
subScribeMessageUseCase(chatRoomId, myMemberId).collect { newMessage ->
_chats.value = listOf(newMessage.toUiModel()) + _chats.value
_newChatEvent.emit()
launch {
newChatMessageCount += 1
saveChatMessageUseCase(chatRoomId, newMessage)
}
}
}
}
}

@OptIn(FlowPreview::class)
fun getChatMessage(
chatRoomId: Long,
currentPosition: Int,
) {
launch {
getChatMessagesUseCase(
myMemberId = myMemberId,
chatRoomId = chatRoomId,
offset = currentPosition,
limit = PAGING_MESSAGE_SIZE,
).debounce(DEBOUNCE_TIME_OUT).collect {
_chats.value = _chats.value.plus(it.map { it.toUiModel() })
@OptIn(FlowPreview::class)
fun getChatMessage(
chatRoomId: Long,
currentPosition: Int,
) {
launch {
getChatMessagesUseCase(
myMemberId = myMemberId,
chatRoomId = chatRoomId,
offset = currentPosition,
limit = PAGING_MESSAGE_SIZE,
).debounce(DEBOUNCE_TIME_OUT).collect {
_chats.value = _chats.value.plus(it.map { it.toUiModel() })
}
}
}
}

fun sendMessage(
chatRoomId: Long,
content: String,
) {
launch {
publishSendMessageUseCase(chatRoomId, content)
fun sendMessage(
chatRoomId: Long,
content: String,
) {
launch {
publishSendMessageUseCase(chatRoomId, content)
}
}
}

override fun onCleared() {
launch {
disconnectWebsocketUseCase()
override fun onCleared() {
launch {
disconnectWebsocketUseCase()
}
super.onCleared()
}
super.onCleared()
}

companion object {
private const val DEFAULT_MEMBER_ID = -1L
private const val INITIAL_POSITION = 0
private const val PAGING_MESSAGE_SIZE = 50
private const val DEBOUNCE_TIME_OUT = 500L
companion object {
private const val DEFAULT_MEMBER_ID = -1L
private const val INITIAL_POSITION = 0
private const val PAGING_MESSAGE_SIZE = 50
private const val DEBOUNCE_TIME_OUT = 500L
}
}
}

0 comments on commit 6df6833

Please sign in to comment.