/*
* Copyright (C) 2015 Naman Dwivedi
*
* Licensed under the GNU General Public License v3
*
* This is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*/
package com.naman14.timber.subfragments;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.naman14.timber.R;
import com.naman14.timber.utils.Constants;
import com.naman14.timber.utils.NavigationUtils;
import com.naman14.timber.widgets.MultiViewPager;
public class StyleSelectorFragment extends Fragment {
public String ACTION = "action";
FragmentStatePagerAdapter adapter;
MultiViewPager pager;
private SubStyleSelectorFragment selectorFragment;
private SharedPreferences preferences;
public static StyleSelectorFragment newInstance(String what) {
StyleSelectorFragment fragment = new StyleSelectorFragment();
Bundle bundle = new Bundle();
bundle.putString(Constants.SETTINGS_STYLE_SELECTOR_WHAT, what);
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
ACTION = getArguments().getString(Constants.SETTINGS_STYLE_SELECTOR_WHAT);
}
preferences = getActivity().getSharedPreferences(Constants.FRAGMENT_ID, Context.MODE_PRIVATE);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_style_selector, container, false);
if (ACTION.equals(Constants.SETTINGS_STYLE_SELECTOR_NOWPLAYING)) {
}
pager = (MultiViewPager) rootView.findViewById(R.id.pager);
adapter = new FragmentStatePagerAdapter(getChildFragmentManager()) {
@Override
public int getCount() {
return 6;
}
@Override
public Fragment getItem(int position) {
selectorFragment = SubStyleSelectorFragment.newInstance(position, ACTION);
return selectorFragment;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
};
pager.setAdapter(adapter);
scrollToCurrentStyle();
return rootView;
}
public void updateCurrentStyle() {
if (selectorFragment != null) {
adapter.notifyDataSetChanged();
scrollToCurrentStyle();
}
}
public void scrollToCurrentStyle() {
String fragmentID = preferences.getString(Constants.NOWPLAYING_FRAGMENT_ID, Constants.TIMBER3);
pager.setCurrentItem(NavigationUtils.getIntForCurrentNowplaying(fragmentID));
}
}