MultiContentDetailFragment.java 7.76 KB
package com.sunvote.xpadapp.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnRenderListener;
import com.sunvote.util.LogUtil;
import com.sunvote.xpadapp.R;
import com.sunvote.xpadapp.activity.PageManager;
import com.sunvote.xpadapp.bean.Bills;
import com.sunvote.xpadapp.db.DBManager;

import java.io.File;

/**
 * Created by Elvis on 2017/11/30 15:07
 * Email:Eluis@psunsky.com
 * 版权所有:长沙中天电子设计开发有限公司
 * Description: 人大通用版XPadAppRD重构
 *  表决界面
 */
public class MultiContentDetailFragment extends BaseFragment {
	private String TAG = "MultiContentDetailFr";
	private Bills bill;
	private TextView tvTitle;
	private TextView tvTips;
	private TextView ivReuslt;
	private Button btnA;
	private Button btnB;
	private Button btnC;
	private PDFView pdfView;
	private Button btnLookPdf;

	private RelativeLayout panelVote;
	private RelativeLayout panelModify;

	public String[] options;
	private boolean showBackBtn;

	//关闭当前Fragment
	public boolean isClose = true;

	//pdf 页码
	public int pdfPageNum = 0;
	private String filename;

	//pdf延迟加载
	private int pdfDelayLoadTime=500;

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

	public String[] voteOptions = new String[13];

	public ContentVoteOnBack contentBackListener;
	private String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();

	public interface ContentVoteOnBack {
		void onBack();
	}

	public void setInfo(Bills billInfo, boolean isShowBack) {
		bill = billInfo;
		showBackBtn = isShowBack;
	}

	private void initOptions() {
		voteOptions[0] = getString(R.string.zc_fd);
		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);
		LogUtil.d(TAG, "options:" + options.toString());
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		LogUtil.d(TAG, "onCreateView");
		View view = inflater.inflate(R.layout.fragment_multi_content_detail, container, false);
		initOptions();
		tvTitle =  view.findViewById(R.id.content_detail_title);
		pdfView =  view.findViewById(R.id.content_detail_pdfview);
		panelVote =  view.findViewById(R.id.content_detail_pannal_vote);
		panelModify =  view.findViewById(R.id.content_detail_pannal_modify);
		tvTips =  view.findViewById(R.id.content_detail_tv_tips);
		ivReuslt =  view.findViewById(R.id.content_detail_tv_result);
		btnLookPdf=  view.findViewById(R.id.btn_look_pdf);
		btnA =  view.findViewById(R.id.content_detail_btnA);
		btnB =  view.findViewById(R.id.content_detail_btnB);
		btnC =  view.findViewById(R.id.content_detail_btnC);
		ImageButton btnBack =  view.findViewById(R.id.content_detail_btnback);
		Button btnModify =  view.findViewById(R.id.content_detail_btn_modify);
		confirmLayout =  view.findViewById(R.id.content_detail_confirm_panel);
		tvConfirmText = view.findViewById(R.id.content_detail_confirm_textview);
		Button btnConfirmCancel =  view.findViewById(R.id.content_detail_btn_confirm_cancel);
		Button btnConfirmOK =  view.findViewById(R.id.content_detail_btn_confirm_ok);

		if (bill != null && bill.getTitle() != null) {
			tvTitle.setText(bill.getTitle());
		}

		filename = DATABASE_PATH + "/sunvote/" + DBManager.getInstance().getConfId() + "/" + bill.getBillFile();
		ivReuslt.setVisibility(View.INVISIBLE);

		new Handler().postDelayed(new Runnable(){
			public void run() {
				openPdfFile();
			}
		}, pdfDelayLoadTime);
		btnBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				goBack();
			}
		});

		if (!showBackBtn) {
			btnBack.setVisibility(View.INVISIBLE);
		}

		btnModify.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				panelModify.setVisibility(View.GONE);
				showVote();
				ivReuslt.setVisibility(View.VISIBLE);
			}
		});

		btnConfirmOK.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				hideConfirm();
				doVoteWithIndex(voteValue);
			}
		});

		btnConfirmCancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				hideConfirm();
			}
		});
		return view;
	}

	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);
		btnA.setVisibility(View.INVISIBLE);
		btnB.setVisibility(View.INVISIBLE);
		btnC.setVisibility(View.INVISIBLE);
	}

	private void hideConfirm() {
		confirmLayout.setVisibility(View.GONE);
		btnA.setVisibility(View.VISIBLE);
		if (options.length == 3) {
			btnB.setVisibility(View.VISIBLE);
		}
		btnC.setVisibility(View.VISIBLE);
	}

	@Override
	public void onResume() {
		super.onResume();
//		if (voteInfo != null) {
			btnA.setVisibility(View.VISIBLE);
			btnB.setVisibility(View.VISIBLE);
			btnC.setVisibility(View.VISIBLE);
			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]);
			}
			showVote();
			showResult();
			checkIsVoted();
//		} else {
//			hideVote();
//		}
	}

	private void doVoteWithIndex(int index) {

	}

	// private int result;

	private void showResult() {
	}

	private void goBack() {
		PageManager.getInstance().popStack();
	}

	public void hideVote() {
		panelVote.setVisibility(View.GONE);
		panelModify.setVisibility(View.GONE);
	}

	public void showVote() {
		panelVote.setVisibility(View.VISIBLE);
		enableVote();
	}

	public void disableVote() {
		btnA.setEnabled(false);
		btnB.setEnabled(false);
		btnC.setEnabled(false);
	}

	public void enableVote() {
		btnA.setEnabled(true);
		btnB.setEnabled(true);
		btnC.setEnabled(true);
	}

	private void showModify() {
		panelModify.setVisibility(View.VISIBLE);
		tvTips.setText(getString(R.string.submited));
	}


	private  void showModifyOrDisable(){

	}
	public void onVoteSubmitSuccess() {
		tvTips.setText(getString(R.string.submited));
		showModifyOrDisable();
		saveVoteInfo();
	}


	private void saveVoteInfo(){
	}

	private void checkIsVoted(){

	}

	private void openPdfFile(){
		File file=new File(filename);
		if (file.exists()) {
			pdfView.fromFile(file)
					.enableSwipe(true) // allows to block changing pages using swipe
					.swipeHorizontal(false)
					.enableDoubletap(true)
					.defaultPage(0)
					.onRender(new OnRenderListener() {
						@Override
						public void onInitiallyRendered(int nbPages, float pageWidth, float pageHeight) {
							pdfView.fitToWidth(pdfPageNum-1);
						}
					})
					.enableAnnotationRendering(false)
					.password(null)
					.scrollHandle(null)
					.enableAntialiasing(true)
					.spacing(0)
					.load();
		}else {
			Toast.makeText(getActivity(),"议案文件未找到",Toast.LENGTH_LONG).show();
		}
	}
}