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;
import com.google.android.material.tabs.TabLayout;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import de.bps.asist.R;
......@@ -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++){
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);
......@@ -117,6 +127,12 @@ public class MensaFragment extends AbstractASiSTFragment {
int limit = (adapter.getCount() > 1 ? adapter.getCount() - 1 : 1);
viewPager.setOffscreenPageLimit(limit);
if (savedInstanceState == null) {
viewPager.setCurrentItem(preferredMensaTabIndex);
} else {
viewPager.setCurrentItem(savedInstanceState.getInt("selected_canteen_tab", preferredMensaTabIndex));
}
return view;
......@@ -136,25 +152,17 @@ public class MensaFragment extends AbstractASiSTFragment {
return super.handleMenuItemClicked(menuItem);
}
private int getIndexItemLocation(String canteenName, List<Canteen> canteenList){
int i =0;
for(Canteen canteen : canteenList){
if(canteen.getTitle().equals(canteenName))
return i;
else
i++;
}
return 0;
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("selected_canteen_tab", viewPager.getCurrentItem());
}
@Override
public void onResume() {
super.onResume();
//Goes to in Preferences used Tab
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
//TODO replace Key
String canteen = sharedPreferences.getString("canteens",null);
viewPager.setCurrentItem(getIndexItemLocation(canteen,canteens));
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
if (savedInstanceState != null) {
viewPager.setCurrentItem(savedInstanceState.getInt("selected_canteen_tab", 0));
}
}
}
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