package com.ved.musicmapapp.fragments;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
import android.widget.TextView;
import com.ved.musicmapapp.MainActivity;
import com.ved.musicmapapp.R;
import com.ved.musicmapapp.utils.Statics;
import com.viewpagerindicator.TabPageIndicator;
public class MainFragment extends Fragment {
private TabHost mTabHost;
private ViewPager mViewPager;
private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, MainFragment.TabInfo>();
// private MainFragmentAdapter mPagerAdapter;
ViewPager pager;
TabPageIndicator indicator;
private List<Fragment> mFragments;
GoogleMusicAdapter adapter;
private List<Fragment> mBackFragments;
int index = 0;
// private int curTab = 0;
public static final int FIRST_PAGE = 0;
public static final int SECOND_PAGE = 1;
public static final int THIRD_PAGE = 2;
private static final String[] CONTENT = new String[] {"Followings", "Followers","Playlists"};
public Fragment getTab(int position) {
if(mFragments.size() > 0) {
Fragment frag = mFragments.get(position);
if(frag == null)
return adapter.getItem(position);
else
return frag;
}
return null;
}
public class TabInfo {
private String tag;
public Fragment fragment;
private int number;
public TextView tvCount;
private TextView tvTag;
public TabInfo(String tag, Class<?> clazz, Bundle args) {
this.tag = tag;
}
/**
* @return the tvCount
*/
}
class TabFactory implements TabContentFactory {
private final Context mContext;
/**
* @param context
*/
public TabFactory(Context context) {
mContext = context;
}
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (null == getActivity())
return;
adapter = new GoogleMusicAdapter(getChildFragmentManager());
pager = (ViewPager)view.findViewById(R.id.pager);
pager.setAdapter(adapter);
indicator = (TabPageIndicator)view.findViewById(R.id.indicator);
indicator.setContext(getActivity());
indicator.setViewPager(pager);
// this.initialiseTabHost(savedInstanceState);
// this.intialiseViewPager();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
// private void initialiseTabHost(Bundle args) {
// mTabHost = (TabHost) getView().findViewById(android.R.id.tabhost);
// mTabHost.setup();
// mTabHost.getTabWidget().setDividerDrawable(
// R.drawable.verticalseprater);
// TabInfo tabInfo = new TabInfo("Followings", YouFollowFragment.class,
// args);
// int totalFollowYou = 0;
// int totalYouFollow = 0;
// if (null != Statics.curUser) {
// if (null != Statics.curUser) {
// tabInfo.number = 0;
// for (String key : Statics.hashUsers.keySet()) {
// User user = Statics.hashUsers.get(key);
// if (!user.getFbid().equals(Statics.curUser.getFbid())) {
// try {
// if (user.isFollowed()) {
// totalYouFollow++;
// }
// if (user.isFollowMe()) {
// totalFollowYou++;
// }
// } catch (Exception ex) {
//
// }
// }
// }
//
// }
// }
// tabInfo.number = totalYouFollow;
//
// addTab(this, this.mTabHost, tabInfo);
// this.mapTabInfo.put(tabInfo.tag, tabInfo);
//
// tabInfo = new TabInfo("Followers", YouFollowFragment.class, args);
//
// if (null != Statics.curUser) {
// tabInfo.number = totalFollowYou;
// } else {
// tabInfo.number = 0;
// }
//
// addTab(this, this.mTabHost, tabInfo);
// this.mapTabInfo.put(tabInfo.tag, tabInfo);
//
// tabInfo = new TabInfo("Playlists", PlaylistFragment.class, args);
//
// tabInfo.number = 0;
// if (null != Statics.curUser) {
// ArrayList<Playlist> playlists = Statics.curUser.getPlaylists();
// if(playlists != null) {
// tabInfo.number = playlists.size();
// }
// }
//
//// tabInfo = new TabInfo("Likes", LikeFragment.class, args);
////
//// // TODO
//// if (null != Statics.curUser) {
//// // tabInfo.number = Statics.curUser.getTotalLike();
//// int numLikes = 0;
//// if (Statics.likeDatas != null) {
//// for (Artist item : Statics.likeDatas) {
//// if (item != null)
//// numLikes += item.getSongs().size();
//// }
//// }
//// tabInfo.number = numLikes;
//// } else {
//// tabInfo.number = 0;
//// }
//
// addTab(this, this.mTabHost, tabInfo);
// this.mapTabInfo.put(tabInfo.tag, tabInfo);
//
// mTabHost.setOnTabChangedListener(this);
// }
// private void addTab(MainFragment fragment, TabHost tabHost, TabInfo tabInfo) {
// // Attach a Tab view factory to the spec
//
// TabSpec tabSpec = tabHost.newTabSpec(tabInfo.tag);
// TabWidget tabWidget = (TabWidget) getView().findViewById(
// android.R.id.tabs);
// View tabIndicator = LayoutInflater.from(fragment.getActivity())
// .inflate(R.layout.tab_item, tabWidget, false);
// tabInfo.tvTag = (TextView) tabIndicator.findViewById(R.id.tv_tab_tag);
// tabInfo.tvCount = (TextView) tabIndicator
// .findViewById(R.id.tv_tab_item_count);
//
// Typeface tf11 = Typeface.createFromAsset(getActivity().getAssets(), "open_sans_regular.ttf");
// tabInfo.tvTag.setTypeface(tf11);
// tabInfo.tvCount.setTypeface(tf11);
//
// tabInfo.tvTag.setText(tabInfo.tag);
// tabInfo.tvCount.setText(tabInfo.number + "");
// tabSpec.setIndicator(tabIndicator);
// tabSpec.setContent(fragment.new TabFactory(fragment.getActivity()));
// tabHost.addTab(tabSpec);
//
// }
/**
* (non-Javadoc)
*
* @see android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String)
*/
// public void onTabChanged(String tag) {
// // TabInfo newTab = this.mapTabInfo.get(tag);
// int pos = this.mTabHost.getCurrentTab();
// if (null != mViewPager) {
// curTab = pos;
// try {
// this.mViewPager.setCurrentItem(pos);
// } catch (Exception e) {
// }
// }
// }
// @Override
// public void onPageScrolled(int position, float positionOffset,
// int positionOffsetPixels) {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public void onPageSelected(int position) {
// // TODO Auto-generated method stub
// this.mTabHost.setCurrentTab(position);
// }
// @Override
// public void onPageScrollStateChanged(int state) {
// // TODO Auto-generated method stub
//
// }
/*private void intialiseViewPager() {
List<Fragment> fragments = new Vector<Fragment>();
YouFollowFragment yff = new YouFollowFragment();
fragments.add(mapTabInfo.get("Followings").fragment = yff);
// FollowYouFragment frg = new FollowYouFragment();
// if (null != Statics.curUser && null != Statics.followYous
// && Statics.curUser.getNoFollowYou() == Statics.followYous.size()) {
// frg.setReloadData(false);
// } else {
// frg.setReloadData(true);
// }
FollowYouFragment fyf = new FollowYouFragment();
fragments.add(mapTabInfo.get("Followers").fragment = fyf);
// fragments.add(mapTabInfo.get("Likes").fragment = new LikeFragment());
PlaylistFragment playlistFragment = new PlaylistFragment();
playlistFragment.setUser(Statics.curUser);
fragments.add(mapTabInfo.get("Playlists").fragment = playlistFragment);
this.mPagerAdapter = new MainFragmentAdapter(getChildFragmentManager(),
fragments);
//
this.mViewPager = (ViewPager) getView().findViewById(R.id.viewpager);
this.mViewPager.setAdapter(this.mPagerAdapter);
this.mViewPager.setOnPageChangeListener(this);
this.mViewPager.setPersistentDrawingCache(3);
this.mViewPager.setCurrentItem(curTab);
}*/
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
((MainActivity)getActivity()).hideShareAction();
}
// private class MainFragmentAdapter extends FragmentStatePagerAdapter {
// private List<Fragment> mFragments;
//
// public MainFragmentAdapter(FragmentManager fm, List<Fragment> fragments) {
// super(fm);
// mFragments = fragments;
// }
//
// @Override
// public Fragment getItem(int position) {
//
// // If fragment not yet initialized, create its instance.
// if (mFragments.get(position) == null) {
// switch (position) {
// case FIRST_PAGE: {
// return mFragments.get(position);
// }
// case SECOND_PAGE: {
// return mFragments.get(position);
// }
// case THIRD_PAGE: {
// mFragments.set(SECOND_PAGE, PlaylistFragment.newInstance(Statics.curUser));
// }
// break;
// }
// }
// // Return fragment instance at requested position.
// return mFragments.get(position);
//
// }
//
// @Override
// public int getCount() {
// // TODO Auto-generated method stub
// return mFragments.size();
// }
//
// }
/**
* @return the mapTabInfo
*/
public HashMap<String, TabInfo> getMapTabInfo() {
return mapTabInfo;
}
/**
* @return the curTab
*/
public int getCurTab() {
return index;
}
/**
* @param curTab
* the curTab to set
*/
public void setCurTab(int curTab) {
this.index = curTab;
if (null != mViewPager)
mViewPager.setCurrentItem(curTab);
}
@Override
public void onDestroyView() {
if (getView() != null) {
((ViewGroup) getView().getParent()).removeView(getView());
}
super.onDestroyView();
}
public void update(Fragment fragment, boolean pushPrevious){
Log.e("check","Current position is : " + pager.getCurrentItem() + ".. Index is : " + index);
adapter.replace(pager.getCurrentItem(),fragment, pushPrevious);
}
class GoogleMusicAdapter extends FragmentStatePagerAdapter {
FragmentManager mFragmentManager;
public GoogleMusicAdapter(FragmentManager fm) {
super(fm);
mFragmentManager = fm;
mFragments = new ArrayList<Fragment>(){};
mBackFragments = new ArrayList<Fragment>(){};
for (int i = 0; i < 3; i++) {
mFragments.add(null);
mBackFragments.add(null);
}
}
public void replace(int position, Fragment fragment, boolean pushPrevious) {
// Get currently active fragment.
if(mFragments.get(FIRST_PAGE) == null || mFragments.get(SECOND_PAGE) == null || mFragments.get(THIRD_PAGE) == null){
mFragments.add(getItem(FIRST_PAGE));
mFragments.add(getItem(SECOND_PAGE));
mFragments.add(getItem(THIRD_PAGE));
}
Fragment old_fragment = mFragments.get(position);
if (old_fragment == null) {
return;
}
if(pushPrevious)
mBackFragments.add(old_fragment);
mFragments.set(position, fragment);
notifyDataSetChanged();
}
/*public void start(int position, Fragment fragment) {
// Remember current fragment.
mBackFragments.set(position, mFragments.get(position));
// Replace the displayed fragment.
this.replace(position, fragment);
}*/
/**
* Replaces the current fragment by fragment stored in back stack. Does nothing and returns
* false if no fragment is back-stacked.
*/
/*public boolean back() {
int position = pager.getCurrentItem();
Fragment fragment = mBackFragments.get(position);
if (fragment == null) {
// Nothing to go back.
return false;
}
update(fragment,position );
mBackFragments.set(position, null);
return true;
}*/
public boolean back() {
if(mBackFragments.size() > 0) {
Fragment fragment = mBackFragments.get(mBackFragments.size()-1);
if (fragment == null) {
// Nothing to go back.
return false;
}
update(fragment, false);
mBackFragments.remove(fragment);
return true;
}
else {
return false;
}
}
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public Fragment getItem(int position) {
index = position;
Log.e("check","Current position is : " + pager.getCurrentItem() + ".. Index is : " + index);
// If fragment not yet initialized, create its instance.
if (mFragments.get(position) == null) {
switch (position) {
case FIRST_PAGE: {
YouFollowFragment yff = new YouFollowFragment();
mFragments.set(FIRST_PAGE, yff);
}
break;
case SECOND_PAGE: {
FollowYouFragment fyf = new FollowYouFragment();
mFragments.set(SECOND_PAGE, fyf);
}
break;
case THIRD_PAGE: {
PlaylistFragment playlistFragment = new PlaylistFragment();
playlistFragment.setUser(Statics.curUser);
mFragments.set(THIRD_PAGE, playlistFragment);
}
break;
}
}
// Return fragment instance at requested position.
return mFragments.get(position);
}
@Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length];
}
@Override
public int getCount() {
return CONTENT.length;
}
}
public boolean onBack() {
if(adapter.back())
return true;
else{
int currentPage = pager.getCurrentItem();
if(currentPage == 2){
pager.setCurrentItem(1);
return true;
}
else if(currentPage == 1){
pager.setCurrentItem(0);
return true;
}
else
return false;
}
// return false;
}
}