Skip to content

Commit

Permalink
remove: 불필요한 코드 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
oreocube committed Jan 20, 2025
1 parent fed9ebf commit 1993bbd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import com.hrhn.databinding.FragmentTodayBinding
import com.hrhn.presentation.ui.screen.addchallenge.AddChallengeActivity
import com.hrhn.presentation.ui.screen.edit.EditChallengeActivity
import com.hrhn.presentation.util.observeEvent
import com.hrhn.presentation.util.showToast
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach

@AndroidEntryPoint
class TodayFragment : Fragment() {
Expand All @@ -37,23 +34,9 @@ class TodayFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initViews()
observeData()
}

override fun onResume() {
super.onResume()
viewModel.fetchData()
}

private fun initViews() {
with(binding) {
vm = viewModel
lifecycleOwner = viewLifecycleOwner
srlToday.setOnRefreshListener { viewModel.fetchData() }
}
}

private fun observeData() {
with(viewModel) {
addEvent.observeEvent(viewLifecycleOwner) {
Expand All @@ -65,14 +48,11 @@ class TodayFragment : Fragment() {
editEvent.observeEvent(viewLifecycleOwner) {
startActivity(EditChallengeActivity.newIntent(requireContext(), it))
}
isRefreshing.onEach {
binding.srlToday.isRefreshing = it
}.launchIn(viewLifecycleOwner.lifecycleScope)
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
super.onDestroyView()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fun TodayScreen(viewModel: TodayViewModel) {
val todayString by remember(today) {
derivedStateOf { today.formatDateWithYearString() }
}
val todayChallenge by viewModel.todayChallengeFlow.collectAsStateWithLifecycle()
val todayChallenge by viewModel.todayChallenge.collectAsStateWithLifecycle()

Column(
modifier = Modifier.fillMaxSize(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,34 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.hrhn.domain.model.Challenge
import com.hrhn.domain.repository.ChallengeRepository
import com.hrhn.domain.usecase.GetTodayChallengeUseCase
import com.hrhn.presentation.util.Event
import com.hrhn.presentation.util.emit
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import java.time.LocalDate
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.stateIn
import java.time.LocalDateTime
import javax.inject.Inject

@HiltViewModel
class TodayViewModel @Inject constructor(
private val repository: ChallengeRepository
getTodayChallengeUseCase: GetTodayChallengeUseCase,
) : ViewModel() {

private val _isRefreshing = MutableSharedFlow<Boolean>()
val isRefreshing = _isRefreshing.asSharedFlow()

private val _today = MutableSharedFlow<LocalDateTime>()
private val _today = MutableStateFlow(LocalDateTime.now())
val today = _today.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(500),
initialValue = LocalDateTime.now()
)

val todayChallengeFlow: StateFlow<Challenge?> = _today.map { today ->
repository.getChallengesWithPeriod(today, today.plusDays(1)).map {
if (it.isNotEmpty()) it.getOrNull(0) else null
}.first()
}.catch { e ->
e.message?.let { _message.emit(it) }
}.onEach {
_isRefreshing.emit(false)
}.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(500),
initialValue = null
)

val isEmpty: StateFlow<Boolean> = todayChallengeFlow.map { it == null }.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(500),
initialValue = true
)
val todayChallenge: StateFlow<Challenge?> = getTodayChallengeUseCase()
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(500),
initialValue = null
)

private val _message = MutableLiveData<Event<String>>()
val message: LiveData<Event<String>> get() = _message
Expand All @@ -59,16 +43,11 @@ class TodayViewModel @Inject constructor(
private val _editEvent = MutableLiveData<Event<Challenge>>()
val editEvent: LiveData<Event<Challenge>> get() = _editEvent

fun fetchData() {
viewModelScope.launch {
_isRefreshing.emit(true)
_today.emit(LocalDate.now().atStartOfDay())
}
}

fun addTodayChallenge() = _addEvent.emit()

fun editTodayChallenge() {
_editEvent.emit(todayChallengeFlow.value!!)
todayChallenge.value?.let { challenge ->
_editEvent.emit(challenge)
}
}
}
}
37 changes: 6 additions & 31 deletions app/src/main/res/layout/fragment_today.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,11 @@
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

<import type="android.view.View" />

<variable
name="vm"
type="com.hrhn.presentation.ui.screen.main.today.TodayViewModel" />
</data>

<FrameLayout
<androidx.compose.ui.platform.ComposeView
android:id="@+id/composeView"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/srl_today"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.compose.ui.platform.ComposeView
android:id="@+id/composeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</FrameLayout>
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</layout>

0 comments on commit 1993bbd

Please sign in to comment.