Skip to content

Commit f5d5e24

Browse files
bngshyonghanJu
andcommitted
✨ 러닝 종료 화면 생성 및 네비게이션 연결
Co-authored-by: yonghanJu <[email protected]>
1 parent ae45e4b commit f5d5e24

File tree

5 files changed

+133
-1
lines changed

5 files changed

+133
-1
lines changed

presentation/src/main/java/com/whyranoid/presentation/running/RunningViewModel.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,8 @@ class RunningViewModel @Inject constructor(
110110
// TODO: 액티비티에 이벤트 알려주기
111111
runningRepository.finishRunning()
112112
}
113+
114+
companion object {
115+
const val RUNNING_FINISH_DATA_KEY = "runningFinishData"
116+
}
113117
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.whyranoid.presentation.runningfinish
2+
3+
import android.os.Bundle
4+
import android.view.View
5+
import androidx.fragment.app.viewModels
6+
import com.whyranoid.presentation.R
7+
import com.whyranoid.presentation.base.BaseFragment
8+
import com.whyranoid.presentation.databinding.FragmentRunningFinishBinding
9+
import com.whyranoid.presentation.model.UiState
10+
import com.whyranoid.presentation.running.RunningFinishData
11+
import com.whyranoid.presentation.util.repeatWhenUiStarted
12+
import dagger.hilt.android.AndroidEntryPoint
13+
import timber.log.Timber
14+
15+
@AndroidEntryPoint
16+
internal class RunningFinishFragment :
17+
BaseFragment<FragmentRunningFinishBinding>(R.layout.fragment_running_finish) {
18+
19+
private val viewModel: RunningFinishViewModel by viewModels()
20+
21+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
22+
super.onViewCreated(view, savedInstanceState)
23+
24+
initViews()
25+
observeState()
26+
}
27+
28+
private fun initViews() {
29+
binding.vm = viewModel
30+
}
31+
32+
private fun observeState() {
33+
viewLifecycleOwner.repeatWhenUiStarted {
34+
viewModel.runningFinishDataState.collect { state ->
35+
when (state) {
36+
is UiState.UnInitialized -> handleDataStateUninitialized()
37+
is UiState.Loading -> handleDataStateLoading()
38+
is UiState.Success -> handleDataStateSuccess(state.value)
39+
is UiState.Failure -> handleDataStateFailure(state.throwable)
40+
}
41+
}
42+
}
43+
}
44+
45+
private fun handleDataStateUninitialized() {
46+
Timber.d("running finish Uninitialized")
47+
}
48+
49+
private fun handleDataStateLoading() {
50+
Timber.d("running finish Loading")
51+
}
52+
53+
private fun handleDataStateSuccess(runningFinishData: RunningFinishData) {
54+
Timber.d(runningFinishData.toString())
55+
}
56+
57+
private fun handleDataStateFailure(throwable: Throwable) {
58+
Timber.d(throwable.message)
59+
}
60+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.whyranoid.presentation.runningfinish
2+
3+
import androidx.lifecycle.SavedStateHandle
4+
import androidx.lifecycle.ViewModel
5+
import androidx.lifecycle.viewModelScope
6+
import com.whyranoid.domain.model.MoGakRunException
7+
import com.whyranoid.presentation.model.UiState
8+
import com.whyranoid.presentation.running.RunningFinishData
9+
import com.whyranoid.presentation.running.RunningViewModel.Companion.RUNNING_FINISH_DATA_KEY
10+
import dagger.hilt.android.lifecycle.HiltViewModel
11+
import kotlinx.coroutines.flow.MutableStateFlow
12+
import kotlinx.coroutines.flow.asStateFlow
13+
import kotlinx.coroutines.launch
14+
import javax.inject.Inject
15+
16+
@HiltViewModel
17+
class RunningFinishViewModel @Inject constructor(
18+
private val stateHandle: SavedStateHandle
19+
) : ViewModel() {
20+
21+
private val _runningFinishDataState =
22+
MutableStateFlow<UiState<RunningFinishData>>(UiState.UnInitialized)
23+
val runningFinishDataState get() = _runningFinishDataState.asStateFlow()
24+
25+
init {
26+
getRunningFinishDataState()
27+
}
28+
29+
private fun getRunningFinishDataState() {
30+
_runningFinishDataState.value = UiState.Loading
31+
viewModelScope.launch {
32+
stateHandle.get<RunningFinishData>(RUNNING_FINISH_DATA_KEY)?.let { runningFinishData ->
33+
_runningFinishDataState.value = UiState.Success(runningFinishData)
34+
} ?: kotlin.run {
35+
_runningFinishDataState.value = UiState.Failure(
36+
MoGakRunException.FileNotFoundedException
37+
)
38+
}
39+
}
40+
}
41+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<layout xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<data>
5+
<variable
6+
name="vm"
7+
type="com.whyranoid.presentation.runningfinish.RunningFinishViewModel" />
8+
</data>
9+
10+
<androidx.constraintlayout.widget.ConstraintLayout
11+
android:layout_width="match_parent"
12+
android:layout_height="match_parent">
13+
14+
</androidx.constraintlayout.widget.ConstraintLayout>
15+
</layout>

presentation/src/main/res/navigation/navigation_graph.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
android:id="@+id/runningStartFragment"
2424
android:name="com.whyranoid.presentation.runningstart.RunningStartFragment"
2525
android:label="RunningStartFragment"
26-
tools:layout="@layout/fragment_running_start"/>
26+
tools:layout="@layout/fragment_running_start">
27+
<action
28+
android:id="@+id/action_runningStartFragment_to_runningFinish"
29+
app:destination="@id/runningFinishFragment" />
30+
</fragment>
2731
<fragment
2832
android:id="@+id/communityFragment"
2933
android:name="com.whyranoid.presentation.community.CommunityFragment"
@@ -83,5 +87,13 @@
8387
android:name="selectedRunningHistory"
8488
app:argType="com.whyranoid.presentation.model.RunningHistoryUiModel" />
8589
</fragment>
90+
<fragment
91+
android:id="@+id/runningFinishFragment"
92+
android:name="com.whyranoid.presentation.runningfinish.RunningFinishFragment">
93+
<argument
94+
android:name="runningFinishData"
95+
app:argType="com.whyranoid.presentation.running.RunningFinishData"
96+
app:nullable="true" />
97+
</fragment>
8698

8799
</navigation>

0 commit comments

Comments
 (0)