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;
|
2016-06-13 22:53:41 +02:00
|
|
|
import android.support.v4.app.FragmentStatePagerAdapter;
|
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;
|
|
|
|
|
2017-12-31 16:51:44 +01:00
|
|
|
import info.nightscout.androidaps.MainActivity;
|
2016-06-14 23:45:55 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.PluginBase;
|
2016-06-09 16:33:44 +02:00
|
|
|
|
2016-06-04 17:28:05 +02:00
|
|
|
/**
|
|
|
|
* Created by mike on 30.05.2016.
|
|
|
|
*/
|
2016-06-13 22:53:41 +02:00
|
|
|
public class TabPageAdapter extends FragmentStatePagerAdapter {
|
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
|
|
|
|
2017-12-31 16:51:44 +01:00
|
|
|
private static Logger log = LoggerFactory.getLogger(TabPageAdapter.class);
|
|
|
|
|
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);
|
|
|
|
return Fragment.instantiate(context, visibleFragmentList.get(position).getFragmentClass());
|
2016-06-04 17:28:05 +02:00
|
|
|
}
|
|
|
|
|
2017-04-21 18:56:48 +02:00
|
|
|
@Override
|
|
|
|
public void finishUpdate(ViewGroup container) {
|
|
|
|
try{
|
|
|
|
super.finishUpdate(container);
|
|
|
|
} catch (NullPointerException nullPointerException){
|
|
|
|
System.out.println("Catch the NullPointerException in FragmentStatePagerAdapter.finishUpdate");
|
2017-12-31 16:51:44 +01:00
|
|
|
} catch (IllegalStateException e){
|
|
|
|
log.error(e.getMessage());
|
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);
|
|
|
|
if(preferences.getBoolean("short_tabtitles", false)){
|
|
|
|
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) {
|
2017-06-30 09:46:56 +02:00
|
|
|
if (plugin.hasFragment() && plugin.isVisibleInTabs(plugin.getType())) {
|
2016-06-13 22:53:41 +02:00
|
|
|
visibleFragmentList.add(plugin);
|
2016-06-09 16:33:44 +02:00
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
2016-06-13 22:53:41 +02:00
|
|
|
}
|
2017-04-21 18:56:48 +02:00
|
|
|
|
|
|
|
|
2016-06-04 17:28:05 +02:00
|
|
|
}
|