Skip to content

Commit cdfc9a2

Browse files
committed
✨ 네트워크 연결 상태 CreateRunningPostFragment에 적용
1 parent 3c5e0c1 commit cdfc9a2

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

presentation/src/main/java/com/whyranoid/presentation/community/runningpost/CreateRunningPostFragment.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,20 @@ internal class CreateRunningPostFragment :
2525
observeState()
2626
}
2727

28+
override fun onDestroyView() {
29+
viewModel.finishObservingNetworkState()
30+
super.onDestroyView()
31+
}
32+
2833
private fun initViews() {
2934
binding.vm = viewModel
3035
binding.selectedRunningHistory = viewModel.selectedRunningHistory
36+
binding.executePendingBindings()
3137
setUpMenu()
3238
}
3339

3440
private fun observeState() {
41+
viewModel.startObservingNetworkState()
3542
viewLifecycleOwner.repeatWhenUiStarted {
3643
viewModel.createPostState.collect { createPostState ->
3744
when (createPostState) {

presentation/src/main/java/com/whyranoid/presentation/community/runningpost/CreateRunningPostViewModel.kt

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ package com.whyranoid.presentation.community.runningpost
33
import androidx.lifecycle.SavedStateHandle
44
import androidx.lifecycle.ViewModel
55
import androidx.lifecycle.viewModelScope
6+
import com.whyranoid.domain.repository.NetworkRepository
67
import com.whyranoid.domain.usecase.CreateRunningPostUseCase
78
import com.whyranoid.presentation.model.RunningHistoryUiModel
89
import com.whyranoid.presentation.model.UiState
10+
import com.whyranoid.presentation.util.networkconnection.NetworkState
911
import dagger.hilt.android.lifecycle.HiltViewModel
12+
import kotlinx.coroutines.FlowPreview
1013
import kotlinx.coroutines.flow.MutableStateFlow
1114
import kotlinx.coroutines.flow.SharingStarted
1215
import kotlinx.coroutines.flow.StateFlow
1316
import kotlinx.coroutines.flow.asStateFlow
17+
import kotlinx.coroutines.flow.combine
18+
import kotlinx.coroutines.flow.debounce
1419
import kotlinx.coroutines.flow.map
1520
import kotlinx.coroutines.flow.stateIn
1621
import kotlinx.coroutines.launch
@@ -19,6 +24,7 @@ import javax.inject.Inject
1924
@HiltViewModel
2025
class CreateRunningPostViewModel @Inject constructor(
2126
private val createRunningPostUseCase: CreateRunningPostUseCase,
27+
private val networkRepository: NetworkRepository,
2228
savedState: SavedStateHandle
2329
) : ViewModel() {
2430

@@ -27,8 +33,8 @@ class CreateRunningPostViewModel @Inject constructor(
2733
val runningPostContent = MutableStateFlow<String?>(null)
2834

2935
val createPostButtonEnableState: StateFlow<Boolean>
30-
get() = runningPostContent.map { content ->
31-
content.isNullOrBlank().not()
36+
get() = runningPostContent.combine(networkState) { content, networkState ->
37+
content.isNullOrBlank().not() && networkState is NetworkState.Connection
3238
}.stateIn(
3339
initialValue = false,
3440
started = SharingStarted.WhileSubscribed(5000),
@@ -39,6 +45,18 @@ class CreateRunningPostViewModel @Inject constructor(
3945
val createPostState: StateFlow<UiState<Boolean>>
4046
get() = _createPostState.asStateFlow()
4147

48+
@OptIn(FlowPreview::class)
49+
val networkState = networkRepository.getNetworkConnectionState()
50+
.map { isConnected ->
51+
if (isConnected) NetworkState.Connection else NetworkState.DisConnection
52+
}
53+
.debounce(500)
54+
.stateIn(
55+
scope = viewModelScope,
56+
started = SharingStarted.WhileSubscribed(5000),
57+
initialValue = NetworkState.UnInitialized
58+
)
59+
4260
fun createRunningPost() {
4361
viewModelScope.launch {
4462
_createPostState.value = UiState.Loading
@@ -56,6 +74,14 @@ class CreateRunningPostViewModel @Inject constructor(
5674
}
5775
}
5876

77+
fun startObservingNetworkState() {
78+
networkRepository.addNetworkConnectionCallback()
79+
}
80+
81+
fun finishObservingNetworkState() {
82+
networkRepository.removeNetworkConnectionCallback()
83+
}
84+
5985
companion object {
6086
private const val RUNNING_HISTORY_SAFE_ARGS_KEY = "selectedRunningHistory"
6187
}

presentation/src/main/res/layout/fragment_create_running_post.xml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@
1818
android:layout_width="match_parent"
1919
android:layout_height="match_parent">
2020

21+
<TextView
22+
android:id="@+id/tv_network_connection"
23+
networkConnectionVisibility="@{vm.networkState}"
24+
android:layout_width="match_parent"
25+
android:layout_height="wrap_content"
26+
android:background="@color/gray"
27+
android:gravity="center"
28+
android:padding="4dp"
29+
android:text="@string/network_connection_alert"
30+
app:layout_constraintEnd_toEndOf="parent"
31+
app:layout_constraintStart_toStartOf="parent"
32+
app:layout_constraintTop_toBottomOf="@id/app_bar" />
33+
2134
<com.google.android.material.appbar.AppBarLayout
2235
android:id="@+id/app_bar"
2336
android:layout_width="match_parent"
@@ -42,7 +55,7 @@
4255
android:layout_height="wrap_content"
4356
android:text="@string/community_label_running_history"
4457
android:textAppearance="@style/MoGakRunText.Bold.Medium"
45-
app:layout_constraintTop_toBottomOf="@id/app_bar"
58+
app:layout_constraintTop_toBottomOf="@id/tv_network_connection"
4659
app:layout_constraintStart_toStartOf="parent"
4760
android:layout_marginTop="20dp"
4861
android:layout_marginStart="16dp"

0 commit comments

Comments
 (0)