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..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 @@ -5,8 +5,10 @@ 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 +17,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 +55,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) { @@ -57,6 +71,20 @@ class ManittoRoomViewModel @ViewModelInject constructor( _members.value = manittoRoomData.members _invitationCode = manittoRoomData.invitationCode _isAdmin.value = userDataSource.getUserId() == manittoRoomData.creator.userId + _isMatched = manittoRoomData.isMatched + } + + override fun onFailed() { + _networkErrorOccur.value = true + } + }) + } + + fun match() { + roomRequest.matchManitto(roomId, object : RoomRequest.MatchManittoCallback { + override fun onSuccessMatching(missions: List) { + isMatched = true + findMyMission(missions) } override fun onFailed() { @@ -65,4 +93,26 @@ class ManittoRoomViewModel @ViewModelInject constructor( }) } + 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 + 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/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 314542e8..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 @@ -6,6 +6,7 @@ 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 +61,10 @@ class WaitingRoomFragment: Fragment() { Snackbar.LENGTH_SHORT ).show() } + santabottombuttonWaitingroom.setOnClickListener { + manittoRoomViewModel.match() + navigateMatchingFragment() + } } } @@ -67,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/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/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/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/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/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 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) + } +} 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,