Skip to content

Commit d69b6bc

Browse files
authored
Auto load items when list end is reached
Auto load items when list end is reached
2 parents 0294185 + 70cabde commit d69b6bc

File tree

4 files changed

+49
-24
lines changed

4 files changed

+49
-24
lines changed

app/src/main/java/com/github/code/gambit/ui/fragment/home/HomeFragment.kt

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import com.github.code.gambit.utility.extention.longToast
2828
import com.github.code.gambit.utility.extention.shortToast
2929
import com.github.code.gambit.utility.extention.show
3030
import com.github.code.gambit.utility.extention.snackbar
31+
import com.github.code.gambit.utility.sharedpreference.LastEvaluatedKeyManager
3132
import com.google.android.material.bottomsheet.BottomSheetBehavior
3233
import com.google.android.material.bottomsheet.BottomSheetBehavior.BottomSheetCallback
3334
import com.takusemba.spotlight.OnSpotlightListener
@@ -56,12 +57,17 @@ class HomeFragment : Fragment(R.layout.fragment_home), FileUrlClickCallback, Bot
5657
private lateinit var _urlBinding: FileUrlLayoutBinding
5758
private val urlBinding get() = _urlBinding
5859

60+
private var isFirstLoading = true
61+
5962
@Inject
6063
lateinit var homeRepository: HomeRepository
6164

6265
@Inject
6366
lateinit var adapter: FileListAdapter
6467

68+
@Inject
69+
lateinit var lekManager: LastEvaluatedKeyManager
70+
6571
lateinit var fileSearchComponent: FileSearchComponent
6672

6773
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@@ -71,9 +77,10 @@ class HomeFragment : Fragment(R.layout.fragment_home), FileUrlClickCallback, Bot
7177
_searchBinding = binding.searchLayout
7278
_urlBinding = binding.fileUrlLayout
7379
activity?.window?.exitFullscreen()
80+
lekManager.flush()
7481
registerFilterComponents()
7582
registerUrlComponent()
76-
83+
binding.linearProgress.hide()
7784
binding.filterButton.setOnClickListener {
7885
showFilter()
7986
}
@@ -92,6 +99,7 @@ class HomeFragment : Fragment(R.layout.fragment_home), FileUrlClickCallback, Bot
9299
}
93100

94101
binding.swipeRefresh.setOnRefreshListener {
102+
binding.swipeRefresh.isRefreshing = false
95103
viewModel.setEvent(HomeEvent.GetFiles)
96104
}
97105

@@ -112,7 +120,11 @@ class HomeFragment : Fragment(R.layout.fragment_home), FileUrlClickCallback, Bot
112120
is HomeState.Error -> longToast(it.message)
113121
is HomeState.FilesLoaded -> {
114122
if (!it.isSearchResult) {
115-
stopShimmer()
123+
if (isFirstLoading) {
124+
stopShimmer()
125+
} else {
126+
binding.linearProgress.hide()
127+
}
116128
adapter.addAll(it.files, true)
117129
binding.noFileIllustrationContainer.hide()
118130
binding.swipeRefresh.isRefreshing = false
@@ -122,7 +134,11 @@ class HomeFragment : Fragment(R.layout.fragment_home), FileUrlClickCallback, Bot
122134
}
123135
is HomeState.Loading -> {
124136
if (!it.isSearchResultLoading) {
125-
showShimmer()
137+
if (isFirstLoading) {
138+
showShimmer()
139+
} else {
140+
binding.linearProgress.show()
141+
}
126142
return@observe
127143
}
128144
fileSearchComponent.setRefreshing()
@@ -151,6 +167,18 @@ class HomeFragment : Fragment(R.layout.fragment_home), FileUrlClickCallback, Bot
151167
binding.fileList.adapter = adapter
152168
adapter.listener = this
153169
binding.fileList.addOnScrollListener(object : RecyclerView.OnScrollListener() {
170+
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
171+
super.onScrollStateChanged(recyclerView, newState)
172+
val visibleItemCount = layoutManager.childCount
173+
val totalItemCount = layoutManager.itemCount
174+
val pastVisibleItem = layoutManager.findFirstCompletelyVisibleItemPosition()
175+
if (!recyclerView.canScrollVertically(1) &&
176+
newState == RecyclerView.SCROLL_STATE_IDLE &&
177+
pastVisibleItem + visibleItemCount >= totalItemCount
178+
) {
179+
viewModel.setEvent(HomeEvent.GetFiles)
180+
}
181+
}
154182
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
155183
if (searchBinding.root.isVisible) {
156184
super.onScrolled(recyclerView, dx, dy)
@@ -161,11 +189,6 @@ class HomeFragment : Fragment(R.layout.fragment_home), FileUrlClickCallback, Bot
161189
} else {
162190
showBottomNav()
163191
}
164-
if (!recyclerView.canScrollVertically(1) && layoutManager.findLastVisibleItemPosition() == layoutManager.itemCount - 1) {
165-
// shortToast("End of list")
166-
println()
167-
}
168-
Timber.tag("home").i("(dx: $dx, dy: $dy)")
169192
super.onScrolled(recyclerView, dx, dy)
170193
}
171194
})
@@ -230,6 +253,7 @@ class HomeFragment : Fragment(R.layout.fragment_home), FileUrlClickCallback, Bot
230253
}
231254

