com.semoncat.geach.core.BaseGameFragment Maven / Gradle / Ivy
package com.semoncat.geach.core;
import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.semoncat.geach.core.bean.ScoreResult;
import com.semoncat.geach.core.bean.Topic;
import com.semoncat.geach.core.bean.VoteResult;
/**
* Created by SemonCat on 2014/7/3.
*/
public abstract class BaseGameFragment extends Fragment{
public enum GameType{
Game,Vote
}
public interface OnGameEventListener{
public void OnGameOver(ScoreResult gameScore);
public void OnVoteOver(VoteResult gameScore);
}
private static final String TAG = BaseGameFragment.class.getName();
private Context mContext;
private OnGameEventListener mListener;
private Topic mGameResource;
public BaseGameFragment(Context mContext) {
this.mContext = mContext;
mGameResource = new Topic();
testDataInject(mGameResource);
InjectGeachGameResource(mGameResource);
}
protected Context getContext(){
return mContext;
}
protected Topic getGeachGameResource(){
if (mGameResource==null)
throw new NullPointerException("Please wait for Resource Inject.");
return mGameResource;
}
protected void InjectGeachGameResource(Topic bundle){
this.mGameResource = bundle;
}
public abstract void OnGameStart();
protected void OnGameOver(ScoreResult gameScore){
if (mListener!=null){
mListener.OnGameOver(gameScore);
}
}
protected void OnVoteOver(VoteResult voteResult){
if (mListener!=null){
mListener.OnVoteOver(voteResult);
}
}
protected void testDataInject(Topic GameResource){
}
protected abstract void setupView(View rootView);
protected abstract void setupAdapter();
protected abstract void setupEvent();
protected abstract int setupLayout();
protected abstract GameType setupGameType();
public GameType getGameType(){
return setupGameType();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (setupLayout()==0){
throw new IllegalArgumentException("You must overwrite setupLayout() method.");
}
LayoutInflater context_inflater=(LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rootView = context_inflater.inflate(setupLayout(), container, false);
setupView(rootView);
setupAdapter();
setupEvent();
return rootView;
}
protected void finish(){
if (getFragmentManager()!=null){
getFragmentManager().beginTransaction().remove(this).commit();
}
}
protected SharedPreferences getSharedPreferences(){
return PreferenceManager.getDefaultSharedPreferences(getContext());
}
Toast mToast;
protected void showToast(String Message){
if (mToast==null){
mToast = Toast.makeText(getActivity(),Message,Toast.LENGTH_SHORT);
}else{
mToast.setText(Message);
}
mToast.show();
}
public void setGameEventListener(OnGameEventListener mListener) {
this.mListener = mListener;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy