start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made hedgeroid
authorXeli
Thu, 11 Aug 2011 14:59:14 +0200
branchhedgeroid
changeset 5542 141c12a23787
parent 5540 76392a62da2f
child 5544 f72a87bfa655
start of implementation of the save button, check if there's something to save and update TeamSelectionActivity if a change has been made
project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/TeamCreatorActivity.java
project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/TeamSelectionActivity.java
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/TeamCreatorActivity.java	Thu Aug 11 14:56:31 2011 +0200
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/TeamCreatorActivity.java	Thu Aug 11 14:59:14 2011 +0200
@@ -17,6 +17,7 @@
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
+import android.view.View.OnFocusChangeListener;
 import android.widget.AdapterView;
 import android.widget.AdapterView.OnItemSelectedListener;
 import android.widget.ArrayAdapter;
@@ -42,6 +43,8 @@
 	private ScrollView scroller;
 	private MediaPlayer mp = null;
 	private ArrayList<RelativeLayout> hogs;
+	private boolean settingsChanged = false;
+	private boolean saved = false;
 
 	public void onCreate(Bundle savedInstanceState){
 		super.onCreate(savedInstanceState);
@@ -59,16 +62,17 @@
 		back = (ImageButton) findViewById(R.id.btnBack);
 		save = (ImageButton) findViewById(R.id.btnSave);
 		voiceButton = (ImageButton) findViewById(R.id.btnPlay);
-		
+
 		scroller = (ScrollView) findViewById(R.id.scroller);
 
 		save.setOnClickListener(saveClicker);
-		
+		back.setOnClickListener(backClicker);
+
 		LinearLayout ll = (LinearLayout) findViewById(R.id.HogsContainer);
 		hogs = new ArrayList<RelativeLayout>(ll.getChildCount());
 		for(int i = 0; i < ll.getChildCount(); i++){
 			RelativeLayout team_creation_entry = (RelativeLayout) ll.getChildAt(i);
-			
+
 			hogHat.add((Spinner)team_creation_entry.findViewById(R.id.spinTeam1));
 			hogDice.add((ImageButton)team_creation_entry.findViewById(R.id.btnTeam1));
 			hogName.add((EditText)team_creation_entry.findViewById(R.id.txtTeam1));
@@ -77,16 +81,19 @@
 		SimpleAdapter sa = new SimpleAdapter(this, gravesData, R.layout.spinner_textimg_entry, new String[]{"txt", "img"}, new int[]{R.id.spinner_txt, R.id.spinner_img});
 		sa.setViewBinder(viewBinder);
 		grave.setAdapter(sa);
+		grave.setOnFocusChangeListener(focusser);
 
 		ArrayList<HashMap<String, ?>> flagsData = FrontendDataUtils.getFlags(this);
 		sa = new SimpleAdapter(this, flagsData, R.layout.spinner_textimg_entry, new String[]{"txt", "img"}, new int[]{R.id.spinner_txt, R.id.spinner_img});
 		sa.setViewBinder(viewBinder);
 		flag.setAdapter(sa);
-		
+		flag.setOnFocusChangeListener(focusser);
+
 		ArrayList<HashMap<String, ?>> typesData = FrontendDataUtils.getTypes(this);
 		sa = new SimpleAdapter(this, typesData, R.layout.spinner_textimg_entry, new String[]{"txt", "img"}, new int[]{R.id.spinner_txt, R.id.spinner_img});
 		difficulty.setAdapter(sa);
-		
+		difficulty.setOnFocusChangeListener(focusser);
+
 		ArrayList<HashMap<String, ?>> hatsData = FrontendDataUtils.getHats(this);
 		sa = new SimpleAdapter(this, hatsData, R.layout.spinner_textimg_entry, new String[]{"txt", "img"}, new int[]{R.id.spinner_txt, R.id.spinner_img});
 		sa.setViewBinder(viewBinder);
@@ -96,21 +103,23 @@
 
 		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, FrontendDataUtils.getVoices(this));
 		voice.setAdapter(adapter);
+		voice.setOnFocusChangeListener(focusser);
 		voiceButton.setOnClickListener(voiceClicker);
-		
+
 		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, FrontendDataUtils.getForts(this));
 		fort.setAdapter(adapter);
 		fort.setOnItemSelectedListener(fortSelector);
-		
-		
+		fort.setOnFocusChangeListener(focusser);
+
 		Team t = this.getIntent().getParcelableExtra("team");
 		if(t != null){
 			name.setText(t.name);
 			int position = ((ArrayAdapter<String>)voice.getAdapter()).getPosition(t.voice);
 			voice.setSelection(position);
+
 			position = ((ArrayAdapter<String>)fort.getAdapter()).getPosition(t.fort);
 			fort.setSelection(position);
-			
+
 			position = 0;
 			for(HashMap<String, ?> hashmap : typesData){
 				if(hashmap.get("txt").equals(t.levels[0])){
@@ -118,7 +127,7 @@
 					break;
 				}
 			}
-			
+
 			position = 0;
 			for(HashMap<String, ?> hashmap : gravesData){
 				if(hashmap.get("txt").equals(t.grave)){
@@ -126,7 +135,7 @@
 					break;
 				}
 			}
-			
+
 			position = 0;
 			for(HashMap<String, ?> hashmap : typesData){
 				if(hashmap.get("txt").equals(t.flag)){
@@ -134,7 +143,7 @@
 					break;
 				}
 			}
-			
+
 			for(int i = 0; i < Team.maxNumberOfHogs; i++){
 				position = 0;
 				for(HashMap<String, ?> hashmap : hatsData){
@@ -142,7 +151,7 @@
 						hogHat.get(i).setSelection(position);
 					}
 				}
-				
+
 				hogName.get(i).setText(t.hogNames[i]);
 			}
 		}
