Add icons, set layout, add end dialog
							
								
								
									
										
											BIN
										
									
								
								app/src/main/ic_launcher-web.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| After Width: | Height: | Size: 40 KiB | 
							
								
								
									
										35
									
								
								app/src/main/java/adrienmalin/pingpoints/EndOfMatchDialog.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						| @ -0,0 +1,35 @@ | ||||
| package adrienmalin.pingpoints | ||||
|  | ||||
| import android.content.DialogInterface | ||||
| import android.app.AlertDialog | ||||
| import android.app.Dialog | ||||
| import android.content.Intent | ||||
| import android.os.Bundle | ||||
| import android.support.v4.app.DialogFragment | ||||
|  | ||||
|  | ||||
| class EndOfMatchDialog: DialogFragment() { | ||||
|     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | ||||
|         val builder = AlertDialog.Builder(activity) | ||||
|         val winnerName = arguments?.getString("WINNER_NAME") | ||||
|         val winnerScore = arguments?.getInt("WINNER_SCORE") | ||||
|         val loserScore = arguments?.getInt("LOSER_SCORE") | ||||
|         builder.setTitle(getString(R.string.end_match_dialog_title, winnerName)) | ||||
|                 .setMessage(getString(R.string.score, winnerScore, loserScore)) | ||||
|                 .setPositiveButton( | ||||
|                         R.string.new_match_button, | ||||
|                         DialogInterface.OnClickListener { | ||||
|                             dialog, id -> startActivity(Intent(context, MainActivity::class.java)) | ||||
|                             activity?.finish() | ||||
|                         } | ||||
|                 ) | ||||
|                 .setNegativeButton( | ||||
|                         R.string.quit_button, | ||||
|                         DialogInterface.OnClickListener { | ||||
|                             dialog, id -> activity?.finish() | ||||
|                         } | ||||
|                 ) | ||||
|  | ||||
|         return builder.create() | ||||
|     } | ||||
| } | ||||
| @ -2,8 +2,24 @@ package adrienmalin.pingpoints | ||||
|  | ||||
| import android.support.v7.app.AppCompatActivity | ||||
| import android.os.Bundle | ||||
| import android.text.Html | ||||
| import android.view.View | ||||
| import android.widget.Button | ||||
| import android.os.Build | ||||
| import android.text.Spanned | ||||
| import android.text.TextUtils.join | ||||
| import kotlin.math.abs | ||||
|  | ||||
|  | ||||
| @SuppressWarnings("deprecation") | ||||
| fun fromHtml(html: String): Spanned { | ||||
|     return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | ||||
|         Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY) | ||||
|     } else { | ||||
|         Html.fromHtml(html) | ||||
|  | ||||
|     } | ||||
| } | ||||
|  | ||||
|  | ||||
| class MainActivity : AppCompatActivity() { | ||||
| @ -14,16 +30,12 @@ class MainActivity : AppCompatActivity() { | ||||
|     var server: Int = 0 | ||||
|     var notServer: Int = 1 | ||||
|  | ||||
|     var buttonPlayers: Array<Button> = emptyArray() | ||||
|     var serviceTexts: Array<Array<String>> = arrayOf( | ||||
|             arrayOf("""_o/°""", ""), | ||||
|             arrayOf("", """°\o_""") | ||||
|     ) | ||||
|  | ||||
|     var textScore: android.widget.TextView? = null | ||||
|     var textService: android.widget.TextView? = null | ||||
|     var stringScore:String = "" | ||||
|     var textService: android.widget.TextView? = null | ||||
|     var stringService:String = "" | ||||
|     var buttons: Array<Button> = emptyArray() | ||||
|  | ||||
|  | ||||
|     override fun onCreate(savedInstanceState: Bundle?) { | ||||
| @ -37,12 +49,7 @@ class MainActivity : AppCompatActivity() { | ||||
|  | ||||
|         textScore = findViewById(R.id.textScore) | ||||
|         textService = findViewById(R.id.textService) | ||||
|  | ||||
|         stringScore = getString(R.string.score) | ||||
|         stringService = getString(R.string.service) | ||||
|  | ||||
|  | ||||
|         buttonPlayers = arrayOf( | ||||
|         buttons = arrayOf( | ||||
|                 findViewById(R.id.buttonPlayer1), | ||||
|                 findViewById(R.id.buttonPlayer2) | ||||
|         ) | ||||
| @ -50,14 +57,23 @@ class MainActivity : AppCompatActivity() { | ||||
|         update_ui() | ||||
|     } | ||||
|  | ||||
|     fun updateScore(scoringPlayerId: Int) { | ||||
|         players[scoringPlayerId].score ++ | ||||
|     fun update_ui() { | ||||
|  | ||||
|         if (players.sumBy { it.score } % 2 == 0) { | ||||
|             server = notServer.also { notServer = server } | ||||
|         textScore?.text = getString(R.string.score, players[server].score, players[notServer].score) | ||||
|  | ||||
|         textService?.text = getString(R.string.service, players[server].name) | ||||
|  | ||||
|         for ((button, player) in buttons.zip(players)) { | ||||
|             button.text = fromHtml(getString(R.string.button_text, player.name, player.score)) | ||||
|         } | ||||
|  | ||||
|         update_ui() | ||||
|         if (server == 0) { | ||||
|             buttons[0].setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_button, 0, 0, 0) | ||||
|             buttons[1].setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0) | ||||
|         } else { | ||||
|             buttons[0].setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0) | ||||
|             buttons[1].setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_button, 0) | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     fun onClickPlayer1(view: View) { | ||||
| @ -68,27 +84,42 @@ class MainActivity : AppCompatActivity() { | ||||
|         updateScore(1) | ||||
|     } | ||||
|  | ||||
|     fun update_ui(){ | ||||
|     fun updateScore(scoringPlayerId: Int) { | ||||
|         players[scoringPlayerId].score++ | ||||
|  | ||||
|         textScore?.text = "$stringScore ${players[server].score} - ${players[notServer].score}" | ||||
|  | ||||
|         textService?.text = "$stringService ${players[server].name}" | ||||
|  | ||||
|         for ((player, serviceText) in players.zip(serviceTexts[server])) { | ||||
|             player.serviceText = serviceText | ||||
|         if (players.sumBy { it.score } % 2 == 0) { | ||||
|             server = notServer.also { notServer = server } | ||||
|         } | ||||
|  | ||||
|         for ((button, player) in buttonPlayers.zip(players)) { | ||||
|             button.text = """ | ||||
|                     |${player.name} | ||||
|                     |${player.score} | ||||
|                     |${player.serviceText}""".trimMargin() | ||||
|         update_ui() | ||||
|  | ||||
|         if ( | ||||
|                 (players.map { it -> it.score } .max() ?: 0 >= 11) and | ||||
|                 (abs(players[0].score - players[1].score) >= 2) | ||||
|         ) { | ||||
|             endOfMatch() | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     fun endOfMatch() { | ||||
|         val (loser, winner) = players.sortedBy { it.score } | ||||
|         var endOfMatchDialog: EndOfMatchDialog = EndOfMatchDialog() | ||||
|  | ||||
|         val bundle = Bundle() | ||||
|         bundle.putString("WINNER_NAME", winner.name) | ||||
|         bundle.putInt("WINNER_SCORE", winner.score) | ||||
|         bundle.putInt("LOSER_SCORE", loser.score) | ||||
|  | ||||
|         endOfMatchDialog.arguments = bundle | ||||
|         endOfMatchDialog.show( | ||||
|                 supportFragmentManager, | ||||
|                 join(" ", arrayOf(winner.name, winner.score.toString(), "-", loser.name, loser.score.toString())) | ||||
|         ) | ||||
|     } | ||||
|  | ||||
| } | ||||
|  | ||||
| class Player( | ||||
| data class Player( | ||||
|     var name: String = "", | ||||
|     var score: Int = 0, | ||||
|     var serviceText: String = "" | ||||
|  | ||||
| @ -7,7 +7,7 @@ import android.app.Dialog | ||||
| import android.content.DialogInterface | ||||
| 
 | ||||
| 
 | ||||
| class Dialog : DialogFragment() { | ||||
| class StarterNameDialog : DialogFragment() { | ||||
|     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | ||||
|         // Use the Builder class for convenient dialog construction | ||||
|         val builder = AlertDialog.Builder(activity) | ||||
| @ -19,7 +19,7 @@ class Dialog : DialogFragment() { | ||||
|                 .setNegativeButton(R.string.quit, DialogInterface.OnClickListener { dialog, id -> | ||||
|                     // User cancelled the dialog | ||||
|                 }) | ||||
|                 .setView(view) | ||||
|                 .setView(R.layout.dialog) | ||||
|         // Create the AlertDialog object and return it | ||||
|         return builder.create() | ||||
|     } | ||||
							
								
								
									
										
											BIN
										
									
								
								app/src/main/res/drawable-hdpi/ic_button.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| After Width: | Height: | Size: 1005 B | 
							
								
								
									
										
											BIN
										
									
								
								app/src/main/res/drawable-mdpi/ic_button.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| After Width: | Height: | Size: 665 B | 
							
								
								
									
										
											BIN
										
									
								
								app/src/main/res/drawable-xhdpi/ic_button.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| After Width: | Height: | Size: 1.4 KiB | 
							
								
								
									
										
											BIN
										
									
								
								app/src/main/res/drawable-xxhdpi/ic_button.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| After Width: | Height: | Size: 2.0 KiB | 
| @ -7,113 +7,116 @@ | ||||
|     tools:context=".MainActivity" | ||||
|     tools:layout_editor_absoluteY="73dp"> | ||||
|  | ||||
|     <TextView | ||||
|         android:id="@+id/textService" | ||||
|         android:layout_width="0dp" | ||||
|         android:layout_height="wrap_content" | ||||
|         android:layout_marginBottom="8dp" | ||||
|         android:layout_marginEnd="8dp" | ||||
|         android:layout_marginLeft="8dp" | ||||
|         android:layout_marginRight="8dp" | ||||
|         android:layout_marginStart="8dp" | ||||
|         android:layout_marginTop="8dp" | ||||
|         android:text="@string/service" | ||||
|         android:textAppearance="@android:style/TextAppearance.Material.Large" | ||||
|         app:layout_constraintBottom_toTopOf="@+id/linearLayout" | ||||
|         app:layout_constraintEnd_toEndOf="parent" | ||||
|         app:layout_constraintHorizontal_bias="1.0" | ||||
|         app:layout_constraintStart_toStartOf="parent" | ||||
|         app:layout_constraintTop_toBottomOf="@+id/textScore" /> | ||||
|  | ||||
|     <TextView | ||||
|         android:id="@+id/textScore" | ||||
|         android:layout_width="0dp" | ||||
|         android:layout_height="wrap_content" | ||||
|         android:layout_marginEnd="16dp" | ||||
|         android:layout_marginLeft="8dp" | ||||
|         android:layout_marginRight="16dp" | ||||
|         android:layout_marginStart="8dp" | ||||
|         android:layout_marginTop="8dp" | ||||
|         android:text="@string/score" | ||||
|         android:textAppearance="@android:style/TextAppearance.Material.Large" | ||||
|         app:layout_constraintEnd_toEndOf="parent" | ||||
|         app:layout_constraintHorizontal_bias="1.0" | ||||
|         app:layout_constraintStart_toStartOf="parent" | ||||
|         app:layout_constraintTop_toBottomOf="@+id/textView2" /> | ||||
|  | ||||
|     <TextView | ||||
|         android:id="@+id/textView2" | ||||
|         android:layout_width="0dp" | ||||
|         android:layout_height="18dp" | ||||
|         android:layout_marginEnd="8dp" | ||||
|         android:layout_marginLeft="8dp" | ||||
|         android:layout_marginRight="8dp" | ||||
|         android:layout_marginStart="8dp" | ||||
|         android:layout_marginTop="8dp" | ||||
|         android:text="@string/info" | ||||
|         app:layout_constraintEnd_toEndOf="parent" | ||||
|         app:layout_constraintStart_toStartOf="parent" | ||||
|         app:layout_constraintTop_toTopOf="parent" /> | ||||
|  | ||||
|     <LinearLayout | ||||
|         android:id="@+id/linearLayout" | ||||
|         android:layout_width="0dp" | ||||
|         android:layout_height="0dp" | ||||
|         android:layout_marginBottom="8dp" | ||||
|         android:layout_marginEnd="8dp" | ||||
|         android:layout_marginLeft="8dp" | ||||
|         android:layout_marginRight="8dp" | ||||
|         android:layout_marginStart="8dp" | ||||
|         android:layout_marginTop="8dp" | ||||
|         android:orientation="horizontal" | ||||
|         app:layout_constraintBottom_toBottomOf="parent" | ||||
|         app:layout_constraintEnd_toEndOf="parent" | ||||
|         app:layout_constraintStart_toStartOf="parent" | ||||
|         app:layout_constraintTop_toBottomOf="@+id/textService"> | ||||
|  | ||||
|         <Button | ||||
|             android:id="@+id/buttonPlayer1" | ||||
|             style="@style/Widget.AppCompat.Button.Colored" | ||||
|             android:layout_width="match_parent" | ||||
|             android:layout_height="match_parent" | ||||
|             android:layout_marginBottom="8dp" | ||||
|         <LinearLayout | ||||
|             android:id="@+id/linearLayoutText" | ||||
|             android:layout_width="0dp" | ||||
|             android:layout_height="wrap_content" | ||||
|             android:layout_marginEnd="8dp" | ||||
|             android:layout_marginLeft="8dp" | ||||
|             android:layout_marginRight="8dp" | ||||
|             android:layout_marginStart="8dp" | ||||
|             android:layout_marginTop="8dp" | ||||
|             android:layout_weight="1" | ||||
|             android:onClick="onClickPlayer1" | ||||
|             android:text="Button" | ||||
|             android:textAllCaps="false" | ||||
|             android:textSize="24sp" | ||||
|             android:textStyle="bold" | ||||
|             app:layout_constraintBottom_toTopOf="@+id/textScore" | ||||
|             app:layout_constraintEnd_toStartOf="@+id/buttonPlayer2" | ||||
|             app:layout_constraintHorizontal_chainStyle="spread_inside" | ||||
|             app:layout_constraintStart_toStartOf="parent" | ||||
|             app:layout_constraintTop_toBottomOf="@+id/textService" /> | ||||
|  | ||||
|         <Button | ||||
|             android:id="@+id/buttonPlayer2" | ||||
|             style="@style/Widget.AppCompat.Button.Colored" | ||||
|             android:layout_width="match_parent" | ||||
|             android:layout_height="match_parent" | ||||
|             android:layout_marginBottom="8dp" | ||||
|             android:layout_marginEnd="8dp" | ||||
|             android:layout_marginLeft="8dp" | ||||
|             android:layout_marginRight="8dp" | ||||
|             android:layout_marginStart="8dp" | ||||
|             android:layout_marginTop="8dp" | ||||
|             android:layout_weight="1" | ||||
|             android:onClick="onClickPlayer2" | ||||
|             android:text="Button" | ||||
|             android:textAllCaps="false" | ||||
|             android:textSize="24sp" | ||||
|             android:textStyle="bold" | ||||
|             app:layout_constraintBottom_toTopOf="@+id/textScore" | ||||
|             android:orientation="horizontal" | ||||
|             app:layout_constraintEnd_toEndOf="parent" | ||||
|             app:layout_constraintStart_toEndOf="@+id/buttonPlayer1" | ||||
|             app:layout_constraintTop_toBottomOf="@+id/textService" /> | ||||
|     </LinearLayout> | ||||
|             app:layout_constraintStart_toStartOf="parent" | ||||
|             app:layout_constraintTop_toTopOf="parent"> | ||||
|  | ||||
|             <TextView | ||||
|                 android:id="@+id/textScore" | ||||
|                 android:layout_width="match_parent" | ||||
|                 android:layout_height="wrap_content" | ||||
|                 android:layout_marginLeft="8dp" | ||||
|                 android:layout_marginStart="8dp" | ||||
|                 android:layout_weight="1" | ||||
|                 android:paddingLeft="16dp" | ||||
|                 android:paddingRight="16dp" | ||||
|                 android:text="@string/score" | ||||
|                 android:textAppearance="@style/TextAppearance.AppCompat.Large" | ||||
|                 app:layout_constraintStart_toStartOf="parent" | ||||
|                 app:layout_constraintTop_toTopOf="parent" /> | ||||
|  | ||||
|             <TextView | ||||
|                 android:id="@+id/textService" | ||||
|                 android:layout_width="match_parent" | ||||
|                 android:layout_height="wrap_content" | ||||
|                 android:layout_marginEnd="8dp" | ||||
|                 android:layout_marginStart="8dp" | ||||
|                 android:layout_weight="1" | ||||
|                 android:paddingLeft="16dp" | ||||
|                 android:paddingRight="16dp" | ||||
|                 android:text="@string/service" | ||||
|                 android:textAppearance="@style/TextAppearance.AppCompat.Large" | ||||
|                 app:layout_constraintBottom_toTopOf="@+id/linearLayoutButtons" | ||||
|                 app:layout_constraintEnd_toEndOf="parent" | ||||
|                 app:layout_constraintHorizontal_bias="1.0" | ||||
|                 app:layout_constraintStart_toEndOf="@+id/textScore" | ||||
|                 app:layout_constraintTop_toTopOf="parent" /> | ||||
|         </LinearLayout> | ||||
|  | ||||
|         <LinearLayout | ||||
|             android:id="@+id/linearLayoutButtons" | ||||
|             android:layout_width="0dp" | ||||
|             android:layout_height="0dp" | ||||
|             android:layout_marginBottom="8dp" | ||||
|             android:layout_marginEnd="8dp" | ||||
|             android:layout_marginLeft="8dp" | ||||
|             android:layout_marginRight="8dp" | ||||
|             android:layout_marginStart="8dp" | ||||
|             android:layout_marginTop="8dp" | ||||
|             android:orientation="horizontal" | ||||
|             app:layout_constraintBottom_toBottomOf="parent" | ||||
|             app:layout_constraintEnd_toEndOf="parent" | ||||
|             app:layout_constraintStart_toStartOf="parent" | ||||
|             app:layout_constraintTop_toBottomOf="@+id/linearLayoutText"> | ||||
|  | ||||
|             <Button | ||||
|                 android:id="@+id/buttonPlayer1" | ||||
|                 style="@style/Widget.AppCompat.Button.Colored" | ||||
|                 android:layout_width="match_parent" | ||||
|                 android:layout_height="match_parent" | ||||
|                 android:layout_marginBottom="8dp" | ||||
|                 android:layout_marginEnd="8dp" | ||||
|                 android:layout_marginLeft="8dp" | ||||
|                 android:layout_marginRight="8dp" | ||||
|                 android:layout_marginStart="8dp" | ||||
|                 android:layout_marginTop="8dp" | ||||
|                 android:layout_weight="1" | ||||
|                 android:bufferType="spannable" | ||||
|                 android:onClick="onClickPlayer1" | ||||
|                 android:text="Button" | ||||
|                 android:textAllCaps="false" | ||||
|                 android:textAppearance="@style/TextAppearance.AppCompat.Button" | ||||
|                 android:textSize="24sp" | ||||
|                 android:textStyle="bold" | ||||
|                 app:layout_constraintBottom_toTopOf="@+id/textScore" | ||||
|                 app:layout_constraintEnd_toStartOf="@+id/buttonPlayer2" | ||||
|                 app:layout_constraintHorizontal_chainStyle="spread_inside" | ||||
|                 app:layout_constraintStart_toStartOf="parent" | ||||
|                 app:layout_constraintTop_toBottomOf="@+id/textService" /> | ||||
|  | ||||
|             <Button | ||||
|                 android:id="@+id/buttonPlayer2" | ||||
|                 style="@style/Widget.AppCompat.Button.Colored" | ||||
|                 android:layout_width="match_parent" | ||||
|                 android:layout_height="match_parent" | ||||
|                 android:layout_marginBottom="8dp" | ||||
|                 android:layout_marginEnd="8dp" | ||||
|                 android:layout_marginLeft="8dp" | ||||
|                 android:layout_marginRight="8dp" | ||||
|                 android:layout_marginStart="8dp" | ||||
|                 android:layout_marginTop="8dp" | ||||
|                 android:layout_weight="1" | ||||
|                 android:bufferType="spannable" | ||||
|                 android:onClick="onClickPlayer2" | ||||
|                 android:text="Button" | ||||
|                 android:textAllCaps="false" | ||||
|                 android:textAppearance="@style/TextAppearance.AppCompat.Button" | ||||
|                 android:textSize="24sp" | ||||
|                 android:textStyle="bold" | ||||
|                 app:layout_constraintBottom_toTopOf="@+id/textScore" | ||||
|                 app:layout_constraintEnd_toEndOf="parent" | ||||
|                 app:layout_constraintStart_toEndOf="@+id/buttonPlayer1" | ||||
|                 app:layout_constraintTop_toBottomOf="@+id/textService" /> | ||||
|         </LinearLayout> | ||||
|  | ||||
| </android.support.constraint.ConstraintLayout> | ||||
| Before Width: | Height: | Size: 3.0 KiB After Width: | Height: | Size: 3.6 KiB | 
| Before Width: | Height: | Size: 2.0 KiB After Width: | Height: | Size: 2.1 KiB | 
| Before Width: | Height: | Size: 4.5 KiB After Width: | Height: | Size: 4.7 KiB | 
| Before Width: | Height: | Size: 6.3 KiB After Width: | Height: | Size: 7.6 KiB | 
| Before Width: | Height: | Size: 9.0 KiB After Width: | Height: | Size: 11 KiB | 
| @ -5,11 +5,14 @@ | ||||
|     <string name="dialog_title">Nouvelle partie</string> | ||||
|     <string name="dialog_message">Qui commence ?</string> | ||||
|     <string name="info">Cliquez sur le joueur qui a marqué</string> | ||||
|     <string name="service">Service :</string> | ||||
|     <string name="score">Score :</string> | ||||
|     <string name="service">Service : %1s</string> | ||||
|     <string name="score">Score : %1d - %2d</string> | ||||
|     <string name="quit">Quitter</string> | ||||
|     <string-array name="players_names"> | ||||
|         <item>Joueur 1</item> | ||||
|         <item>Joueur 2</item> | ||||
|     </string-array> | ||||
|     <string name="end_match_dialog_title">Bravo %1s !</string> | ||||
|     <string name="new_match_button">Nouvelle partie</string> | ||||
|     <string name="quit_button">Quitter</string> | ||||
| </resources> | ||||
| @ -5,10 +5,14 @@ | ||||
|     <string name="dialog_title">New match</string> | ||||
|     <string name="dialog_message">Who starts?</string> | ||||
|     <string name="info">Click on the scoring player</string> | ||||
|     <string name="service">Service:</string> | ||||
|     <string name="score">Score:</string> | ||||
|     <string name="service">Service: %1s</string> | ||||
|     <string name="score">Score: %1d - %2d</string> | ||||
|     <string name="button_text" translatable="false">%1s <br /> <big> <big> %2d </big> </big></string> | ||||
|     <string name="end_match_dialog_title">Congratulations, %1s!</string> | ||||
|     <string-array name="players_names"> | ||||
|         <item>Player 1</item> | ||||
|         <item>Player 2</item> | ||||
|     </string-array> | ||||
|     <string name="new_match_button">New match</string> | ||||
|     <string name="quit_button">Quit</string> | ||||
| </resources> | ||||
|  | ||||