diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index 388a57df..cb188e8c 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -29,6 +29,7 @@ import androidx.appcompat.widget.SearchView; import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.core.app.NotificationManagerCompat; +import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.lifecycle.LiveData; import androidx.navigation.NavBackStackEntry; @@ -56,6 +57,7 @@ import awais.instagrabber.asyncs.SuggestionsFetcher; import awais.instagrabber.customviews.helpers.CustomHideBottomViewOnScrollBehavior; import awais.instagrabber.databinding.ActivityMainBinding; import awais.instagrabber.fragments.PostViewV2Fragment; +import awais.instagrabber.fragments.main.FeedFragment; import awais.instagrabber.fragments.settings.MorePreferencesFragmentDirections; import awais.instagrabber.interfaces.FetchListener; import awais.instagrabber.models.IntentModel; @@ -429,6 +431,16 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage firstFragmentGraphIndex); navControllerLiveData.observe(this, navController -> setupNavigation(binding.toolbar, navController)); currentNavControllerLiveData = navControllerLiveData; + binding.bottomNavView.setOnNavigationItemReselectedListener(item -> { + // Log.d(TAG, "setupBottomNavigationBar: item: " + item); + final Fragment navHostFragment = getSupportFragmentManager().findFragmentById(R.id.main_nav_host); + if (navHostFragment != null) { + final Fragment fragment = navHostFragment.getChildFragmentManager().getPrimaryNavigationFragment(); + if (fragment instanceof FeedFragment) { + ((FeedFragment) fragment).scrollToTop(); + } + } + }); } private void setBottomNavSelectedItem(final int navId) { diff --git a/app/src/main/java/awais/instagrabber/customviews/PostsRecyclerView.java b/app/src/main/java/awais/instagrabber/customviews/PostsRecyclerView.java index 280eb06c..aee8930f 100644 --- a/app/src/main/java/awais/instagrabber/customviews/PostsRecyclerView.java +++ b/app/src/main/java/awais/instagrabber/customviews/PostsRecyclerView.java @@ -9,6 +9,7 @@ import androidx.annotation.Nullable; import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelStoreOwner; +import androidx.recyclerview.widget.LinearSmoothScroller; import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.StaggeredGridLayoutManager; import androidx.transition.ChangeBounds; @@ -80,6 +81,13 @@ public class PostsRecyclerView extends RecyclerView { } }; + private final RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(getContext()) { + @Override + protected int getVerticalSnapPreference() { + return LinearSmoothScroller.SNAP_TO_START; + } + }; + public PostsRecyclerView(@NonNull final Context context) { super(context); } @@ -313,4 +321,10 @@ public class PostsRecyclerView extends RecyclerView { super.onDetachedFromWindow(); lifeCycleOwner = null; } + + @Override + public void smoothScrollToPosition(final int position) { + smoothScroller.setTargetPosition(position); + layoutManager.startSmoothScroll(smoothScroller); + } } diff --git a/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java index ebc93d99..5c31797d 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java @@ -380,4 +380,9 @@ public class FeedFragment extends Fragment implements SwipeRefreshLayout.OnRefre preferences -> new Handler().postDelayed(() -> binding.feedRecyclerView.setLayoutPreferences(preferences), 200)); fragment.show(getChildFragmentManager(), "posts_layout_preferences"); } + + public void scrollToTop() { + binding.feedRecyclerView.smoothScrollToPosition(0); + binding.storiesContainer.setExpanded(true); + } } diff --git a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java index f264b4b1..f026d04b 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -759,7 +759,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe new ServiceCallback() { @Override public void onSuccess(final FriendshipRepoChangeRootResponse result) { - Log.d(TAG, "Unfollow success: " + result); + // Log.d(TAG, "Unfollow success: " + result); onRefresh(); } @@ -776,7 +776,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe new ServiceCallback() { @Override public void onSuccess(final FriendshipRepoChangeRootResponse result) { - Log.d(TAG, "Follow success: " + result); + // Log.d(TAG, "Follow success: " + result); onRefresh(); } diff --git a/app/src/main/java/awais/instagrabber/utils/NavigationExtensions.java b/app/src/main/java/awais/instagrabber/utils/NavigationExtensions.java index 7b5d1654..2717cdfa 100644 --- a/app/src/main/java/awais/instagrabber/utils/NavigationExtensions.java +++ b/app/src/main/java/awais/instagrabber/utils/NavigationExtensions.java @@ -19,9 +19,11 @@ import java.util.List; import awais.instagrabber.R; +/** + * This is a Java rewrite of NavigationExtensions + * from architecture-components-samples. Some modifications have been done, check git history. + */ public class NavigationExtensions { - // private static final String TAG = "NavigationExtensions"; - @NonNull public static LiveData setupWithNavController(@NonNull final BottomNavigationView bottomNavigationView, @NonNull List navGraphIds,