pass values to dialogs with parent reference instead of bundle
This commit is contained in:
parent
a914912b76
commit
7843848653
@ -9,65 +9,52 @@ import android.support.v4.app.DialogFragment
|
|||||||
|
|
||||||
|
|
||||||
class EndOfMatchDialog: DialogFragment() {
|
class EndOfMatchDialog: DialogFragment() {
|
||||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
override fun onCreateDialog(savedInstanceState: Bundle?)=
|
||||||
var names: Array<String> = arrayOf("", "")
|
AlertDialog.Builder(activity).apply{
|
||||||
var winnerName = ""
|
val players = (activity as MainActivity).players
|
||||||
var score = IntArray(2)
|
val names = players.map { it.name }.toTypedArray()
|
||||||
|
val winnerName = players.maxBy { it.score }?.name ?: ""
|
||||||
|
val score = players.map { it.score }.sortedDescending().toIntArray()
|
||||||
|
|
||||||
arguments?.apply {
|
setTitle(getString(R.string.end_match_dialog_title, winnerName))
|
||||||
names = getStringArray("names")
|
setMessage(getString(R.string.score, score[0], score[1]))
|
||||||
winnerName = getString("winnerName")
|
setPositiveButton(
|
||||||
score = getIntArray("score")
|
R.string.new_match,
|
||||||
}
|
DialogInterface.OnClickListener { dialog, id ->
|
||||||
|
startActivity(
|
||||||
return AlertDialog.Builder(activity).apply{
|
Intent(context, MainActivity::class.java).apply {
|
||||||
setTitle(getString(R.string.end_match_dialog_title, winnerName))
|
putExtra("names", names)
|
||||||
setMessage(getString(R.string.score, score[0], score[1]))
|
}
|
||||||
setPositiveButton(
|
|
||||||
R.string.new_match,
|
|
||||||
DialogInterface.OnClickListener { dialog, id ->
|
|
||||||
startActivity(
|
|
||||||
Intent(context, MainActivity::class.java).apply {
|
|
||||||
putExtra("names", names)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
//activity?.finish()
|
|
||||||
}
|
|
||||||
)
|
|
||||||
setNeutralButton(
|
|
||||||
R.string.share_button,
|
|
||||||
DialogInterface.OnClickListener { dialog, id ->
|
|
||||||
val newMatchIntent: Intent = Intent().apply {
|
|
||||||
action = Intent.ACTION_SEND
|
|
||||||
putExtra(
|
|
||||||
Intent.EXTRA_SUBJECT,
|
|
||||||
getString(
|
|
||||||
R.string.share_subject,
|
|
||||||
names[Side.LEFT.value],
|
|
||||||
names[Side.RIGHT.value]
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
putExtra(
|
|
||||||
Intent.EXTRA_TEXT,
|
|
||||||
getString(R.string.share_message,
|
|
||||||
names[Side.LEFT.value],
|
|
||||||
names[Side.RIGHT.value],
|
|
||||||
winnerName,
|
|
||||||
score[0],
|
|
||||||
score[1]
|
|
||||||
)
|
|
||||||
)
|
|
||||||
type = "text/plain"
|
|
||||||
}
|
}
|
||||||
startActivity(newMatchIntent)
|
)
|
||||||
}
|
setNeutralButton(
|
||||||
)
|
R.string.share_button,
|
||||||
/*setNegativeButton(
|
DialogInterface.OnClickListener { dialog, id ->
|
||||||
R.string.quit_button,
|
val newMatchIntent: Intent = Intent().apply {
|
||||||
DialogInterface.OnClickListener { dialog, id ->
|
action = Intent.ACTION_SEND
|
||||||
activity?.finish()
|
putExtra(
|
||||||
}
|
Intent.EXTRA_SUBJECT,
|
||||||
)*/
|
getString(
|
||||||
}.create()
|
R.string.share_subject,
|
||||||
}
|
names[Side.LEFT.value],
|
||||||
}
|
names[Side.RIGHT.value]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
putExtra(
|
||||||
|
Intent.EXTRA_TEXT,
|
||||||
|
getString(R.string.share_message,
|
||||||
|
names[Side.LEFT.value],
|
||||||
|
names[Side.RIGHT.value],
|
||||||
|
winnerName,
|
||||||
|
score[0],
|
||||||
|
score[1]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
type = "text/plain"
|
||||||
|
}
|
||||||
|
startActivity(newMatchIntent)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}.create()
|
||||||
|
}
|
||||||
|
@ -15,7 +15,7 @@ import android.view.MenuItem
|
|||||||
import android.widget.ImageView
|
import android.widget.ImageView
|
||||||
|
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity(), StarterNameDialog.StarterNameDialogListener{
|
class MainActivity : AppCompatActivity() {
|
||||||
var players: Array<Player> = emptyArray()
|
var players: Array<Player> = emptyArray()
|
||||||
var serviceSide: Side = Side.LEFT
|
var serviceSide: Side = Side.LEFT
|
||||||
var relaunchSide: Side = Side.RIGHT
|
var relaunchSide: Side = Side.RIGHT
|
||||||
@ -54,7 +54,7 @@ class MainActivity : AppCompatActivity(), StarterNameDialog.StarterNameDialogLis
|
|||||||
findViewById(R.id.imgRightService)
|
findViewById(R.id.imgRightService)
|
||||||
)
|
)
|
||||||
updateUI()
|
updateUI()
|
||||||
openStarterNameDialog()
|
StarterNameDialog().show( supportFragmentManager, "StarterNameDialog")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||||
@ -124,16 +124,7 @@ class MainActivity : AppCompatActivity(), StarterNameDialog.StarterNameDialogLis
|
|||||||
updateUI()
|
updateUI()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun openStarterNameDialog() {
|
fun setStarterName(serviceSide: Side, names: Collection<String>) {
|
||||||
StarterNameDialog().apply {
|
|
||||||
arguments = Bundle().apply {
|
|
||||||
putStringArray("names", players.map{ it.name }.toTypedArray())
|
|
||||||
}
|
|
||||||
show( supportFragmentManager, "StarterNameDialog")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun setStarterName(serviceSide: Side, names: Collection<String>) {
|
|
||||||
players.zip(names).forEach { (player, name) -> player.name = name}
|
players.zip(names).forEach { (player, name) -> player.name = name}
|
||||||
this.serviceSide = serviceSide
|
this.serviceSide = serviceSide
|
||||||
relaunchSide = when(serviceSide) {
|
relaunchSide = when(serviceSide) {
|
||||||
@ -184,7 +175,7 @@ class MainActivity : AppCompatActivity(), StarterNameDialog.StarterNameDialogLis
|
|||||||
updateUI()
|
updateUI()
|
||||||
}
|
}
|
||||||
if ( matchIsFinished() ) {
|
if ( matchIsFinished() ) {
|
||||||
openEndOfMatchDialog()
|
EndOfMatchDialog().show(supportFragmentManager,"EndOfMatchDialog")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,15 +183,4 @@ class MainActivity : AppCompatActivity(), StarterNameDialog.StarterNameDialogLis
|
|||||||
val (minScore, maxScore) = players.map { it.score }.sorted()
|
val (minScore, maxScore) = players.map { it.score }.sorted()
|
||||||
return (maxScore >= 11) and (maxScore - minScore >= 2)
|
return (maxScore >= 11) and (maxScore - minScore >= 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun openEndOfMatchDialog() {
|
|
||||||
EndOfMatchDialog().apply {
|
|
||||||
arguments = Bundle().apply {
|
|
||||||
putStringArray("names", players.map{ it.name }.toTypedArray())
|
|
||||||
putString("winnerName", players.maxBy { it.score }?.name)
|
|
||||||
putIntArray("score", players.map{ it.score }.sortedDescending().toIntArray())
|
|
||||||
}
|
|
||||||
show(supportFragmentManager,"EndOfMatchDialog")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -14,48 +14,31 @@ import android.widget.TextView
|
|||||||
|
|
||||||
|
|
||||||
class StarterNameDialog : DialogFragment() {
|
class StarterNameDialog : DialogFragment() {
|
||||||
interface StarterNameDialogListener {
|
override fun onCreateDialog(savedInstanceState: Bundle?) =
|
||||||
fun setStarterName(serviceSide: Side, names: Collection<String>)
|
AlertDialog.Builder(activity).apply {
|
||||||
}
|
val mainActivity = activity as MainActivity
|
||||||
|
val inflater:LayoutInflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
|
||||||
var mainActivity: StarterNameDialogListener? = null
|
val namesView: View = inflater.inflate(R.layout.starter_name_dialog, null)
|
||||||
|
val inputsPlayersNames: Array<EditText?> = arrayOf(
|
||||||
override fun onAttach(activity: Activity?) {
|
namesView.findViewById(R.id.inputLeftPlayerName),
|
||||||
super.onAttach(activity)
|
namesView.findViewById(R.id.inputRightPlayerName)
|
||||||
try {
|
|
||||||
mainActivity = activity as StarterNameDialogListener
|
|
||||||
} catch (e: ClassCastException) {
|
|
||||||
throw ClassCastException(activity!!.toString() + " must implement StarterNameDialogListener")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
|
||||||
val inflater:LayoutInflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
|
|
||||||
val namesView: View = inflater.inflate(R.layout.starter_name_dialog, null)
|
|
||||||
val inputsPlayersNames: Array<EditText?> = arrayOf(
|
|
||||||
namesView.findViewById(R.id.inputLeftPlayerName),
|
|
||||||
namesView.findViewById(R.id.inputRightPlayerName)
|
|
||||||
)
|
|
||||||
arguments?.getStringArray("names")?.apply{
|
|
||||||
zip(inputsPlayersNames).forEach {
|
|
||||||
(name, inputPlayerName) -> inputPlayerName?.setText(name, TextView.BufferType.EDITABLE)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return AlertDialog.Builder(activity).apply {
|
|
||||||
setTitle(R.string.starter_name_dialog_message)
|
|
||||||
setView(namesView)
|
|
||||||
setPositiveButton(R.string.go_button) { dialog, id ->
|
|
||||||
mainActivity?.setStarterName(
|
|
||||||
when (namesView.findViewById<RadioGroup>(R.id.radioGroup)?.checkedRadioButtonId) {
|
|
||||||
R.id.radioLeftPlayer -> Side.LEFT
|
|
||||||
else -> Side.RIGHT
|
|
||||||
},
|
|
||||||
inputsPlayersNames.map{ it?.text.toString() }
|
|
||||||
)
|
)
|
||||||
dismiss()
|
|
||||||
}
|
inputsPlayersNames.zip(mainActivity.players.map{ it.name }).forEach { (inputPlayerName, name) ->
|
||||||
//setNegativeButton(R.string.quit_button) { dialog, id -> activity?.finish() }
|
inputPlayerName?.setText(name, TextView.BufferType.EDITABLE)
|
||||||
}.create()
|
}
|
||||||
}
|
|
||||||
}
|
setTitle(R.string.starter_name_dialog_message)
|
||||||
|
setView(namesView)
|
||||||
|
setPositiveButton(R.string.go_button) { dialog, id ->
|
||||||
|
dismiss()
|
||||||
|
mainActivity.setStarterName(
|
||||||
|
when (namesView.findViewById<RadioGroup>(R.id.radioGroup)?.checkedRadioButtonId) {
|
||||||
|
R.id.radioLeftPlayer -> Side.LEFT
|
||||||
|
else -> Side.RIGHT
|
||||||
|
},
|
||||||
|
inputsPlayersNames.map{ it?.text.toString() }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}.create()
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user