-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
425 additions
and
161 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
app/src/main/java/net/imshit/aircraftwar/data/scoreboard/SaveScoreDialog.kt
This file was deleted.
Oops, something went wrong.
30 changes: 3 additions & 27 deletions
30
app/src/main/java/net/imshit/aircraftwar/data/scoreboard/ScoreInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,7 @@ | ||
package net.imshit.aircraftwar.data.scoreboard | ||
|
||
import android.os.Parcel | ||
import android.os.Parcelable | ||
import java.util.Date | ||
|
||
data class ScoreInfo( | ||
val name: String, val score: Int, val time: String | ||
) : Parcelable { | ||
|
||
constructor(parcel: Parcel) : this( | ||
name = parcel.readString() ?: "", score = parcel.readInt(), time = parcel.readString() ?: "" | ||
) | ||
|
||
override fun describeContents(): Int = 0 | ||
|
||
override fun writeToParcel(dest: Parcel, flags: Int) { | ||
dest.writeString(this.name) | ||
dest.writeInt(this.score) | ||
dest.writeString(this.time) | ||
} | ||
|
||
companion object CREATOR : Parcelable.Creator<ScoreInfo> { | ||
override fun createFromParcel(parcel: Parcel): ScoreInfo { | ||
return ScoreInfo(parcel) | ||
} | ||
|
||
override fun newArray(size: Int): Array<ScoreInfo?> { | ||
return arrayOfNulls(size) | ||
} | ||
} | ||
} | ||
val id: Int, val name: String, val score: Int, val time: Date | ||
) |
44 changes: 44 additions & 0 deletions
44
app/src/main/java/net/imshit/aircraftwar/data/scoreboard/ScoreboardAgent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package net.imshit.aircraftwar.data.scoreboard | ||
|
||
import androidx.appcompat.app.AppCompatActivity | ||
import net.imshit.aircraftwar.data.account.LoginManager | ||
import net.imshit.aircraftwar.data.scoreboard.offline.SaveScoreDialog | ||
import net.imshit.aircraftwar.data.scoreboard.offline.ScoreboardDaoSharedPreferences | ||
import net.imshit.aircraftwar.data.scoreboard.online.ScoreboardDaoOnline | ||
import net.imshit.aircraftwar.gui.ScoreboardActivity | ||
import net.imshit.aircraftwar.logic.game.Difficulty | ||
import java.util.Date | ||
|
||
class ScoreboardAgent( | ||
private val activity: AppCompatActivity, | ||
private val onlineMode: Boolean, | ||
private val gameMode: Difficulty | ||
) : ScoreboardDao by if (onlineMode) ScoreboardDaoOnline( | ||
activity, gameMode | ||
) else ScoreboardDaoSharedPreferences(activity, gameMode) { | ||
var onFinishCallback: (() -> Unit)? = null | ||
private val loginManager = if (onlineMode) LoginManager(activity) else null | ||
|
||
fun requireSave(score: Int) { | ||
if (onlineMode && loginManager != null) { | ||
if (loginManager.isLogin) { | ||
saveAndStart(loginManager.name!!, score) | ||
} | ||
onFinishCallback?.invoke() | ||
} else { | ||
SaveScoreDialog().apply { | ||
onSubmitCallback = { name -> | ||
saveAndStart(name, score) | ||
} | ||
this.onFinishCallback = this@ScoreboardAgent.onFinishCallback | ||
}.show(activity.supportFragmentManager, "save_score") | ||
} | ||
} | ||
|
||
private fun saveAndStart(name: String, score: Int) { | ||
this.addItem( | ||
ScoreInfo(0, name, score, Date(System.currentTimeMillis())) | ||
) | ||
ScoreboardActivity.actionStart(this@ScoreboardAgent.activity, gameMode, onlineMode) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
app/src/main/java/net/imshit/aircraftwar/data/scoreboard/offline/SaveScoreDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package net.imshit.aircraftwar.data.scoreboard.offline | ||
|
||
import android.app.Dialog | ||
import android.content.DialogInterface | ||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.appcompat.app.AlertDialog | ||
import androidx.appcompat.app.AppCompatDialog | ||
import androidx.appcompat.app.AppCompatDialogFragment | ||
import androidx.core.widget.doOnTextChanged | ||
import com.google.android.material.dialog.MaterialAlertDialogBuilder | ||
import net.imshit.aircraftwar.R | ||
import net.imshit.aircraftwar.databinding.DialogSaveScoreBinding | ||
|
||
class SaveScoreDialog : AppCompatDialogFragment() { | ||
private var dialogView: View? = null | ||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
return inflater.inflate(R.layout.dialog_save_score, container, false) | ||
} | ||
|
||
var onFinishCallback: (() -> Unit)? = null | ||
var onSubmitCallback: ((String) -> Unit)? = null | ||
|
||
override fun onDismiss(dialog: DialogInterface) { | ||
onFinishCallback?.invoke() | ||
super.onDismiss(dialog) | ||
} | ||
|
||
override fun onCreateDialog(savedInstanceState: Bundle?): AppCompatDialog { | ||
return activity?.let { activity -> | ||
onCreateView(requireActivity().layoutInflater, null, savedInstanceState)?.let { view -> | ||
this.dialogView = view | ||
MaterialAlertDialogBuilder(activity).apply { | ||
setTitle(R.string.dialog_save_score_title) | ||
setIcon(R.drawable.ic_assignment_turned_in_24) | ||
setPositiveButton(android.R.string.ok, null) | ||
setNegativeButton(android.R.string.cancel, null) | ||
setView(view) | ||
}.create() | ||
} | ||
} ?: throw IllegalStateException("Activity cannot be null") | ||
} | ||
|
||
override fun getView(): View? { | ||
return this.dialogView | ||
} | ||
|
||
override fun onResume() { | ||
super.onResume() | ||
(dialog as? AlertDialog)?.apply { | ||
val submitButton = getButton(Dialog.BUTTON_POSITIVE) | ||
val cancelButton = getButton((Dialog.BUTTON_NEGATIVE)) | ||
view?.let { DialogSaveScoreBinding.bind(it) }?.apply { | ||
dssTiet.doOnTextChanged { text, _, _, _ -> | ||
submitButton.isEnabled = (text?.length in 2..16) | ||
} | ||
submitButton.isEnabled = false | ||
|
||
submitButton.setOnClickListener { | ||
onSubmitCallback?.invoke(dssTiet.text.toString()) | ||
onFinishCallback?.invoke() | ||
} | ||
cancelButton.setOnClickListener { | ||
onFinishCallback?.invoke() | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.