Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sdex committed Mar 16, 2024
1 parent 3fee0d0 commit 93d7e53
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
16 changes: 9 additions & 7 deletions app/src/main/java/com/sdex/activityrunner/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,11 @@ class MainActivity : BaseActivity() {
binding.list.adapter = adapter

viewModel.items.observe(this) {
val scrollToTop =
// scroll to top when the filter dialog is shown
supportFragmentManager.findFragmentByTag(FilterBottomSheetDialogFragment.TAG) != null ||
// scroll to top when the list already is at the top to display new items
(binding.list.layoutManager as LinearLayoutManager).findFirstCompletelyVisibleItemPosition() == 0
adapter.submitList(it) {
if (it.isNotEmpty()) {
binding.progress.hide()
}
if (scrollToTop) {
if (shouldScrollToTop()) {
binding.list.scrollToPosition(0)
}
}
Expand All @@ -119,9 +114,16 @@ class MainActivity : BaseActivity() {
}
}

private fun shouldScrollToTop(): Boolean {
// scroll to top when the filter dialog is shown
return supportFragmentManager.findFragmentByTag(FilterBottomSheetDialogFragment.TAG) != null ||
// scroll to top when the list already is at the top to display new items
(binding.list.layoutManager as LinearLayoutManager).findFirstCompletelyVisibleItemPosition() == 0
}

fun refresh() {
adapter.update()
viewModel.search(viewModel.searchQuery.value)
viewModel.refresh()
}

fun update() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class MainViewModel @Inject constructor(
_searchQuery.value = text
}

fun refresh() {
search(searchQuery.value)
}

private fun syncDatabase() {
GlobalScope.launch(Dispatchers.IO) {
applicationsLoader.syncDatabase()
Expand Down

0 comments on commit 93d7e53

Please sign in to comment.