Skip to content

Commit

Permalink
Merge pull request #34 from SEONGGYU96/feature/request_matching
Browse files Browse the repository at this point in the history
Feature/request matching
  • Loading branch information
ethan-223 authored Feb 22, 2021
2 parents 8012f08 + 8318653 commit ae7ade5
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -49,6 +55,14 @@ class ManittoRoomViewModel @ViewModelInject constructor(
val isAdmin: LiveData<Boolean>
get() = _isAdmin

private val _myManittoName = MutableLiveData("")
val myManittoName : LiveData<String>
get() = _myManittoName

private val _myMission = MutableLiveData("")
val myMission: LiveData<String>
get() = _myMission

fun refreshManittoRoomInfo() {
roomRequest.getManittoRoomData(roomId, object: RoomRequest.GetManittoRoomCallback {
override fun onLoadManittoRoomData(manittoRoomData: ManittoRoomData) {
Expand All @@ -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<ManittoRoomMatchedMissions>) {
isMatched = true
findMyMission(missions)
}

override fun onFailed() {
Expand All @@ -65,4 +93,26 @@ class ManittoRoomViewModel @ViewModelInject constructor(
})
}

private fun findMyMission(missions: List<ManittoRoomMatchedMissions>) {
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
}
})
}
}
Original file line number Diff line number Diff line change
@@ -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: 미션 프래그먼트로 이동
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -60,11 +61,20 @@ class WaitingRoomFragment: Fragment() {
Snackbar.LENGTH_SHORT
).show()
}
santabottombuttonWaitingroom.setOnClickListener {
manittoRoomViewModel.match()
navigateMatchingFragment()
}
}
}

override fun onResume() {
super.onResume()
manittoRoomViewModel.refreshManittoRoomInfo()
}

private fun navigateMatchingFragment() {
findNavController()
.navigate(WaitingRoomFragmentDirections.actionWaitingRoomFragmentToMatchingFragment())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.sopt.santamanitto.room.manittoroom.network

data class ManittoMatchingData(
val roomId: Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -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<ManittoRoomMission>,
@SerializedName("Members") val members: List<ManittoRoomMember>
)
) {
val isMatched: Boolean
get() = _isMatched == "true"
}
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -26,6 +27,12 @@ interface RoomRequest {
fun onFailed()
}

interface MatchManittoCallback {
fun onSuccessMatching(missions: List<ManittoRoomMatchedMissions>)

fun onFailed()
}

enum class JoinRoomError {
WrongInvitationCode, DuplicatedMember, AlreadyMatched, Els
}
Expand All @@ -35,4 +42,6 @@ interface RoomRequest {
fun joinRoom(joinRoomData: JoinRoomData, callback: JoinRoomCallback)

fun getManittoRoomData(roomId: Int, callback: GetManittoRoomCallback)

fun matchManitto(roomId: Int, callback: MatchManittoCallback)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,4 +28,7 @@ interface RoomService {

@GET("rooms/{roomId}")
fun getManittoRoomData(@Path("roomId") roomId: Int): Call<Response<ManittoRoomData>>

@POST("rooms/match")
fun matchManitto(@Body manittoMatchingData: ManittoMatchingData): Call<Response<List<ManittoRoomMatchedMissions>>>
}
13 changes: 13 additions & 0 deletions app/src/main/res/layout/fragment_matching.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>

<layout>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Todo: 레이아웃 작성하기 -->

</androidx.constraintlayout.widget.ConstraintLayout>

</layout>
16 changes: 15 additions & 1 deletion app/src/main/res/navigation/navigation_manittoroom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<action
android:id="@+id/action_waitingRoomFragment_to_matchingFragment"
app:destination="@id/matchingFragment"
app:popUpTo="@id/matchingFragment" />

</fragment>

<fragment
android:id="@+id/matchingFragment"
android:name="org.sopt.santamanitto.room.manittoroom.fragment.MatchingFragment"
android:label="MatchingFragment"
tool:layout="@layout/fragment_matching" />

</navigation>
Loading

0 comments on commit ae7ade5

Please sign in to comment.