Use a swipe to refresh layout

main
Clément FRÉVILLE 2 years ago
parent 6396947de9
commit e01dcf059b

@ -57,6 +57,7 @@ dependencies {
implementation "androidx.paging:paging-runtime:$paging_version"
implementation "androidx.fragment:fragment-ktx:$fragment_version"
implementation "androidx.preference:preference-ktx:$preference_version"
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
//implementation 'androidx.core:core-ktx:+'
testImplementation 'junit:junit:4.13.2'

@ -14,6 +14,7 @@ import androidx.paging.*
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import fr.uca.iut.clfreville2.teaiswarm.R
import fr.uca.iut.clfreville2.teaiswarm.REPOSITORY_NAME
import fr.uca.iut.clfreville2.teaiswarm.REPOSITORY_OWNER
@ -32,16 +33,20 @@ class ActivityListFragment : Fragment(R.layout.activity_list) {
var repository: RepositoryIdentifier? = null
var sha: String? = null
private lateinit var pagingAdapter: ActivityAdapter
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (view is SwipeRefreshLayout) {
view.setOnRefreshListener {
updateCommits()
view.isRefreshing = false
}
}
repository = RepositoryIdentifier(
requireArguments().getString(REPOSITORY_OWNER)!!,
requireArguments().getString(REPOSITORY_NAME)!!
)
updateCommits()
}
private fun updateCommits() {
val viewModel by viewModels<ActivityViewModel>(
factoryProducer = {
ActivityViewModelFactory(
@ -51,8 +56,8 @@ class ActivityListFragment : Fragment(R.layout.activity_list) {
)
}
)
val pagingAdapter = ActivityAdapter(ActivityComparator)
val recyclerView = requireView().findViewById<RecyclerView>(R.id.activity_list_view)
pagingAdapter = ActivityAdapter(ActivityComparator)
val recyclerView = view.findViewById<RecyclerView>(R.id.activity_list_view)
recyclerView.adapter = pagingAdapter
recyclerView.layoutManager = LinearLayoutManager(requireContext())
viewLifecycleOwner.lifecycleScope.launch {
@ -62,6 +67,10 @@ class ActivityListFragment : Fragment(R.layout.activity_list) {
}
}
private fun updateCommits() {
pagingAdapter.refresh()
}
class ActivitySource(
private val service: RepositoryService,
private val repository: RepositoryIdentifier,

@ -15,6 +15,7 @@ import androidx.lifecycle.viewModelScope
import androidx.paging.*
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import fr.uca.iut.clfreville2.teaiswarm.R
import fr.uca.iut.clfreville2.teaiswarm.TeaIsWarm
import fr.uca.iut.clfreville2.teaiswarm.adapter.RepositoryListAdapter
@ -47,6 +48,12 @@ class RepositoryListFragment(
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (view is SwipeRefreshLayout) {
view.setOnRefreshListener {
updateRepositories()
view.isRefreshing = false
}
}
recyclerView = view.findViewById(R.id.repositories_view)
val spinner: Spinner = view.findViewById(R.id.sort_by_spinner)
ArrayAdapter.createFromResource(

@ -1,9 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/activity_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/activity_list_view" />
android:layout_height="wrap_content" />
</FrameLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

@ -1,11 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Spinner
android:id="@+id/sort_by_spinner"
android:layout_width="wrap_content"
@ -13,10 +20,10 @@
<ToggleButton
android:id="@+id/desc_sort_toggle"
android:textOff="@string/asc_sort"
android:textOn="@string/desc_sort"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:textOff="@string/asc_sort"
android:textOn="@string/desc_sort" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
@ -24,3 +31,4 @@
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

Loading…
Cancel
Save