Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Refresh #377

Merged
merged 3 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,18 +22,38 @@ class HomeViewModel
@Inject
constructor(
private val feedRepository: FeedRepository,
) : ViewModel(), FeedItemEventListener {
) : ViewModel(),
FeedItemEventListener {
private val _uiEvent: MutableLiveData<Event<HomeEvent>> = MutableLiveData()
val uiEvent: LiveData<Event<HomeEvent>>
get() = _uiEvent

val feedData: LiveData<PagingData<Recipe>> =
Pager(
config = PagingConfig(pageSize = PAGE_SIZE, enablePlaceholders = false),
pagingSourceFactory = { FeedPagingSource(feedRepository) },
)
.liveData
.cachedIn(viewModelScope)
private val _feedData = MutableLiveData<PagingData<Recipe>>()
val feedData: LiveData<PagingData<Recipe>> = _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))
Expand All @@ -45,5 +65,7 @@ class HomeViewModel
}

sealed interface HomeEvent {
data class NavigateToDetail(val recipe: Recipe) : HomeEvent
data class NavigateToDetail(
val recipe: Recipe,
) : HomeEvent
}
28 changes: 17 additions & 11 deletions android/app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

<!-- RecyclerView for posts -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/homeRcView"
android:layout_width="0dp"
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:paddingHorizontal="12dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/main_logo"
tools:listitem="@layout/item_feed" />
app:layout_constraintTop_toBottomOf="@id/main_logo">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/homeRcView"
android:layout_width="0dp"
android:layout_height="0dp"
android:paddingHorizontal="12dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/item_feed" />

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

</layout>
Loading