11package org .indilib .i4j .iparcos ;
22
3+ import android .content .Context ;
34import android .os .Bundle ;
45import android .util .Log ;
5- import android .util .Pair ;
66import android .view .LayoutInflater ;
7+ import android .view .Menu ;
8+ import android .view .MenuInflater ;
9+ import android .view .MenuItem ;
710import android .view .View ;
811import android .view .ViewGroup ;
912import android .widget .LinearLayout ;
1013import android .widget .TextView ;
1114
1215import androidx .annotation .NonNull ;
16+ import androidx .appcompat .widget .SearchView ;
1317import androidx .fragment .app .Fragment ;
1418import androidx .viewpager2 .adapter .FragmentStateAdapter ;
1519import androidx .viewpager2 .widget .ViewPager2 ;
1620
21+ import com .google .android .material .tabs .TabLayout ;
1722import com .google .android .material .tabs .TabLayoutMediator ;
1823
1924import org .indilib .i4j .client .INDIDevice ;
2227
2328import java .util .ArrayList ;
2429import java .util .Date ;
30+ import java .util .HashMap ;
2531import java .util .List ;
2632
27- public class ControlPanelFragment extends Fragment implements INDIServerConnectionListener {
33+ public class ControlPanelFragment extends Fragment
34+ implements INDIServerConnectionListener , SearchView .OnQueryTextListener {
2835
29- private final ArrayList <Pair <INDIDevice , Fragment >> devicesAndFragments = new ArrayList <>();
30- /**
31- * Manages the connection with the INDI server.
32- *
33- * @see ConnectionManager
34- */
36+ private static final String KEY_VIEWPAGER_STATE = "DevicesViewPagerState" ;
37+ private static Bundle viewPagerBundle ;
38+ private final ArrayList <INDIDevice > devices = new ArrayList <>();
39+ private final HashMap <Integer , Fragment > fragmentsMap = new HashMap <>();
3540 private ConnectionManager connectionManager ;
36- private DevicesFragmentAdapter devicesFragmentAdapter ;
41+ private DevicesFragmentAdapter fragmentAdapter ;
3742 private LinearLayout controlLayout ;
3843 private TextView noDevicesText ;
44+ private ViewPager2 viewPager ;
45+ private TabLayout tabLayout ;
46+ private Context context ;
47+
48+ @ Override
49+ public void onAttach (@ NonNull Context context ) {
50+ super .onAttach (context );
51+ this .context = context ;
52+ }
3953
4054 @ Override
4155 public View onCreateView (@ NonNull LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ) {
4256 View rootView = inflater .inflate (R .layout .fragment_control_panel , container , false );
4357 controlLayout = rootView .findViewById (R .id .indi_control_layout );
4458 noDevicesText = rootView .findViewById (R .id .no_devices_label );
45- devicesAndFragments .clear ();
46- ViewPager2 viewPager = rootView .findViewById (R .id .indi_control_pager );
47- viewPager .setAdapter (devicesFragmentAdapter = new DevicesFragmentAdapter (this ));
48- viewPager .setSaveEnabled (false );
49- new TabLayoutMediator (rootView .findViewById (R .id .indi_control_tabs ), viewPager ,
50- (tab , position ) -> tab .setText (devicesAndFragments .get (position ).first .getName ())).attach ();
59+ viewPager = rootView .findViewById (R .id .indi_control_pager );
60+ fragmentAdapter = new DevicesFragmentAdapter (this );
61+ viewPager .setAdapter (fragmentAdapter );
62+ viewPager .setOffscreenPageLimit (5 );
63+ tabLayout = rootView .findViewById (R .id .indi_control_tabs );
64+ new TabLayoutMediator (tabLayout , viewPager ,
65+ (tab , position ) -> tab .setText (devices .get (position ).getName ())).attach ();
66+ setHasOptionsMenu (true );
5167 return rootView ;
5268 }
5369
@@ -65,28 +81,70 @@ public void onStart() {
6581 if (list .isEmpty ()) {
6682 noDevices ();
6783 } else {
68- devicesAndFragments .clear ();
6984 for (INDIDevice device : list ) {
70- newDevice (device );
85+ if (! devices . contains ( device )) newDevice (device );
7186 }
72- devicesFragmentAdapter .notifyDataSetChanged ();
87+ viewPager .post (() -> {
88+ fragmentAdapter .notifyDataSetChanged ();
89+ if (viewPagerBundle != null ) {
90+ int position = viewPagerBundle .getInt (KEY_VIEWPAGER_STATE );
91+ viewPager .setCurrentItem (position , false );
92+ tabLayout .selectTab (tabLayout .getTabAt (position ));
93+ }
94+ });
7395 devices ();
7496 }
7597 }
7698 }
7799
100+ @ Override
101+ public void onPause () {
102+ super .onPause ();
103+ viewPagerBundle = new Bundle ();
104+ viewPagerBundle .putInt (KEY_VIEWPAGER_STATE , viewPager .getCurrentItem ());
105+ }
106+
107+ @ Override
108+ public void onStop () {
109+ super .onStop ();
110+ viewPager .setAdapter (null );
111+ }
112+
78113 @ Override
79114 public void onDestroy () {
80115 super .onDestroy ();
81116 noDevices ();
82117 connectionManager .removeListener (this );
83118 }
84119
120+ @ Override
121+ public void onCreateOptionsMenu (Menu menu , @ NonNull MenuInflater inflater ) {
122+ MenuItem item = menu .add (R .string .mount_goto );
123+ item .setIcon (R .drawable .search );
124+ item .setShowAsAction (MenuItem .SHOW_AS_ACTION_IF_ROOM );
125+ SearchView searchView = new SearchView (context );
126+ searchView .setOnQueryTextListener (this );
127+ item .setActionView (searchView );
128+ }
129+
130+ @ Override
131+ public boolean onQueryTextChange (String newText ) {
132+ Fragment fragment = fragmentsMap .get (viewPager .getCurrentItem ());
133+ if (fragment instanceof DeviceControlFragment )
134+ ((DeviceControlFragment ) fragment ).findPref (newText );
135+ return false ;
136+ }
137+
138+ @ Override
139+ public boolean onQueryTextSubmit (String query ) {
140+ return false ;
141+ }
142+
85143 private void noDevices () {
86- devicesAndFragments .clear ();
144+ devices .clear ();
87145 noDevicesText .post (() -> noDevicesText .setVisibility (View .VISIBLE ));
88146 controlLayout .post (() -> controlLayout .setVisibility (View .GONE ));
89- devicesFragmentAdapter . notifyDataSetChanged ();
147+ viewPager . post (() -> fragmentAdapter . notifyDataSetChanged () );
90148 }
91149
92150 private void devices () {
@@ -104,32 +162,23 @@ public void connectionLost(INDIServerConnection connection) {
104162 @ Override
105163 public void newDevice (INDIServerConnection connection , INDIDevice device ) {
106164 Log .i ("ControlPanelFragment" , "New device: " + device .getName ());
107- devicesFragmentAdapter . notifyItemInserted (newDevice (device ));
165+ viewPager . post (() -> fragmentAdapter . notifyItemInserted (newDevice (device ) ));
108166 devices ();
109167 }
110168
111169 private int newDevice (INDIDevice device ) {
112- PrefsFragment fragment = new PrefsFragment ();
113- fragment .setDevice (device );
114- Pair <INDIDevice , Fragment > pair = new Pair <>(device , fragment );
115- devicesAndFragments .add (pair );
116- return devicesAndFragments .indexOf (pair );
170+ devices .add (device );
171+ return devices .indexOf (device );
117172 }
118173
119174 @ Override
120175 public void removeDevice (INDIServerConnection connection , INDIDevice device ) {
121176 Log .d ("ControlPanelFragment" , "Device removed: " + device .getName ());
122- for (int i = 0 ; i < devicesAndFragments .size (); i ++) {
123- Pair <INDIDevice , Fragment > pair = devicesAndFragments .get (i );
124- if (pair .first == device ) {
125- devicesAndFragments .remove (pair );
126- if (devicesAndFragments .isEmpty ()) {
127- noDevices ();
128- } else {
129- devicesFragmentAdapter .notifyItemRemoved (i );
130- }
131- return ;
132- }
177+ int index = devices .indexOf (device );
178+ if (index != -1 ) {
179+ devices .remove (device );
180+ if (devices .isEmpty ()) noDevices ();
181+ viewPager .post (() -> fragmentAdapter .notifyItemRemoved (index ));
133182 }
134183 }
135184
@@ -147,12 +196,15 @@ public DevicesFragmentAdapter(@NonNull Fragment fragment) {
147196 @ NonNull
148197 @ Override
149198 public Fragment createFragment (int position ) {
150- return devicesAndFragments .get (position ).second ;
199+ DeviceControlFragment fragment = new DeviceControlFragment ();
200+ fragment .setDevice (devices .get (position ));
201+ fragmentsMap .put (position , fragment );
202+ return fragment ;
151203 }
152204
153205 @ Override
154206 public int getItemCount () {
155- return devicesAndFragments .size ();
207+ return devices .size ();
156208 }
157209 }
158210}
0 commit comments