232255
private fun stopShimmer() {
256+
isFirstLoading = false
233257
showBottomNav()
234258
binding.shimmerLayout.stopShimmer()
235259
binding.shimmerLayout.hide()

app/src/main/java/com/github/code/gambit/utility/AppConstant.kt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,13 @@ object AppConstant {
44
const val AUTH_ATTRIBUTE_CUSTOM_PROFILE = "custom:profile_image"
55
const val BASE_URL = "https://mhv71te0rh.execute-api.ap-south-1.amazonaws.com/beta/"
66
const val SHARE_URL = "https://vt-webclient.herokuapp.com/"
7-
object RequestCode {
8-
const val PERMISSIONS = 101
9-
const val GALLERY = 100
10-
const val CAMERA = 102
11-
const val DOCUMENT = 103
12-
}
137

148
object Named {
159
const val PERMISSION_ARRAY = "PERMISSION"
1610
const val USER_ID = "UID"
1711
const val BASE_URL = "BRL"
1812
}
1913

20-
@Deprecated(level = DeprecationLevel.WARNING, message = "ImageService related constants no longer used")
21-
object Cloudinary {
22-
const val UPLOAD_PRESET = "mqlj7ft0"
23-
const val RESULT_URL_KEY = "url"
24-
const val BASE_URL =
25-
"http://res.cloudinary.com/code-gambit/image/upload/v1619956278/Profile%20Image/"
26-
}
27-
2814
object API_PATH {
2915
const val USER_ID = "userId"
3016
const val FILE_ID = "fileId"

app/src/main/java/com/github/code/gambit/utility/sharedpreference/LastEvaluatedKeyManager.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,10 @@ constructor(@ApplicationContext context: Context) : PreferenceManager(context) {
2020
}
2121
}
2222

23+
fun flush() {
24+
putLastEvalKey("", KeyType.URL)
25+
putLastEvalKey("", KeyType.URL)
26+
}
27+
2328
enum class KeyType { FILE, URL }
2429
}

app/src/main/res/layout/fragment_home.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,20 @@
6363

6464
</LinearLayout>
6565

66+
<com.google.android.material.progressindicator.LinearProgressIndicator
67+
android:id="@+id/linear_progress"
68+
android:layout_width="match_parent"
69+
android:layout_height="wrap_content"
70+
android:layout_below="@id/top_container"
71+
android:indeterminate="true"
72+
app:indicatorColor="@color/secondary"
73+
android:visibility="gone" />
74+
6675
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
6776
android:id="@+id/swipe_refresh"
6877
android:layout_width="match_parent"
6978
android:layout_height="match_parent"
70-
android:layout_below="@id/top_container"
79+
android:layout_below="@id/linear_progress"
7180
android:visibility="gone">
7281

7382
<androidx.recyclerview.widget.RecyclerView
@@ -114,7 +123,8 @@
114123
android:id="@+id/shimmer_layout"
115124
android:layout_width="match_parent"
116125
android:layout_height="match_parent"
117-
app:shimmer_duration="800">
126+
app:shimmer_duration="800"
127+
android:visibility="visible">
118128

119129
<include layout="@layout/home_placeholder_layout" />
120130

0 commit comments

Comments
 (0)