SigninFragment.java 3.58 KB
package com.sunvote.xpadapp.fragments;

import com.sunvote.xpadapp.MainActivity;
import com.sunvote.xpadapp.R;
import com.sunvote.xpadapp.base.BaseFragment;
import com.sunvote.xpadapp.db.DBManager;
import com.sunvote.xpadapp.utils.SharedPreferencesUtil;
import com.sunvote.xpadcomm.XPadApi;
import com.sunvote.xpadcomm.XPadApiInterface.VoteInfo;

import android.app.Fragment;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class SigninFragment extends BaseFragment {
	private TextView tv;
	private Button btnSignin;
	private VoteInfo voteInfo;
	View bgView;
	
	public void setInfo(VoteInfo info) {
		voteInfo = info;
	}
	 
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		Log.d("SigninFragment", "onCreateView");
		View view = inflater.inflate(R.layout.fragment_signin, container, false);
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				;
			}
		});
		bgView = view.findViewById(R.id.signin_bg);

		tv = (TextView)view.findViewById(R.id.signin_title);
		btnSignin = (Button)view.findViewById(R.id.signin_btnSign);
		btnSignin.setEnabled(false);
		btnSignin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tv.setText(getString(R.string.signining));
				 MainActivity mact = (MainActivity)getActivity();
				 mact.presenter.submitVote( XPadApi.AnsType_Single,"1");
				 btnSignin.setEnabled(false);
				 
			}
		});
		 
		if(voteInfo!=null){
			onVoteEvent(voteInfo);
		}
		checkIsVoted();
		return view;
	}
	
	@Override 
	public void onVoteEvent( VoteInfo info) {
		 if(info.mode ==XPadApi.VoteType_Signin){
			 btnSignin.setEnabled(true);
			 
		 }else if(info.mode == XPadApi.VoteType_Stop){
			 btnSignin.setEnabled(false);
			 
		 }
	}

	/**
	 * 0:已签到,1:未签到
	 */
	private void saveVoteInfo(String voteValue){
		SharedPreferencesUtil.saveData(getActivity(),DATAPOS,Integer.valueOf(voteInfo.dataPos));
		SharedPreferencesUtil.saveData(getActivity(),VOTEMODE,Integer.valueOf(voteInfo.mode));
		SharedPreferencesUtil.saveData(getActivity(),VOTEVALUES,voteValue);
	}

	private void checkIsVoted(){
		Integer datapos = (Integer) SharedPreferencesUtil.getData(getActivity(),DATAPOS,Integer.valueOf(-1));
		Integer votemode = (Integer) SharedPreferencesUtil.getData(getActivity(),VOTEMODE,Integer.valueOf(-1));
		String voteValues = (String) SharedPreferencesUtil.getData(getActivity(),VOTEVALUES,"");

		if(datapos.intValue() == voteInfo.dataPos && votemode.intValue() == voteInfo.mode && voteValues.length()>0){
			if(voteValues.equals("0")){
				tv.setText(getString(R.string.signined));
				btnSignin.setEnabled(false);
				((MainActivity)getActivity()).myHandler.sendEmptyMessageDelayed(MainActivity.MSG_DELAY_TO_VIEW,2000);
			}else{
				tv.setText(getString(R.string.please_signin));
				btnSignin.setEnabled(true);
			}
		}
	}

	@Override
	public void onVoteSubmitSuccess() {
		tv.setText(getString(R.string.signined));
		btnSignin.setEnabled(false);
		saveVoteInfo("0");
	//	bgView.setBackgroundColor(0xFF009966);
 
		((MainActivity)getActivity()).myHandler.sendEmptyMessageDelayed(MainActivity.MSG_DELAY_TO_VIEW,2000);
	}
	@Override
	public void onVoteSubmitError() {
		super.onVoteSubmitError();
		tv.setText(getString(R.string.please_signin));
		btnSignin.setEnabled(true);
		saveVoteInfo("1");
	}
	
}