package com.osc.tweet.app.fragments;

import java.io.IOException;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.os.AsyncTaskCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chopping.application.BasicPrefs;
import com.chopping.fragments.BaseFragment;
import com.chopping.net.TaskHelper;
import com.osc.tweet.R;
import com.osc.tweet.app.App;
import com.osc.tweet.utils.Prefs;
import com.osc.tweet.views.RoundedNetworkImageView;
import com.osc4j.OscApi;
import com.osc4j.ds.personal.Actives;
import com.osc4j.ds.personal.Am;
import com.osc4j.ds.personal.MyInformation;
import com.osc4j.exceptions.OscTweetException;

/**
 * Show my information.
 *
 * @author Xinyue Zhao
 */
public final class MyInfoFragment extends BaseFragment {
	/**
	 * Main layout for this component.
	 */
	private static final int LAYOUT = R.layout.fragment_my_info;
	/**
	 * My photo.
	 */
	private RoundedNetworkImageView mUserPhotoIv;
	/**
	 * My name.
	 */
	private TextView mUserNameTv;
	/**
	 * My information personal.
	 */
	private MyInformation mMyInfo;

	/**
	 * Initialize an {@link  MyInfoFragment}.
	 *
	 * @param context
	 * 		A {@link android.content.Context} object.
	 *
	 * @return An instance of {@link MyInfoFragment}.
	 */
	public static MyInfoFragment newInstance(Context context) {
		return (MyInfoFragment) Fragment.instantiate(context, MyInfoFragment.class.getName());
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(LAYOUT, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		mUserPhotoIv = (RoundedNetworkImageView) view.findViewById(R.id.user_photo_iv);
		mUserNameTv = (TextView) view.findViewById(R.id.user_name_tv);

		getMyInformation();
		view.findViewById(R.id.refresh_btn).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				getMyInformation();
			}
		});
	}


	/**
	 * Get my personal information.
	 */
	private void getMyInformation() {
		AsyncTaskCompat.executeParallel(new AsyncTask<Object, MyInformation, MyInformation>() {
			@Override
			protected MyInformation doInBackground(Object... params) {
				try {
					return OscApi.myInformation(App.Instance);
				} catch (IOException e) {
					return null;
				} catch (OscTweetException e) {
					return null;
				}
			}

			@Override
			protected void onPostExecute(MyInformation myInfo) {
				super.onPostExecute(myInfo);
				if (myInfo != null && myInfo.getAm() != null) {
					mMyInfo = myInfo;
					Am am = mMyInfo.getAm();
					mUserPhotoIv.setDefaultImageResId(R.drawable.ic_portrait_preview);
					mUserPhotoIv.setImageUrl(am.getPortrait(), TaskHelper.getImageLoader());
					mUserNameTv.setText(am.getName());

					if (myInfo.getActives() != null) {
						Actives actives = new Actives(com.osc4j.ds.common.Status.STATUS_OK, myInfo.getActives());
						getChildFragmentManager().beginTransaction().replace(R.id.my_last_actives_fl,
								ActivesListFragment.newInstance(App.Instance, actives)).commit();
					}
				}
			}
		});
	}


	@Override
	protected BasicPrefs getPrefs() {
		return Prefs.getInstance();
	}
}

@@ -299,50 +299,8 @@
 ew;%0A
-import android.view.View.OnClickListener;%0A
 impo
@@ -479,24 +479,77 @@
 TaskHelper;%0A
+import com.nineoldandroids.animation.ObjectAnimator;%0A
 import com.o
@@ -620,24 +620,82 @@
 tils.Prefs;%0A
+import com.osc.tweet.views.OnViewAnimatedClickedListener;%0A
 import com.o
@@ -1341,16 +1341,80 @@
 MyInfo;%0A
+%09/**%0A%09 * Click to refresh my-info.%0A%09 */%0A%09private View mRefreshV;
 %0A%09/**%0A%09 
@@ -2188,33 +2188,22 @@
 v);%0A
-%0A
 %09%09
-getMyInformation();%0A%09%09
+mRefreshV = 
 view
@@ -2233,16 +2233,51 @@
 esh_btn)
+;%0A%09%09getMyInformation();%0A%09%09mRefreshV
 .setOnCl
@@ -2294,21 +2294,35 @@
 r(new On
+ViewAnimated
 Click
+ed
 Listener
@@ -2362,22 +2362,16 @@
 onClick(
-View v
 ) %7B%0A%09%09%09%09
@@ -2570,24 +2570,342 @@
 mation%3E() %7B%0A
+%09%09%09ObjectAnimator objectAnimator;%0A%0A%0A%09%09%09@Override%0A%09%09%09protected void onPreExecute() %7B%0A%09%09%09%09objectAnimator = ObjectAnimator.ofFloat(mRefreshV, %22rotation%22, 0, 360f);%0A%09%09%09%09objectAnimator.setDuration(800);%0A%09%09%09%09objectAnimator.setRepeatCount(ObjectAnimator.INFINITE);%0A%09%09%09%09objectAnimator.start();%0A%09%09%09%09super.onPreExecute();%0A%09%09%09%7D%0A%0A
 %09%09%09@Override
@@ -3839,24 +3839,54 @@
 %09%09%09%09%09%7D%0A%09%09%09%09%7D
+%0A%0A%09%09%09%09objectAnimator.cancel();
 %0A%09%09%09%7D%0A%09%09%7D);%0A

Add animation for loading my-info