@@ -155,20 +164,49 @@
 			mp = null;
 		}
 	}
+
+	private OnFocusChangeListener focusser = new OnFocusChangeListener(){
+		public void onFocusChange(View v, boolean hasFocus) {
+			settingsChanged = true;
+		}
+		
+	};
 	
+	public void onBackPressed(){
+		onFinishing();
+		super.onBackPressed();
+		
+	}
+	
+	private OnClickListener backClicker = new OnClickListener(){
+		public void onClick(View v){
+			onFinishing();
+			finish();
+		}
+	};
+
+	private void onFinishing(){
+		if(settingsChanged){
+			setResult(RESULT_OK);
+		}else{
+			setResult(RESULT_CANCELED);
+		}
+	}
+
 	private OnClickListener saveClicker = new OnClickListener(){
 		public void onClick(View v) {
+			saved = true;
 			Team team = new Team();
 			team.name = name.getText().toString();
 			HashMap<String, Object> hashmap = (HashMap<String, Object>) flag.getSelectedItem();
-			
+
 			team.flag = (String)hashmap.get("txt");
 			team.fort = fort.getSelectedItem().toString();
 			hashmap = (HashMap<String, Object>)grave.getSelectedItem();
 			team.grave = hashmap.get("txt").toString();
 			team.hash = "0";
 			team.voice = voice.getSelectedItem().toString();
-			
+
 			hashmap =  ((HashMap<String, Object>)difficulty.getSelectedItem());
 			String levelString = hashmap.get("txt").toString();
 			int levelInt;
@@ -185,7 +223,7 @@
 			}else {
 				levelInt = 5;
 			}
-			
+
 			for(int i = 0; i < hogName.size(); i++){
 				team.hogNames[i] = hogName.get(i).getText().toString();
 				hashmap = (HashMap<String, Object>)hogHat.get(i).getSelectedItem();
@@ -201,12 +239,13 @@
 				e.printStackTrace();
 			}
 		}
-		
+
 	};
-	
+
 	private OnItemSelectedListener fortSelector = new OnItemSelectedListener(){
 		@SuppressWarnings("unchecked")
 		public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
+			settingsChanged = true;
 			String fortName = (String) arg0.getAdapter().getItem(position);
 			Drawable fortIconDrawable = Drawable.createFromPath(Utils.getDownloadPath(TeamCreatorActivity.this) + "Forts/" + fortName + "L.png");
 			imgFort.setImageDrawable(fortIconDrawable);
@@ -226,7 +265,7 @@
 				File[] dirs = dir.listFiles();
 				File f = dirs[(int)Math.round(Math.random()*dirs.length)];
 				if(f.getName().endsWith(".ogg"))file = f.getAbsolutePath();
-				
+
 				if(mp == null) mp = new MediaPlayer();
 				else mp.reset();
 				mp.setDataSource(file);
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/TeamSelectionActivity.java	Thu Aug 11 14:56:31 2011 +0200
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/TeamSelectionActivity.java	Thu Aug 11 14:59:14 2011 +0200
@@ -27,6 +27,8 @@
 
 public class TeamSelectionActivity extends Activity{
 
+	private static final int ACTIVITY_TEAMCREATION = 0;
+	
 	private ImageButton addTeam, back;
 	private ListView availableTeams, selectedTeams;
 	private ArrayList<HashMap<String, ?>> availableTeamsList, selectedTeamsList;
@@ -86,6 +88,35 @@
 		}
 	};
 
+	public void onActivityResult(int requestCode, int resultCode, Intent data){
+		if(requestCode == ACTIVITY_TEAMCREATION){
+			if(resultCode == Activity.RESULT_OK){
+				updateListViews();
+			}
+		}else{
+			super.onActivityResult(requestCode, resultCode, data);
+		}
+	}
+	
+	private void updateListViews(){
+		unregisterForContextMenu(availableTeams);
+		availableTeamsList = FrontendDataUtils.getTeams(this);
+		for(HashMap<String, ?> hashmap : selectedTeamsList){
+			String name = (String)hashmap.get("txt");
+			for(HashMap<String, ?> hash : availableTeamsList){
+				if(name.equals((String)hash.get("txt"))){
+					availableTeamsList.remove(hash);
+				}
+			}
+		}
+		SimpleAdapter adapter = new SimpleAdapter(this, availableTeamsList, R.layout.team_selection_entry, new String[]{"txt", "img"}, new int[]{R.id.txtName, R.id.imgDifficulty});
+		availableTeams.setAdapter(adapter);
+		registerForContextMenu(availableTeams);
+		availableTeams.setOnItemClickListener(availableClicker);
+		
+		
+	}
+	
 	private void setTeamColor(int position, int color){
 		View iv = ((RelativeLayout)selectedTeams.getChildAt(position)).findViewById(R.id.teamCount);
 		setTeamColor(iv, color);
@@ -142,8 +173,7 @@
 
 	private OnClickListener addTeamClicker = new OnClickListener(){
 		public void onClick(View v) {
-			startActivity(new Intent(TeamSelectionActivity.this, TeamCreatorActivity.class));
-
+			startActivityForResult(new Intent(TeamSelectionActivity.this, TeamCreatorActivity.class), ACTIVITY_TEAMCREATION);
 		}
 	};
 
@@ -192,7 +222,7 @@
 			Intent i = new Intent(TeamSelectionActivity.this, TeamCreatorActivity.class);
 			Team t = (Team)availableTeamsList.get(position).get("team");
 			i.putExtra("team", t);
-			startActivity(i);
+			startActivityForResult(i, ACTIVITY_TEAMCREATION);
 			return true;
 		}
 		return false;