Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#102 refactor createshortgame #115

Merged
merged 5 commits into from
Oct 20, 2023
Merged
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
5 changes: 0 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@
android:exported="false"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize" />
<activity
android:name=".presentation.shortgame.missiondetailcreate.MissionDetailCreateActivity"
android:exported="false"
android:screenOrientation="portrait"
android:theme="@style/Theme.UNIAOS.GrayStatusBar" />
<activity
android:name=".presentation.history.HistoryActivity"
android:exported="false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,26 @@ import kotlinx.serialization.Serializable

@Serializable
data class MissionCategory(
@SerialName("description")
val description: String,
@SerialName("expectedTime")
val expectedTime: Int,
@SerialName("id")
val id: Int,
@SerialName("title")
val title: String,
@SerialName("description")
val description: String,
@SerialName("rule")
val rule: String,
@SerialName("tip")
val tip: String,
@SerialName("example")
val example: String,
@SerialName("image")
val image: String,
@SerialName("level")
val level: Int,
@SerialName("missionType")
val missionType: String,
@SerialName("tip")
val tip: String,
@SerialName("title")
val title: String,
@SerialName("missionTool")
val missionTool: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ data class MissionContent(
val content: String,
@SerialName("id")
val id: Int,
@SerialName("image")
val image: String,
@SerialName("recommendTime")
val recommendTime: String?,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ถ”์ฒœ ์‹œ๊ฐ„์ด null์ผ ์ˆ˜๋„ ์ž‡๋‚˜์š”..? ํ—ˆ๊ฑฑ

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ผ๋‹จ ์„œ๋ฒ„๊ฐ€ ๋‚ด๋ ค์ฃผ๋Š”๊ฒŒ nullableํ•˜๊ฒŒ ๋‚ด๋ ค์ค๋‹ˆ๋‹ค...ใ…œใ…œ

@SerialName("missionCategory")
val missionCategory: MissionCategory,
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ data class MissionExample(
val id: Int,
@SerialName("content")
val content: String,
@SerialName("recommend_time")
val recommendTime: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ data class ResponseShortGameResultDto(
@SerialName("myRoundMission")
val myRoundMission: RoundMission,
@SerialName("partnerRoundMission")
val partnerRoundMission: RoundMission?,
val partnerRoundMission: RoundMission? = null,
)
Original file line number Diff line number Diff line change
@@ -1,64 +1,31 @@
package sopt.uni.presentation.shortgame.createshortgame

import android.content.Context
import android.content.Intent
import android.graphics.Rect
import android.os.Bundle
import android.view.MotionEvent
import android.view.inputmethod.InputMethodManager
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.databinding.BindingAdapter
import dagger.hilt.android.AndroidEntryPoint
import sopt.uni.R
import sopt.uni.databinding.ActivityCreateShortGameBinding
import sopt.uni.presentation.entity.MissionIdPosition
import sopt.uni.presentation.shortgame.createshortgame.dialog.CreateShortGameDialogFragment
import sopt.uni.presentation.shortgame.missiondetailcreate.MissionDetailCreateActivity
import sopt.uni.presentation.shortgame.missionrecord.MissionRecordActivity
import sopt.uni.util.ItemDecorations
import sopt.uni.presentation.shortgame.missiondetailcreate.MissionDetailCreateFragment
import sopt.uni.util.binding.BindingActivity
import sopt.uni.util.extension.parcelable
import sopt.uni.util.extension.setOnSingleClickListener

@AndroidEntryPoint
class CreateShortGameActivity :
BindingActivity<ActivityCreateShortGameBinding>(R.layout.activity_create_short_game) {

private val viewModel: CreateShortGameViewModel by viewModels()

private val activityLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == RESULT_OK) {
val data =
it.data?.parcelable<MissionIdPosition>(MissionDetailCreateActivity.MISSION_ID_POSITION) as MissionIdPosition
selectItem(data.id, data.position)
}
}
private val shortGameFragment by lazy { ShortGameFragment.newInstance() }
private val missionDetailCreateFragment by lazy { MissionDetailCreateFragment.newInstance() }

private val missionAdapter by lazy {
MissionCategoryAdapter(
goToMissionDetailClickListener = { missionIdPosition ->
val intent = Intent(this, MissionDetailCreateActivity::class.java)
intent.putExtra(MissionDetailCreateActivity.MISSION_ID_POSITION, missionIdPosition)
activityLauncher.launch(intent)
},
selectMissionClickListener = { missionIdPosition ->
selectItem(missionIdPosition.id, missionIdPosition.position)
},
)
}
private val viewModel: CreateShortGameViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.viewModel = viewModel
setViewModelObserve()
setAdapter()
setClickListener()
changeFragment(getString(R.string.label_short_game))
}

// ํ™”๋ฉด ํด๋ฆญํ•˜์—ฌ ํ‚ค๋ณด๋“œ ์ˆจ๊ธฐ๊ธฐ ๋ฐ ํฌ์ปค์Šค ์ œ๊ฑฐ
Expand All @@ -79,115 +46,29 @@ class CreateShortGameActivity :
return super.dispatchTouchEvent(event)
}

