Android Open Source - Wardrobe_app Step One Fragment






From Project

Back to project page Wardrobe_app.

License

The source code is released under:

Apache License

If you think the Android project Wardrobe_app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.android.busolo.apps.wardrobe.engine;
//ww w. j  a v a 2  s  .c  o  m
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.PopupMenu;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.busolo.apps.wardrobe.engine.model.ColorPicker;
import com.makeramen.RoundedImageView;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link StepOneFragment.OnFragmentOneInteractionListener} interface
 * to handle interaction events.
 * Use the {@link StepOneFragment#newInstance} factory method to
 * create an instance of this fragment.
 *
 */
public class StepOneFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private static final int MODIFY_COLOR_1 = 1;
    private static final int MODIFY_COLOR_2 = 2;
    private static final int ADD_COLOR = 0;
    private static int MODIFY_COLOR_VIEW = ADD_COLOR;

    static final String ACTIVE_FRAG = "active_frag";
    static final String LOG_TAG = "StepOneFragment";
    FragmentActivity activity;

    ImageView img_pic_1;
    ImageView img_pic_2;
    ImageView img_pic_3;
    Spinner item_category;
    Spinner item_gender;
    View color_1;
    View color_2;
    ImageView btn_add_color;

    private ColorPickerFragment colorPickerFragment;
    private ArrayList<ColorPicker> colors;
    String[] color_names;
    String[] color_codes;

//    private Stack<ColorPicker> pickedColors = new Stack<ColorPicker>();
    private HashMap<View, ColorPicker> pickedColors = new HashMap<View, ColorPicker>();

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentOneInteractionListener mListener;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment StepOne.
     */
    // TODO: Rename and change types and number of parameters
    public static StepOneFragment newInstance(String param1, String param2) {
        StepOneFragment fragment = new StepOneFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public static StepOneFragment newInstance(){
        return new StepOneFragment();
    }

    public StepOneFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        fillColors();
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.fragment_step_one, container, false);
        img_pic_1 = (ImageView)rootView.findViewById(R.id.img_pic_1);
        img_pic_2 = (ImageView)rootView.findViewById(R.id.img_pic_2);
        img_pic_3 = (ImageView)rootView.findViewById(R.id.img_pic_3);
        item_category = (Spinner)rootView.findViewById(R.id.item_category);
        item_gender = (Spinner)rootView.findViewById(R.id.item_gender);
        color_1 = (View)rootView.findViewById(R.id.color_1);
        color_2 = (View)rootView.findViewById(R.id.color_2);
        btn_add_color = (ImageView)rootView.findViewById(R.id.btn_add_color);

        img_pic_1.setOnClickListener(clickListener);
        img_pic_2.setOnClickListener(clickListener);
        img_pic_3.setOnClickListener(clickListener);
        color_1.setOnClickListener(clickListener);
        color_2.setOnClickListener(clickListener);
        btn_add_color.setOnClickListener(clickListener);

        return rootView;
    }

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.img_pic_1:    //add pic 1
                    toast("add pic 1");
                    break;
                case R.id.img_pic_2:    //add pic 1
                    toast("add pic 2");
                    break;
                case R.id.img_pic_3:    //add pic 1
                    toast("add pic 3");
                    break;
                case R.id.color_1:    //add color 1
                    alterColorMenu(color_1);
                    break;
                case R.id.color_2:    //add color 2
                    alterColorMenu(color_2);
                    break;
                case R.id.btn_add_color:
                    pickColor();
                    break;
                default:break;
            }
        }
    };

    void pickColor(){
        if(color_1.getVisibility() == View.VISIBLE && color_2.getVisibility()==View.VISIBLE)
            toast("you can only select a maximum 2 colors");
        else {
            Log.e(LOG_TAG, "add a color");
            showColorPicker();
        }
    }

    void routeColoring(ColorPicker colorPicker){
        switch (MODIFY_COLOR_VIEW){
            case ADD_COLOR:
                Log.e(LOG_TAG, "add color_view "+ADD_COLOR);
                addColor(colorPicker.getColorCode(), colorPicker.getColorName());
                break;
            case MODIFY_COLOR_1:
                Log.e(LOG_TAG, "modify color_view "+ADD_COLOR);
                if(color_1 != null) modifyColor(color_1, colorPicker);
                break;
            case MODIFY_COLOR_2:
                if(color_1 != null) modifyColor(color_2, colorPicker);
                break;
            default:
                Log.e(LOG_TAG, "color_view:"+MODIFY_COLOR_VIEW+" at routColoring");
                break;
        }
    }

    void addColor(String color_code, String color_name){
        if(color_1.getVisibility() != View.VISIBLE){
            pickedColors.put(color_1, new ColorPicker(color_code, color_name));
            color_1.setBackgroundColor(Color.parseColor(color_code));
            ((RoundedImageView)color_1).setImageResource(Color.parseColor(color_code));
            color_1.setVisibility(View.VISIBLE);
            setVisibilityAnimation(color_1);
            Log.e(LOG_TAG, "color_1 empty! adding new color "+color_code);
        }else if(color_2.getVisibility() != View.VISIBLE){
            pickedColors.put(color_2, new ColorPicker(color_code, color_name));
            color_2.setBackgroundColor(Color.parseColor(color_code));
            ((RoundedImageView)color_2).setImageResource(Color.parseColor(color_code));
            color_2.setVisibility(View.VISIBLE);
            setVisibilityAnimation(color_2);
            Log.e(LOG_TAG, "color_2 empty! adding new color "+color_code);
        }
    }

    void modifyColor(View view, ColorPicker colorPicker){
        pickedColors.put(view, colorPicker);
        view.setBackgroundColor(Color.parseColor(colorPicker.getColorCode()));
        ((RoundedImageView)view).setImageResource(Color.parseColor(colorPicker.getColorCode()));
        MODIFY_COLOR_VIEW = ADD_COLOR;
    }


    void removeColor(View view){
        pickedColors.remove(view);
        Log.e(LOG_TAG, "removing color for view: "+view.toString());
        view.setBackgroundResource(R.drawable.color_picker_bg);
        hideVisibilityAnimation(view);
        view.setVisibility(View.GONE);
    }

    void showColorPicker(){
        colorPickerFragment = ColorPickerFragment.newInstance(colors);
        colorPickerFragment.setOnClickListener(colorPickerListener);
        colorPickerFragment.show(activity.getSupportFragmentManager(),ACTIVE_FRAG);
    }

    AdapterView.OnItemClickListener colorPickerListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            toast("picked color: " + color_names[position]);
            Log.e(LOG_TAG, "picked color:" +color_names[position]);
            ColorPicker colorPicker = new ColorPicker(color_codes[position], color_names[position]);
            routeColoring(colorPicker);
            if(colorPickerFragment != null) colorPickerFragment.dismiss();
        }
    };

    void fillColors(){
        colors = new ArrayList<ColorPicker>();
        color_names = activity.getResources().getStringArray(R.array.color_names);
        color_codes = activity.getResources().getStringArray(R.array.color_codes);
        for(int i=0; i<color_names.length && i<color_names.length; i++)
            colors.add(new ColorPicker(color_codes[i], color_names[i]));
    }

    void alterColorMenu(final View view){
        PopupMenu popupMenu = new PopupMenu(this.activity, view);
        popupMenu.getMenuInflater().inflate(R.menu.alter_color, popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.modify_color: //TODO modify color
                        MODIFY_COLOR_VIEW  = view == color_1 ?  MODIFY_COLOR_1 : MODIFY_COLOR_2;
                        showColorPicker();
                        return true;
                    case R.id.remove_color: //TODO remove color
                        removeColor(view);
                        return true;
                    default: return false;
                }
            }
        });
        popupMenu.show();
    }

    void setVisibilityAnimation(View view){
        Animation animation = AnimationUtils.loadAnimation(this.activity, R.anim.left_to_right);
        view.setAnimation(animation);
    }

    void hideVisibilityAnimation(View view){
        Animation animation = AnimationUtils.loadAnimation(this.activity, android.R.anim.fade_out);
        view.setAnimation(animation);
    }

    void toast(String text){
        Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentOneInteraction(uri);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = (FragmentActivity)activity;
        try {
            mListener = (OnFragmentOneInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentOneInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentOneInteraction(Uri uri);
    }

}




Java Source Code List

com.android.busolo.apps.wardrobe.Config.java
com.android.busolo.apps.wardrobe.engine.BaseActivity.java
com.android.busolo.apps.wardrobe.engine.ColorPickerFragment.java
com.android.busolo.apps.wardrobe.engine.CommentsFragment.java
com.android.busolo.apps.wardrobe.engine.FollowActivity.java
com.android.busolo.apps.wardrobe.engine.HomeActivity.java
com.android.busolo.apps.wardrobe.engine.ItemDetailsActivity.java
com.android.busolo.apps.wardrobe.engine.LoginFragment.java
com.android.busolo.apps.wardrobe.engine.MatchFragment.java
com.android.busolo.apps.wardrobe.engine.NewPostActivity.java
com.android.busolo.apps.wardrobe.engine.PrivateStreamFragment.java
com.android.busolo.apps.wardrobe.engine.ProfileFragment.java
com.android.busolo.apps.wardrobe.engine.PublicStreamActivity.java
com.android.busolo.apps.wardrobe.engine.PublicStreamFragment.java
com.android.busolo.apps.wardrobe.engine.SignupFragment.java
com.android.busolo.apps.wardrobe.engine.StepOneFragment.java
com.android.busolo.apps.wardrobe.engine.StepTwoFragment.java
com.android.busolo.apps.wardrobe.engine.UserAccountActivity.java
com.android.busolo.apps.wardrobe.engine.adapter.ColorListAdapter.java
com.android.busolo.apps.wardrobe.engine.adapter.ColorSpinnerAdapter.java
com.android.busolo.apps.wardrobe.engine.adapter.FeedsListAdapter.java
com.android.busolo.apps.wardrobe.engine.adapter.GridViewPhotoAdapter.java
com.android.busolo.apps.wardrobe.engine.adapter.ViewInflaterBaseAdapter.java
com.android.busolo.apps.wardrobe.engine.model.ColorPicker.java
com.android.busolo.apps.wardrobe.engine.model.FilterParam.java
com.android.busolo.apps.wardrobe.engine.model.Follow.java
com.android.busolo.apps.wardrobe.engine.model.Stream.java
com.android.busolo.apps.wardrobe.sync.SyncHelper.java
com.android.busolo.apps.wardrobe.sync.SyncService.java
com.android.busolo.apps.wardrobe.utils.AccountUtils.java
com.android.busolo.apps.wardrobe.utils.BitmapCache.java
com.android.busolo.apps.wardrobe.utils.ImageLoader.java
com.android.busolo.apps.wardrobe.utils.LogUtils.java
com.android.busolo.apps.wardrobe.utils.LruBitmapCache.java
com.android.busolo.apps.wardrobe.utils.NetUtils.java
com.android.busolo.apps.wardrobe.utils.ParserUtils.java
com.android.busolo.apps.wardrobe.utils.PrefUtils.java
com.android.busolo.apps.wardrobe.utils.ServerResponse.java
com.android.busolo.apps.wardrobe.utils.UIUtils.java
com.android.busolo.apps.wardrobe.utils.VolleyAppController.java
com.android.busolo.apps.wardrobe.utils.model.FeedResult.java
com.android.busolo.apps.wardrobe.widget.BezelImageView.java
com.android.busolo.apps.wardrobe.widget.CheckableFrameLayout.java
com.android.busolo.apps.wardrobe.widget.EllipsizedTextView.java
com.android.busolo.apps.wardrobe.widget.ObservableScrollView.java
com.android.busolo.apps.wardrobe.widget.SquareImageView.java