SingleTitleFragment.java 17.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
package com.sunvote.xpadapp.fragments;

import android.annotation.TargetApi;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunvote.util.LogUtil;
import com.sunvote.xpadapp.MainActivity;
import com.sunvote.xpadapp.R;
import com.sunvote.xpadapp.base.BaseFragment;
import com.sunvote.xpadapp.db.modal.BillInfo;
import com.sunvote.xpadapp.db.modal.UIType;
import com.sunvote.xpadapp.db.modal.VoteResult;
import com.sunvote.xpadapp.fragments.MultiContentDetailFragment.ContentVoteOnBack;
import com.sunvote.xpadapp.utils.SharedPreferencesUtil;
import com.sunvote.xpadcomm.XPadApi;
import com.sunvote.xpadcomm.XPadApiInterface.VoteInfo;
import com.ycanfunc.util.StringUtils;

public class SingleTitleFragment extends BaseFragment implements ContentVoteOnBack {

	private TextView tvTitle;
	private TextView tvTips;
	private TextView ivReuslt;
	private Button btnA;
	private Button btnB;
	private Button btnC;
	private Button btnModify;
	private String[] options;
	private BillInfo bill;
	private VoteInfo voteInfo;
	private RelativeLayout singlevoteLayout;
	private RelativeLayout singlevoteTvTipsLayout;
	private TextView votetype;


	private String TAG = "SingleTitleFragment";
	MultiContentDetailFragment fDetail;

	private int voteValue;
	private RelativeLayout confirmLayout;
	private TextView tvConfirmText;
	MainActivity mact;

	public String[] voteOptions = new String[13];
	//btn一行个数
	private  int btnNum = 6;
	//自定义评测
	private final int customVote=101;

	public void setInfo(BillInfo info) {
		bill = info;
		options = bill.billOptions.split("/");
	}

	public void setInfo(BillInfo info, VoteInfo vote) {

		bill = info;
		if (bill == null) {
			bill = new BillInfo();
		}
		voteInfo = vote;

	}

