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
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,8 @@ dependencies {
implementation "com.google.dagger:hilt-android:2.44"
kapt "com.google.dagger:hilt-compiler:2.44"

//Socket
// implementation 'com.github.nkzawa:socket.io-client:0.3.0'
implementation 'io.socket:socket.io-client:2.1.0'

}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import javax.inject.Singleton
@InstallIn(SingletonComponent::class)
object AppModule {

private const val baseUrl = "http://118.67.135.247:80/"
// private const val baseUrl = "http://118.67.135.247:80/"
const val baseUrl = "https://www.farmus.kro.kr/"
private val jwtToken = UserPrefsStorage.accessToken ?: ""

@Provides
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.farm.farmus_application.ui.farm

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
Expand Down Expand Up @@ -87,6 +88,7 @@ class ReserveFarmListFragment : Fragment() {
recentAdapter.registerAdapterDataObserver(emptyDataObserve)

farmListViewModel.currentFarmList.observe(viewLifecycleOwner) {
Log.d("CurrentFarmList:",it.toString())
recentAdapter.submitList(it.result)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.farm.farmus_application.ui.message

import android.annotation.SuppressLint
import android.content.Context
import android.nfc.Tag
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
Expand All @@ -11,12 +13,27 @@ import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.PopupMenu
import androidx.recyclerview.widget.LinearLayoutManager
import com.farm.farmus_application.MyApplication
import com.farm.farmus_application.R
import com.farm.farmus_application.databinding.FragmentClientChatBinding
import com.farm.farmus_application.di.AppModule
import com.farm.farmus_application.repository.UserPrefsStorage
import com.farm.farmus_application.ui.MainActivity
import com.farm.farmus_application.ui.message.adapter.ClientChatRVAdapter
import com.farm.farmus_application.utilities.JWTUtils
import io.socket.client.IO
import io.socket.client.Socket
import io.socket.emitter.Emitter
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONException
import org.json.JSONObject
import java.net.URISyntaxException
import java.text.SimpleDateFormat
import java.util.Locale


class ClientChatFragment : Fragment() {
Expand All @@ -25,24 +42,28 @@ class ClientChatFragment : Fragment() {
private val binding get() = _binding!!
private val jwtToken = UserPrefsStorage.accessToken
private val name = JWTUtils.decoded(jwtToken.toString())?.tokenBody?.name ?: ""

private var mSocket: Socket? = null
private val onMessageReceived = Emitter.Listener { args ->
Log.d(TAG, "update 이벤트 수신처리")
}
// TODO : 임시 데이터. 사용 예시
private val chatMessages = ArrayList<ChatMessage>().apply {
// senderId 와 receivedId가 "0", "0"일 때는 날짜 업데이트를 하게 구현되어있음(rvAdapter와 함께 봐야함)
// 기능 구현시 그 부분을 고려해서 수정필요.
// add(ChatMessage("0", "0", "2023년 01월 08일", "00:00"))

// 일반 대화 임시 데이터
add(ChatMessage(name, "내가 보냄", "오후 2:16"))
add(ChatMessage("kong", "상대방이 보냄", "오후 3:10"))
}
private lateinit var clientChatRVAdapter: ClientChatRVAdapter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
settingSocket()
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentClientChatBinding.inflate(inflater, container, false)
mSocket?.connect()
return binding.root
}

Expand All @@ -58,14 +79,55 @@ class ClientChatFragment : Fragment() {
_binding = null
}

private fun settingSocket() {
try {
mSocket = IO.socket("http://118.67.135.247:80")
Log.d(TAG,"${mSocket?.id()}")
} catch (e: URISyntaxException) {
Log.d(TAG,"${e.message}")
}
// CoroutineScope(Dispatchers.IO).launch {
// try {
// mSocket = IO.socket("https://www.farmus.kro.kr")
// mSocket.connect()
// } catch (e: Exception) {
// withContext(Dispatchers.Main) {
// Log.d(TAG,"${e.message}")
// }
// }
// }
// mSocket.on("update", onMessageReceived)
}

private fun init() {
initSendButton()
initRecyclerView()
initBackButton()
initMoreButton()
initBottomSheet()
// TODO: 추가할 초기화가 있다면 초기화
}

private fun initSendButton() {

binding.commonChatLayout.buttonSendMsg.setOnClickListener {
// sendMessage()
// 클라이언트에서 메시지를 서버로 전송
val message = binding.commonChatLayout.edittextSendMsg.text.toString()
val name = name
val data = JSONObject().apply {
// put("name", name)
// put("message", message)
put("type","message")
put("message",message)
}
Log.d(TAG,"${mSocket?.connected()}")
// mSocket.emit("send", data)
clientChatRVAdapter.addMessage(ChatMessage(name,message,"00:00"))
binding.commonChatLayout.edittextSendMsg.text.clear()
}
}

private fun initRecyclerView() {
// TODO : sender(현 사용자)의 id를 잘 전달해야함
clientChatRVAdapter = ClientChatRVAdapter(chatMessages)
Expand All @@ -88,13 +150,13 @@ class ClientChatFragment : Fragment() {
private fun initBackButton() {
binding.toolbarLayout.toolbarWithTitleAndMoreBackButton.setOnClickListener {
hideKeyboard()

// keyboard를 숨긴 후에 back되도록 (keyboard hide가 안된 것이 이전 화면에 영향을 줌)
it.postDelayed({
activity?.supportFragmentManager?.apply {
beginTransaction().remove(this@ClientChatFragment).commit()
popBackStack()
}
}
}, 100) // 100초 뒤에 처리됨
}
}
Expand All @@ -120,4 +182,19 @@ class ClientChatFragment : Fragment() {
imm?.hideSoftInputFromWindow(view?.windowToken, 0)
}


private fun settingTime() {
val currentTime = System.currentTimeMillis()
val dateFormat = SimpleDateFormat("yyyy년 MM월 dd일", Locale.KOREA)
val timeFormat = SimpleDateFormat("aa hh:mm", Locale.KOREA)
println(dateFormat.format(currentTime))
println(timeFormat.format(currentTime))
}

override fun onDestroy() {
super.onDestroy()
// Socket 연결 해제
mSocket?.disconnect()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.farm.farmus_application.utilities.JWTUtils

// TODO : chatMessages List를 어떻게 받는 가에 따라서 파라미터로 처리가 안될 수도 있음
// TODO : 구현시 나의 채팅 상대방 프로필 이미지를 끌어오는 방법이 필요함
class ClientChatRVAdapter(private val chatMessages: List<ChatMessage>) :
class ClientChatRVAdapter(private val chatMessages: ArrayList<ChatMessage>) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {

private val jwtToken = UserPrefsStorage.accessToken
Expand Down Expand Up @@ -84,6 +84,11 @@ class ClientChatRVAdapter(private val chatMessages: List<ChatMessage>) :
}
}

fun addMessage(item: ChatMessage) {
chatMessages.add(item)
notifyDataSetChanged()
}

class SentMessageViewHolder(private val binding: ItemContainerSentMessageBinding) :
RecyclerView.ViewHolder(binding.root) {
fun setData(chatMessage: ChatMessage) {
Expand Down
39 changes: 25 additions & 14 deletions app/src/main/res/layout/fragment_farm_tab1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,32 @@
android:textColor="@color/text_second"
app:layout_constraintTop_toTopOf="parent" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_recent"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/text_recent"
tools:listitem="@layout/rv_get_farm_item" />

<include
android:id="@+id/empty_current_data_parent"
layout="@layout/item_empty_farm_list"
android:layout_width="match_parent"
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/current_list_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/text_recent" />
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/text_recent">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_recent"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/rv_get_farm_item" />

<include
android:id="@+id/empty_current_data_parent"
layout="@layout/item_empty_farm_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>


<TextView
android:id="@+id/text_past"
Expand All @@ -49,7 +60,7 @@
android:paddingStart="16dp"
android:text="과거 이용 내역"
android:textColor="@color/text_second"
app:layout_constraintTop_toBottomOf="@id/empty_current_data_parent" />
app:layout_constraintTop_toBottomOf="@id/current_list_container" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_past"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/layout_chat_common.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
android:textSize="15sp" />

<ImageButton
android:id="@+id/button_send_msg"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/white"
Expand Down