diff --git a/app/src/main/java/com/sdex/activityrunner/MainActivity.kt b/app/src/main/java/com/sdex/activityrunner/MainActivity.kt index 28a9e2a7..68b6cbf5 100644 --- a/app/src/main/java/com/sdex/activityrunner/MainActivity.kt +++ b/app/src/main/java/com/sdex/activityrunner/MainActivity.kt @@ -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) } } @@ -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() { diff --git a/app/src/main/java/com/sdex/activityrunner/app/MainViewModel.kt b/app/src/main/java/com/sdex/activityrunner/app/MainViewModel.kt index e4b9bfb6..2b0958ef 100644 --- a/app/src/main/java/com/sdex/activityrunner/app/MainViewModel.kt +++ b/app/src/main/java/com/sdex/activityrunner/app/MainViewModel.kt @@ -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()