Skip to content
Open
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
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,6 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

//Facebook shimmer library
implementation "com.facebook.shimmer:shimmer:0.5.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.sgs.devcamp2.flametalk_android.data.common

/**
* @author boris
* @created 2022/03/03
*/
object Status {
const val OK = 200
const val BAD_REQUEST = 400
const val UNAUTHORIZED = 401
const val FILE_NOT_FOUND = 404
const val SERVER_ERROR = 500
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sgs.devcamp2.flametalk_android.data.repository

import com.sgs.devcamp2.flametalk_android.data.common.Status
import com.sgs.devcamp2.flametalk_android.data.common.WrappedResponse
import com.sgs.devcamp2.flametalk_android.data.model.auth.AuthReq
import com.sgs.devcamp2.flametalk_android.data.model.auth.AuthRes
Expand All @@ -24,15 +25,20 @@ class AuthRepositoryImpl @Inject constructor(
return flow {
val response = remote.signUp(authReq)
if (response.isSuccessful) {
if (response.body()!!.status == 200) {
val data = response.body()!!.data
emit(Results.Success(data!!))
} else if (response.body()!!.status == 400) {
emit(Results.Error("잘못된 요청입니다"))
} else if (response.body()!!.status == 401) {
emit(Results.Error("권한이 없습니다"))
} else {
emit(Results.Error("서버 에러입니다"))
when (response.body()!!.status) {
Status.OK -> {
val data = response.body()!!.data
emit(Results.Success(data!!))
}
Status.BAD_REQUEST -> {
emit(Results.Error("잘못된 요청입니다"))
}
Status.UNAUTHORIZED -> {
emit(Results.Error("권한이 없습니다"))
}
else -> {
emit(Results.Error("서버 에러입니다"))
}
}
}
}.flowOn(ioDispatcher)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.sgs.devcamp2.flametalk_android.data.repository

import android.util.Log
import com.sgs.devcamp2.flametalk_android.data.common.Status
import com.sgs.devcamp2.flametalk_android.data.common.WrappedResponse
import com.sgs.devcamp2.flametalk_android.data.mapper.mapperToChat
import com.sgs.devcamp2.flametalk_android.data.mapper.mapperToChatRoomUpdateModel
Expand All @@ -10,7 +10,11 @@ import com.sgs.devcamp2.flametalk_android.data.source.remote.api.ChatApi
import com.sgs.devcamp2.flametalk_android.domain.entity.LocalResults
import com.sgs.devcamp2.flametalk_android.domain.entity.Results
import com.sgs.devcamp2.flametalk_android.domain.repository.ChatRepository
import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
Expand All @@ -20,94 +24,93 @@ import javax.inject.Inject
/**
* @author 김현국
* @created 2022/01/31
* Domain layer의 Chatrespository의 구현체
* Domain layer 의 Chat repository 의 구현체
*/
class ChatRepositoryImp @Inject constructor(
private val ioDispatcher: CoroutineDispatcher,
private val db: AppDatabase,
private val remote: ChatApi
) : ChatRepository {
/**
* Websocket으로 수신받은 Message를 Local Database에 저장하는 function입니다.
* WebSocket 으로 수신받은 Message 를 Local Database 에 저장하는 function 입니다.
* @param chatRes 채팅 수신 response
* @desc 채팅을 수신하면 chat data model로 chatres를 mapping하고 database에 저장합니다.
* @desc 채팅을 수신하면 chat data model 로 ChatRes 를 mapping 하고 database 에 저장합니다.
*/
override fun saveReceivedMessage(chatRes: ChatRes): Flow<Long> {
return flow {
val TAG: String = "로그"
val chat = mapperToChat(chatRes)
Log.d(TAG, "chat - $chat")
val deferr: Deferred<Long> = CoroutineScope(ioDispatcher).async {
val deferred: Deferred<Long> = CoroutineScope(ioDispatcher).async {
db.chatDao().insert(chat)
}
val index = deferr.await()
Log.d(TAG, "index - $index")
val index = deferred.await()
val chatRoomUpdateModel = mapperToChatRoomUpdateModel(chatRes)
val deferr2: Deferred<Unit> = CoroutineScope(ioDispatcher).async {
val deferred2: Deferred<Unit> = CoroutineScope(ioDispatcher).async {
db.chatRoomDao().updateLastReadMessageId(chatRoomUpdateModel)
}
deferr2.await()
deferred2.await()
emit(index)
}.flowOn(ioDispatcher)
}
/**
* 마지막으로 읽은 메시지 이후에 모든 메시지를 가져오는 function입니다.
* @param roomId 채팅방id
* 마지막으로 읽은 메시지 이후에 모든 메시지를 가져오는 function 입니다.
* @param roomId 채팅방 id
* @param lastReadMessage 마지막으로 읽은 메세지 id
* @desc 채팅 텍스트 리스트를 응답받으며, 받은 응답을 room database에 저장합니다.
* @desc 채팅 텍스트 리스트를 응답받으며, 받은 응답을 room database 에 저장합니다.
*/
override fun getMessageHistory(
roomId: String,
lastReadMessage: String
): Flow<Results<List<ChatRes>, WrappedResponse<List<ChatRes>>>> {
return flow {
val response = remote.getChatMessageHistory(roomId, lastReadMessage)
val TAG: String = "로그"
if (response.isSuccessful) {
if (response.body()!!.status == 200) {
val data = response.body()!!.data

var deferred = coroutineScope {
async {
try {
for (i in 0 until data!!.size) {
val chat = mapperToChat(data[i])
db.chatDao().insert(chat)
when (response.body()!!.status) {
Status.OK -> {
val data = response.body()!!.data
val deferred = coroutineScope {
async {
try {
for (i in 0 until data!!.size) {
val chat = mapperToChat(data[i])
db.chatDao().insert(chat)
}
} catch (e: Exception) {
}
Log.d(TAG, "ChatRepositoryImp - getMessageHistory(1) called")
} catch (e: Exception) {
}
}
deferred.await()
val deferred2: Deferred<Int> = CoroutineScope(ioDispatcher).async {
db.chatRoomDao().updateMessageCount(roomId)
}
deferred2.await()
emit(Results.Success(data!!))
}
Status.BAD_REQUEST -> {
emit(Results.Error("잘못된 요청입니다"))
}
Status.UNAUTHORIZED -> {
emit(Results.Error("권한이 없습니다"))
}
deferred.await()
var messageCount = 0
val deferr2: Deferred<Int> = CoroutineScope(ioDispatcher).async {
db.chatRoomDao().updateMessageCount(roomId)
else -> {
emit(Results.Error("서버 에러입니다"))
}
messageCount = deferr2.await()
Log.d(TAG, "ChatRepositoryImp - getMessageHistory(2) called")
emit(Results.Success(data!!))
}
} else if (response.body()!!.status == 400) {
emit(Results.Error("잘못된 요청입니다"))
} else if (response.body()!!.status == 401) {
emit(Results.Error("권한이 없습니다"))
} else {
emit(Results.Error("서버 에러입니다"))
}
}.flowOn(ioDispatcher)
}

override fun getLastReadMessageId(chatroomId: String): Flow<LocalResults<String>?> {
return flow {
val TAG: String = "로그"
var messageId: String = ""

val deferr: Deferred<String?> = CoroutineScope(ioDispatcher).async {
val messageId: String
val deferred: Deferred<String?> = CoroutineScope(ioDispatcher).async {
db.chatDao().getLastMessageWithRoomId(chatroomId)
}
messageId = deferr.await().toString()
emit(LocalResults.Success(messageId))
messageId = deferred.await().toString()
if (messageId != "") {
emit(LocalResults.Success(messageId))
} else {
emit(LocalResults.Error(""))
}
}.flowOn(ioDispatcher)
}
}
Loading