Skip to content
Open
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
73 changes: 65 additions & 8 deletions app/src/main/java/org/transdroid/core/gui/TorrentsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
Expand Down Expand Up @@ -109,9 +111,10 @@ public class TorrentsFragment extends Fragment implements OnLabelPickedListener
@ViewById
protected TextView nosettingsText;
@ViewById
protected TextView errorText;
@ViewById
protected TextView errorText; @ViewById
protected ProgressBar loadingProgress;
@ViewById
protected ImageButton scrollToTopButton;
private MultiChoiceModeListener onTorrentsSelected = new MultiChoiceModeListener() {

private SelectionManagerMode selectionManagerMode;
Expand Down Expand Up @@ -233,9 +236,7 @@ public void onDestroyActionMode(ActionMode mode) {
addmenuButton.setVisibility(View.VISIBLE);
}

};

@AfterViews
}; @AfterViews
protected void init() {

// Load the requested sort order from the user settings
Expand All @@ -258,6 +259,59 @@ protected void init() {
}
nosettingsText.setText(getString(R.string.navigation_nosettings, getString(R.string.app_name)));

// Set up the scroll to top button
scrollToTopButton.setOnClickListener(v -> scrollToTop());
// Add scroll listener to show/hide scroll to top button
torrentsList.setOnScrollListener(new AbsListView.OnScrollListener() {

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// No implementation needed
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// Show button when scrolling down, hide when at top
if (totalItemCount > 0) {
if (firstVisibleItem > 1) {
// User has scrolled down, show the button with animation
if (scrollToTopButton.getVisibility() != View.VISIBLE) {
scrollToTopButton.setVisibility(View.VISIBLE);
scrollToTopButton.startAnimation(android.view.animation.AnimationUtils.loadAnimation(
getActivity(), R.anim.fade_in));
}
} else {
// User is at the top, hide the button with animation
if (scrollToTopButton.getVisibility() == View.VISIBLE) {
android.view.animation.Animation fadeOut = android.view.animation.AnimationUtils.loadAnimation(
getActivity(), R.anim.fade_out);
fadeOut.setAnimationListener(new android.view.animation.Animation.AnimationListener() {
@Override
public void onAnimationStart(android.view.animation.Animation animation) {}

@Override
public void onAnimationEnd(android.view.animation.Animation animation) {
scrollToTopButton.setVisibility(View.GONE);
}

@Override
public void onAnimationRepeat(android.view.animation.Animation animation) {}
});
scrollToTopButton.startAnimation(fadeOut);
}
}
}
}
});
}

/**
* Scrolls the torrents list to the top position
*/
private void scrollToTop() {
if (torrentsList != null) {
torrentsList.smoothScrollToPosition(0);
}
}

/**
Expand Down Expand Up @@ -472,9 +526,7 @@ public void updateError(String connectionErrorMessage) {
} else {
updateViewVisibility();
}
}

private void updateViewVisibility() {
} private void updateViewVisibility() {
if (!hasAConnection) {
return;
}
Expand All @@ -486,6 +538,11 @@ private void updateViewVisibility() {
loadingProgress.setVisibility(!hasError && isLoading ? View.VISIBLE : View.GONE);
emptyText.setVisibility(!hasError && !isLoading && isEmpty ? View.VISIBLE : View.GONE);
swipeRefreshLayout.setEnabled(true);

// If list is not visible, ensure scroll to top button is also hidden
if (torrentsList.getVisibility() != View.VISIBLE) {
scrollToTopButton.setVisibility(View.GONE);
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/anim/fade_in.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="250"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
5 changes: 5 additions & 0 deletions app/src/main/res/anim/fade_out.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="250"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_arrow_up.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFF"
android:pathData="M7.41,15.41L12,10.83l4.59,4.58L18,14l-6,-6 -6,6z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/scroll_to_top_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="?attr/colorAccent" />
</shape>
14 changes: 14 additions & 0 deletions app/src/main/res/layout/fragment_torrents.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

<ImageButton
android:id="@+id/scroll_to_top_button"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:background="@drawable/scroll_to_top_background"
android:contentDescription="@string/action_scroll_to_top"
android:elevation="6dp"
android:src="@drawable/ic_arrow_up"
android:translationY="-16dp"
android:visibility="gone"
tools:visibility="visible" />

<ProgressBar
android:id="@+id/loading_progress"
android:layout_width="128dp"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<string name="action_removesettings">Remove settings</string>
<string name="action_visitwebsite">Visit transdroid.org</string>
<string name="action_close">Close</string>
<string name="action_scroll_to_top">Scroll to top</string>

<string name="navigation_opendrawer">Select server and filter</string>
<string name="navigation_closedrawer">Close filters list</string>
Expand Down