pass values to dialogs with parent reference instead of bundle
This commit is contained in:
		| @ -9,18 +9,13 @@ 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 { |  | ||||||
|             names = getStringArray("names") |  | ||||||
|             winnerName = getString("winnerName") |  | ||||||
|             score = getIntArray("score") |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         return AlertDialog.Builder(activity).apply{ |  | ||||||
|                 setTitle(getString(R.string.end_match_dialog_title, winnerName)) |                 setTitle(getString(R.string.end_match_dialog_title, winnerName)) | ||||||
|                 setMessage(getString(R.string.score, score[0], score[1])) |                 setMessage(getString(R.string.score, score[0], score[1])) | ||||||
|                 setPositiveButton( |                 setPositiveButton( | ||||||
| @ -31,7 +26,6 @@ class EndOfMatchDialog: DialogFragment() { | |||||||
|                                         putExtra("names", names) |                                         putExtra("names", names) | ||||||
|                                     } |                                     } | ||||||
|                             ) |                             ) | ||||||
|                         //activity?.finish() |  | ||||||
|                         } |                         } | ||||||
|                 ) |                 ) | ||||||
|                 setNeutralButton( |                 setNeutralButton( | ||||||
| @ -62,12 +56,5 @@ class EndOfMatchDialog: DialogFragment() { | |||||||
|                             startActivity(newMatchIntent) |                             startActivity(newMatchIntent) | ||||||
|                         } |                         } | ||||||
|                 ) |                 ) | ||||||
|             /*setNegativeButton( |  | ||||||
|                     R.string.quit_button, |  | ||||||
|                     DialogInterface.OnClickListener { dialog, id -> |  | ||||||
|                         activity?.finish() |  | ||||||
|                     } |  | ||||||
|             )*/ |  | ||||||
|             }.create() |             }.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 | ||||||
|  |  | ||||||
|     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 inflater:LayoutInflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater | ||||||
|                 val namesView: View = inflater.inflate(R.layout.starter_name_dialog, null) |                 val namesView: View = inflater.inflate(R.layout.starter_name_dialog, null) | ||||||
|                 val inputsPlayersNames: Array<EditText?> = arrayOf( |                 val inputsPlayersNames: Array<EditText?> = arrayOf( | ||||||
|                         namesView.findViewById(R.id.inputLeftPlayerName), |                         namesView.findViewById(R.id.inputLeftPlayerName), | ||||||
|                         namesView.findViewById(R.id.inputRightPlayerName) |                         namesView.findViewById(R.id.inputRightPlayerName) | ||||||
|                 ) |                 ) | ||||||
|         arguments?.getStringArray("names")?.apply{ |  | ||||||
|             zip(inputsPlayersNames).forEach { |                 inputsPlayersNames.zip(mainActivity.players.map{ it.name }).forEach { (inputPlayerName, name) -> | ||||||
|                 (name, inputPlayerName) -> inputPlayerName?.setText(name, TextView.BufferType.EDITABLE) |                     inputPlayerName?.setText(name, TextView.BufferType.EDITABLE) | ||||||
|             } |  | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|         return AlertDialog.Builder(activity).apply { |  | ||||||
|                 setTitle(R.string.starter_name_dialog_message) |                 setTitle(R.string.starter_name_dialog_message) | ||||||
|                 setView(namesView) |                 setView(namesView) | ||||||
|                 setPositiveButton(R.string.go_button) { dialog, id -> |                 setPositiveButton(R.string.go_button) { dialog, id -> | ||||||
|                 mainActivity?.setStarterName( |                     dismiss() | ||||||
|  |                     mainActivity.setStarterName( | ||||||
|                             when (namesView.findViewById<RadioGroup>(R.id.radioGroup)?.checkedRadioButtonId) { |                             when (namesView.findViewById<RadioGroup>(R.id.radioGroup)?.checkedRadioButtonId) { | ||||||
|                                 R.id.radioLeftPlayer -> Side.LEFT |                                 R.id.radioLeftPlayer -> Side.LEFT | ||||||
|                                 else -> Side.RIGHT |                                 else -> Side.RIGHT | ||||||
|                             }, |                             }, | ||||||
|                             inputsPlayersNames.map{ it?.text.toString() } |                             inputsPlayersNames.map{ it?.text.toString() } | ||||||
|                     ) |                     ) | ||||||
|                 dismiss() |  | ||||||
|                 } |                 } | ||||||
|             //setNegativeButton(R.string.quit_button) { dialog, id -> activity?.finish() } |  | ||||||
|             }.create() |             }.create() | ||||||
|         } |         } | ||||||
| } |  | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user