NewMissionAdapter.java
package com.opendream.onechange.adapter;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.opendream.onechange.LogExerciseActivity;
import com.opendream.onechange.LogFoodActivity;
import com.opendream.onechange.MissionActivity_;
import com.opendream.onechange.R;
import com.opendream.onechange.helper.Constants;
import com.opendream.onechange.model.Goal;
import com.opendream.onechange.model.Mission;
import com.opendream.onechange.service.MissionService;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
import butterknife.ButterKnife;
public class NewMissionAdapter extends BaseAdapter {
private List<Object> mData = new ArrayList<Object>();
private TreeSet mSeparatorsSet = new TreeSet();
protected int mItemView1;
protected int mItemView2;
protected int mItemView3;
protected int mItemHeader;
public static final String EXTRA_MISSION_OBJECT = Constants.INTENT_EXTRA_MISSION_OBJECT;
protected static final int TYPE_IN_PROGRESS = 0;
protected static final int TYPE_SUCCESS = 1;
protected static final int TYPE_FAILED = 2;
protected LayoutInflater mInflater;
Context mContext;
private void initData() {
mItemView1 = R.layout._item_mission_progress_view;
mItemView2 = R.layout._item_mission_success_view;
mItemView3 = R.layout._item_mission_failed_view;
mItemHeader = R.layout._item_dummy_header;
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void removeItem(int position) {
mData.remove(position);
}
public void addItem(Mission item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparatorItem(int item) {
mData.add(item);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
public NewMissionAdapter(Context context) {
mContext = context;
initData();
}
public NewMissionAdapter(Context context, int item_layout, int item_header) {
mContext = context;
initData();
}
public NewMissionAdapter(Context context, int item_layout) {
mItemView2 = R.layout._item_dummy_view;
mItemView3 = R.layout._item_mission_success_view;
mContext = context;
initData();
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Mission getItem(int i) {
return (Mission) mData.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
int type = getItemViewType(position);
final Mission mission = (Mission)mData.get(position);
final Goal goal = mission.getCurrentGoal();
SharedViewHolder holder;
if (convertView == null) {
holder = new SharedViewHolder();
switch (type) {
case TYPE_IN_PROGRESS:
convertView = mInflater.inflate(mItemView1, null);
holder.textViewLimit = (TextView)convertView.findViewById(R.id.limit_text_view);
holder.textCurrentLogs = (TextView) convertView.findViewById(R.id.current_text_view);
holder.textCurrentSubstitute = (TextView) convertView.findViewById(R.id.current_substitute_text_view);
holder.missionProgressBlock1 = (LinearLayout)convertView.findViewById(R.id.mission_progress_block1);
holder.missionProgressBlock2 = (LinearLayout)convertView.findViewById(R.id.mission_progress_block2);
break;
case TYPE_SUCCESS:
convertView = mInflater.inflate(mItemView2, null);
holder.newMissionImageView = ButterKnife.findById(convertView,
R.id.mission_result_new_mission_button_image_view);
holder.summaryTextView = ButterKnife.findById(convertView,
R.id.mission_result_summary_text_view);
break;
case TYPE_FAILED:
convertView = mInflater.inflate(mItemView3, null);
holder.newMissionImageView = ButterKnife.findById(convertView,
R.id.mission_result_new_mission_button_image_view);
holder.summaryTextView = ButterKnife.findById(convertView,
R.id.mission_result_summary_text_view);
holder.tryAgainButton = ButterKnife.findById(convertView,
R.id.mission_result_try_again_button_image_view);
break;
}
// shared
holder.textViewTitle = ButterKnife.findById(convertView, R.id.title_text_view);
holder.textViewWeekLeft = ButterKnife.findById(convertView, R.id.week_left_text_view);
holder.logIcon = ButterKnife.findById(convertView, R.id.log_icon);
holder.convertView = convertView;
convertView.setTag(holder);
}
else {
holder = (SharedViewHolder) convertView.getTag();
}
////////// AFTER FIRST TIME /////////
switch (type) {
case TYPE_IN_PROGRESS:
prepareSharedUiLayout(holder, mission, goal);
bindInProgressCellListener(holder, mission);
updateProgressDataUi(position, holder, mission, goal);
break;
case TYPE_SUCCESS:
prepareSharedUiLayout(holder, mission, goal);
bindSuccessUiListener(position, holder, mission, goal);
updateSuccessUi(position, holder , mission, goal);
break;
case TYPE_FAILED:
prepareSharedUiLayout(holder, mission, goal);
bindFailUiListener(position, holder, mission, goal);
updateFailedUi(position, holder, mission, goal);
break;
}
return convertView;
}
private void bindInProgressCellListener(SharedViewHolder holder, final Mission mission) {
holder.logIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent;
if (mission.missionType.equals(Constants.GOAL_MISSION_TYPE_EXERCISE)) {
intent = new Intent(mContext, LogExerciseActivity.class);
intent.putExtra(EXTRA_MISSION_OBJECT, mission);
mContext.startActivity(intent);
}
else {
intent = new Intent(mContext, LogFoodActivity.class);
intent.putExtra(EXTRA_MISSION_OBJECT, mission);
mContext.startActivity(intent);
}
}
});
}
private void bindFailUiListener(final int position, final SharedViewHolder holder,
final Mission mission, final Goal goal) {
if (holder.newMissionImageView == null)
holder.newMissionImageView = ButterKnife.findById(holder.convertView,
R.id.mission_result_new_mission_button_image_view);
holder.newMissionImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
removeItem(position);
MissionService.confirmYourSelfToChooseMissionFailed(mission);
Intent intent = new Intent(mContext, MissionActivity_.class);
mContext.startActivity(intent);
notifyDataSetChanged();
}
});
holder.logIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
removeItem(position);
MissionService.confirmYourSelfToChooseMissionFailed(mission);
notifyDataSetChanged();
}
});
if (holder.tryAgainButton == null)
holder.tryAgainButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MissionService.initGoalAndSave(mission, goal);
notifyDataSetChanged();
}
});
}
private void bindSuccessUiListener(final int position, final SharedViewHolder holder,
final Mission mission, final Goal goal) {
if (holder.newMissionImageView == null)
holder.newMissionImageView = ButterKnife.findById(holder.convertView,
R.id.mission_result_new_mission_button_image_view);
holder.newMissionImageView.setOnClickListener(null);
holder.newMissionImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MissionService.confirmYourSelfToChooseMissionSuccess(mission);
Intent intent = new Intent(mContext, MissionActivity_.class);
mContext.startActivity(intent);
removeItem(position);
notifyDataSetChanged();
}
});
holder.logIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
removeItem(position);
MissionService.confirmYourSelfToChooseMissionSuccess(mission);
notifyDataSetChanged();
}
});
}
private void updateProgressDataUi(final int position, final SharedViewHolder holder,
final Mission mission, final Goal goal) {
holder.textViewLimit.setText("" + goal.suggestedTimes);
holder.textCurrentLogs.setText(String.valueOf(goal.logCounter));
if (mission.missionType.equals(Constants.GOAL_MISSION_TYPE_EXERCISE)){
((View)holder.textCurrentSubstitute.getParent()).setVisibility(View.GONE);
}
else {
holder.textCurrentSubstitute.setText(String.valueOf(goal.substituteCounter));
((View)holder.textCurrentSubstitute.getParent()).setVisibility(View.VISIBLE);
}
painCircle(goal, holder.missionProgressBlock1, holder.missionProgressBlock2);
}
private void updateFailedUi(final int position, final SharedViewHolder holder,
final Mission mission, final Goal goal) {
String text;
if (mission.progressType.equals(Constants.GOAL_PROGRESS_TYPE_DECREASE)) {
text = String.format("Limit is %d, but you have %d.",
goal.suggestedTimes, goal.logCounter);
}
else {
text = String.format("Goal is %d, but you have %d.",
goal.suggestedTimes, goal.logCounter);
}
holder.summaryTextView.setText(text);
}
private void updateSuccessUi(final int position, final SharedViewHolder holder,
final Mission mission, final Goal goal) {
String text = String.format("Limit is %d, but you have %d.",
goal.suggestedTimes, goal.logCounter);
holder.summaryTextView.setText(text);
}
private void painCircle(Goal goal, LinearLayout upperBlock, LinearLayout lowerBlock) {
// fill class
class Filler {
public void fill1(int what, int times, LinearLayout to) {
for(int i =0 ;i < times; i++) {
to.addView(getCircle(what));
}
}
public void update(int what, int times, LinearLayout parent) {
for(int i =0 ;i < times; i++) {
ImageView imageView = (ImageView) parent.getChildAt(i);
imageView.setImageResource(what);
}
}
}
// initial
new Filler().fill1(R.color.mca__holo_blue_light_transparent, 10, upperBlock);
new Filler().fill1(R.color.mca__holo_blue_light_transparent, 10, lowerBlock);
int logCounter = goal.logCounter;
if (logCounter > 20) {
logCounter = 20;
}
if (goal.suggestedTimes > 10) { // 2 rows
new Filler().update(R.drawable.circle_grey, 10, upperBlock);
new Filler().update(R.drawable.circle_grey, 10, lowerBlock);
if (logCounter > 10) {
new Filler().update(R.drawable.circle_blue, 10, upperBlock);
new Filler().update(R.drawable.circle_blue, goal.logCounter - 10, lowerBlock);
} else {
new Filler().update(R.drawable.circle_blue, goal.logCounter, upperBlock);
}
}
else if (goal.suggestedTimes <= 10) {
new Filler().update(R.drawable.circle_grey, goal.suggestedTimes, upperBlock);
// REACHED AND CONTINUE
if (logCounter > 10) {
new Filler().update(R.drawable.circle_blue, 10, upperBlock);
new Filler().update(R.drawable.circle_blue, logCounter - 10, lowerBlock);
}
else {
new Filler().update(R.drawable.circle_blue, goal.logCounter, upperBlock);
}
}
}
public ImageView getCircle(int resourceId) {
// int id = mContext.getResources().getIdentifier("circle_blue", "drawable", mContext.getPackageName());
ImageView imageView = new ImageView(mContext);
LinearLayout.LayoutParams vp =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
int marginRightPixel = (mContext.getResources().getDimensionPixelSize(R.dimen.circle_margin_right));
vp.setMargins(0, 0, marginRightPixel, 0);
imageView.setLayoutParams(vp);
imageView.setImageResource(resourceId);
return imageView;
}
private void prepareSharedUiLayout(final SharedViewHolder holder, final Mission mission, final Goal goal) {
holder.textViewTitle.setText(mission.getName());
String formattedString = String.format("WEEK %d", goal.week);
holder.textViewWeekLeft.setText(formattedString);
}
@Override
public int getItemViewType(int position) {
Log.d("get type position", String.format("%d type: %s", position, getItem(position).
getStatus()));
int type;
String missionStatus = getItem(position).getStatus();
if (missionStatus.equals(Constants.MISSION_STATUS_IN_PROGRESS)) {
type = TYPE_IN_PROGRESS;
}
else if (missionStatus.equals(Constants.MISSION_STATUS_WAIT_SUCCESS)) {
type = TYPE_SUCCESS;
}
else if (missionStatus.equals(Constants.MISSION_STATUS_WAIT_FAILED)) {
type = TYPE_FAILED;
}
else {
type = -1; // error when not in case
}
return type;
}
@Override
public int getViewTypeCount() {
return 4;
}
public static class SharedViewHolder {
public View convertView;
// SHARED LAYOUT
public TextView textViewTitle;
public TextView textViewWeekLeft;
public ImageView logIcon;
// success ui + fail
public ImageView newMissionImageView;
public TextView summaryTextView;
// IN PROGRESS
public TextView textViewLimit;
public TextView textCurrentLogs;
public TextView textCurrentSubstitute;
public LinearLayout missionProgressBlock1;
public LinearLayout missionProgressBlock2;
// fail ui
public ImageView tryAgainButton;
}
}
package com.opendream.onechange.service;
import android.util.Log;
import com.opendream.onechange.helper.Constants;
import com.opendream.onechange.helper.Utils;
import com.opendream.onechange.model.Goal;
import com.opendream.onechange.model.Mission;
import java.util.Calendar;
public class MissionService {
public static Goal initGoal() {
Goal g = new Goal();
g.status = Constants.GOAL_STATUS_PROGRESS;
return g;
}
public static void initGoalAndSave(Mission mission, Goal goal) {
Calendar now = Calendar.getInstance();
mission.setStatus(Constants.MISSION_STATUS_IN_PROGRESS);
mission.save();
initGoalAndSave(goal);
}
public static void initGoalAndSave(Goal goal) {
Calendar now = Calendar.getInstance();
goal.logCounter = 0;
goal.startDate = now.getTime();
goal.endDate = Utils.getNextNSecond(10, now).getTime();
goal.save();
}
public static void passWeek(final Mission m, final Goal goal) {
int progressingWeek = goal.week;
long goalId;
if (m.getStatus().equals(Constants.MISSION_STATUS_WAIT_SUCCESS)) {
Log.d("MissionService-5", "MISSION SUCCESS STAY UNTIL DISMISS");
return;
}
else if (m.getStatus().equals(Constants.MISSION_STATUS_WAIT_FAILED)) {
Log.d("MissionService-5", "MISSION =FAILED= STAY UNTIL DISMISS");
return;
}
if (progressingWeek < 3) {
Log.d("Mission service-1", "pass week " + m.getName());
goalId = m.getAllGoals().get(progressingWeek).getId();
m.currentGoalId = goalId;
m.save();
Goal g = m.getCurrentGoal();
initGoalAndSave(m, g);
}
else {
Log.d("Mission service-2", "Success condition has been made... WAIT For User Dismiss");
setMissionWaitForSuccess(m);
}
}
public static void setMissionWaitForSuccess(final Mission m) {
m.setStatus(Constants.MISSION_STATUS_WAIT_SUCCESS);
m.save();
}
public static void setMissionWaitForFail(final Mission m) {
m.setStatus(Constants.MISSION_STATUS_WAIT_FAILED);
m.save();
}
public static void confirmYourSelfToChooseMissionFailed(final Mission m) {
m.setComplete("true");
m.setStatus(Constants.MISSION_STATUS_FAILED);
m.save();
}
public static void confirmYourSelfToChooseMissionSuccess(final Mission m) {
m.setComplete("true");
m.setStatus(Constants.MISSION_STATUS_SUCCESS);
m.save();
}
public static boolean isWaitForOver(final Mission mission) {
String missionStatus = mission.getStatus();
return missionStatus.equals(Constants.MISSION_STATUS_SUCCESS) ||
missionStatus.equals(Constants.MISSION_STATUS_FAILED);
}
public static boolean isOver(final Mission mission) {
String missionStatus = mission.getStatus();
return missionStatus.equals(Constants.MISSION_STATUS_WAIT_SUCCESS) ||
missionStatus.equals(Constants.MISSION_STATUS_WAIT_FAILED);
}
public static void judgeMission(Mission mission, Goal currentGoal) {
final boolean is_goal_endded =
Calendar.getInstance().getTimeInMillis() > currentGoal.endDate.getTime();
if (isOver(mission) || isWaitForOver(mission)) {
return;
}
if (is_goal_endded) {
if (mission.progressType.equals(Constants.GOAL_PROGRESS_TYPE_INCREASE)) {
if (currentGoal.logCounter >= currentGoal.suggestedTimes) {
passWeek(mission, currentGoal);
}
else {
setMissionWaitForFail(mission);
}
}
else if (mission.progressType.equals(Constants.GOAL_PROGRESS_TYPE_DECREASE)) {
if (currentGoal.logCounter <= currentGoal.suggestedTimes) {
passWeek(mission, currentGoal);
}
else {
setMissionWaitForFail(mission);
}
}
}
else {
Log.d("MISSION", "Remaining.. " +
String.valueOf(
currentGoal.endDate.getTime()-
Calendar.getInstance().getTimeInMillis()
));
}
}
public static void initMission(Mission mission) {
mission.setComplete("false");
}
}