From 46542cd9f7193e7f22bb0d0b46c98fda4b773400 Mon Sep 17 00:00:00 2001 From: js Date: Fri, 26 May 2023 06:28:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=BB=E7=95=8C=E9=9D=A2=E8=AE=BE?= =?UTF-8?q?=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../data/account/AccountManager.kt | 45 ++++++++---- .../imshit/aircraftwar/gui/MainActivity.kt | 23 ++++--- app/src/main/res/drawable/ic_public_24.xml | 10 +++ .../main/res/drawable/ic_public_off_24.xml | 10 +++ app/src/main/res/layout/activity_main.xml | 68 ++++++++++++++----- app/src/main/res/values-en/strings.xml | 2 + app/src/main/res/values-zh-rCN/strings.xml | 2 + app/src/main/res/values-zh/strings.xml | 2 + app/src/main/res/values/strings.xml | 2 + 9 files changed, 123 insertions(+), 41 deletions(-) create mode 100644 app/src/main/res/drawable/ic_public_24.xml create mode 100644 app/src/main/res/drawable/ic_public_off_24.xml diff --git a/app/src/main/java/net/imshit/aircraftwar/data/account/AccountManager.kt b/app/src/main/java/net/imshit/aircraftwar/data/account/AccountManager.kt index 2500f25..6590528 100644 --- a/app/src/main/java/net/imshit/aircraftwar/data/account/AccountManager.kt +++ b/app/src/main/java/net/imshit/aircraftwar/data/account/AccountManager.kt @@ -57,24 +57,38 @@ object AccountManager { } private fun register(account: String, password: String, onSuccess: Runnable, onFail: Runnable) { - Thread { - // TODO - login(account, password, onSuccess, onFail) - }.start() + val requestBody = + FormBody.Builder().add("user", account).add("password", encodePassword(password)) + .build() + val request = Request.Builder().url(REGISTER_URL).post(requestBody).build() + httpClient.newCall(request).enqueue(object : Callback { + override fun onFailure(call: Call, e: IOException) { + onFail.run() + } + + override fun onResponse(call: Call, response: Response) { + if (response.isSuccessful) { + // TODO + login(account, password, onSuccess, onFail) + } else { + onFail.run() + } + } + }) } fun showLoginDialog(context: Context, onSuccess: Runnable, onFail: Runnable) { - with(MaterialAlertDialogBuilder(context)) { - with(DialogLoginBinding.inflate(LayoutInflater.from(context), null, false)) { - val dialogListener = DialogInterface.OnClickListener { _, which -> - val account = dlTietAc.text.toString() - val password = dlTietPw.text.toString() - when (which) { - DialogInterface.BUTTON_POSITIVE -> ::login - DialogInterface.BUTTON_NEUTRAL -> ::register - else -> null - }?.invoke(account, password, onSuccess, onFail) - } + with(DialogLoginBinding.inflate(LayoutInflater.from(context), null, false)) { + val dialogListener = DialogInterface.OnClickListener { _, which -> + val account = dlTietAc.text.toString() + val password = dlTietPw.text.toString() + when (which) { + DialogInterface.BUTTON_POSITIVE -> ::login + DialogInterface.BUTTON_NEUTRAL -> ::register + else -> null + }?.invoke(account, password, onSuccess, onFail) + } + MaterialAlertDialogBuilder(context).run { setView(root) setTitle(R.string.dialog_login_title) setIcon(R.drawable.ic_login_24) @@ -83,6 +97,7 @@ object AccountManager { setNegativeButton(android.R.string.cancel, dialogListener) show() } + } } } \ No newline at end of file diff --git a/app/src/main/java/net/imshit/aircraftwar/gui/MainActivity.kt b/app/src/main/java/net/imshit/aircraftwar/gui/MainActivity.kt index af1d6fc..02cddbe 100644 --- a/app/src/main/java/net/imshit/aircraftwar/gui/MainActivity.kt +++ b/app/src/main/java/net/imshit/aircraftwar/gui/MainActivity.kt @@ -8,6 +8,9 @@ import net.imshit.aircraftwar.databinding.ActivityMainBinding import net.imshit.aircraftwar.logic.game.Difficulty class MainActivity : AppCompatActivity() { + + var gameMode = Difficulty.EASY + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -25,15 +28,19 @@ class MainActivity : AppCompatActivity() { return@setOnMenuItemClickListener true } - // setup listener - amBtnEasy.setOnClickListener { - GameActivity.actionStart(this@MainActivity, Difficulty.EASY, amSwSound.isChecked) - } - amBtnMedium.setOnClickListener { - GameActivity.actionStart(this@MainActivity, Difficulty.MEDIUM, amSwSound.isChecked) + amMbtgMode.addOnButtonCheckedListener { toggleButton, checkedId, isChecked -> + if (isChecked) { + gameMode = when (checkedId) { + R.id.am_btn_easy -> Difficulty.EASY + R.id.am_btn_medium -> Difficulty.MEDIUM + R.id.am_btn_hard -> Difficulty.HARD + else -> Difficulty.EASY + } + } } - amBtnHard.setOnClickListener { - GameActivity.actionStart(this@MainActivity, Difficulty.HARD, amSwSound.isChecked) + + amBtnOffline.setOnClickListener { + GameActivity.actionStart(this@MainActivity, gameMode, amSwSound.isChecked) } } } diff --git a/app/src/main/res/drawable/ic_public_24.xml b/app/src/main/res/drawable/ic_public_24.xml new file mode 100644 index 0000000..c5b8e48 --- /dev/null +++ b/app/src/main/res/drawable/ic_public_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/ic_public_off_24.xml b/app/src/main/res/drawable/ic_public_off_24.xml new file mode 100644 index 0000000..21ff840 --- /dev/null +++ b/app/src/main/res/drawable/ic_public_off_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 4c43bfb..904d33d 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -17,10 +17,10 @@ android:layout_height="wrap_content" android:fitsSystemWindows="true" app:menu="@menu/main" - app:subtitle="@string/main_subtitle" - app:subtitleCentered="true" app:navigationContentDescription="@string/main_account" app:navigationIcon="@drawable/ic_account_circle_24" + app:subtitle="@string/main_subtitle" + app:subtitleCentered="true" app:title="@string/main_label" app:titleCentered="true" /> @@ -29,52 +29,84 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - + + + app:layout_constraintTop_toTopOf="parent" + app:selectionRequired="true" + app:singleSelection="true">