我的家庭切换

This commit is contained in:
2026-05-16 10:15:04 +08:00
parent e00d867265
commit a8f202a906
11 changed files with 220 additions and 64 deletions

View File

@@ -1,19 +1,21 @@
package com.example.defenseapplication.adapter package com.example.defenseapplication.adapter
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.example.defenseapplication.R import com.example.defenseapplication.R
import com.example.defenseapplication.bean.FamilyBean
import com.example.defenseapplication.databinding.ItemFamilyBinding import com.example.defenseapplication.databinding.ItemFamilyBinding
class FamilyAdapter( class FamilyAdapter(
private val onItemClick: (String) -> Unit private val onItemClick: (String) -> Unit
) : RecyclerView.Adapter<FamilyAdapter.FamilyViewHolder>() { ) : RecyclerView.Adapter<FamilyAdapter.FamilyViewHolder>() {
private val items = mutableListOf<String>() private val items = mutableListOf<FamilyBean>()
private var selectedFamily: String? = null private var selectedFamily: String? = null
fun submitList(familyList: List<String>, currentFamily: String) { fun submitList(familyList: List<FamilyBean>, currentFamily: String) {
items.clear() items.clear()
items.addAll(familyList) items.addAll(familyList)
selectedFamily = currentFamily selectedFamily = currentFamily
@@ -34,11 +36,36 @@ class FamilyAdapter(
inner class FamilyViewHolder(private val binding: ItemFamilyBinding) : inner class FamilyViewHolder(private val binding: ItemFamilyBinding) :
RecyclerView.ViewHolder(binding.root) { RecyclerView.ViewHolder(binding.root) {
fun bind(item: String) { fun bind(item: FamilyBean) {
binding.tvFamilyName.text = item binding.tvFamilyName.text = item.parentName
val textColor = if (item == selectedFamily) R.color.green else R.color.black binding.tvFamilyRole.text = getRoleText(item.role)
binding.tvFamilyName.setTextColor(binding.root.context.getColor(textColor))
binding.root.setOnClickListener { onItemClick(item) } val isSelected = item.parentName == selectedFamily
updateSelectedState(isSelected)
binding.root.setOnClickListener { onItemClick(item.parentName) }
}
private fun getRoleText(role: String): String {
return when (role) {
"appadmin" ->binding.root.context.getString(R.string.admin_user)
else -> binding.root.context.getString(R.string.normal_user)
}
}
private fun updateSelectedState(isSelected: Boolean) {
if (isSelected) {
binding.layoutFamilyItem.setBackgroundResource(R.drawable.bg_family_selected)
binding.tvFamilyName.setTextColor(binding.root.context.getColor(R.color.white))
binding.tvFamilyRole.setTextColor(binding.root.context.getColor(R.color.white))
binding.ivCheck.visibility = View.VISIBLE
binding.ivCheck.setImageResource(R.drawable.ic_check_white)
} else {
binding.layoutFamilyItem.setBackgroundResource(0)
binding.tvFamilyName.setTextColor(binding.root.context.getColor(R.color.black))
binding.tvFamilyRole.setTextColor(binding.root.context.getColor(R.color.gray))
binding.ivCheck.visibility = View.GONE
}
} }
} }
} }

View File

@@ -35,9 +35,4 @@ data class ForgotRequest(
val phonenumber: String, val phonenumber: String,
val smsCode: String, val smsCode: String,
val password: String val password: String
)
data class LoginResponse(
val clientid: String,
val content_language: String,
) )

View File

@@ -0,0 +1,9 @@
package com.example.defenseapplication.bean
data class SwitchFamilyRequest(
val clientId: String,
val grantType: String,
val id: String,
val userType: String,
val role: String
)

View File

@@ -14,16 +14,16 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import bean.LoginRequest
import bean.LoginResponse
import com.example.defenseapplication.R import com.example.defenseapplication.R
import com.example.defenseapplication.adapter.DeviceAdapter import com.example.defenseapplication.adapter.DeviceAdapter
import com.example.defenseapplication.adapter.DeviceUi import com.example.defenseapplication.adapter.DeviceUi
import com.example.defenseapplication.adapter.FamilyAdapter import com.example.defenseapplication.adapter.FamilyAdapter
import com.example.defenseapplication.bean.DeviceBean import com.example.defenseapplication.bean.DeviceBean
import com.example.defenseapplication.bean.FamilyBean import com.example.defenseapplication.bean.FamilyBean
import com.example.defenseapplication.bean.SwitchFamilyRequest
import com.example.defenseapplication.databinding.HomeMenuFragmentBinding import com.example.defenseapplication.databinding.HomeMenuFragmentBinding
import com.example.defenseapplication.utils.RetrofitNetwork import com.example.defenseapplication.utils.RetrofitNetwork
import com.example.defenseapplication.utils.UserinfoUtil
import com.example.defenseapplication.view.DefenseDialogs import com.example.defenseapplication.view.DefenseDialogs
import com.example.defenseapplication.view.showDefenseDialog import com.example.defenseapplication.view.showDefenseDialog
import com.gyf.immersionbar.ImmersionBar import com.gyf.immersionbar.ImmersionBar
@@ -45,6 +45,10 @@ class HomeMenuFragment : Fragment() {
private var familyList: List<String> = emptyList() private var familyList: List<String> = emptyList()
private var familyBeanList: List<FamilyBean> = emptyList() private var familyBeanList: List<FamilyBean> = emptyList()
private var deviceBeanList: List<DeviceBean> = emptyList() private var deviceBeanList: List<DeviceBean> = emptyList()
private var canViewDevice = false
private val currentFamilyBean: FamilyBean?
get() = familyBeanList.find { it.parentName == currentFamily }
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
@@ -110,11 +114,6 @@ class HomeMenuFragment : Fragment() {
private fun fetchFamilyList() { private fun fetchFamilyList() {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
try { try {
val loginRequest = LoginResponse(
clientid = "428a8310cd442757ae699df5d894f051",
content_language = "en_US",
)
val response = RetrofitNetwork.getNetworkService().getFamilyList() val response = RetrofitNetwork.getNetworkService().getFamilyList()
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
if (response.code == 200 && response.data != null) { if (response.code == 200 && response.data != null) {
@@ -123,7 +122,14 @@ class HomeMenuFragment : Fragment() {
if (familyList.isNotEmpty()) { if (familyList.isNotEmpty()) {
currentFamily = familyList.first() currentFamily = familyList.first()
binding.tvCurrentFamily.text = currentFamily binding.tvCurrentFamily.text = currentFamily
familyAdapter.submitList(familyList, currentFamily) familyAdapter.submitList(familyBeanList, currentFamily)
val userInfo = UserinfoUtil.getUserInfo()
val currentFamilyData = familyBeanList.firstOrNull()
canViewDevice = userInfo?.role == "appadmin" && currentFamilyData?.role != "member"
updateAdminUI()
switchToFamily(currentFamily)
} }
} else { } else {
Toast.makeText(requireContext(), response.msg, Toast.LENGTH_SHORT).show() Toast.makeText(requireContext(), response.msg, Toast.LENGTH_SHORT).show()
@@ -134,28 +140,79 @@ class HomeMenuFragment : Fragment() {
Toast.makeText(requireContext(), R.string.network_error, Toast.LENGTH_SHORT).show() Toast.makeText(requireContext(), R.string.network_error, Toast.LENGTH_SHORT).show()
} }
} }
fetchDeviceList() }
}
private fun updateAdminUI() {
if (canViewDevice) {
binding.tvDeviceTitle.visibility = View.VISIBLE
binding.rvDeviceList.visibility = View.VISIBLE
binding.message.visibility = View.VISIBLE
binding.addFamily.visibility = View.VISIBLE
binding.add.visibility = View.VISIBLE
binding.layoutNoPermission.visibility = View.GONE
} else {
binding.tvDeviceTitle.visibility = View.GONE
binding.rvDeviceList.visibility = View.GONE
binding.message.visibility = View.GONE
binding.addFamily.visibility = View.GONE
binding.add.visibility = View.GONE
binding.layoutNoPermission.visibility = View.VISIBLE
}
}
private fun switchToFamily(familyName: String) {
val familyBean = familyBeanList.find { it.parentName == familyName } ?: return
CoroutineScope(Dispatchers.IO).launch {
try {
val userInfo = UserinfoUtil.getUserInfo()
val request = SwitchFamilyRequest(
clientId = "428a8310cd442757ae699df5d894f051",
grantType = "switch",
id = familyBean.id,
userType = "app_user",
role = userInfo?.role ?: ""
)
val response = RetrofitNetwork.getNetworkService().switchFamily(request)
withContext(Dispatchers.Main) {
if (response.code == 200) {
val userInfo = UserinfoUtil.getUserInfo()
canViewDevice = userInfo?.role == "appadmin" && familyBean.role != "member"
updateAdminUI()
fetchDeviceList()
} else {
Toast.makeText(requireContext(), response.msg, Toast.LENGTH_SHORT).show()
}
}
} catch (e: Exception) {
withContext(Dispatchers.Main) {
Toast.makeText(requireContext(), R.string.network_error, Toast.LENGTH_SHORT).show()
}
}
} }
} }
private fun fetchDeviceList() { private fun fetchDeviceList() {
// CoroutineScope(Dispatchers.IO).launch { if (!canViewDevice) return
// try {
// val response = RetrofitNetwork.getNetworkService().getDeviceList() CoroutineScope(Dispatchers.IO).launch {
// withContext(Dispatchers.Main) { try {
// if (response.code == 200 && response.data != null) { val response = RetrofitNetwork.getNetworkService().getDeviceList()
// deviceBeanList = response.data withContext(Dispatchers.Main) {
// updateDeviceList() if (response.code == 200 && response.data != null) {
// } else { deviceBeanList = response.data
// Toast.makeText(requireContext(), response.msg, Toast.LENGTH_SHORT).show() updateDeviceList()
// } } else {
// } Toast.makeText(requireContext(), response.msg, Toast.LENGTH_SHORT).show()
// } catch (e: Exception) { }
// withContext(Dispatchers.Main) { }
// Toast.makeText(requireContext(), R.string.network_error, Toast.LENGTH_SHORT).show() } catch (e: Exception) {
// } withContext(Dispatchers.Main) {
// } Toast.makeText(requireContext(), R.string.network_error, Toast.LENGTH_SHORT).show()
// } }
}
}
} }
private fun toggleFamilyList() { private fun toggleFamilyList() {
@@ -172,25 +229,25 @@ class HomeMenuFragment : Fragment() {
private fun onFamilySelected(familyName: String) { private fun onFamilySelected(familyName: String) {
currentFamily = familyName currentFamily = familyName
binding.tvCurrentFamily.text = familyName binding.tvCurrentFamily.text = familyName
familyAdapter.submitList(familyList, currentFamily) familyAdapter.submitList(familyBeanList, currentFamily)
updateDeviceList() switchToFamily(familyName)
if (isFamilyExpanded) { if (isFamilyExpanded) {
toggleFamilyList() toggleFamilyList()
} }
} }
private fun updateDeviceList() { private fun updateDeviceList() {
val currentFamilyBean = familyBeanList.find { it.parentName == currentFamily } val currentFamilyId = currentFamilyBean?.id
val filteredDevices = if (currentFamilyBean != null) { val filteredDevices = if (currentFamilyId != null) {
deviceBeanList.filter { it.parentId == currentFamilyBean.id } deviceBeanList.filter { it.parentId == currentFamilyId }
} else { } else {
deviceBeanList deviceBeanList
} }
val deviceUiList = filteredDevices.map { val deviceUiList = filteredDevices.map {
DeviceUi(it.deviceName, it.status == "online") DeviceUi(it.deviceName, it.status == "online")
} }
if (deviceUiList.isEmpty()) { if (deviceUiList.isEmpty()) {
deviceAdapter.submitList(listOf( deviceAdapter.submitList(listOf(
DeviceUi(getString(R.string.no_device), false) DeviceUi(getString(R.string.no_device), false)

View File

@@ -237,7 +237,6 @@ class LoginActivity : AppCompatActivity() {
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent) startActivity(intent)
finish() finish()
//1
} else { } else {
Toast.makeText(this@LoginActivity, response.msg, Toast.LENGTH_SHORT).show() Toast.makeText(this@LoginActivity, response.msg, Toast.LENGTH_SHORT).show()
} }

View File

@@ -7,6 +7,7 @@ import com.example.defenseapplication.bean.GetCodeBean
import bean.LoginBean import bean.LoginBean
import bean.LoginRequest import bean.LoginRequest
import bean.RegisterRequest import bean.RegisterRequest
import com.example.defenseapplication.bean.SwitchFamilyRequest
import retrofit2.http.Body import retrofit2.http.Body
import retrofit2.http.GET import retrofit2.http.GET
import retrofit2.http.POST import retrofit2.http.POST
@@ -46,7 +47,15 @@ interface ApiService {
/** /**
* 切换家庭 * 切换家庭
*/ */
//@POST("/app/switchFamily") @POST("/app/switchFamily")
//suspend fun switchFamily(@Body request: com.example.defenseapplication.bean.SwitchFamilyRequest): GetCodeBean<Any> suspend fun switchFamily(@Body request: SwitchFamilyRequest): GetCodeBean<Any>
/**
* 获取设备列表
*/
@GET("/app/getdeviceList")
suspend fun getDeviceList(): GetCodeBean<List<DeviceBean>>
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

@@ -36,17 +36,20 @@
<TextView <TextView
android:id="@+id/tvCurrentFamily" android:id="@+id/tvCurrentFamily"
android:layout_width="wrap_content" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/my_family_home" android:text="@string/my_family_home"
android:textColor="@color/black" android:textColor="@color/black"
android:textSize="20dp" android:textSize="20dp"
android:maxLines="1"
android:ellipsize="end"
android:textStyle="bold" /> android:textStyle="bold" />
<ImageView <ImageView
android:id="@+id/ivFamilyArrow" android:id="@+id/ivFamilyArrow"
android:layout_width="12dp" android:layout_width="12dp"
android:layout_height="wrap_content" android:layout_height="6dp"
android:layout_marginStart="6dp" android:layout_marginStart="6dp"
android:src="@drawable/triangle" /> android:src="@drawable/triangle" />
</LinearLayout> </LinearLayout>
@@ -144,6 +147,24 @@
android:nestedScrollingEnabled="false" android:nestedScrollingEnabled="false"
android:paddingBottom="16dp" android:paddingBottom="16dp"
tools:listitem="@layout/item_device" /> tools:listitem="@layout/item_device" />
<LinearLayout
android:id="@+id/layoutNoPermission"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:paddingHorizontal="32dp"
android:background="@color/white"
android:visibility="gone">
<ImageView
android:layout_width="244dp"
android:layout_height="131dp"
android:src="@drawable/default_page" />
</LinearLayout>
</LinearLayout> </LinearLayout>
<View <View
@@ -157,15 +178,15 @@
<LinearLayout <LinearLayout
android:id="@+id/familyPopupCard" android:id="@+id/familyPopupCard"
android:layout_width="180dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="16dp" android:layout_marginStart="16dp"
android:layout_marginTop="62dp" android:layout_marginTop="92dp"
android:background="@drawable/textinput_white_bg" android:background="@drawable/bg_family_popup"
android:elevation="8dp" android:elevation="12dp"
android:orientation="vertical" android:orientation="vertical"
android:visibility="gone" android:visibility="gone"
android:padding="10dp"> android:padding="4dp">
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvFamilyList" android:id="@+id/rvFamilyList"

View File

@@ -1,12 +1,42 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tvFamilyName" android:id="@+id/layoutFamilyItem"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/textinput_white_bg" android:orientation="horizontal"
android:layout_marginBottom="8dp" android:gravity="center_vertical"
android:paddingHorizontal="14dp" android:paddingHorizontal="12dp"
android:paddingVertical="12dp" android:paddingVertical="10dp"
android:textColor="@color/black" android:background="?attr/selectableItemBackground"
android:text="张三" android:clickable="true">
android:textSize="16sp" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/tvFamilyName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#333333"/>
<TextView
android:id="@+id/tvFamilyRole"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:textSize="12sp"
android:textColor="#999999"/>
</LinearLayout>
<ImageView
android:id="@+id/ivCheck"
android:layout_width="20dp"
android:layout_height="20dp"
android:visibility="gone"/>
</LinearLayout>

View File

@@ -60,6 +60,8 @@
<!-- Home Activity --> <!-- Home Activity -->
<string name="home">首页</string> <string name="home">首页</string>
<string name="profile">个人</string> <string name="profile">个人</string>
<string name="admin_user">管理者</string>
<string name="normal_user">普通用户</string>
<!-- Home Menu --> <!-- Home Menu -->
<string name="my_devices">我的设备</string> <string name="my_devices">我的设备</string>
@@ -82,6 +84,8 @@
<string name="bind_device">绑定设备</string> <string name="bind_device">绑定设备</string>
<string name="unbind_device">解绑设备</string> <string name="unbind_device">解绑设备</string>
<string name="device_not_found">暂无设备</string> <string name="device_not_found">暂无设备</string>
<string name="no_permission_title">暂无权限</string>
<string name="no_permission_desc">您暂无权查看设备,请联系管理员</string>
<!-- Add Device --> <!-- Add Device -->
<string name="add_device_title">添加设备</string> <string name="add_device_title">添加设备</string>

View File

@@ -54,6 +54,9 @@
<!-- Home Activity --> <!-- Home Activity -->
<string name="home">Home</string> <string name="home">Home</string>
<string name="profile">Profile</string> <string name="profile">Profile</string>
<string name="admin_user">Administrator</string>
<string name="normal_user">Ordinary User</string>
<!-- Home Menu --> <!-- Home Menu -->
<string name="my_devices">My Devices</string> <string name="my_devices">My Devices</string>
@@ -75,6 +78,8 @@
<string name="unbind_device">Unbind Device</string> <string name="unbind_device">Unbind Device</string>
<string name="device_not_found">No devices found</string> <string name="device_not_found">No devices found</string>
<string name="no_device">No device</string> <string name="no_device">No device</string>
<string name="no_permission_title">No permission</string>
<string name="no_permission_desc">You do not have permission to view devices</string>
<!-- Add Device --> <!-- Add Device -->
<string name="add_device_title">Add Device</string> <string name="add_device_title">Add Device</string>