Commit 696bc5cf authored by Toni Beier's avatar Toni Beier

Feature: The Mensa view reselects last shown Mensa, if the view was in the...

Feature: The Mensa view reselects last shown Mensa, if the view was in the background and get back to foreground
parent c10b076c
...@@ -13,11 +13,12 @@ import android.widget.Toast; ...@@ -13,11 +13,12 @@ import android.widget.Toast;
import com.google.android.material.tabs.TabLayout; import com.google.android.material.tabs.TabLayout;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentPagerAdapter; import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager; import androidx.viewpager.widget.ViewPager;
import de.bps.asist.R; import de.bps.asist.R;
...@@ -100,8 +101,17 @@ public class MensaFragment extends AbstractASiSTFragment { ...@@ -100,8 +101,17 @@ public class MensaFragment extends AbstractASiSTFragment {
*/ */
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
//TODO replace Key
String preferredMensaName = sharedPreferences.getString("canteens", null);
int preferredMensaTabIndex = 0;
for(int i = 0; i < canteens.size();i++){ for(int i = 0; i < canteens.size();i++){
adapter.AddFragment(new ExpandableMensaList(canteens.get(i)),canteens.get(i).getTitle()); Canteen addingCanteen = canteens.get(i);
adapter.AddFragment(new ExpandableMensaList(addingCanteen), addingCanteen.getTitle());
if (addingCanteen.getTitle().equals(preferredMensaName)) {
preferredMensaTabIndex = i;
}
} }
viewPager.setAdapter(adapter); viewPager.setAdapter(adapter);
...@@ -117,6 +127,12 @@ public class MensaFragment extends AbstractASiSTFragment { ...@@ -117,6 +127,12 @@ public class MensaFragment extends AbstractASiSTFragment {
int limit = (adapter.getCount() > 1 ? adapter.getCount() - 1 : 1); int limit = (adapter.getCount() > 1 ? adapter.getCount() - 1 : 1);
viewPager.setOffscreenPageLimit(limit); viewPager.setOffscreenPageLimit(limit);
if (savedInstanceState == null) {
viewPager.setCurrentItem(preferredMensaTabIndex);
} else {
viewPager.setCurrentItem(savedInstanceState.getInt("selected_canteen_tab", preferredMensaTabIndex));
}
return view; return view;
...@@ -136,25 +152,17 @@ public class MensaFragment extends AbstractASiSTFragment { ...@@ -136,25 +152,17 @@ public class MensaFragment extends AbstractASiSTFragment {
return super.handleMenuItemClicked(menuItem); return super.handleMenuItemClicked(menuItem);
} }
private int getIndexItemLocation(String canteenName, List<Canteen> canteenList){ @Override
int i =0; public void onSaveInstanceState(@NonNull Bundle outState) {
for(Canteen canteen : canteenList){ super.onSaveInstanceState(outState);
if(canteen.getTitle().equals(canteenName)) outState.putInt("selected_canteen_tab", viewPager.getCurrentItem());
return i;
else
i++;
}
return 0;
} }
@Override @Override
public void onResume() { public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
super.onResume(); super.onViewStateRestored(savedInstanceState);
//Goes to in Preferences used Tab if (savedInstanceState != null) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); viewPager.setCurrentItem(savedInstanceState.getInt("selected_canteen_tab", 0));
//TODO replace Key }
String canteen = sharedPreferences.getString("canteens",null);
viewPager.setCurrentItem(getIndexItemLocation(canteen,canteens));
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment