StarterNameActivity code
This commit is contained in:
		| @ -9,7 +9,9 @@ | ||||
|         android:roundIcon="@mipmap/ic_launcher_round" | ||||
|         android:supportsRtl="true" | ||||
|         android:theme="@style/AppTheme" > | ||||
|         <activity android:name=".StarterNameActivity" > | ||||
|         <activity | ||||
|             android:name=".StarterNameActivity" | ||||
|             android:windowSoftInputMode="adjustResize" > | ||||
|             <intent-filter> | ||||
|                 <action android:name="android.intent.action.MAIN" /> | ||||
|  | ||||
|  | ||||
| @ -1,12 +1,76 @@ | ||||
| package adrienmalin.pingpoints | ||||
|  | ||||
| import android.content.Context | ||||
| import android.content.SharedPreferences | ||||
| import android.support.v7.app.AppCompatActivity | ||||
| import android.os.Bundle | ||||
| import android.view.View | ||||
| import android.widget.* | ||||
|  | ||||
|  | ||||
| class StarterNameActivity : AppCompatActivity() { | ||||
|     var player1NameInput: AutoCompleteTextView = null | ||||
|     var player2NameInput: AutoCompleteTextView = null | ||||
|     var starterRadioGroup: RadioGroup = null | ||||
|     var previousMatch: SharedPreferences = null | ||||
|     var previousPlayers: Set<String> = emptySet() | ||||
|  | ||||
|     override fun onCreate(savedInstanceState: Bundle?) { | ||||
|         super.onCreate(savedInstanceState) | ||||
|         setContentView(R.layout.activity_starter_name) | ||||
|  | ||||
|         previousMatch = getPreferences(Context.MODE_PRIVATE) | ||||
|         previousPlayers = previousMatch.getStringSet("previousPlayers", emptySet()) | ||||
|         val previousPlayersAdapter = ArrayAdapter<String>( | ||||
|             this, | ||||
|             R.layout.activity_starter_name, | ||||
|             previousPlayers.toList()) | ||||
|  | ||||
|         player1NameInput = findViewById<AutoCompleteTextView>(R.id.player1Name) | ||||
|         player1NameInput?.run { | ||||
|             setText( | ||||
|                 previousMatch.getString( | ||||
|                     "previousPlayer2", | ||||
|                     getString(R.string.player_1_default_name)), | ||||
|                 TextView.BufferType.EDITABLE) | ||||
|             setAdapter(previousPlayersAdapter) | ||||
|             threshold = 1 | ||||
|         } | ||||
|  | ||||
|         player2NameInput = findViewById<AutoCompleteTextView>(R.id.player2Name) | ||||
|         player2NameInput?.run{ | ||||
|             setText( | ||||
|                 previousMatch.getString( | ||||
|                     "previousPlayer1", | ||||
|                     getString(R.string.player_2_default_name)), | ||||
|                 TextView.BufferType.EDITABLE) | ||||
|             setAdapter(previousPlayersAdapter) | ||||
|             threshold = 1 | ||||
|         } | ||||
|  | ||||
|         starterRadioGroup = findViewById<RadioGroup>(R.id.starterRadioGroup) | ||||
|         starterRadioGroup?.check(previousMatch.getInt("previousStarterId", 0)) | ||||
|     } | ||||
|  | ||||
|     fun swapNames(view: View) { | ||||
|         player1NameInput?.text = player2NameInput?.text.also { | ||||
|             player2NameInput?.text = player1NameInput?.text | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     fun startMatch(view: View) { | ||||
|         val player1Name = player1NameInput?.text.toString() | ||||
|         val player2Name = player2NameInput?.text.toString() | ||||
|  | ||||
|         // Save | ||||
|         previousMatch.edit().run{ | ||||
|             putString("previousPlayer1", player1Name) | ||||
|             putString("previousPlayer2", player2Name) | ||||
|             putInt("previousStarterId", starterRadioGroup?.checkedRadioButtonId) | ||||
|             putStringSet( | ||||
|                 "previousPlayers", | ||||
|                 previousPlayers.plus(player1Name).plus(player2Name)) | ||||
|             commit() | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
							
								
								
									
										5
									
								
								app/src/main/res/drawable/ic_swap_horiz.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								app/src/main/res/drawable/ic_swap_horiz.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,5 @@ | ||||
| <vector android:height="24dp" android:tint="#FFFFFF" | ||||
|     android:viewportHeight="24.0" android:viewportWidth="24.0" | ||||
|     android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||||
|     <path android:fillColor="#FF000000" android:pathData="M6.99,11L3,15l3.99,4v-3H14v-2H6.99v-3zM21,9l-3.99,-4v3H10v2h7.01v3L21,9z"/> | ||||
| </vector> | ||||
							
								
								
									
										5
									
								
								app/src/main/res/drawable/ic_swap_vert.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								app/src/main/res/drawable/ic_swap_vert.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,5 @@ | ||||
| <vector android:height="24dp" android:tint="#FFFFFF" | ||||
|     android:viewportHeight="24.0" android:viewportWidth="24.0" | ||||
|     android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||||
|     <path android:fillColor="#FF000000" android:pathData="M16,17.01V10h-2v7.01h-3L15,21l4,-3.99h-3zM9,3L5,6.99h3V14h2V6.99h3L9,3z"/> | ||||
| </vector> | ||||
| @ -20,15 +20,16 @@ | ||||
|             android:layout_width="wrap_content" | ||||
|             android:layout_height="wrap_content" | ||||
|             android:id="@+id/button2" | ||||
|             android:drawableLeft="@drawable/ic_new_match" android:layout_marginBottom="8dp" | ||||
|             android:layout_marginBottom="8dp" | ||||
|             app:layout_constraintBottom_toBottomOf="parent" android:layout_marginEnd="8dp" | ||||
|             app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="8dp" | ||||
|             style="@style/Widget.AppCompat.Button.Colored"/> | ||||
|             style="@style/Widget.AppCompat.Button.Colored" android:drawableStart="@drawable/ic_new_match" | ||||
|             android:onClick="startMatch"/> | ||||
|     <RadioGroup | ||||
|             android:layout_width="0dp" | ||||
|             android:layout_height="wrap_content" | ||||
|             android:layout_marginTop="8dp" | ||||
|             app:layout_constraintTop_toBottomOf="@+id/WhoStarts" android:id="@+id/radioGroup" | ||||
|             app:layout_constraintTop_toBottomOf="@+id/WhoStarts" android:id="@+id/starterRadioGroup" | ||||
|             android:layout_marginStart="8dp" app:layout_constraintStart_toStartOf="parent" | ||||
|             android:layout_marginLeft="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp" | ||||
|             android:layout_marginRight="8dp" android:orientation="horizontal"> | ||||
| @ -43,6 +44,11 @@ | ||||
|                 android:id="@+id/player1Name" | ||||
|                 tools:layout_editor_absoluteX="143dp" android:hint="@string/player_name_hint" | ||||
|                 android:layout_weight="1"/> | ||||
|         <ImageButton | ||||
|                 android:layout_width="wrap_content" | ||||
|                 android:layout_height="wrap_content" app:srcCompat="@drawable/ic_swap_horiz" | ||||
|                 android:layout_weight="0" android:id="@+id/swapNamesButton" | ||||
|                 android:contentDescription="@string/swap_names" android:onClick="swapNames"/> | ||||
|         <RadioButton | ||||
|                 android:layout_width="wrap_content" | ||||
|                 android:layout_height="wrap_content" tools:layout_editor_absoluteY="93dp" | ||||
| @ -55,21 +61,21 @@ | ||||
|                 android:hint="@string/player_name_hint" android:layout_height="wrap_content" android:layout_weight="1"/> | ||||
|     </RadioGroup> | ||||
|     <Switch | ||||
|             android:text="Switch" | ||||
|             android:text="@string/TTS" | ||||
|             android:layout_width="0dp" | ||||
|             android:layout_height="wrap_content" | ||||
|             android:id="@+id/enableTTS" tools:text="@string/TTS" | ||||
|             android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/radioGroup" | ||||
|             android:id="@+id/enableTTS" | ||||
|             android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/starterRadioGroup" | ||||
|             android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="8dp" | ||||
|             android:layout_marginStart="8dp" app:layout_constraintStart_toStartOf="parent" | ||||
|             android:layout_marginLeft="8dp"/> | ||||
|     <Switch | ||||
|             android:text="Switch" | ||||
|             android:text="@string/STT" | ||||
|             android:layout_width="0dp" | ||||
|             android:layout_height="wrap_content" | ||||
|             android:id="@+id/enableSTT" android:layout_marginTop="8dp" | ||||
|             app:layout_constraintTop_toBottomOf="@+id/enableTTS" app:layout_constraintEnd_toEndOf="parent" | ||||
|             android:layout_marginEnd="8dp" android:layout_marginRight="8dp" | ||||
|             app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="8dp" | ||||
|             android:layout_marginStart="8dp" tools:text="@string/STT"/> | ||||
|             android:layout_marginStart="8dp"/> | ||||
| </android.support.constraint.ConstraintLayout> | ||||
| @ -23,25 +23,27 @@ | ||||
|             app:layout_constraintTop_toBottomOf="@+id/WhoStarts" | ||||
|             app:layout_constraintStart_toStartOf="parent" | ||||
|             android:layout_marginLeft="8dp" android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent" | ||||
|             android:layout_marginEnd="8dp" android:layout_marginRight="8dp" android:id="@+id/linearLayout"> | ||||
|             android:layout_marginEnd="8dp" android:layout_marginRight="8dp" android:id="@+id/linearLayout" | ||||
|             android:layout_margin="8dp"> | ||||
|         <RadioGroup | ||||
|                 android:layout_width="wrap_content" | ||||
|                 android:layout_height="match_parent" | ||||
|                 android:layout_marginTop="8dp" | ||||
|                 app:layout_constraintTop_toBottomOf="@+id/WhoStarts" android:id="@+id/radioGroup" | ||||
|                 app:layout_constraintTop_toBottomOf="@+id/WhoStarts" android:id="@+id/starterRadioGroup" | ||||
|                 android:layout_marginStart="8dp" app:layout_constraintStart_toStartOf="parent" | ||||
|                 android:layout_marginLeft="8dp" app:layout_constraintEnd_toEndOf="parent" | ||||
|                 android:layout_marginEnd="8dp" | ||||
|                 android:layout_marginRight="8dp"> | ||||
|                 android:layout_marginRight="8dp" android:layout_marginBottom="8dp"> | ||||
|             <RadioButton | ||||
|                     android:layout_width="wrap_content" | ||||
|                     android:layout_height="wrap_content" tools:layout_editor_absoluteY="93dp" | ||||
|                     tools:layout_editor_absoluteX="16dp" android:id="@+id/radioPlayer1Starts" | ||||
|                     android:layout_weight="1"/> | ||||
|                     android:layout_weight="1" android:gravity="top"/> | ||||
|             <RadioButton | ||||
|                     android:layout_height="wrap_content" tools:layout_editor_absoluteY="93dp" | ||||
|                     tools:layout_editor_absoluteX="16dp" android:id="@+id/radioPlayer2Starts" | ||||
|                     android:layout_width="wrap_content" android:layout_weight="1"/> | ||||
|                     android:layout_width="wrap_content" android:layout_weight="1" | ||||
|                     android:gravity="bottom"/> | ||||
|         </RadioGroup> | ||||
|         <LinearLayout | ||||
|                 android:orientation="vertical" | ||||
| @ -54,6 +56,11 @@ | ||||
|                     android:id="@+id/player1Name" | ||||
|                     tools:layout_editor_absoluteX="143dp" android:hint="@string/player_name_hint" | ||||
|                     android:selectAllOnFocus="true"/> | ||||
|             <ImageButton | ||||
|                     android:layout_width="match_parent" | ||||
|                     android:layout_height="wrap_content" app:srcCompat="@drawable/ic_swap_vert" | ||||
|                     android:id="@+id/swapNamesButton" android:contentDescription="@string/swap_names" | ||||
|                     android:onClick="swapNames"/> | ||||
|             <AutoCompleteTextView | ||||
|                     android:text="@string/player_2_default_name" | ||||
|                     tools:layout_editor_absoluteX="136dp" android:id="@+id/player2Name" | ||||
| @ -87,5 +94,5 @@ | ||||
|             style="@style/Widget.AppCompat.Button.Colored" | ||||
|             android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="8dp" | ||||
|             android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="parent" | ||||
|             android:drawableStart="@drawable/ic_new_match"/> | ||||
|             android:drawableStart="@drawable/ic_new_match" android:onClick="startMatch"/> | ||||
| </android.support.constraint.ConstraintLayout> | ||||
| @ -8,4 +8,5 @@ | ||||
|     <string name="TTS">Synthèse vocale</string> | ||||
|     <string name="STT">Reconnaissance vocale</string> | ||||
|     <string name="start">Allons-y !</string> | ||||
|     <string name="swap_names">Échanger les noms</string> | ||||
| </resources> | ||||
| @ -7,4 +7,5 @@ | ||||
|     <string name="TTS">Text to speech</string> | ||||
|     <string name="STT">Voice recognizer</string> | ||||
|     <string name="start">Let\'s go!</string> | ||||
|     <string name="swap_names">Swap names</string> | ||||
| </resources> | ||||
|  | ||||
		Reference in New Issue
	
	Block a user