Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Top/bottom jump on torrent list screen #93

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -179,6 +182,7 @@ class MainActivity : AppCompatActivity() {
if (currentFragment != null) {
supportActionBar?.subtitle = null

binding.fab.setOnClickListener(null)
supportFragmentManager.commit {
setReorderingAllowed(true)
remove(currentFragment)
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_arrow_down.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M7.41,8.59L12,13.17l4.59,-4.58L18,10l-6,6 -6,-6 1.41,-1.41z" />
</vector>
11 changes: 11 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
android:layout_height="match_parent">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/layout_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">

Expand Down Expand Up @@ -50,6 +51,16 @@
android:visibility="gone"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@drawable/ic_arrow_down"
app:tint="#000000"
tools:ignore="ContentDescription" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

<com.google.android.material.navigation.NavigationView
Expand Down