add undo/redo, link
This commit is contained in:
		| @ -1,9 +1,12 @@ | ||||
| package adrienmalin.pingpoints | ||||
|  | ||||
| import android.content.Intent | ||||
| import android.support.v7.app.AppCompatActivity | ||||
| import android.os.Bundle | ||||
| import android.view.MenuItem | ||||
| import android.text.method.LinkMovementMethod | ||||
| import android.widget.TextView | ||||
|  | ||||
|  | ||||
|  | ||||
| class CreditsActivity : AppCompatActivity() { | ||||
|     override fun onCreate(savedInstanceState: Bundle?) { | ||||
| @ -11,6 +14,7 @@ class CreditsActivity : AppCompatActivity() { | ||||
|         setContentView(R.layout.activity_credits) | ||||
|         setSupportActionBar(findViewById(R.id.toolbar)) | ||||
|         supportActionBar?.setDisplayHomeAsUpEnabled(true) | ||||
|         findViewById<TextView>(R.id.iconsCredit)?.movementMethod = LinkMovementMethod.getInstance() | ||||
|     } | ||||
|  | ||||
|     override fun onOptionsItemSelected(item: MenuItem): Boolean { | ||||
|  | ||||
| @ -8,6 +8,7 @@ import android.view.View | ||||
| import android.widget.Button | ||||
| import android.os.Build | ||||
| import android.support.v7.app.AppCompatDelegate | ||||
| import android.support.v7.view.menu.ActionMenuItem | ||||
| import android.view.Menu | ||||
| import android.widget.Toast | ||||
| import android.view.MenuItem | ||||
| @ -22,8 +23,10 @@ class MainActivity : AppCompatActivity(), StarterNameDialog.StarterNameDialogLis | ||||
|     var textService: android.widget.TextView? = null | ||||
|     var buttons: Array<Button> = emptyArray() | ||||
|     var imageViews: Array<ImageView?> = emptyArray() | ||||
|     var history: ArrayList<State> = ArrayList() | ||||
|     var history: MutableList<State> = ArrayList() | ||||
|     var step: Int = 0 | ||||
|     var undo: MenuItem? = null | ||||
|     var redo: MenuItem? = null | ||||
|  | ||||
|  | ||||
|     override fun onCreate(savedInstanceState: Bundle?) { | ||||
| @ -31,7 +34,6 @@ class MainActivity : AppCompatActivity(), StarterNameDialog.StarterNameDialogLis | ||||
|         AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); | ||||
|         setContentView(R.layout.activity_main) | ||||
|         setSupportActionBar(findViewById(R.id.toolbar)) | ||||
|  | ||||
|         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { | ||||
|             requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE | ||||
|         } | ||||
| @ -41,7 +43,6 @@ class MainActivity : AppCompatActivity(), StarterNameDialog.StarterNameDialogLis | ||||
|                 Player(names[Side.LEFT.value]), | ||||
|                 Player(names[Side.RIGHT.value]) | ||||
|         ) | ||||
|  | ||||
|         textScore = findViewById(R.id.textScore) | ||||
|         textService = findViewById(R.id.textService) | ||||
|         buttons = arrayOf( | ||||
| @ -52,18 +53,30 @@ class MainActivity : AppCompatActivity(), StarterNameDialog.StarterNameDialogLis | ||||
|                 findViewById(R.id.imgLeftService), | ||||
|                 findViewById(R.id.imgRightService) | ||||
|         ) | ||||
|  | ||||
|         updateUI() | ||||
|  | ||||
|         openStarterNameDialog() | ||||
|     } | ||||
|  | ||||
|     override fun onCreateOptionsMenu(menu: Menu): Boolean { | ||||
|         menuInflater.inflate(R.menu.main, menu) | ||||
|         undo = menu.findItem(R.id.action_undo) | ||||
|         redo = menu.findItem(R.id.action_redo) | ||||
|         return super.onCreateOptionsMenu(menu) | ||||
|     } | ||||
|  | ||||
|     override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) { | ||||
|         R.id.action_undo -> { | ||||
|             step-- | ||||
|             reloadState() | ||||
|             redo?.isVisible = true | ||||
|             true | ||||
|         } | ||||
|         R.id.action_redo -> { | ||||
|             step++ | ||||
|             reloadState() | ||||
|             undo?.isVisible = true | ||||
|             true | ||||
|         } | ||||
|         R.id.action_new_match -> { | ||||
|             startActivity( | ||||
|                     Intent(this, MainActivity::class.java).apply { | ||||
| @ -81,15 +94,34 @@ class MainActivity : AppCompatActivity(), StarterNameDialog.StarterNameDialogLis | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     fun reloadState(): { | ||||
|         history[step].apply{ | ||||
|             players.zip(score).forEach{(player, playerScore) -> player.score = playerScore} | ||||
|             serviceSide = service | ||||
|     fun saveState() { | ||||
|         val state = State(players.map { it.score }, serviceSide) | ||||
|         if (step == history.size) { | ||||
|             history.add(state) | ||||
|         } else { | ||||
|             history[step] = state | ||||
|             history = history.subList(0, step+1).toMutableList() | ||||
|         } | ||||
|         if (step > 0) { | ||||
|             undo?.isVisible = true | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     fun reloadState() { | ||||
|         history[step].let{ | ||||
|             players.zip(it.score).forEach{(player, score) -> player.score = score} | ||||
|             serviceSide = it.serviceSide | ||||
|             relaunchSide = when(serviceSide) { | ||||
|                 Side.LEFT -> Side.RIGHT | ||||
|                 Side.RIGHT -> Side.LEFT | ||||
|             } | ||||
|         } | ||||
|         when(step){ | ||||
|             0 -> undo?.isVisible = false | ||||
|             history.size - 1 -> redo?.isVisible = false | ||||
|         } | ||||
|         this.step = step | ||||
|         updateUI() | ||||
|     } | ||||
|  | ||||
|     fun openStarterNameDialog() { | ||||
| @ -108,7 +140,7 @@ class MainActivity : AppCompatActivity(), StarterNameDialog.StarterNameDialogLis | ||||
|             Side.LEFT -> Side.RIGHT | ||||
|             Side.RIGHT -> Side.LEFT | ||||
|         } | ||||
|  | ||||
|         saveState() | ||||
|         updateUI() | ||||
|         Toast.makeText(applicationContext, R.string.info, Toast.LENGTH_LONG).show() | ||||
|     } | ||||
| @ -142,25 +174,18 @@ class MainActivity : AppCompatActivity(), StarterNameDialog.StarterNameDialogLis | ||||
|     } | ||||
|  | ||||
|     fun updateScore(scoringPlayer: Player) { | ||||
|         val state = State(players.map { it.score }, serviceSide) | ||||
|         if (step >= history.size) { | ||||
|             history.add(state) | ||||
|         } else { | ||||
|             history.removeAt(step + 1) | ||||
|             history[step] = state | ||||
|         } | ||||
|         step ++ | ||||
|  | ||||
|         if ( !matchIsFinished() ) { | ||||
|             step++ | ||||
|             scoringPlayer.score++ | ||||
|             if (players.sumBy { it.score } % 2 == 0) { | ||||
|                 serviceSide = relaunchSide.also { relaunchSide = serviceSide } | ||||
|             } | ||||
|             saveState() | ||||
|             updateUI() | ||||
|         } | ||||
|         if ( matchIsFinished() ) { | ||||
|             openEndOfMatchDialog() | ||||
|         } | ||||
|         updateUI() | ||||
|     } | ||||
|  | ||||
|     fun matchIsFinished(): Boolean { | ||||
|  | ||||
| @ -47,7 +47,7 @@ class StarterNameDialog : DialogFragment() { | ||||
|             setView(namesView) | ||||
|             setPositiveButton(R.string.go_button) { dialog, id -> | ||||
|                 mainActivity?.setStarterName( | ||||
|                         when ((namesView.findViewById(R.id.radioGroup) as RadioGroup).checkedRadioButtonId) { | ||||
|                         when (namesView.findViewById<RadioGroup>(R.id.radioGroup)?.checkedRadioButtonId) { | ||||
|                             R.id.radioLeftPlayer -> Side.LEFT | ||||
|                             else -> Side.RIGHT | ||||
|                         }, | ||||
|  | ||||
| @ -19,7 +19,7 @@ enum class Side(val value:Int) { | ||||
|  | ||||
| data class State( | ||||
|         val score: List<Int>, | ||||
|         val service: Side | ||||
|         val serviceSide: Side | ||||
| ) | ||||
|  | ||||
| @SuppressWarnings("deprecation") | ||||
|  | ||||
| @ -42,8 +42,7 @@ | ||||
|                 android:layout_marginEnd="8dp" | ||||
|                 android:layout_marginStart="8dp" | ||||
|                 android:layout_weight="1" | ||||
|                 android:paddingLeft="16dp" | ||||
|                 android:paddingRight="16dp" | ||||
|                 android:gravity="left" | ||||
|                 android:textAppearance="@style/TextAppearance.AppCompat.Large" | ||||
|                 app:layout_constraintBottom_toTopOf="@+id/linearLayoutButtons" | ||||
|                 app:layout_constraintEnd_toEndOf="parent" | ||||
| @ -59,8 +58,7 @@ | ||||
|                 android:layout_marginLeft="8dp" | ||||
|                 android:layout_marginStart="8dp" | ||||
|                 android:layout_weight="1" | ||||
|                 android:paddingLeft="16dp" | ||||
|                 android:paddingRight="16dp" | ||||
|                 android:gravity="right|center" | ||||
|                 android:textAppearance="@style/TextAppearance.AppCompat.Large" | ||||
|                 app:layout_constraintStart_toStartOf="parent" | ||||
|                 app:layout_constraintTop_toTopOf="parent" | ||||
|  | ||||
| @ -19,13 +19,13 @@ | ||||
|             android:id="@+id/radioLeftPlayer" | ||||
|             android:layout_width="wrap_content" | ||||
|             android:layout_height="wrap_content" | ||||
|             android:layout_weight="1" | ||||
|             android:layout_weight="0" | ||||
|             android:checked="true" | ||||
|             android:inputType="text" /> | ||||
|  | ||||
|         <android.support.design.widget.TextInputEditText | ||||
|             android:id="@+id/inputLeftPlayerName" | ||||
|             android:layout_width="100dp" | ||||
|             android:layout_width="match_parent" | ||||
|             android:layout_height="wrap_content" | ||||
|             android:layout_weight="1" | ||||
|             android:hint="@string/name" | ||||
| @ -36,12 +36,12 @@ | ||||
|             android:id="@+id/radioRightPlayer" | ||||
|             android:layout_width="wrap_content" | ||||
|             android:layout_height="wrap_content" | ||||
|             android:layout_weight="1" | ||||
|             android:layout_weight="0" | ||||
|             android:inputType="none" /> | ||||
|  | ||||
|         <android.support.design.widget.TextInputEditText | ||||
|             android:id="@+id/inputRightPlayerName" | ||||
|             android:layout_width="100dp" | ||||
|             android:layout_width="match_parent" | ||||
|             android:layout_height="wrap_content" | ||||
|             android:layout_weight="1" | ||||
|             android:hint="@string/name" | ||||
|  | ||||
| @ -1,6 +1,20 @@ | ||||
| <menu xmlns:app="http://schemas.android.com/apk/res-auto" | ||||
|     xmlns:android="http://schemas.android.com/apk/res/android"> | ||||
|  | ||||
|     <item | ||||
|         android:id="@+id/action_undo" | ||||
|         android:icon="@drawable/ic_undo" | ||||
|         android:title="@string/undo" | ||||
|         app:showAsAction="ifRoom" | ||||
|         android:enabled="false" /> | ||||
|  | ||||
|     <item | ||||
|         android:id="@+id/action_redo" | ||||
|         android:icon="@drawable/ic_redo" | ||||
|         android:title="@string/redo" | ||||
|         app:showAsAction="ifRoom" | ||||
|         android:visible="false" /> | ||||
|  | ||||
|     <item | ||||
|         android:id="@+id/action_new_match" | ||||
|         android:icon="@drawable/ic_new_match" | ||||
|  | ||||
| @ -17,8 +17,10 @@ | ||||
|     <string name="share_button">Partager</string> | ||||
|     <string name="share_subject">Match Ping Points : %s contre %s</string> | ||||
|     <string name="share_message">%s contre %s:\n%s a gagné par %d à %d\nPing Points est disponible sur Google Play</string> | ||||
|     <string name="PingPointsCredits">Ping Points par Adrien Malingrey</string> | ||||
|     <string name="PingPointsCredits">Ping Points par Adrien Malin</string> | ||||
|     <string name="iconCredits"><div>Icônes par <a href="http://www.freepik.com" title="Freepik">Freepik</a> chez <a href="https://www.flaticon.com/" title="Flaticon">www.flaticon.com</a> Licence <a href="http://creativecommons.org/licenses/by/3.0/" title="Creative Commons BY 3.0" target="_blank">CC 3.0 BY</a></div></string> | ||||
|     <string name="about">À propos</string> | ||||
|     <string name="service_img_description">Service</string> | ||||
|     <string name="undo">Annuler</string> | ||||
|     <string name="redo">Rétablir</string> | ||||
| </resources> | ||||
| @ -18,8 +18,10 @@ | ||||
|     <string name="share_subject">Ping Points Match: %s vs. %s</string> | ||||
|     <string name="share_message">%s vs. %s:\n%s won by %d to %d\n\nGet Ping Points on Google Play</string> | ||||
|     <string name="radioText" translatable="false"></string> | ||||
|     <string name="PingPointsCredits">Ping Points by Adrien Malingrey</string> | ||||
|     <string name="PingPointsCredits">Ping Points by Adrien Malin</string> | ||||
|     <string name="iconCredits">Icons made by <a href="http://www.freepik.com" title="Freepik">Freepik</a> from <a href="https://www.flaticon.com/" title="Flaticon">www.flaticon.com</a> is licensed by <a href="http://creativecommons.org/licenses/by/3.0/" title="Creative Commons BY 3.0" target="_blank">CC 3.0 BY</a></string> | ||||
|     <string name="about">About</string> | ||||
|     <string name="service_img_description">Service</string> | ||||
|     <string name="undo">Undo</string> | ||||
|     <string name="redo">Redo</string> | ||||
| </resources> | ||||
|  | ||||
		Reference in New Issue
	
	Block a user