pass values to dialogs with parent reference instead of bundle
This commit is contained in:
		| @ -9,65 +9,52 @@ import android.support.v4.app.DialogFragment | ||||
|  | ||||
|  | ||||
| class EndOfMatchDialog: DialogFragment() { | ||||
|     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | ||||
|         var names: Array<String> = arrayOf("", "") | ||||
|         var winnerName = "" | ||||
|         var score = IntArray(2) | ||||
|     override fun onCreateDialog(savedInstanceState: Bundle?)= | ||||
|             AlertDialog.Builder(activity).apply{ | ||||
|                 val players = (activity as MainActivity).players | ||||
|                 val names = players.map { it.name }.toTypedArray() | ||||
|                 val winnerName = players.maxBy { it.score }?.name ?: "" | ||||
|                 val score = players.map { it.score }.sortedDescending().toIntArray() | ||||
|  | ||||
|         arguments?.apply { | ||||
|             names = getStringArray("names") | ||||
|             winnerName = getString("winnerName") | ||||
|             score = getIntArray("score") | ||||
|         } | ||||
|  | ||||
|         return AlertDialog.Builder(activity).apply{ | ||||
|             setTitle(getString(R.string.end_match_dialog_title, winnerName)) | ||||
|             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] | ||||
|                                     ) | ||||
|                 setTitle(getString(R.string.end_match_dialog_title, winnerName)) | ||||
|                 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) | ||||
|                                     } | ||||
|                             ) | ||||
|                             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) | ||||
|                     } | ||||
|             ) | ||||
|             /*setNegativeButton( | ||||
|                     R.string.quit_button, | ||||
|                     DialogInterface.OnClickListener { dialog, id -> | ||||
|                         activity?.finish() | ||||
|                     } | ||||
|             )*/ | ||||
|         }.create() | ||||
|     } | ||||
| } | ||||
|                 ) | ||||
|                 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) | ||||
|                         } | ||||
|                 ) | ||||
|             }.create() | ||||
|         } | ||||
|  | ||||
| @ -15,7 +15,7 @@ import android.view.MenuItem | ||||
| import android.widget.ImageView | ||||
|  | ||||
|  | ||||
| class MainActivity : AppCompatActivity(), StarterNameDialog.StarterNameDialogListener{ | ||||
| class MainActivity : AppCompatActivity() { | ||||
|     var players: Array<Player> = emptyArray() | ||||
|     var serviceSide: Side = Side.LEFT | ||||
|     var relaunchSide: Side = Side.RIGHT | ||||
| @ -54,7 +54,7 @@ class MainActivity : AppCompatActivity(), StarterNameDialog.StarterNameDialogLis | ||||
|                 findViewById(R.id.imgRightService) | ||||
|         ) | ||||
|         updateUI() | ||||
|         openStarterNameDialog() | ||||
|         StarterNameDialog().show( supportFragmentManager, "StarterNameDialog") | ||||
|     } | ||||
|  | ||||
|     override fun onCreateOptionsMenu(menu: Menu): Boolean { | ||||
| @ -124,16 +124,7 @@ class MainActivity : AppCompatActivity(), StarterNameDialog.StarterNameDialogLis | ||||
|         updateUI() | ||||
|     } | ||||
|  | ||||
|     fun openStarterNameDialog() { | ||||
|         StarterNameDialog().apply { | ||||
|             arguments = Bundle().apply { | ||||
|                 putStringArray("names", players.map{ it.name }.toTypedArray()) | ||||
|             } | ||||
|             show( supportFragmentManager, "StarterNameDialog") | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     override fun setStarterName(serviceSide: Side, names: Collection<String>) { | ||||
|     fun setStarterName(serviceSide: Side, names: Collection<String>) { | ||||
|         players.zip(names).forEach { (player, name) -> player.name = name} | ||||
|         this.serviceSide = serviceSide | ||||
|         relaunchSide = when(serviceSide) { | ||||
| @ -184,7 +175,7 @@ class MainActivity : AppCompatActivity(), StarterNameDialog.StarterNameDialogLis | ||||
|             updateUI() | ||||
|         } | ||||
|         if ( matchIsFinished() ) { | ||||
|             openEndOfMatchDialog() | ||||
|             EndOfMatchDialog().show(supportFragmentManager,"EndOfMatchDialog") | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @ -192,15 +183,4 @@ class MainActivity : AppCompatActivity(), StarterNameDialog.StarterNameDialogLis | ||||
|         val (minScore, maxScore) = players.map { it.score }.sorted() | ||||
|         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() { | ||||
|     interface StarterNameDialogListener { | ||||
|         fun setStarterName(serviceSide: Side, names: Collection<String>) | ||||
|     } | ||||
|  | ||||
|     var mainActivity: StarterNameDialogListener? = null | ||||
|  | ||||
|     override fun onAttach(activity: Activity?) { | ||||
|         super.onAttach(activity) | ||||
|         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() } | ||||
|     override fun onCreateDialog(savedInstanceState: Bundle?) = | ||||
|             AlertDialog.Builder(activity).apply { | ||||
|                 val mainActivity = activity as MainActivity | ||||
|                 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) | ||||
|                 ) | ||||
|                 dismiss() | ||||
|             } | ||||
|             //setNegativeButton(R.string.quit_button) { dialog, id -> activity?.finish() } | ||||
|         }.create() | ||||
|     } | ||||
| } | ||||
|  | ||||
|                 inputsPlayersNames.zip(mainActivity.players.map{ it.name }).forEach { (inputPlayerName, name) -> | ||||
|                     inputPlayerName?.setText(name, TextView.BufferType.EDITABLE) | ||||
|                 } | ||||
|  | ||||
|                 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() | ||||
|         } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user