Skip to content

Commit

Permalink
Merge pull request #73 from Fill-ENERGY/Feature/#4-community
Browse files Browse the repository at this point in the history
[Feat] 커뮤니티 -> 채팅 api 수정
  • Loading branch information
znayeonzn authored Aug 23, 2024
2 parents 2a8c395 + cad001b commit 489560e
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 49 deletions.
15 changes: 14 additions & 1 deletion .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface ChatInterface {
fun sendMessages(

@Header("Authorization") accessToken: String,
@Body request: MessageRequest
@Body request: MessageRequest?

) :Call<MessageResponse>

Expand All @@ -38,7 +38,7 @@ interface ChatInterface {
@GET("/api/v1/threads/{threadId}/messages")
fun getMessages (
@Header("Authorization") accessToken: String,
@Path("threadId") threadId: Int,
@Path("threadId") threadId: Int?,
@Query("cursor") cursor: Int?,
@Query("limit") limit: Int?
) :Call<GetMessageResponse>
Expand All @@ -48,7 +48,7 @@ interface ChatInterface {
@PATCH("/api/v1/threads/{threadId}/messages")
fun updateReadMessage(
@Header("Authorization") accessToken: String,
@Path("threadId") threadId: Int,
@Path("threadId") threadId: Int?,
@Query("cursor") cursor: Int?,
@Query("limit") limit: Int?
) : Call<GetMessageResponse>
Expand All @@ -59,7 +59,7 @@ interface ChatInterface {

fun leaveChatRoom(
@Header("Authorization") accessToken: String,
@Path("threadId") threadId: Int
@Path("threadId") threadId: Int?
): Call<LeaveChatRoomResponse>


Expand All @@ -68,7 +68,7 @@ interface ChatInterface {

fun communityGetMessages(
@Header("Authorization") accessToken: String,
@Path("memberId") memberId: Int,
@Path("memberId") memberId: Int?,
): Call<CommunityGetMessagesResponse>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class NoteRepository {


// 메시지를 보내는 함수
fun sendMessage(accessToken: String, messageRequest: MessageRequest, callback: (MessageResponse?) -> Unit) {
fun sendMessage(accessToken: String, messageRequest: MessageRequest?, callback: (MessageResponse?) -> Unit) {


val noteApiService = getRetrofit().create(ChatInterface::class.java)
Expand Down Expand Up @@ -78,10 +78,10 @@ class NoteRepository {


//쪽지 목록 조회
fun getMessages(accessToken: String, threadId: Int, cursor: Int?, limit: Int?, callback: (GetMessageResponse)-> Unit)
fun getMessages(accessToken: String, threadId: Int?, cursor: Int?, limit: Int?, callback: (GetMessageResponse)-> Unit)
{
val noteService = getRetrofit().create(ChatInterface::class.java)
val call = noteService.getMessages(accessToken, threadId, cursor, limit)
val call = noteService.getMessages(accessToken, threadId!!, cursor, limit)


call.enqueue(object : Callback<GetMessageResponse> {
Expand Down Expand Up @@ -117,9 +117,9 @@ class NoteRepository {


//쪽지 읽음 상태 업데이트
fun updateReadMessage(accessToken: String, threadId: Int, cursor: Int?, limit: Int?, callback: (GetMessageResponse)-> Unit) {
fun updateReadMessage(accessToken: String, threadId: Int?, cursor: Int?, limit: Int?, callback: (GetMessageResponse)-> Unit) {
val noteService = getRetrofit().create(ChatInterface::class.java)
val call = noteService.updateReadMessage(accessToken, threadId, cursor, limit)
val call = noteService.updateReadMessage(accessToken, threadId!!, cursor, limit)


call.enqueue(object : Callback<GetMessageResponse> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,37 +132,6 @@ class CommunityDetailActivity : AppCompatActivity(){
}



// 채팅하기 버튼 클릭 시
binding.communityDetailChattingBtn.setOnClickListener {
NoteRepository.communityGetMessages(accessToken!!, writerId!!) { response ->

if (response != null) {
Log.d("커뮤니티채팅버튼","채팅으로 이동 성공 ${writerId}")

//if (response.result?.threadId == null) {

//}

val intent = Intent(this, NoteLiveChatActivity::class.java)

intent.putExtra("Username", writerId)
intent.putExtra("Id","dfdkssf")
intent.putExtra("threadId", writerId)
intent.putExtra("receiverId", writerId)


//intent.putExtra("cursor", )



startActivity(intent)



}
}
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class NoteLiveChatActivity : AppCompatActivity() {

private lateinit var binding: ActivityNoteLiveChatBinding
private var cursor: Int? = 0
//private var threadId: Int? = null
private var getUnreadMessage: Int = 0
private var lastDisplayedDate: String? = null
private var lastTimeTextView: TextView? = null
Expand All @@ -38,19 +39,26 @@ class NoteLiveChatActivity : AppCompatActivity() {
setContentView(binding.root)

// Intent에서 threadId, receiverId, username 및 userId 가져오기
val threadId = intent.getIntExtra("threadId", -1)
val receiverId = intent.getIntExtra("receiverId", -1)
//val threadId: Int? = intent.extras?.getInt("threadId")



val threadId: Int = intent.extras?.getInt("threadId") ?: 0


val receiverId = intent.getIntExtra("receiverId", 0)
val username = intent.getStringExtra("Username") ?: "Unknown User"
val userId = intent.getStringExtra("Id") ?: "Unknown ID"
getUnreadMessage = intent.getIntExtra("unreadMessageCount", -1)
getUnreadMessage = intent.getIntExtra("unreadMessageCount", 0)
//cursor = intent.getIntExtra("cursor", 0)



// 메시지 로딩
if (getUnreadMessage == 0) {
//val initialCursor = cursor ?: 0 // cursor가 null이면 0으로 설정

GetMessages(threadId, cursor, receiverId)

} else {
UpdateReadMessages(threadId, cursor, receiverId)
}
Expand All @@ -68,6 +76,10 @@ class NoteLiveChatActivity : AppCompatActivity() {
val accessToken = getSharedPreferences("userToken", Context.MODE_PRIVATE)
.getString("accessToken", "none")
sendMessage(accessToken, threadId, receiverId)




}

// 프로필 전환 클릭 리스너
Expand All @@ -88,8 +100,13 @@ class NoteLiveChatActivity : AppCompatActivity() {








// 쪽지 목록 조회(안 읽은 메시지가 없을 때)
private fun GetMessages(threadId: Int, cursor: Int?, receiverId: Int) {
private fun GetMessages(threadId: Int?, cursor: Int?, receiverId: Int) {
val sharedPreferences = getSharedPreferences("userToken", Context.MODE_PRIVATE)
val accessToken = sharedPreferences?.getString("accessToken", "none")

Expand All @@ -115,7 +132,7 @@ class NoteLiveChatActivity : AppCompatActivity() {
}

// 쪽지 목록 조회 (안 읽은 메시지가 있을 때)
private fun UpdateReadMessages(threadId: Int, cursor: Int?, receiverId: Int) {
private fun UpdateReadMessages(threadId: Int?, cursor: Int?, receiverId: Int) {
val sharedPreferences = getSharedPreferences("userToken", Context.MODE_PRIVATE)
val accessToken = sharedPreferences?.getString("accessToken", "none")

Expand Down Expand Up @@ -176,14 +193,35 @@ class NoteLiveChatActivity : AppCompatActivity() {
}

val images = emptyList<String>()
val messageRequest = MessageRequest(threadId, message, images, receiverId)
val messageRequest = MessageRequest(threadId!!, message, images, receiverId)



// 메시지 전송 api 호출

NoteRepository.sendMessage(accessToken!!, messageRequest) { response ->
if (response != null) {
Log.d("NoteLiveChatActivity", "서버 응답: ${response.result}")

//if (response.result.threadId == null) {
//threadId = response.result.threadId


val createdAt = response.result.createdAt
val dateFormat = formatDate(createdAt)
val timeFormat = formatTime(createdAt)

addChatBubble(message, dateFormat, timeFormat, true)
binding.messageInput.text.clear()
binding.chatScrollView.post {
binding.chatScrollView.fullScroll(View.FOCUS_DOWN)
}

cursor = response.result.messageId

//}

/*
val createdAt = response.result.createdAt
val dateFormat = formatDate(createdAt)
val timeFormat = formatTime(createdAt)
Expand All @@ -196,6 +234,8 @@ class NoteLiveChatActivity : AppCompatActivity() {
cursor = response.result.messageId
*/




Expand Down

0 comments on commit 489560e

Please sign in to comment.