Add Starter Name Dialog, minor UI changes

This commit is contained in:
adrienmalin 2018-08-14 18:30:20 +02:00
parent 7500de9e73
commit 0de1a4aa91
12 changed files with 228 additions and 95 deletions

Binary file not shown.

View File

@ -8,9 +8,10 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat">
android:theme="@style/PingPoints">
<activity android:name=".MainActivity"
android:screenOrientation="sensorLandscape">
android:screenOrientation="sensorLandscape"
android:theme="@style/PingPoints">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

View File

@ -11,22 +11,54 @@ import android.support.v4.app.DialogFragment
class EndOfMatchDialog: DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = AlertDialog.Builder(activity)
val player1Name = arguments?.getString("PLAYER_1_NAME")
val player2Name = arguments?.getString("PLAYER_2_NAME")
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))
R.string.new_match,
DialogInterface.OnClickListener {dialog, id ->
startActivity(Intent(context, MainActivity::class.java))
activity?.finish()
}
)
.setNeutralButton(
R.string.share_button,
DialogInterface.OnClickListener { dialog, id ->
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(
Intent.EXTRA_SUBJECT,
getString(
R.string.share_subject,
player1Name,
player2Name
)
)
putExtra(
Intent.EXTRA_TEXT,
getString(
R.string.share_message,
player1Name,
player2Name,
winnerName,
winnerScore,
loserScore
)
)
type = "text/plain"
}
startActivity(sendIntent)
}
)
.setNegativeButton(
R.string.quit_button,
DialogInterface.OnClickListener {
dialog, id -> activity?.finish()
DialogInterface.OnClickListener { dialog, id ->
activity?.finish()
}
)

View File

@ -11,6 +11,8 @@ import android.text.Spanned
import android.text.TextUtils.join
import kotlin.math.abs
import android.support.design.widget.Snackbar
import android.support.v4.app.DialogFragment
import android.widget.Toast
@SuppressWarnings("deprecation")
@ -24,7 +26,7 @@ fun fromHtml(html: String): Spanned {
}
class MainActivity : AppCompatActivity() {
class MainActivity : AppCompatActivity(), StarterNameDialog.StarterNameDialogListener{
var players: Array<Player> = arrayOf(
Player(),
Player()
@ -34,9 +36,7 @@ class MainActivity : AppCompatActivity() {
var textScore: android.widget.TextView? = null
var stringScore:String = ""
var textService: android.widget.TextView? = null
var stringService:String = ""
var buttons: Array<Button> = emptyArray()
@ -45,12 +45,12 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;
}
val names: Array<String> = resources.getStringArray(R.array.players_names)
for ((player, name) in players.zip(names)) {
player.name = name
val defaultNames: Array<String> = resources.getStringArray(R.array.players_names)
for ((player, defaultName) in players.zip(defaultNames)) {
player.name = defaultName
}
textScore = findViewById(R.id.textScore)
@ -60,12 +60,33 @@ class MainActivity : AppCompatActivity() {
findViewById(R.id.buttonPlayer2)
)
openStarterNameDialog()
update_ui()
Snackbar.make(findViewById(R.layout.activity_main), R.string.info, Snackbar.LENGTH_LONG)
Toast.makeText(applicationContext, R.string.info, Snackbar.LENGTH_LONG)
.show()
}
fun openStarterNameDialog() {
val (loser, winner) = players.sortedBy { it.score }
var starterNameDialog: EndOfMatchDialog = EndOfMatchDialog()
starterNameDialog.arguments = Bundle()
starterNameDialog.arguments?.putString("PLAYER_1_NAME", players[0].name)
starterNameDialog.arguments?.putString("PLAYER_2_NAME", players[1].name)
starterNameDialog.show(
supportFragmentManager,
join(" ", arrayOf(winner.name, winner.score.toString(), "-", loser.name, loser.score.toString()))
)
}
override fun onStaterNameDialogPositiveClick(dialog: DialogFragment) {
val inputPlayer1Name: android.widget.EditText? = findViewById(R.id.input_player_1_name)
players[0].name = inputPlayer1Name?.text.toString()
val inputPlayer2Name: android.widget.EditText? = findViewById(R.id.input_player_2_name)
players[1].name = inputPlayer2Name?.text.toString()
}
fun update_ui() {
textScore?.text = getString(R.string.score, players[server].score, players[notServer].score)
@ -86,39 +107,40 @@ class MainActivity : AppCompatActivity() {
}
fun onClickPlayer1(view: View) {
updateScore(0)
updateScore(players[0])
}
fun onClickPlayer2(view: View) {
updateScore(1)
updateScore(players[1])
}
fun updateScore(scoringPlayerId: Int) {
players[scoringPlayerId].score++
fun finishedMatch() = (
(players.map { it -> it.score } .max() ?: 0 >= 11) or
(abs(players[0].score - players[1].score) >= 2)
)
if (
(players.map { it -> it.score } .max() ?: 0 < 11) or
(abs(players[0].score - players[1].score) < 2)
) {
fun updateScore(scoringPlayer: Player) {
if ( !finishedMatch() ) {
scoringPlayer.score++
if (players.sumBy { it.score } % 2 == 0) {
server = notServer.also { notServer = server }
}
update_ui()
} else {
endOfMatch()
}
if ( finishedMatch() ) {
openEndOfMatchDialog()
}
update_ui()
}
fun endOfMatch() {
val (loser, winner) = players.sortedBy { it.score }
fun openEndOfMatchDialog() {
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
val (loser, winner) = players.sortedBy { it.score }
endOfMatchDialog.arguments = Bundle()
endOfMatchDialog.arguments?.putString("PLAYER_1_NAME", players[0].name)
endOfMatchDialog.arguments?.putString("PLAYER_2_NAME", players[1].name)
endOfMatchDialog.arguments?.putString("WINNER_NAME", winner.name)
endOfMatchDialog.arguments?.putInt("WINNER_SCORE", winner.score)
endOfMatchDialog.arguments?.putInt("LOSER_SCORE", loser.score)
endOfMatchDialog.show(
supportFragmentManager,
join(" ", arrayOf(winner.name, winner.score.toString(), "-", loser.name, loser.score.toString()))

View File

@ -1,26 +1,56 @@
package adrienmalin.pingpoints
import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.app.AlertDialog
import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.support.v4.app.DialogFragment
import java.util.ArrayList
import android.app.Activity
class StarterNameDialog : DialogFragment() {
interface StarterNameDialogListener {
fun onStaterNameDialogPositiveClick(dialog: DialogFragment)
}
var listener: StarterNameDialogListener? = null
override fun onAttach(activity: Activity?) {
super.onAttach(activity)
try {
listener = activity as StarterNameDialogListener?
} catch (e: ClassCastException) {
throw ClassCastException(activity!!.toString() + " must implement StarterNameDialogListener")
}
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
// Use the Builder class for convenient dialog construction
val inputPlayer1Name: android.widget.EditText? = findViewById(R.id.input_player_1_name)
val player1Name = arguments?.getString("PLAYER_1_NAME")
inputPlayer1Name?.setText(player1Name, TextView.BufferType.EDITABLE)
val inputPlayer2Name: android.widget.EditText? = findViewById(R.id.input_player_2_name)
val player2Name = arguments?.getString("PLAYER_2_NAME")
inputPlayer2Name?.setText(player2Name, TextView.BufferType.EDITABLE)
val builder = AlertDialog.Builder(activity)
builder.setTitle(R.string.dialog_title)
.setMessage(R.string.dialog_message)
.setPositiveButton(R.string.go, DialogInterface.OnClickListener { dialog, id ->
// FIRE ZE MISSILES!
})
.setNegativeButton(R.string.quit, DialogInterface.OnClickListener { dialog, id ->
// User cancelled the dialog
})
.setView(R.layout.dialog)
// Create the AlertDialog object and return it
// Set the dialog title
builder.setTitle(R.string.new_match)
// Specify the list array, the items to be selected by default (null for none),
// and the listener through which to receive callbacks when items are selected
.setMultiChoiceItems(0, null)
.setPositiveButton(R.string.go_button) { dialog, id ->
// User clicked OK, so save the mSelectedItems results somewhere
// or return them to the component that opened the dialog
//...
}
.setNegativeButton(R.string.quit_button) { dialog, id ->
activity?.finish()
}
return builder.create()
}
}
}

View File

@ -0,0 +1,44 @@
package adrienmalin.pingpoints;
public class test {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
mSelectedItems = new ArrayList(); // Where we track the selected items
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Set the dialog title
builder.setTitle(R.string.pick_toppings)
// Specify the list array, the items to be selected by default (null for none),
// and the listener through which to receive callbacks when items are selected
.setMultiChoiceItems(R.array.toppings, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
mSelectedItems.add(which);
} else if (mSelectedItems.contains(which)) {
// Else, if the item is already in the array, remove it
mSelectedItems.remove(Integer.valueOf(which));
}
}
})
// Set the action buttons
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// User clicked OK, so save the mSelectedItems results somewhere
// or return them to the component that opened the dialog
...
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
...
}
});
return builder.create();
}
}

View File

@ -21,20 +21,6 @@
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"
@ -51,6 +37,20 @@
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/textScore"
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" />
</LinearLayout>
<LinearLayout
@ -71,7 +71,7 @@
<Button
android:id="@+id/buttonPlayer1"
style="@style/Widget.AppCompat.Button.Colored"
style="@style/Base.Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
@ -91,7 +91,8 @@
app:layout_constraintEnd_toStartOf="@+id/buttonPlayer2"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textService" />
app:layout_constraintTop_toBottomOf="@+id/textService"
tools:text="@string/button_text" />
<Button
android:id="@+id/buttonPlayer2"
@ -114,7 +115,8 @@
app:layout_constraintBottom_toTopOf="@+id/textScore"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/buttonPlayer1"
app:layout_constraintTop_toBottomOf="@+id/textService" />
app:layout_constraintTop_toBottomOf="@+id/textService"
tools:text="@string/button_text" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>

View File

@ -2,7 +2,7 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
tools:context=".NomsJoueursEtPremierServeurDialog">
<!-- TODO: Update blank fragment layout -->
@ -14,10 +14,9 @@
<RadioButton
android:id="@+id/radioButtonJoueur1"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="RadioButton" />
android:layout_weight="1" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
@ -25,18 +24,17 @@
android:layout_weight="1">
<android.support.design.widget.TextInputEditText
android:id="@+id/InputJoueur1"
android:id="@+id/input_player_1_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="hint" />
android:hint="@string/name" />
</android.support.design.widget.TextInputLayout>
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="RadioButton" />
android:layout_weight="1" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
@ -44,10 +42,10 @@
android:layout_weight="1">
<android.support.design.widget.TextInputEditText
android:id="@+id/InputJoueur2"
android:id="@+id/input_player_2_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="hint" />
android:hint="@string/name" />
</android.support.design.widget.TextInputLayout>
</RadioGroup>
</FrameLayout>

View File

@ -1,18 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Ping Points</string>
<string name="go">Allons-y !</string>
<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 : %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="new_match">Nouveau match</string>
<string name="quit_button">Quitter</string>
<string name="name">Nom</string>
<string name="go_button">Allons-y !</string>
<string name="starter_name_dialog_message">Qui commence ?</string>
<string name="share_button">Partager</string>
<string name="share_subject">Match Ping Points : %1s contre %2s</string>
<string name="share_message">%1s vs. %2s\\nVainqueur : %3s\\nScore : %4d - %5d\\n\\n--\\nArbitré avec l\'application gratuite Ping Points disponible sur Google Play</string>
</resources>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3f51b5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
<color name="colorPrimary">#4caf50</color>
<color name="colorPrimaryDark">#009688</color>
<color name="colorAccent">#69f0ae</color>
</resources>

View File

@ -1,18 +1,20 @@
<resources>
<string name="app_name">Ping Points</string>
<string name="go">Play</string>
<string name="quit">Quit</string>
<string name="dialog_title">New match</string>
<string name="dialog_message">Who starts?</string>
<string name="go_button">Play</string>
<string name="starter_name_dialog_message">Who starts?</string>
<string name="info">Click on the scoring player</string>
<string name="service">Service: %1s</string>
<string name="score">Score: %1d - %2d</string>
<string name="button_text" translatable="false">%1s &lt;br /&gt; &lt;big&gt; &lt;big&gt; %2d &lt;/big&gt; &lt;/big&gt;</string>
<string name="button_text" translatable="false">%1s &lt;br /&gt; &lt;br /&gt; &lt;big&gt; &lt;big&gt; %2d &lt;/big&gt; &lt;/big&gt;</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="new_match">New match</string>
<string name="quit_button">Quit</string>
<string name="name">Name</string>
<string name="share_button">Share</string>
<string name="share_subject">Ping Points Match: %1s vs. %2s</string>
<string name="share_message">%1s vs. %2s\\nWinner: %3s\\nScore: %4d - %5d\\n\\n--\\nRefereed with Ping Points free Android app on Google Play</string>
</resources>

View File

@ -1,11 +1,11 @@
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<style name="PingPoints" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
</resources>