package com.dinhcv.lifelogpedometer.activity; import android.app.DatePickerDialog; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.DatePicker; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.dinhcv.lifelogpedometer.R; import com.dinhcv.lifelogpedometer.adapter.NoticeAdapter; import com.dinhcv.lifelogpedometer.customview.ExpandableListCustomView; import com.dinhcv.lifelogpedometer.interfaces.LLAPIManagerListener; import com.dinhcv.lifelogpedometer.model.structure.home.NoticeInfo; import com.dinhcv.lifelogpedometer.model.structure.home.TagetInfo; import com.dinhcv.lifelogpedometer.portal.ApiServices; import com.dinhcv.lifelogpedometer.portal.LLAPIManager; import com.dinhcv.lifelogpedometer.utils.Const; import com.dinhcv.lifelogpedometer.utils.Debug; import com.dinhcv.lifelogpedometer.utils.Utils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import de.hdodenhof.circleimageview.CircleImageView; public class TopDateFragment extends FragmentBase implements SettingFragmentPresenter { private TextView tvDate; private TextView tvStep; private TextView tvRemain; private TextView tvRateDone; private ImageView ivBack; private ImageView ivNext; private LinearLayout llBike; private LinearLayout llWalking; private LinearLayout llRunning; private Date mAnaDate; private Calendar mCalendar; private int mAnaDay; private int mAnaMonth; private int mAnaYear; private Context mContext; private Const.STEP_TYPE stepType; private TagetInfo mTagetInfo = new TagetInfo(); private CircleImageView ivToDay; private ExpandableListCustomView lvNotice; private NoticeAdapter mNoticeAdapter; private View mRootview; private TopFragment mTopFragment; public void setRootFragment(TopFragment frag) { this.mTopFragment = frag; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment mRootview = inflater.inflate(R.layout.fragment_top_date, container, false); mContext = getActivity(); initView(mRootview); initData(); return mRootview; } private void initView(View rootView) { tvDate = (TextView) rootView.findViewById(R.id.tv_date); tvStep = (TextView) rootView.findViewById(R.id.tv_step); tvRemain = (TextView) rootView.findViewById(R.id.tv_remainingStep); tvRateDone = (TextView) rootView.findViewById(R.id.tv_completeRate); ivBack = (ImageView) rootView.findViewById(R.id.iv_back); ivNext = (ImageView) rootView.findViewById(R.id.iv_next); llBike = (LinearLayout) rootView.findViewById(R.id.ll_bike); llWalking = (LinearLayout) rootView.findViewById(R.id.ll_walking); llRunning = (LinearLayout) rootView.findViewById(R.id.ll_running); ivToDay = (CircleImageView) rootView.findViewById(R.id.iv_toDay); lvNotice = (ExpandableListCustomView) rootView.findViewById(R.id.lv_notice); handleEvent(); } private void handleEvent(){ tvDate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { handleAnaDatePicker(); } }); ivBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mCalendar = Calendar.getInstance(); mCalendar.setTime(mAnaDate); mCalendar.add(Calendar.DAY_OF_MONTH, -1); Date date = mCalendar.getTime(); tvDate.setText(Utils.dateToStringFormatDayMonthYearJp(date)); mAnaDate = date; getHomePage(mAnaDate, stepType); getNews(mAnaDate); } }); ivNext.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mCalendar = Calendar.getInstance(); mCalendar.setTime(mAnaDate); mCalendar.add(Calendar.DAY_OF_MONTH, +1); Date date = mCalendar.getTime(); tvDate.setText(Utils.dateToStringFormatDayMonthYearJp(date)); mAnaDate = date; getHomePage(mAnaDate, stepType); getNews(mAnaDate); } }); llWalking.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { stepType = Const.STEP_TYPE.WALKING; updateUiStepType(false, true, false); // add data getHomePage(mAnaDate, stepType); } }); llRunning.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { stepType = Const.STEP_TYPE.RUNNING; updateUiStepType(false, false, true); // add getHomePage(mAnaDate, stepType); } }); llBike.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { stepType = Const.STEP_TYPE.BIKE; updateUiStepType(true, false, false); // add data getHomePage(mAnaDate, stepType); } }); ivToDay.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mTopFragment.showDetailFragment(); } }); } private void updateUiStepType(boolean b1, boolean b2, boolean b3) { llBike.setSelected(b1); llWalking.setSelected(b2); llRunning.setSelected(b3); } /** * Show date picker dialog */ private void handleAnaDatePicker() { new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mCalendar = Calendar.getInstance(); mCalendar.set(year, monthOfYear, dayOfMonth); mAnaYear = year; mAnaMonth = monthOfYear; mAnaDay = dayOfMonth; Date date = mCalendar.getTime(); tvDate.setText(Utils.dateToStringFormatDayMonthYearJp(date)); mAnaDate = date; getHomePage(mAnaDate, stepType); getNews(mAnaDate); } }, mAnaYear, mAnaMonth, mAnaDay).show(); } /** * init data for */ private void initData() { mCalendar = Calendar.getInstance(); mCalendar.setTime(new Date()); mAnaDate = mCalendar.getTime(); mAnaYear = mCalendar.get(Calendar.YEAR); mAnaMonth = mCalendar.get(Calendar.MONTH); mAnaDay = mCalendar.get(Calendar.DAY_OF_MONTH); tvDate.setText(Utils.dateToStringFormatDayMonthYearJp(mAnaDate)); stepType = Const.STEP_TYPE.WALKING; updateUiStepType(false, true, false); getHomePage(mAnaDate, stepType); getNews(mAnaDate); } private void getHomePage(Date date, Const.STEP_TYPE stepType){ showDialog(mContext); ApiServices.homePage(date, stepType, new LLAPIManagerListener() { @Override public void onError(Error error) { Debug.error("Get data history error"); hiddenDialog(); showDialogNotData(); } @Override public void onSuccess(String json) { Debug.error("Get data history success"); hiddenDialog(); loadDataDone(json); } @Override public void onSuccess(JSONObject object) { Debug.error("Get data history success"); hiddenDialog(); } }); } private void showDialogNotData(){ showAlerDialog(mContext, getResources().getString(R.string.can_not_get_data)); } private void loadDataDone(String jsonString) { JSONObject jsonObject = null; try { jsonObject = new JSONObject(jsonString); int status = jsonObject.optInt("status"); if (status == 1) { JSONObject jsonObject1 = jsonObject.optJSONObject("result"); JSONObject targetInf = jsonObject1.getJSONObject("targetInf"); JSONArray listNotice = jsonObject1.getJSONArray("listNotice"); if (targetInf != null){ String target = targetInf.optString("target_step"); Debug.normal("Target: "+ target); mTagetInfo.setTaget(targetInf.optString("target_step")); mTagetInfo.setSteps(targetInf.optString("num_step")); mTagetInfo.setStepRemain(targetInf.optString("remaining_step")); mTagetInfo.setCompletePercent(targetInf.optString("complete_percent")); } if (listNotice != null && listNotice.length() > 0) { List infoLists = new ArrayList<>(); for (int i = 0; i < listNotice.length(); i++){ NoticeInfo noticeInfo = new NoticeInfo(); JSONObject ob = (JSONObject) listNotice.get(i); noticeInfo.setId(ob.optInt("id")); noticeInfo.setContent(ob.optString("notice_content")); infoLists.add(noticeInfo); } mTagetInfo.setNoticeList(infoLists); } } } catch (JSONException e) { e.printStackTrace(); mTagetInfo = new TagetInfo();; } loadUI(); } private void loadUI(){ tvStep.setText(String.valueOf(mTagetInfo.getSteps())); tvRemain.setText(String.valueOf(mTagetInfo.getStepRemain())); tvRateDone.setText(mContext.getResources().getString(R.string.percent_unit, mTagetInfo.getCompletePercent())); } private void getNews(Date date){ ApiServices.getNews(date, new LLAPIManagerListener() { @Override public void onError(Error error) { Debug.error("Get data news error"); } @Override public void onSuccess(String json) { Debug.error("Get data news success"); loadNewsDone(json); } @Override public void onSuccess(JSONObject object) { Debug.error("Get data news success"); } }); } private void loadNewsDone(String jsonString) { List contentList = new ArrayList<>(); JSONObject jsonObject = null; try { jsonObject = new JSONObject(jsonString); int status = jsonObject.optInt("status"); if (status == 1) { JSONObject jsonResult = jsonObject.optJSONObject("result"); JSONArray listNotice = jsonResult.getJSONArray("listNotice"); if (listNotice != null) { for (int i = 0; i < listNotice.length(); i++) { JSONObject obContent = (JSONObject) listNotice.get(i); String content = obContent.optString("notice_content"); Debug.normal("Content: " + content); contentList.add(new NoticeInfo(i+1,content)); } } } } catch (JSONException e) { e.printStackTrace(); } addNews(contentList); } private void addNews(List noticeInfos){ if (noticeInfos != null && noticeInfos.size() >0){ mNoticeAdapter = new NoticeAdapter(mContext, noticeInfos); lvNotice.setAdapter(mNoticeAdapter); lvNotice.setExpanded(true); }else { lvNotice.setAdapter(null); } } @Override public void onAttach(Context context) { super.onAttach(context); } @Override public void onSaveData() { } @Override public void onInvalidate(boolean isInit) { initData(); } @Override public void onViewStateRestored(@Nullable Bundle savedInstanceState) { super.onViewStateRestored(savedInstanceState); initData(); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); } }