Skip to content

Commit

Permalink
Merge pull request #269 from Runnect/feature/refactor-mypage-running-…
Browse files Browse the repository at this point in the history
…history-viewmodel

[REFACTOR] 마이페이지 러닝기록 상세페이지 / 액티비티의 전역변수를 뷰모델로 이전
  • Loading branch information
leeeha authored Oct 29, 2023
2 parents c39abdd + 9bc477f commit 51aef0d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import dagger.hilt.android.AndroidEntryPoint
class MyHistoryDetailActivity :
BindingActivity<ActivityMyHistoryDetailBinding>(R.layout.activity_my_history_detail) {
private val viewModel: MyHistoryDetailViewModel by viewModels()
private var currentScreenMode: ScreenMode = ScreenMode.ReadOnlyMode
private var temporarilySavedTitle: String = ""

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -62,29 +60,21 @@ class MyHistoryDetailActivity :
}

private fun enterReadMode() {
updateCurrentScreenMode(ScreenMode.ReadOnlyMode)
viewModel.updateCurrentScreenMode(ScreenMode.ReadOnlyMode)
updateTitleInputType()
updateMoreButtonVisibility(true)
updateConstraintForReadMode()
}

private fun enterEditMode() {
updateCurrentScreenMode(ScreenMode.EditMode)
viewModel.updateCurrentScreenMode(ScreenMode.EditMode)
updateTitleInputType()
updateMoreButtonVisibility(false)
updateConstraintForEditMode()

// 중간에 수정을 취소하면 원래 제목으로 되돌리기 위해
// 현재 제목을 전역 변수에 저장해둔다.
saveTemporarilyCurrentTitle()
}

private fun saveTemporarilyCurrentTitle() {
temporarilySavedTitle = viewModel.title
}

private fun updateCurrentScreenMode(mode: ScreenMode) {
currentScreenMode = mode
// 현재 제목을 뷰모델에 저장해둔다.
viewModel.saveCurrentTitle()
}

private fun updateMoreButtonVisibility(isVisible: Boolean) {
Expand Down Expand Up @@ -115,7 +105,7 @@ class MyHistoryDetailActivity :
}

private fun handleBackButtonByCurrentScreenMode() {
when (currentScreenMode) {
when (viewModel.currentScreenMode) {
is ScreenMode.ReadOnlyMode -> navigateToPreviousScreen()
is ScreenMode.EditMode -> showStopEditingDialog()
}
Expand Down Expand Up @@ -251,7 +241,7 @@ class MyHistoryDetailActivity :
}

private fun updateTitleInputType() {
when (currentScreenMode) {
when (viewModel.currentScreenMode) {
is ScreenMode.ReadOnlyMode -> {
binding.etCourseTitle.inputType = InputType.TYPE_NULL
}
Expand Down Expand Up @@ -284,7 +274,7 @@ class MyHistoryDetailActivity :
onNegativeButtonClicked = {},
onPositiveButtonClicked = {
// 편집 모드 -> 뒤로가기 버튼 -> 편집 중단 확인 -> 뷰에 원래 제목으로 보여줌.
viewModel.updateHistoryTitle(temporarilySavedTitle)
viewModel.restoreOriginalTitle()
enterReadMode()
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class MyHistoryDetailViewModel @Inject constructor(private val userRepository: U

val isValidTitle: LiveData<Boolean> = _title.map { it.isNotBlank() }

private var _currentScreenMode: ScreenMode = ScreenMode.ReadOnlyMode
val currentScreenMode get() = _currentScreenMode

private var savedTitle: String = ""
private var historyId: Int = -1

fun updateHistoryTitle(title: String) {
Expand All @@ -44,6 +48,18 @@ class MyHistoryDetailViewModel @Inject constructor(private val userRepository: U
historyId = id
}

fun saveCurrentTitle() {
savedTitle = title
}

fun restoreOriginalTitle() {
_title.value = savedTitle
}

fun updateCurrentScreenMode(mode: ScreenMode) {
_currentScreenMode = mode
}

fun deleteHistory() {
viewModelScope.launch {
_historyDeleteState.value = UiStateV2.Loading
Expand Down

0 comments on commit 51aef0d

Please sign in to comment.