Skip to content

Commit

Permalink
feat: 主界面设计
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim-shop committed May 25, 2023
1 parent 8b7dc66 commit 46542cd
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -83,6 +97,7 @@ object AccountManager {
setNegativeButton(android.R.string.cancel, dialogListener)
show()
}

}
}
}
23 changes: 15 additions & 8 deletions app/src/main/java/net/imshit/aircraftwar/gui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_public_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#000000"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM11,19.93c-3.95,-0.49 -7,-3.85 -7,-7.93 0,-0.62 0.08,-1.21 0.21,-1.79L9,15v1c0,1.1 0.9,2 2,2v1.93zM17.9,17.39c-0.26,-0.81 -1,-1.39 -1.9,-1.39h-1v-3c0,-0.55 -0.45,-1 -1,-1L8,12v-2h2c0.55,0 1,-0.45 1,-1L11,7h2c1.1,0 2,-0.9 2,-2v-0.41c2.93,1.19 5,4.06 5,7.41 0,2.08 -0.8,3.97 -2.1,5.39z" />
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_public_off_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#000000"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M11,8.17L6.49,3.66C8.07,2.61 9.96,2 12,2c5.52,0 10,4.48 10,10c0,2.04 -0.61,3.93 -1.66,5.51l-1.46,-1.46C19.59,14.87 20,13.48 20,12c0,-3.35 -2.07,-6.22 -5,-7.41V5c0,1.1 -0.9,2 -2,2h-2V8.17zM21.19,21.19l-1.41,1.41l-2.27,-2.27C15.93,21.39 14.04,22 12,22C6.48,22 2,17.52 2,12c0,-2.04 0.61,-3.93 1.66,-5.51L1.39,4.22l1.41,-1.41L21.19,21.19zM11,18c-1.1,0 -2,-0.9 -2,-2v-1l-4.79,-4.79C4.08,10.79 4,11.38 4,12c0,4.08 3.05,7.44 7,7.93V18z" />
</vector>
68 changes: 50 additions & 18 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
</com.google.android.material.appbar.AppBarLayout>
Expand All @@ -29,52 +29,84 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:id="@+id/am_ll_buttons"
<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/am_sw_sound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/checkbox_sound"
app:layout_constraintBottom_toTopOf="@id/am_mbtg_mode"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:thumbIcon="@drawable/ic_music_note_24" />

<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/am_mbtg_mode"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:checkedButton="@id/am_btn_easy"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
app:layout_constraintTop_toTopOf="parent"
app:selectionRequired="true"
app:singleSelection="true">

<Button
android:id="@+id/am_btn_easy"
android:layout_width="240dp"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/button_easy"
app:icon="@drawable/ic_brightness_low_24" />

<Button
android:id="@+id/am_btn_medium"
android:layout_width="240dp"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/button_medium"
app:icon="@drawable/ic_brightness_medium_24" />

<Button
android:id="@+id/am_btn_hard"
android:layout_width="240dp"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/button_hard"
app:icon="@drawable/ic_brightness_high_24" />
</LinearLayout>
</com.google.android.material.button.MaterialButtonToggleGroup>

<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/am_sw_sound"
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/checkbox_sound"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/am_ll_buttons"
app:thumbIcon="@drawable/ic_music_note_24" />
app:layout_constraintTop_toBottomOf="@id/am_mbtg_mode">

<Button
android:id="@+id/am_btn_offline"
style="@style/Widget.Material3.Button.TextButton.Icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="@string/button_offline"
app:icon="@drawable/ic_public_off_24" />

<Button
android:id="@+id/am_btn_online"
style="@style/Widget.Material3.Button.TonalButton.Icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="@string/button_online"
app:icon="@drawable/ic_public_24" />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values-en/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@
<item quantity="one">%d item selected.</item>
<item quantity="other">%d items selected.</item>
</plurals>
<string name="button_offline">Play offline</string>
<string name="button_online">Compete online</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<string name="dialog_login_button_register">注册</string>
<string name="account_label">用户信息</string>
<string name="navback">返回</string>
<string name="button_offline">单机游戏</string>
<string name="button_online">在线对战</string>
<plurals name="scoreboard_action_title">
<item quantity="other">已选中 %d 项</item>
</plurals>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<string name="dialog_login_button_register">注册</string>
<string name="account_label">用户信息</string>
<string name="navback">返回</string>
<string name="button_offline">单机游戏</string>
<string name="button_online">在线对战</string>
<plurals name="scoreboard_action_title">
<item quantity="other">已选中 %d 项</item>
</plurals>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
<string name="dialog_login_button_register">注册</string>
<string name="account_label">用户信息</string>
<string name="navback">返回</string>
<string name="button_offline">单机游戏</string>
<string name="button_online">在线对战</string>
<plurals name="scoreboard_action_title">
<item quantity="other">已选中 %d 项</item>
</plurals>
Expand Down

0 comments on commit 46542cd

Please sign in to comment.