override fun onBackPressed() {
exitDialog()
}
fun changeFragment(tag: String) {
val fragmentManager = supportFragmentManager
val fragment = fragmentManager.findFragmentByTag(tag) ?: when (tag) {
getString(R.string.label_mission_detail) -> missionDetailCreateFragment
getString(R.string.label_short_game) -> shortGameFragment
else -> null
} ?: return

private fun setAdapter() {
binding.rvMission.adapter = missionAdapter
binding.rvMission.addItemDecoration(ItemDecorations(0, 9, 0, 9))
}
if (fragment.isVisible) return

private fun setViewModelObserve() {
viewModel.missionList.observe(this) {
missionAdapter.submitList(it)
}
viewModel.isCreateSuccess.observe(this) {
if (it) {
MissionRecordActivity.start(
this,
viewModel.roundGameId.value
?: return@observe,
)
finish()
}
}
}
val transaction = fragmentManager.beginTransaction()

private fun setClickListener() {
binding.apply {
ivClose.setOnSingleClickListener {
exitDialog()
fragmentManager.fragments.filter { it.isVisible }
.forEach {
transaction.hide(it)
}
btnCreate.setOnSingleClickListener {
createDialog()
}
}
}

private fun exitDialog() {
CreateShortGameDialogFragment().apply {
titleText =
[email protected](R.string.create_short_game_exit_dialog_title)
bodyText =
[email protected](R.string.create_short_game_exit_dialog_body)
confirmButtonText =
[email protected](R.string.create_short_game_exit_dialog_exit)
dismissButtonText =
[email protected](R.string.dialog_cancel_text)
confirmClickListener = {
finish()
this.dismiss()
}
dismissClickListener = {
this.dismiss()
}
}.show(supportFragmentManager, "")
}

private fun createDialog() {
CreateShortGameDialogFragment().apply {
titleText =
[email protected](R.string.create_short_game_create_dialog_title)
bodyText =
[email protected](R.string.create_short_game_create_dialog_body)
confirmButtonText =
[email protected](R.string.create_short_game_create_dialog_create)
dismissButtonText =
[email protected](R.string.dialog_cancel_text)
confirmClickListener = {
viewModel.createShortGame()
this.dismiss()
}
dismissClickListener = {
this.dismiss()
}
}.show(supportFragmentManager, "")
}

private fun selectItem(id: Int, position: Int) {
missionAdapter.setSelectedItem(position)
viewModel.setSelectedMissionId(id)
}

companion object {
private const val MAX_LENGTH = 54

@JvmStatic
@BindingAdapter("setContentLength")
fun setContentLength(view: TextView, length: Int) {
if (length > MAX_LENGTH) {
view.setTextColor(view.context.getColor(R.color.Red_500))
} else {
view.setTextColor(view.context.getColor(R.color.Gray_200))
}
view.text = "$length/$MAX_LENGTH"
if (fragment.isAdded) {
transaction.show(fragment)
} else {
transaction.add(R.id.fcv_create_short_game, fragment, tag)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace๊ฐ€ ์•„๋‹Œ hide & add ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”? ๋กœ์ง ์„ค๋ช…ํ•ด์ฃผ์‹œ๋ฉด ๋„ˆ๋ฌด๋„ˆ๋ฌด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace์™€ add ์ฐจ์ด๋ฅผ ๋ณด์‹œ๋ฉด replace๋Š” ์ด์ „ ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ์‚ญ์ œํ•˜๊ณ  ์ƒˆ๋กœ์šด ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ๊ณผ ๋‹ฌ๋ฆฌ add์˜ ๊ฒฝ์šฐ์—๋Š” ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ์‚ญ์ œํ•˜์ง€์•Š์Šต๋‹ˆ๋‹ค. ํ•œํŒ์Šน๋ถ€์ƒ์„ฑ์—์„œ ๋ฏธ์…˜์นดํ…Œ๊ณ ๋ฆฌ๋ทฐ๋กœ ์ž์ฃผ ์ด๋™์„ ํ•ด์•ผํ• ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ•ด์„œ ๊ตณ์ด ๋งค๋ฒˆ ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ์‚ญ์ œํ•  ํ•„์š”์—†๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €ํฌ ์ƒํ™ฉ์—์„œ๋Š” add๋กœ show/hide๋ฅผ ํ•˜๋Š” ๊ฒŒ ๋งž๊ฒ ๊ตฐ์š”..! ์ƒํƒœ๋ฅผ ๋ณด์กดํ•ด์•ผ ๋˜๊ธฐ๋„ ํ•˜๋‹ˆ๊นŒ
์—ญ์‹œ ์•„๋ถ€์ง€๊ฐ€ ์ตœ๊ณ ์•ผ ใ…Žใ…Ž ๊ฐ์‚ผ๋‘ฅ

}

@JvmStatic
@BindingAdapter("setContentLength")
fun setWishContent(view: EditText, length: Int) {
if (length > MAX_LENGTH && view.isFocused) {
view.background = view.context.getDrawable(R.drawable.bg_wish_edit_text_error)
} else {
view.background = view.context.getDrawable(R.drawable.bg_wish_edit_text)
}
}

@JvmStatic
@BindingAdapter("setContentLength")
fun setButtonEnable(view: Button, length: Int) {
view.isEnabled = length <= MAX_LENGTH
}
transaction.commit()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package sopt.uni.presentation.shortgame.createshortgame

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.map
Expand All @@ -13,15 +14,19 @@ import javax.inject.Inject
@HiltViewModel
class CreateShortGameViewModel @Inject constructor(private val shortGameRepository: ShortGameRepository) :
ViewModel() {
private val _selectedMissionId = MutableLiveData<Int>()
val selectedMissionId = _selectedMissionId
private val _missionId = MutableLiveData<Int>()
val missionId = _missionId

private val _roundGameId = MutableLiveData<Int>()
val roundGameId = _roundGameId

private val _missionList = MutableLiveData<List<MissionDetail>>()
val missionList = _missionList

val missionDetail: LiveData<MissionDetail?> = missionId.map { id ->
missionList.value?.find { it.id == id }
}

val wishContent = MutableLiveData<String>("")

val contentLength = wishContent.map {
Expand All @@ -39,7 +44,6 @@ class CreateShortGameViewModel @Inject constructor(private val shortGameReposito
viewModelScope.launch {
shortGameRepository.getMissionCategory().onSuccess {
_missionList.postValue(it)
selectedMissionId.postValue(it[0].id)
}.onFailure {
// TODO: ์‹คํŒจ์‹œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ
}
Expand All @@ -49,7 +53,7 @@ class CreateShortGameViewModel @Inject constructor(private val shortGameReposito
fun createShortGame() {
viewModelScope.launch {
shortGameRepository.createShortGame(
selectedMissionId.value ?: return@launch,
missionId.value ?: return@launch,
wishContent.value ?: "",
).onSuccess {
_roundGameId.value = it.roundGameId
Expand All @@ -61,6 +65,6 @@ class CreateShortGameViewModel @Inject constructor(private val shortGameReposito
}

fun setSelectedMissionId(missionId: Int) {
_selectedMissionId.value = missionId
_missionId.value = missionId
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.ListAdapter
import sopt.uni.data.entity.shortgame.MissionDetail
import sopt.uni.databinding.ItemMissionBinding
import sopt.uni.presentation.entity.MissionIdPosition
import sopt.uni.util.extension.ItemDiffCallback

class MissionCategoryAdapter(
private val goToMissionDetailClickListener: (MissionIdPosition) -> Unit,
private val selectMissionClickListener: (MissionIdPosition) -> Unit,
private val selectMissionClickListener: (Int) -> Unit,
) : ListAdapter<MissionDetail, MissionViewHolder>(
ItemDiffCallback<MissionDetail>(
onContentsTheSame = { old, new -> old == new },
onItemsTheSame = { old, new -> old == new },
),
) {
private var selectedPosition = 0
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MissionViewHolder {
val binding =
ItemMissionBinding.inflate(LayoutInflater.from(parent.context), parent, false)
Expand All @@ -27,17 +24,7 @@ class MissionCategoryAdapter(
override fun onBindViewHolder(holder: MissionViewHolder, position: Int) {
holder.onBind(
getItem(position),
position == selectedPosition,
position,
goToMissionDetailClickListener,
selectMissionClickListener,
)
}

fun setSelectedItem(position: Int) {
val prevPosition = selectedPosition
selectedPosition = position
notifyItemChanged(prevPosition)
notifyItemChanged(selectedPosition)
}
}
Loading
Loading