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

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();
}
}