	private void initOptions() {
		String str = getString(R.string.zc_fd);
		voteOptions[0] = str;
		voteOptions[1] = getString(R.string.zc_fd_qq);
		voteOptions[2] = getString(R.string.ty_fd_qq);
		voteOptions[3] = getString(R.string.my_jbmy_bmy);
		voteOptions[4] = getString(R.string.my_jbmy_bmy_blj);
		voteOptions[5] = getString(R.string.my_jbmy_yb_bmy);
		voteOptions[6] = getString(R.string.fcmy_my_blj_bmy_fcbmy);
		voteOptions[7] = getString(R.string.fcty_ty_bqd_bty_fcbty);
		voteOptions[8] = getString(R.string.yx_cz_bcz);
		voteOptions[9] = getString(R.string.yx_cz_jbcz_bcz);
		voteOptions[10] = getString(R.string.y_l_z_c);
		voteOptions[11] = getString(R.string.my_bmy_fcbmy);

		if (voteInfo.mode == XPadApi.VoteType_Vote) {
			if (voteInfo.mode1_msgType == 1) {
				options = voteOptions[1].split("/");
			} else if (voteInfo.mode1_msgType == 2) {
				options = voteOptions[0].split("/");
			} else if (voteInfo.mode1_msgType == 3) {
				if(voteInfo.mode4==customVote){ //自定义评测
					if(bill!=null && !StringUtils.isEmpty(bill.billOptions)){
						options = bill.billOptions.split("/");
					}
				}else {
					options = voteOptions[voteInfo.mode4 - 1].split("/");
				}
			}
		} else if (voteInfo.mode == XPadApi.VoteType_Evaluate) {
			if (voteInfo.mode1_msgType > 0) {
				options = voteOptions[voteInfo.mode1_msgType - 1].split("/");
			}
		}else{
			options = voteOptions[1].split("/");
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		Log.d(TAG, "onCreateView");

		View view = inflater.inflate(R.layout.fragment_singlevote, container, false);
		singlevoteLayout=(RelativeLayout)view.findViewById(R.id.singlevote_btn_layout1);
		votetype = view.findViewById(R.id.votetype);
		initOptions();
		mact = (MainActivity) getActivity();
		mact.setBackgroundColor(Color.parseColor("#450603"));
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				;
			}
		});
		tvTitle = (TextView) view.findViewById(R.id.singlevote_content);
		singlevoteTvTipsLayout = view.findViewById(R.id.singlevote_tv_tips_layout);
		if (bill != null && bill.title != null && bill.title.length() > 0) {
			tvTitle.setText(bill.title);
		}

		if(voteInfo.mode1_msgType <= 2){
			votetype.setText(R.string.yianbiaojue);
		}
		if(voteInfo.mode1_msgType > 2){
			if(voteInfo.mode4 > 2){
				votetype.setText(R.string.yianceping);
			}else{
				votetype.setText(R.string.yianbiaojue);
			}
		}

		tvTitle.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showMultiContentDetailFragment();
			}
		});

		ImageView iv = (ImageView) view.findViewById(R.id.singlevote_img_detail);
		iv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showMultiContentDetailFragment();
			}
		});

		if (bill != null && bill.billFile != null && bill.billFile.length() > 0
				&& (voteInfo != null && voteInfo.file == 1)) {
			iv.setVisibility(View.VISIBLE);
		} else {
			iv.setVisibility(View.INVISIBLE);
		}


		tvTips = (TextView) view.findViewById(R.id.singlevote_tv_tips);
		tvTips.setText("");
		ivReuslt = (TextView) view.findViewById(R.id.singlevote_tv_result);
		ivReuslt.setVisibility(View.INVISIBLE);

		btnA = (Button) view.findViewById(R.id.singlevote_btnA);
		btnA.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (voteInfo.mode2_modify == 1) {
					doVoteWithIndex(1);
				} else {
					showConfirmWithValue(1);
				}
			}
		});

		btnB = (Button) view.findViewById(R.id.singlevote_btnB);
		btnB.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (voteInfo.mode2_modify == 1) {
					doVoteWithIndex(2);
				} else {
					showConfirmWithValue(2);
				}

			}
		});

		btnC = (Button) view.findViewById(R.id.singlevote_btnC);
		btnC.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (options.length == 2) {
					if (voteInfo.mode2_modify == 1) {
						doVoteWithIndex(2);
					} else {
						showConfirmWithValue(2);
					}
				} else {
					if (voteInfo.mode2_modify == 1) {
						doVoteWithIndex(3);
					} else {
						showConfirmWithValue(3);
					}
				}
			}
		});

		btnModify = (Button) view.findViewById(R.id.singlevote_btn_modify);
		btnModify.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				btnModify.setVisibility(View.INVISIBLE);
				showVote();
				ivReuslt.setVisibility(View.VISIBLE);
			}
		});

		tvConfirmText = (TextView) view.findViewById(R.id.singlevote_confirm_textview);
		confirmLayout = (RelativeLayout) view.findViewById(R.id.singlevote_confirm_panel);
		Button btnConfirmOK = (Button) view.findViewById(R.id.singlevote_btn_confirm_ok);
		btnConfirmOK.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				hideConfirm();
				doVoteWithIndex(voteValue);
			}
		});

		Button btnConfirmCancel = (Button) view.findViewById(R.id.singlevote_btn_confirm_cancel);
		btnConfirmCancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				hideConfirm();
			}
		});

		//添加自定义评测  wutaian 2017.08.23
		if(voteInfo.mode4==customVote){
			if(options!=null){
				setButton(options);
			}
			btnA.setVisibility(View.INVISIBLE);
			btnB.setVisibility(View.INVISIBLE);
			btnC.setVisibility(View.INVISIBLE);
		}else{
			// options = bill.billOptions.split("/");
			if (options.length == 2) {
				btnA.setText(options[0]);
				btnB.setVisibility(View.INVISIBLE);
				btnC.setText(options[1]);
			} else if (options.length == 3) {
				btnA.setText(options[0]);
				btnB.setText(options[1]);
				btnC.setText(options[2]);
			}
			setBtnModle(1,false);
		}
		checkIsVoted();
		return view;
	}
	/**
	 *  自定评测按钮 wutaian 2017.08.23
	 * @param data
	 */
	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	private void setButton(String [] data){
		Button btn []  = new Button[data.length];
		DisplayMetrics dm = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
		int width = dm.widthPixels;
		int height = dm.heightPixels;
		int j = -1;
		int f = btn.length;

		for (int i=0;i<data.length;i++){
			btn[i]=new Button(getActivity());
			btn[i].setText(data[i].toString());
			btn[i].setTag(data[i].toString());
			btn[i].setId(i);
			btn[i].setGravity(Gravity.CENTER);
			btn[i].setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.agree_selector));
			btn[i].setTextAppearance(getActivity(),R.style.ContentDetailButtonStyle);
			if(data[i].toString().getBytes().length > 9){
				btn[i].setTextSize(18);
			}
			btn[i].setOnClickListener(btnClick);
			//TextViewUtils.adjustTvTextSize(btn[i],(width-50)/btnNum,data[i].toString());

			RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((width-180)/btnNum, 100);
			//设置按钮的宽度和高度
			if (i%btnNum == 0) {
				j++;
			}
			f--;
			if(f<btnNum){
				//最后一行不是3个,是2个,或者是1个,就无法整除,就会报错。加了这一个判断之后就会消除错误
				if (i%btnNum == 5 && i%btnNum == 4 && i%btnNum== 3
						&&i%btnNum == 2 && i%btnNum == 1) {
					j++;
				}
			}
			params.leftMargin = ((width-180)/btnNum+10)*(i%btnNum);   //横坐标定位
			params.topMargin = 10 + 2 * 55 * j;   //纵坐标定位
			singlevoteLayout.addView(btn[i],params);
		}
	}

	View.OnClickListener btnClick=new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			for(int i = 0;i<options.length;i++){
				if(options[i].equals(v.getTag())){
					if (voteInfo.mode2_modify == 1) {
						doVoteWithIndex(i+1);
					} else {
						showConfirmWithValue(i+1);
					}
					break;
				}
			}
		}
	};

	/**
	 *  设置按钮模式 wutaian 2017.08.23
	 * @param model
	 *  1:隐藏显示模式 2:编辑与不可编辑
	 * @param isBoolean
	 *  true:显示,可编辑  false:隐藏,不可编辑
	 */
	private void setBtnModle(int model,boolean isBoolean){
		for (int i = 0, j = singlevoteLayout.getChildCount(); i < j; i++) {
			View child = singlevoteLayout.getChildAt(i);
			if (child instanceof Button) {
				Button btn= (Button) child;
				if(model==1){
					if(isBoolean){
						btn.setVisibility(View.VISIBLE);
					}else{
						btn.setVisibility(View.INVISIBLE);
					}
				}else if(model==2){
					btn.setEnabled(isBoolean);
				}
			}
		}
	}

	@Override
	public void onDestroy() {
		closeDetail();
		super.onDestroy();
	}

	@Override
	public void onDetach() {

		super.onDetach();
	}

	@Override
	public void onResume() {

		super.onResume();
	}

	@Override
	public void onStop() {
		super.onStop();
	}

	private void showConfirmWithValue(int value) {

		voteValue = value;
		tvConfirmText.setText(
				getString(R.string.you_selected) + options[value - 1] + getString(R.string.you_selected_confirm));
		confirmLayout.setVisibility(View.VISIBLE);
		if(voteInfo.mode4==customVote){
			setBtnModle(2,false);
		}else{
			btnA.setVisibility(View.INVISIBLE);
			btnB.setVisibility(View.INVISIBLE);
			btnC.setVisibility(View.INVISIBLE);
		}
	}

	private void showMultiContentDetailFragment(){
		if (bill != null && bill.billFile != null && bill.billFile.length() > 0) {
			if (voteInfo != null && voteInfo.file == 1) {
				FragmentManager fm = getFragmentManager();
				FragmentTransaction tx = fm.beginTransaction();
				tx.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
				fDetail = new MultiContentDetailFragment();
				fDetail.setInfo(bill, voteInfo, true,1, 1);
				fDetail.contentBackListener = SingleTitleFragment.this;
				mact.multiContentFragment = fDetail;
				tx.add(R.id.frame_content, fDetail, "ffDetail");
				tx.addToBackStack(null);
				tx.commitAllowingStateLoss();
			}
		}
	}

	private void hideConfirm() {
		confirmLayout.setVisibility(View.GONE);
		if(voteInfo.mode4==customVote){
			setBtnModle(2,true);
		}else{
			btnA.setVisibility(View.VISIBLE);
			if (options.length == 3) {
				btnB.setVisibility(View.VISIBLE);
			}
			btnC.setVisibility(View.VISIBLE);
		}
	}

	private void doVoteWithIndex(int index) {

		if (bill != null) {
			singlevoteTvTipsLayout.setVisibility(View.VISIBLE);
			tvTips.setText(getString(R.string.submiting));
			bill.voteResult = index;
		}
		voteValue = index;
		mMainActivity.presenter.submitVote(XPadApi.AnsType_Single, String.valueOf(index));

		disableVote();
		showResult();

	}

	private void showResult() {
		if (bill.voteResult > 0) {
			ivReuslt.setVisibility(View.INVISIBLE);
			singlevoteTvTipsLayout.setVisibility(View.GONE);
			new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
				@Override
				public void run() {
					if (voteInfo.mode3_secret == 0 && options != null) {//
						if(voteValue -1 >= 0 && voteValue - 1 < options.length) {
							ivReuslt.setText(options[voteValue - 1]);
							ivReuslt.setBackgroundResource(R.drawable.voted_empty);
							tvTitle.setTextColor(Color.parseColor("#B5B5B5"));
						}
					} else {
						ivReuslt.setText("");
						ivReuslt.setBackgroundResource(R.drawable.voted);
						tvTitle.setTextColor(Color.parseColor("#B5B5B5"));
					}
					ivReuslt.setVisibility(View.VISIBLE);
				}
			},200);
		} else {
			ivReuslt.setVisibility(View.INVISIBLE);
		}

	}

	private void hideVote() {
		if(voteInfo.mode4==customVote){
			setBtnModle(1,false);
			setBtnModle(2,false);
		}else{
			btnA.setVisibility(View.INVISIBLE);
			btnB.setVisibility(View.INVISIBLE);
			btnC.setVisibility(View.INVISIBLE);
		}
		btnModify.setVisibility(View.INVISIBLE);
	}

	private void showVote() {
		if(voteInfo.mode4==customVote){
			setBtnModle(2,true);
			setBtnModle(1,true);
		}else{
			btnA.setVisibility(View.VISIBLE);
			if (options.length == 3) {
				btnB.setVisibility(View.VISIBLE);
			}
			btnC.setVisibility(View.VISIBLE);
			enableVote();
		}
		ivReuslt.setVisibility(View.INVISIBLE);
		btnModify.setVisibility(View.INVISIBLE);
	}

	private void showModify() {
		hideVote();
		btnModify.setVisibility(View.VISIBLE);
		// ivReuslt.setVisibility(View.VISIBLE);
	}

	private void enableVote(){
		if(voteInfo.mode4==customVote){
			setBtnModle(2,true);
		}else{
			btnA.setEnabled(true);
			btnB.setEnabled(true);
			btnC.setEnabled(true);
		}

	}

	private void disableVote() {
//		if(voteInfo.mode4==customVote){
//			setBtnModle(2,false);
//		}else{
//			btnA.setEnabled(false);
//			btnB.setEnabled(false);
//			btnC.setEnabled(false);
//		}
		hideVote();
	}

	private  void showModifyOrDisable() {
		if (bill.voteResult > 0) {
			if (voteInfo.mode2_modify == 1) {
				showModify();
				singlevoteTvTipsLayout.setVisibility(View.VISIBLE);
//				tvTips.setText(getString(R.string.submited));
				tvTips.setText("");
			} else {
				disableVote();
				singlevoteTvTipsLayout.setVisibility(View.VISIBLE);
				tvTips.setText("");
			}
		}
	}

	private void closeDetail() {
		if (fDetail != null) {
			if(fDetail.isClose){
				FragmentManager fm = getFragmentManager();
				FragmentTransaction tx = fm.beginTransaction();
				tx.remove(fDetail);
				tx.commit();
				//tx.commitAllowingStateLoss();
			}
		}
	}

	/*
	@Override
	public void onVoteEvent(VoteInfo info) {
		if (info.mode == XPadApi.VoteType_Stop) {
			hideVote();
			saveVoteInfo();

		} else {
			voteInfo = (VoteInfo) info;
			if (voteInfo.mode2_modify == 1) {
				tvTips.setText(getString(R.string.please_vote_can_modify));
			} else {
				tvTips.setText(getString(R.string.please_vote_can_not_modify));
			}



			showVote();

			checkIsVoted();
		}

	}
*/
	@Override
	public void onVoteSubmitSuccess() {
		super.onVoteSubmitSuccess();
		showModifyOrDisable();
		if(voteValue > 0){
			saveVoteInfo();
		}
	}

	@Override
	public void onVoteSubmitError() {
		super.onVoteSubmitError();
		showVote();
	}

	private void saveVoteInfo(){
		SharedPreferencesUtil.saveData(getActivity(),DATAPOS,Integer.valueOf(voteInfo.dataPos));
		SharedPreferencesUtil.saveData(getActivity(),VOTEMODE,Integer.valueOf(voteInfo.mode));
		SharedPreferencesUtil.saveData(getActivity(),VOTEVALUE,Integer.valueOf(voteValue));
	}

	private void checkIsVoted(){
		Log.i(TAG, "checkIsVoted: ");
		Integer datapos = (Integer) SharedPreferencesUtil.getData(getActivity(),DATAPOS,Integer.valueOf(-1));
		Integer votemode = (Integer) SharedPreferencesUtil.getData(getActivity(),VOTEMODE,Integer.valueOf(-1));
		Integer val = (Integer) SharedPreferencesUtil.getData(getActivity(),VOTEVALUE,Integer.valueOf(-1));
		Log.i(TAG, "checkIsVoted: datapos:"+ datapos.intValue() +" voteMode:"+ votemode.intValue()+ " val:"+val.intValue());
		if(datapos.intValue() == voteInfo.dataPos && votemode.intValue() == voteInfo.mode && val.intValue()!=-1){
			voteValue = val.intValue();
			if (bill != null) {
				bill.voteResult = voteValue;
			}
			showResult();
			showModifyOrDisable();
			//tvTips.setText(getString(R.string.submited));
		}
	}

	@Override
	public void onBack(VoteResult result) {
		// TODO Auto-generated method stub
		voteValue = result.selectResult;
		mMainActivity.multiContentFragment = null;
		showResult();
		showModifyOrDisable();
	}
}