diff --git a/android/app/src/main/java/net/pengcook/android/presentation/home/HomeFragment.kt b/android/app/src/main/java/net/pengcook/android/presentation/home/HomeFragment.kt index 94488f4c..7d940190 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/home/HomeFragment.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/home/HomeFragment.kt @@ -8,6 +8,7 @@ import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import androidx.lifecycle.lifecycleScope import androidx.navigation.fragment.findNavController +import androidx.paging.LoadState import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -42,6 +43,19 @@ class HomeFragment : Fragment() { AnalyticsLogging.viewLogEvent("Home") initBinding() observing() + initSwipeRefreshLayout() + } + + private fun initSwipeRefreshLayout() { + binding.swipeRefreshLayout.setOnRefreshListener { + refreshData() + } + } + + private fun refreshData() { + viewModel.refreshFeed() + + binding.swipeRefreshLayout.isRefreshing = false } private fun observing() { @@ -79,5 +93,11 @@ class HomeFragment : Fragment() { binding.lifecycleOwner = viewLifecycleOwner binding.viewModel = viewModel binding.homeRcView.adapter = adapter + + lifecycleScope.launch { + adapter.loadStateFlow.collect { loadStates -> + binding.swipeRefreshLayout.isRefreshing = loadStates.refresh is LoadState.Loading + } + } } } diff --git a/android/app/src/main/java/net/pengcook/android/presentation/home/HomeViewModel.kt b/android/app/src/main/java/net/pengcook/android/presentation/home/HomeViewModel.kt index 68932a1a..a8918fa3 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/home/HomeViewModel.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/home/HomeViewModel.kt @@ -8,8 +8,8 @@ import androidx.paging.Pager import androidx.paging.PagingConfig import androidx.paging.PagingData import androidx.paging.cachedIn -import androidx.paging.liveData import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.launch import net.pengcook.android.data.datasource.FeedPagingSource import net.pengcook.android.data.repository.feed.FeedRepository import net.pengcook.android.presentation.core.model.Recipe @@ -22,18 +22,38 @@ class HomeViewModel @Inject constructor( private val feedRepository: FeedRepository, - ) : ViewModel(), FeedItemEventListener { + ) : ViewModel(), + FeedItemEventListener { private val _uiEvent: MutableLiveData> = MutableLiveData() val uiEvent: LiveData> get() = _uiEvent - val feedData: LiveData> = - Pager( - config = PagingConfig(pageSize = PAGE_SIZE, enablePlaceholders = false), - pagingSourceFactory = { FeedPagingSource(feedRepository) }, - ) - .liveData - .cachedIn(viewModelScope) + private val _feedData = MutableLiveData>() + val feedData: LiveData> = _feedData + + init { + loadFeedData() + } + + private fun loadFeedData() { + val pager = + Pager( + config = PagingConfig(pageSize = PAGE_SIZE, enablePlaceholders = false), + pagingSourceFactory = { FeedPagingSource(feedRepository) }, + ) + + viewModelScope.launch { + pager.flow + .cachedIn(viewModelScope) + .collect { pagingData -> + _feedData.value = pagingData + } + } + } + + fun refreshFeed() { + loadFeedData() + } override fun onNavigateToDetail(recipe: Recipe) { _uiEvent.value = Event(HomeEvent.NavigateToDetail(recipe)) @@ -45,5 +65,7 @@ class HomeViewModel } sealed interface HomeEvent { - data class NavigateToDetail(val recipe: Recipe) : HomeEvent + data class NavigateToDetail( + val recipe: Recipe, + ) : HomeEvent } diff --git a/android/app/src/main/res/layout/fragment_home.xml b/android/app/src/main/res/layout/fragment_home.xml index 6b4cddc2..8bbd8b07 100644 --- a/android/app/src/main/res/layout/fragment_home.xml +++ b/android/app/src/main/res/layout/fragment_home.xml @@ -22,26 +22,32 @@ android:id="@+id/main_logo" android:layout_width="130dp" android:layout_height="50dp" - android:layout_marginStart="16dp" android:layout_marginVertical="8dp" + android:layout_marginStart="16dp" android:src="@drawable/peng_cook" + app:layout_constraintBottom_toTopOf="@id/swipe_refresh_layout" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toTopOf="parent" - app:layout_constraintBottom_toTopOf="@id/homeRcView"/> + app:layout_constraintTop_toTopOf="parent" /> - - + app:layout_constraintTop_toBottomOf="@id/main_logo"> + + +