From 7193d18363b1d4650d0a82751e5eda7be882b7fa Mon Sep 17 00:00:00 2001 From: KIM SEONGGYU Date: Mon, 22 Feb 2021 16:22:08 +0900 Subject: [PATCH 1/3] feat: Add Fake request of matching --- .../room/manittoroom/ManittoRoomViewModel.kt | 53 +++++++++++ .../fragment/WaitingRoomFragment.kt | 7 ++ .../network/ManittoMatchingData.kt | 5 ++ .../network/ManittoRoomMatchedMissions.kt | 11 +++ .../santamanitto/room/network/RoomRequest.kt | 9 ++ .../room/network/FakeRoomRequest.kt | 90 ++++++++++++++----- 6 files changed, 152 insertions(+), 23 deletions(-) create mode 100644 app/src/main/java/org/sopt/santamanitto/room/manittoroom/network/ManittoMatchingData.kt create mode 100644 app/src/main/java/org/sopt/santamanitto/room/manittoroom/network/ManittoRoomMatchedMissions.kt diff --git a/app/src/main/java/org/sopt/santamanitto/room/manittoroom/ManittoRoomViewModel.kt b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/ManittoRoomViewModel.kt index 4ad312b5..6cc46747 100644 --- a/app/src/main/java/org/sopt/santamanitto/room/manittoroom/ManittoRoomViewModel.kt +++ b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/ManittoRoomViewModel.kt @@ -1,12 +1,15 @@ package org.sopt.santamanitto.room.manittoroom +import android.util.Log import androidx.hilt.lifecycle.ViewModelInject import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import org.sopt.santamanitto.NetworkViewModel import org.sopt.santamanitto.room.manittoroom.network.ManittoRoomData +import org.sopt.santamanitto.room.manittoroom.network.ManittoRoomMatchedMissions import org.sopt.santamanitto.room.manittoroom.network.ManittoRoomMember import org.sopt.santamanitto.room.network.RoomRequest +import org.sopt.santamanitto.user.data.User import org.sopt.santamanitto.user.data.source.UserDataSource import javax.inject.Named @@ -15,6 +18,10 @@ class ManittoRoomViewModel @ViewModelInject constructor( private val roomRequest: RoomRequest ): NetworkViewModel() { + companion object { + private const val TAG = "ManittoRoomViewModel" + } + private var _roomId = -1 var roomId: Int get() = _roomId @@ -49,6 +56,14 @@ class ManittoRoomViewModel @ViewModelInject constructor( val isAdmin: LiveData get() = _isAdmin + private val _myManittoName = MutableLiveData("") + val myManittoName : LiveData + get() = _myManittoName + + private val _myMission = MutableLiveData("") + val myMission: LiveData + get() = _myMission + fun refreshManittoRoomInfo() { roomRequest.getManittoRoomData(roomId, object: RoomRequest.GetManittoRoomCallback { override fun onLoadManittoRoomData(manittoRoomData: ManittoRoomData) { @@ -65,4 +80,42 @@ class ManittoRoomViewModel @ViewModelInject constructor( }) } + fun match() { + roomRequest.matchManitto(roomId, object : RoomRequest.MatchManittoCallback { + override fun onSuccessMatching(missions: List) { + isMatched = true + findMyMission(missions) + Log.d(TAG, "matchResult : $missions") + } + + override fun onFailed() { + _networkErrorOccur.value = true + } + }) + } + + private fun findMyMission(missions: List) { + for (mission in missions) { + if (mission.userId == userDataSource.getUserId()) { + setMyMissionInfo(mission) + return + } + } + _networkErrorOccur.value = true + } + + private fun setMyMissionInfo(mission: ManittoRoomMatchedMissions) { + _myMission.value = mission.myMission.content + Log.d(TAG, "myMission : ${mission.myMission.content}") + userDataSource.getUserInfo(mission.manittoUserId, object: UserDataSource.GetUserInfoCallback { + override fun onUserInfoLoaded(user: User) { + _myManittoName.value = user.userName + } + + override fun onDataNotAvailable() { + _networkErrorOccur.value = true + } + }) + } + } \ No newline at end of file diff --git a/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/WaitingRoomFragment.kt b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/WaitingRoomFragment.kt index 314542e8..62f6294d 100644 --- a/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/WaitingRoomFragment.kt +++ b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/WaitingRoomFragment.kt @@ -1,11 +1,13 @@ package org.sopt.santamanitto.room.manittoroom.fragment import android.os.Bundle +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels +import androidx.navigation.fragment.findNavController import com.google.android.material.snackbar.Snackbar import dagger.hilt.android.AndroidEntryPoint import org.sopt.santamanitto.databinding.FragmentWaitingRoomBinding @@ -60,6 +62,11 @@ class WaitingRoomFragment: Fragment() { Snackbar.LENGTH_SHORT ).show() } + santabottombuttonWaitingroom.setOnClickListener { + //Todo: 매칭 프래그먼트로 이동 + Log.d(TAG, "navigationToMatchingFragment(): go!") + manittoRoomViewModel.match() + } } } diff --git a/app/src/main/java/org/sopt/santamanitto/room/manittoroom/network/ManittoMatchingData.kt b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/network/ManittoMatchingData.kt new file mode 100644 index 00000000..3db554fe --- /dev/null +++ b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/network/ManittoMatchingData.kt @@ -0,0 +1,5 @@ +package org.sopt.santamanitto.room.manittoroom.network + +data class ManittoMatchingData( + val roomId: Int +) \ No newline at end of file diff --git a/app/src/main/java/org/sopt/santamanitto/room/manittoroom/network/ManittoRoomMatchedMissions.kt b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/network/ManittoRoomMatchedMissions.kt new file mode 100644 index 00000000..55afdf05 --- /dev/null +++ b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/network/ManittoRoomMatchedMissions.kt @@ -0,0 +1,11 @@ +package org.sopt.santamanitto.room.manittoroom.network + +import com.google.gson.annotations.SerializedName +import org.sopt.santamanitto.room.data.MissionContent + +data class ManittoRoomMatchedMissions( + @SerializedName("UserId") val userId: Int, + @SerializedName("SantaUserId") val santaUserId: Int, + @SerializedName("ManittoUserId") val manittoUserId: Int, + @SerializedName("MyMission") val myMission: MissionContent +) \ No newline at end of file diff --git a/app/src/main/java/org/sopt/santamanitto/room/network/RoomRequest.kt b/app/src/main/java/org/sopt/santamanitto/room/network/RoomRequest.kt index 5e57f17b..ee9396df 100644 --- a/app/src/main/java/org/sopt/santamanitto/room/network/RoomRequest.kt +++ b/app/src/main/java/org/sopt/santamanitto/room/network/RoomRequest.kt @@ -5,6 +5,7 @@ import org.sopt.santamanitto.room.create.network.CreateRoomResponse import org.sopt.santamanitto.room.join.network.JoinRoomData import org.sopt.santamanitto.room.join.network.JoinRoomResponse import org.sopt.santamanitto.room.manittoroom.network.ManittoRoomData +import org.sopt.santamanitto.room.manittoroom.network.ManittoRoomMatchedMissions interface RoomRequest { @@ -26,6 +27,12 @@ interface RoomRequest { fun onFailed() } + interface MatchManittoCallback { + fun onSuccessMatching(missions: List) + + fun onFailed() + } + enum class JoinRoomError { WrongInvitationCode, DuplicatedMember, AlreadyMatched, Els } @@ -35,4 +42,6 @@ interface RoomRequest { fun joinRoom(joinRoomData: JoinRoomData, callback: JoinRoomCallback) fun getManittoRoomData(roomId: Int, callback: GetManittoRoomCallback) + + fun matchManitto(roomId: Int, callback: MatchManittoCallback) } \ No newline at end of file diff --git a/app/src/mock/java/org/sopt/santamanitto/room/network/FakeRoomRequest.kt b/app/src/mock/java/org/sopt/santamanitto/room/network/FakeRoomRequest.kt index 6cbf7049..39c02408 100644 --- a/app/src/mock/java/org/sopt/santamanitto/room/network/FakeRoomRequest.kt +++ b/app/src/mock/java/org/sopt/santamanitto/room/network/FakeRoomRequest.kt @@ -1,7 +1,11 @@ package org.sopt.santamanitto.room.network +import android.os.Handler +import android.os.Looper +import kotlinx.coroutines.* import org.sopt.santamanitto.room.create.network.CreateRoomData import org.sopt.santamanitto.room.create.network.CreateRoomResponse +import org.sopt.santamanitto.room.data.MissionContent import org.sopt.santamanitto.room.join.network.JoinRoomData import org.sopt.santamanitto.room.join.network.JoinRoomInfo import org.sopt.santamanitto.room.join.network.JoinRoomResponse @@ -66,34 +70,42 @@ class FakeRoomRequest : RoomRequest { "12fsfe2" ), mutableListOf().apply { - add(ManittoRoomMission( - 1, - "Fake Mission 1" - )) - add(ManittoRoomMission( - 2, - "Fake Mission 2" - )) - add(ManittoRoomMission( - 3, - "Fake Mission 3" - )) - add(ManittoRoomMission( - 4, - "Fake Mission 4" - )) + add( + ManittoRoomMission( + 1, + "Fake Mission 1" + ) + ) + add( + ManittoRoomMission( + 2, + "Fake Mission 2" + ) + ) + add( + ManittoRoomMission( + 3, + "Fake Mission 3" + ) + ) + add( + ManittoRoomMission( + 4, + "Fake Mission 4" + ) + ) }, mutableListOf().apply { add( ManittoRoomMember( - 1, - "FakeFirstUser", - ManittoRoomRelations( - 2, - 3 + 1, + "FakeFirstUser", + ManittoRoomRelations( + 2, + 3 + ) ) ) - ) add( ManittoRoomMember( 2, @@ -118,4 +130,36 @@ class FakeRoomRequest : RoomRequest { ) ) } -} \ No newline at end of file + + override fun matchManitto(roomId: Int, callback: RoomRequest.MatchManittoCallback) { + Handler(Looper.getMainLooper()).postDelayed({ + + callback.onSuccessMatching(mutableListOf().apply { + add( + ManittoRoomMatchedMissions( + 1, + 2, + 3, + MissionContent("FakeMission1") + ) + ) + add( + ManittoRoomMatchedMissions( + 2, + 3, + 1, + MissionContent("FakeMission2") + ) + ) + add( + ManittoRoomMatchedMissions( + 3, + 1, + 2, + MissionContent("FakeMission3") + ) + ) + }) + }, 5000L) + } +} From 1e8e62e04f060b9e6f1eef64811e8d02d3891928 Mon Sep 17 00:00:00 2001 From: KIM SEONGGYU Date: Mon, 22 Feb 2021 17:23:29 +0900 Subject: [PATCH 2/3] feat: Add MatchingRoomFragment Please write layout of it. --- .../room/manittoroom/ManittoRoomViewModel.kt | 5 +- .../manittoroom/fragment/MatchingFragment.kt | 71 +++++++++++++++++++ .../fragment/WaitingRoomFragment.kt | 9 ++- .../manittoroom/network/ManittoRoomData.kt | 7 +- app/src/main/res/layout/fragment_matching.xml | 13 ++++ .../res/navigation/navigation_manittoroom.xml | 16 ++++- 6 files changed, 111 insertions(+), 10 deletions(-) create mode 100644 app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/MatchingFragment.kt create mode 100644 app/src/main/res/layout/fragment_matching.xml diff --git a/app/src/main/java/org/sopt/santamanitto/room/manittoroom/ManittoRoomViewModel.kt b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/ManittoRoomViewModel.kt index 6cc46747..32c15313 100644 --- a/app/src/main/java/org/sopt/santamanitto/room/manittoroom/ManittoRoomViewModel.kt +++ b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/ManittoRoomViewModel.kt @@ -1,6 +1,5 @@ package org.sopt.santamanitto.room.manittoroom -import android.util.Log import androidx.hilt.lifecycle.ViewModelInject import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData @@ -72,6 +71,7 @@ class ManittoRoomViewModel @ViewModelInject constructor( _members.value = manittoRoomData.members _invitationCode = manittoRoomData.invitationCode _isAdmin.value = userDataSource.getUserId() == manittoRoomData.creator.userId + _isMatched = manittoRoomData.isMatched } override fun onFailed() { @@ -85,7 +85,6 @@ class ManittoRoomViewModel @ViewModelInject constructor( override fun onSuccessMatching(missions: List) { isMatched = true findMyMission(missions) - Log.d(TAG, "matchResult : $missions") } override fun onFailed() { @@ -106,7 +105,6 @@ class ManittoRoomViewModel @ViewModelInject constructor( private fun setMyMissionInfo(mission: ManittoRoomMatchedMissions) { _myMission.value = mission.myMission.content - Log.d(TAG, "myMission : ${mission.myMission.content}") userDataSource.getUserInfo(mission.manittoUserId, object: UserDataSource.GetUserInfoCallback { override fun onUserInfoLoaded(user: User) { _myManittoName.value = user.userName @@ -117,5 +115,4 @@ class ManittoRoomViewModel @ViewModelInject constructor( } }) } - } \ No newline at end of file diff --git a/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/MatchingFragment.kt b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/MatchingFragment.kt new file mode 100644 index 00000000..8d8edd87 --- /dev/null +++ b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/MatchingFragment.kt @@ -0,0 +1,71 @@ +package org.sopt.santamanitto.room.manittoroom.fragment + +import android.os.Bundle +import android.os.Handler +import android.os.Looper +import android.util.Log +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.fragment.app.activityViewModels +import androidx.lifecycle.viewModelScope +import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import org.sopt.santamanitto.databinding.FragmentMatchingBinding +import org.sopt.santamanitto.room.manittoroom.ManittoRoomViewModel + +@AndroidEntryPoint +class MatchingFragment: Fragment() { + + companion object { + private const val TAG = "MatchingFragment" + private const val DELAY_MILLIS = 2000L + } + + private lateinit var binding: FragmentMatchingBinding + + private val manittoRoomViewModel: ManittoRoomViewModel by activityViewModels() + + private var isDelayDone = false + + private var isInBackground = false + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + binding = FragmentMatchingBinding.inflate(inflater, container, false) + + Handler(Looper.getMainLooper()).postDelayed({ + isDelayDone = true + }, DELAY_MILLIS) + + return binding.root + } + + override fun onResume() { + super.onResume() + isInBackground = false + navigateMissionFragment() + } + + override fun onPause() { + super.onPause() + isInBackground = true + } + + private fun navigateMissionFragment() { + Log.d(TAG, "coroutine(): start") + manittoRoomViewModel.viewModelScope.launch(Dispatchers.Default) { + while ((!manittoRoomViewModel.isMatched || !isDelayDone) && !isInBackground) { } + if (isInBackground) { + return@launch + } + + //Todo: 미션 프래그먼트로 이동 + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/WaitingRoomFragment.kt b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/WaitingRoomFragment.kt index 62f6294d..e996bdc2 100644 --- a/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/WaitingRoomFragment.kt +++ b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/WaitingRoomFragment.kt @@ -1,7 +1,6 @@ package org.sopt.santamanitto.room.manittoroom.fragment import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -63,9 +62,8 @@ class WaitingRoomFragment: Fragment() { ).show() } santabottombuttonWaitingroom.setOnClickListener { - //Todo: 매칭 프래그먼트로 이동 - Log.d(TAG, "navigationToMatchingFragment(): go!") manittoRoomViewModel.match() + navigateMatchingFragment() } } } @@ -74,4 +72,9 @@ class WaitingRoomFragment: Fragment() { super.onResume() manittoRoomViewModel.refreshManittoRoomInfo() } + + private fun navigateMatchingFragment() { + findNavController() + .navigate(WaitingRoomFragmentDirections.actionWaitingRoomFragmentToMatchingFragment()) + } } \ No newline at end of file diff --git a/app/src/main/java/org/sopt/santamanitto/room/manittoroom/network/ManittoRoomData.kt b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/network/ManittoRoomData.kt index 6fde4249..6e5bfaa5 100644 --- a/app/src/main/java/org/sopt/santamanitto/room/manittoroom/network/ManittoRoomData.kt +++ b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/network/ManittoRoomData.kt @@ -6,10 +6,13 @@ data class ManittoRoomData( @SerializedName("id") val roomId: Int, val roomName: String, val invitationCode: String, - @SerializedName("isMatchingDone") val isMatched: String, + @SerializedName("isMatchingDone") val _isMatched: String, val expiration: String, val createdAt: String, @SerializedName("Creator") val creator: ManittoRoomCreator, @SerializedName("Missions") val missions: List, @SerializedName("Members") val members: List -) \ No newline at end of file +) { + val isMatched: Boolean + get() = _isMatched == "true" +} \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_matching.xml b/app/src/main/res/layout/fragment_matching.xml new file mode 100644 index 00000000..02208aee --- /dev/null +++ b/app/src/main/res/layout/fragment_matching.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/navigation/navigation_manittoroom.xml b/app/src/main/res/navigation/navigation_manittoroom.xml index 87f9fa9d..7e49512e 100644 --- a/app/src/main/res/navigation/navigation_manittoroom.xml +++ b/app/src/main/res/navigation/navigation_manittoroom.xml @@ -9,5 +9,19 @@ android:id="@+id/waitingRoomFragment" android:name="org.sopt.santamanitto.room.manittoroom.fragment.WaitingRoomFragment" android:label="WaitingRoomFragment" - tool:layout="@layout/fragment_waiting_room"/> + tool:layout="@layout/fragment_waiting_room"> + + + + + + + \ No newline at end of file From 83186537277a9377c43920cbe96db20721820ecb Mon Sep 17 00:00:00 2001 From: KIM SEONGGYU Date: Mon, 22 Feb 2021 17:36:23 +0900 Subject: [PATCH 3/3] feat: Add request for matching manitto to real server --- .../sopt/santamanitto/room/network/RoomService.kt | 5 +++++ .../santamanitto/room/network/RoomRequestImpl.kt | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/app/src/main/java/org/sopt/santamanitto/room/network/RoomService.kt b/app/src/main/java/org/sopt/santamanitto/room/network/RoomService.kt index 7932f945..f35e15f7 100644 --- a/app/src/main/java/org/sopt/santamanitto/room/network/RoomService.kt +++ b/app/src/main/java/org/sopt/santamanitto/room/network/RoomService.kt @@ -6,7 +6,9 @@ import org.sopt.santamanitto.room.create.network.CreateRoomResponse import org.sopt.santamanitto.room.data.PersonalRoomInfo import org.sopt.santamanitto.room.join.network.JoinRoomData import org.sopt.santamanitto.room.join.network.JoinRoomResponse +import org.sopt.santamanitto.room.manittoroom.network.ManittoMatchingData import org.sopt.santamanitto.room.manittoroom.network.ManittoRoomData +import org.sopt.santamanitto.room.manittoroom.network.ManittoRoomMatchedMissions import retrofit2.Call import retrofit2.http.Body import retrofit2.http.GET @@ -26,4 +28,7 @@ interface RoomService { @GET("rooms/{roomId}") fun getManittoRoomData(@Path("roomId") roomId: Int): Call> + + @POST("rooms/match") + fun matchManitto(@Body manittoMatchingData: ManittoMatchingData): Call>> } \ No newline at end of file diff --git a/app/src/prod/java/org/sopt/santamanitto/room/network/RoomRequestImpl.kt b/app/src/prod/java/org/sopt/santamanitto/room/network/RoomRequestImpl.kt index 33cc14ca..03bf43b2 100644 --- a/app/src/prod/java/org/sopt/santamanitto/room/network/RoomRequestImpl.kt +++ b/app/src/prod/java/org/sopt/santamanitto/room/network/RoomRequestImpl.kt @@ -10,7 +10,9 @@ import org.sopt.santamanitto.room.create.network.CreateRoomResponse import org.sopt.santamanitto.room.join.network.JoinRoomData import org.sopt.santamanitto.room.join.network.JoinRoomErrorBody import org.sopt.santamanitto.room.join.network.JoinRoomResponse +import org.sopt.santamanitto.room.manittoroom.network.ManittoMatchingData import org.sopt.santamanitto.room.manittoroom.network.ManittoRoomData +import org.sopt.santamanitto.room.manittoroom.network.ManittoRoomMatchedMissions import retrofit2.Call import retrofit2.Callback import retrofit2.Retrofit @@ -78,6 +80,18 @@ class RoomRequestImpl( }) } + override fun matchManitto(roomId: Int, callback: RoomRequest.MatchManittoCallback) { + roomService.matchManitto(ManittoMatchingData(roomId)).start(object: RequestCallback> { + override fun onSuccess(data: List) { + callback.onSuccessMatching(data) + } + + override fun onFail() { + callback.onFailed() + } + }) + } + fun convert(errorBody: ResponseBody): JoinRoomErrorBody { return retrofitClient.responseBodyConverter( JoinRoomErrorBody::class.java,