From ca4068bf321ebde074680a5c8bc628a3ed8c1576 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartu=20=C3=96zen?= Date: Wed, 2 Aug 2023 08:26:08 +0300 Subject: [PATCH] Add fab to jump to top and bottom of torrent list --- .../qbitcontroller/ui/main/MainActivity.kt | 24 ++++++++++++++++ .../ui/torrentlist/TorrentListFragment.kt | 28 +++++++++++++++++++ app/src/main/res/drawable/ic_arrow_down.xml | 10 +++++++ app/src/main/res/layout/activity_main.xml | 11 ++++++++ 4 files changed, 73 insertions(+) create mode 100644 app/src/main/res/drawable/ic_arrow_down.xml diff --git a/app/src/main/java/dev/bartuzen/qbitcontroller/ui/main/MainActivity.kt b/app/src/main/java/dev/bartuzen/qbitcontroller/ui/main/MainActivity.kt index debae62a2..8da5b2ca6 100755 --- a/app/src/main/java/dev/bartuzen/qbitcontroller/ui/main/MainActivity.kt +++ b/app/src/main/java/dev/bartuzen/qbitcontroller/ui/main/MainActivity.kt @@ -8,12 +8,14 @@ import android.view.Menu import android.view.MenuInflater import android.view.MenuItem import android.view.View +import android.view.animation.OvershootInterpolator import androidx.activity.OnBackPressedCallback import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.viewModels import androidx.appcompat.app.ActionBarDrawerToggle import androidx.appcompat.app.AppCompatActivity import androidx.core.view.MenuProvider +import androidx.core.view.ViewCompat import androidx.drawerlayout.widget.DrawerLayout.DrawerListener import androidx.fragment.app.commit import androidx.recyclerview.widget.ConcatAdapter @@ -169,6 +171,7 @@ class MainActivity : AppCompatActivity() { if (serverConfig != null) { supportActionBar?.subtitle = null + binding.fab.setOnClickListener(null) supportFragmentManager.commit { setReorderingAllowed(true) val fragment = TorrentListFragment(serverConfig.id) @@ -179,6 +182,7 @@ class MainActivity : AppCompatActivity() { if (currentFragment != null) { supportActionBar?.subtitle = null + binding.fab.setOnClickListener(null) supportFragmentManager.commit { setReorderingAllowed(true) remove(currentFragment) @@ -238,6 +242,26 @@ class MainActivity : AppCompatActivity() { binding.recyclerDrawer.adapter = drawerAdapter } + fun setFabListener(block: () -> Boolean?) { + binding.fab.setOnClickListener { + val isMoveToTop = block() + if (isMoveToTop == true) { + binding.layoutAppBar.setExpanded(true, true) + } else if (isMoveToTop == false) { + binding.layoutAppBar.setExpanded(false, true) + } + } + } + + fun rotateFab(isMoveToTop: Boolean) { + ViewCompat.animate(binding.fab) + .rotation(if (isMoveToTop) 0f else 180f) + .withLayer() + .setDuration(300) + .setInterpolator(OvershootInterpolator()) + .start() + } + private fun requestNotificationPermission() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { if (!shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) { diff --git a/app/src/main/java/dev/bartuzen/qbitcontroller/ui/torrentlist/TorrentListFragment.kt b/app/src/main/java/dev/bartuzen/qbitcontroller/ui/torrentlist/TorrentListFragment.kt index 44a2b5bd3..664d1d223 100755 --- a/app/src/main/java/dev/bartuzen/qbitcontroller/ui/torrentlist/TorrentListFragment.kt +++ b/app/src/main/java/dev/bartuzen/qbitcontroller/ui/torrentlist/TorrentListFragment.kt @@ -649,6 +649,34 @@ class TorrentListFragment() : Fragment(R.layout.fragment_torrent_list) { ) } + var isMoveToTop = false + + parentActivity.setFabListener { + if (adapter.itemCount > 0) { + parentActivity.rotateFab(isMoveToTop) + if (isMoveToTop) { + binding.recyclerTorrentList.smoothScrollToPosition(0) + } else { + binding.recyclerTorrentList.smoothScrollToPosition(adapter.itemCount - 1) + } + isMoveToTop = !isMoveToTop + !isMoveToTop + } else { + null + } + } + + binding.recyclerTorrentList.addOnScrollListener(object : RecyclerView.OnScrollListener() { + override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { + val layoutManager = binding.recyclerTorrentList.layoutManager as LinearLayoutManager + val newIsMoveToTop = layoutManager.findFirstVisibleItemPosition() != 0 + + if (isMoveToTop != newIsMoveToTop) { + isMoveToTop = newIsMoveToTop + } + } + }) + binding.swipeRefresh.setOnRefreshListener { viewModel.refreshMainData(serverId) } diff --git a/app/src/main/res/drawable/ic_arrow_down.xml b/app/src/main/res/drawable/ic_arrow_down.xml new file mode 100644 index 000000000..68fd4058e --- /dev/null +++ b/app/src/main/res/drawable/ic_arrow_down.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 8ff3f7ce5..00f66326e 100755 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -12,6 +12,7 @@ android:layout_height="match_parent"> @@ -50,6 +51,16 @@ android:visibility="gone" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> + +