@@ -94,6 +94,7 @@ class MainActivity : HelperBaseActivity(), NavigationView.OnNavigationItemSelect
9494 })
9595
9696 binding.fab.setOnClickListener { handleFabAction() }
97+ binding.fabLocate.setOnClickListener { locateSelectedServer() }
9798 binding.layoutTest.setOnClickListener { handleLayoutTestClick() }
9899
99100 setupGroupTab()
@@ -569,6 +570,47 @@ class MainActivity : HelperBaseActivity(), NavigationView.OnNavigationItemSelect
569570 }
570571 }
571572
573+ /* *
574+ * Locates and scrolls to the currently selected server.
575+ * If the selected server is in a different group, automatically switches to that group first.
576+ */
577+ private fun locateSelectedServer () {
578+ val targetSubscriptionId = mainViewModel.findSubscriptionIdBySelect()
579+ if (targetSubscriptionId.isNullOrEmpty()) {
580+ toast(R .string.title_file_chooser)
581+ return
582+ }
583+
584+ val targetGroupIndex = groupPagerAdapter.groups.indexOfFirst { it.id == targetSubscriptionId }
585+ if (targetGroupIndex < 0 ) {
586+ toast(R .string.toast_server_not_found_in_group)
587+ return
588+ }
589+
590+ // Switch to target group if needed, then scroll to the server
591+ if (binding.viewPager.currentItem != targetGroupIndex) {
592+ binding.viewPager.setCurrentItem(targetGroupIndex, true )
593+ binding.viewPager.postDelayed({ scrollToSelectedServer(targetGroupIndex) }, 1000 )
594+ } else {
595+ scrollToSelectedServer(targetGroupIndex)
596+ }
597+ }
598+
599+ /* *
600+ * Scrolls to the selected server in the specified fragment.
601+ * @param groupIndex The index of the group/fragment to scroll in
602+ */
603+ private fun scrollToSelectedServer (groupIndex : Int ) {
604+ val itemId = groupPagerAdapter.getItemId(groupIndex)
605+ val fragment = supportFragmentManager.findFragmentByTag(" f$itemId " ) as ? GroupServerFragment
606+
607+ if (fragment?.isAdded == true && fragment.view != null ) {
608+ fragment.scrollToSelectedServer()
609+ } else {
610+ toast(R .string.toast_fragment_not_available)
611+ }
612+ }
613+
572614 override fun onKeyDown (keyCode : Int , event : KeyEvent ): Boolean {
573615 if (keyCode == KeyEvent .KEYCODE_BACK || keyCode == KeyEvent .KEYCODE_BUTTON_B ) {
574616 moveTaskToBack(false )
0 commit comments