AndroidAPS/app/src/main/java/info/nightscout/androidaps/tabs/TabPageAdapter.java

85 lines
2.5 KiB
Java
Raw Normal View History

2016-06-04 17:28:05 +02:00
package info.nightscout.androidaps.tabs;
2016-08-05 23:54:03 +02:00
import android.content.Context;
2017-01-21 23:48:05 +01:00
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
2016-06-12 14:18:21 +02:00
import android.support.annotation.Nullable;
2016-06-04 17:28:05 +02:00
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
2018-06-07 22:54:27 +02:00
import android.support.v4.app.FragmentPagerAdapter;
2017-04-21 18:56:48 +02:00
import android.view.ViewGroup;
2016-06-04 17:28:05 +02:00
2017-12-31 16:51:44 +01:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2016-06-04 17:28:05 +02:00
import java.util.ArrayList;
2016-06-14 23:45:55 +02:00
import info.nightscout.androidaps.interfaces.PluginBase;
2018-07-30 15:46:20 +02:00
import info.nightscout.androidaps.logging.L;
2016-06-04 17:28:05 +02:00
/**
* Created by mike on 30.05.2016.
*/
2018-06-07 22:54:27 +02:00
public class TabPageAdapter extends FragmentPagerAdapter {
2016-06-04 17:28:05 +02:00
2016-08-05 23:54:03 +02:00
ArrayList<PluginBase> visibleFragmentList = new ArrayList<>();
2016-06-13 22:53:41 +02:00
2016-08-05 23:54:03 +02:00
Context context;
2016-06-04 17:28:05 +02:00
2018-07-30 15:46:20 +02:00
private static Logger log = LoggerFactory.getLogger(L.CORE);
2017-12-31 16:51:44 +01:00
2016-08-05 23:54:03 +02:00
public TabPageAdapter(FragmentManager fm, Context context) {
2016-06-04 17:28:05 +02:00
super(fm);
2016-08-05 23:54:03 +02:00
this.context = context;
2016-06-04 17:28:05 +02:00
}
@Override
2016-06-12 14:18:21 +02:00
@Nullable
2016-06-04 17:28:05 +02:00
public Fragment getItem(int position) {
2016-08-05 23:54:03 +02:00
//Fragment fragment = (Fragment) visibleFragmentList.get(position);
2018-03-31 00:36:03 +02:00
return Fragment.instantiate(context, visibleFragmentList.get(position).pluginDescription.getFragmentClass());
2016-06-04 17:28:05 +02:00
}
2018-06-04 21:56:31 +02:00
public PluginBase getPluginAt(int position) {
return visibleFragmentList.get(position);
}
2017-04-21 18:56:48 +02:00
@Override
public void finishUpdate(ViewGroup container) {
2018-07-30 15:46:20 +02:00
try {
2017-04-21 18:56:48 +02:00
super.finishUpdate(container);
2018-07-30 15:46:20 +02:00
} catch (NullPointerException nullPointerException) {
2017-04-21 18:56:48 +02:00
System.out.println("Catch the NullPointerException in FragmentStatePagerAdapter.finishUpdate");
2018-07-30 15:46:20 +02:00
} catch (IllegalStateException e) {
log.error("Unhandled exception", e);
2017-04-21 18:56:48 +02:00
}
}
2016-06-04 17:28:05 +02:00
@Override
public CharSequence getPageTitle(int position) {
2017-01-21 23:48:05 +01:00
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
2018-07-30 15:46:20 +02:00
if (preferences.getBoolean("short_tabtitles", false)) {
2017-01-21 23:48:05 +01:00
return visibleFragmentList.get(position).getNameShort();
}
return visibleFragmentList.get(position).getName();
2016-06-04 17:28:05 +02:00
}
@Override
public int getCount() {
2016-06-13 22:53:41 +02:00
return visibleFragmentList.size();
2016-06-04 17:28:05 +02:00
}
2016-08-05 23:54:03 +02:00
public void registerNewFragment(PluginBase plugin) {
2018-03-31 00:36:03 +02:00
if (plugin.hasFragment() && plugin.isFragmentVisible()) {
2016-06-13 22:53:41 +02:00
visibleFragmentList.add(plugin);
notifyDataSetChanged();
}
2016-06-13 22:53:41 +02:00
}
2017-04-21 18:56:48 +02:00
2018-06-07 22:54:27 +02:00
@Override
public long getItemId(int position) {
return System.identityHashCode(visibleFragmentList.get(position));
}
2016-06-04 17:28:05 +02:00
}