[Contributions-Tab] Scroll to top on NavBar Button Pressed (#4668)

* [Contributions-Tab] Scroll to top on NavBar Button Pressed

On NavBar Contributions Button pressed, if already on the Contributions tab,
scroll to the top of the recyclerView in ContributionsListFragment.

* [UnitTest][Contributions-Tab] Scroll to Top on NavBar Button Pressed
This commit is contained in:
codingtosh 2021-10-27 19:56:16 +05:30 committed by GitHub
parent 88b21a678e
commit 55c0b07ebf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 1 deletions

View file

@ -204,6 +204,12 @@ public class ContributionsFragment
throwable -> Timber.e(throwable, "Error occurred while loading notifications")));
}
public void scrollToTop( ){
if (contributionsListFragment != null) {
contributionsListFragment.scrollToTop();
}
}
private void initNotificationViews(List<Notification> notificationList) {
Timber.d("Number of notifications is %d", notificationList.size());
if (notificationList.isEmpty()) {

View file

@ -312,6 +312,10 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl
animateFAB(isFabOpen);
}
public void scrollToTop() {
rvContributionsList.smoothScrollToPosition(0);
}
private void animateFAB(final boolean isFabOpen) {
this.isFabOpen = !isFabOpen;
if (fabPlus.isShown()) {

View file

@ -163,7 +163,9 @@ public class MainActivity extends BaseActivity
//showBottom so that we do not show the bottom tray again when constructing
//from the saved instance state.
if (fragment instanceof ContributionsFragment) {
if (activeFragment == ActiveFragment.CONTRIBUTIONS) { // Do nothing if same tab
if (activeFragment == ActiveFragment.CONTRIBUTIONS) {
// scroll to top if already on the Contributions tab
contributionsFragment.scrollToTop();
return true;
}
contributionsFragment = (ContributionsFragment) fragment;

View file

@ -29,6 +29,7 @@ import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.*
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.mockito.Mockito.verify
import org.mockito.MockitoAnnotations
import org.powermock.reflect.Whitebox
import org.robolectric.Robolectric
@ -217,6 +218,14 @@ class ContributionsFragmentUnitTests {
fragment.updateLimitedConnectionToggle(menu)
}
@Test
@Throws(Exception::class)
fun testScrollToTop(){
Shadows.shadowOf(Looper.getMainLooper()).idle()
fragment.scrollToTop()
verify(contributionsListFragment).scrollToTop()
}
@Test
@Throws(Exception::class)
fun testOnAttach() {

View file

@ -24,6 +24,7 @@ import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.mockito.Mockito.verify
import org.mockito.MockitoAnnotations
import org.powermock.reflect.Whitebox
import org.robolectric.Robolectric
@ -160,6 +161,14 @@ class ContributionsListFragmentUnitTests {
fragment.getContributionStateAt(0)
}
@Test
@Throws(Exception::class)
fun testOnScrollToTop() {
Shadows.shadowOf(Looper.getMainLooper()).idle()
fragment.scrollToTop()
verify(rvContributionsList).smoothScrollToPosition(0)
}
@Test
@Throws(Exception::class)
fun testOnConfirmClicked() {

View file

@ -24,6 +24,7 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.mockito.Mockito.verify
import org.mockito.MockitoAnnotations
import org.powermock.api.mockito.PowerMockito
import org.robolectric.Robolectric
@ -243,6 +244,21 @@ class MainActivityUnitTests {
method.invoke(activity, contributionsFragment, false)
}
@Test
@Throws(Exception::class)
fun testLoadFragmentCaseContributionsFragmentCaseTrue() {
activeFragment = ActiveFragment.CONTRIBUTIONS
activity.activeFragment = activeFragment
val method: Method = MainActivity::class.java.getDeclaredMethod(
"loadFragment",
Fragment::class.java,
Boolean::class.java
)
method.isAccessible = true
method.invoke(activity, contributionsFragment, false)
verify(contributionsFragment).scrollToTop();
}
@Test
@Throws(Exception::class)
fun testLoadFragmentCaseNearbyParentFragmentCaseTrue() {