我的家庭切换

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
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.defenseapplication.R
import com.example.defenseapplication.bean.FamilyBean
import com.example.defenseapplication.databinding.ItemFamilyBinding
class FamilyAdapter(
private val onItemClick: (String) -> Unit
) : RecyclerView.Adapter<FamilyAdapter.FamilyViewHolder>() {
private val items = mutableListOf<String>()
private val items = mutableListOf<FamilyBean>()
private var selectedFamily: String? = null
fun submitList(familyList: List<String>, currentFamily: String) {
fun submitList(familyList: List<FamilyBean>, currentFamily: String) {
items.clear()
items.addAll(familyList)
selectedFamily = currentFamily
@@ -34,11 +36,36 @@ class FamilyAdapter(
inner class FamilyViewHolder(private val binding: ItemFamilyBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(item: String) {
binding.tvFamilyName.text = item
val textColor = if (item == selectedFamily) R.color.green else R.color.black
binding.tvFamilyName.setTextColor(binding.root.context.getColor(textColor))
binding.root.setOnClickListener { onItemClick(item) }
fun bind(item: FamilyBean) {
binding.tvFamilyName.text = item.parentName
binding.tvFamilyRole.text = getRoleText(item.role)
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 smsCode: 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.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import bean.LoginRequest
import bean.LoginResponse
import com.example.defenseapplication.R
import com.example.defenseapplication.adapter.DeviceAdapter
import com.example.defenseapplication.adapter.DeviceUi
import com.example.defenseapplication.adapter.FamilyAdapter
import com.example.defenseapplication.bean.DeviceBean
import com.example.defenseapplication.bean.FamilyBean
import com.example.defenseapplication.bean.SwitchFamilyRequest
import com.example.defenseapplication.databinding.HomeMenuFragmentBinding
import com.example.defenseapplication.utils.RetrofitNetwork
import com.example.defenseapplication.utils.UserinfoUtil
import com.example.defenseapplication.view.DefenseDialogs
import com.example.defenseapplication.view.showDefenseDialog
import com.gyf.immersionbar.ImmersionBar
@@ -45,6 +45,10 @@ class HomeMenuFragment : Fragment() {
private var familyList: List<String> = emptyList()
private var familyBeanList: List<FamilyBean> = emptyList()
private var deviceBeanList: List<DeviceBean> = emptyList()
private var canViewDevice = false
private val currentFamilyBean: FamilyBean?
get() = familyBeanList.find { it.parentName == currentFamily }
override fun onCreateView(
inflater: LayoutInflater,
@@ -110,11 +114,6 @@ class HomeMenuFragment : Fragment() {
private fun fetchFamilyList() {
CoroutineScope(Dispatchers.IO).launch {
try {
val loginRequest = LoginResponse(
clientid = "428a8310cd442757ae699df5d894f051",
content_language = "en_US",
)
val response = RetrofitNetwork.getNetworkService().getFamilyList()
withContext(Dispatchers.Main) {
if (response.code == 200 && response.data != null) {
@@ -123,7 +122,14 @@ class HomeMenuFragment : Fragment() {
if (familyList.isNotEmpty()) {
currentFamily = familyList.first()
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 {
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()
}
}
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() {
// CoroutineScope(Dispatchers.IO).launch {
// try {
// val response = RetrofitNetwork.getNetworkService().getDeviceList()
// withContext(Dispatchers.Main) {
// if (response.code == 200 && response.data != null) {
// deviceBeanList = response.data
// 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()
// }
// }
// }
if (!canViewDevice) return
CoroutineScope(Dispatchers.IO).launch {
try {
val response = RetrofitNetwork.getNetworkService().getDeviceList()
withContext(Dispatchers.Main) {
if (response.code == 200 && response.data != null) {
deviceBeanList = response.data
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()
}
}
}
}
private fun toggleFamilyList() {
@@ -172,25 +229,25 @@ class HomeMenuFragment : Fragment() {
private fun onFamilySelected(familyName: String) {
currentFamily = familyName
binding.tvCurrentFamily.text = familyName
familyAdapter.submitList(familyList, currentFamily)
updateDeviceList()
familyAdapter.submitList(familyBeanList, currentFamily)
switchToFamily(familyName)
if (isFamilyExpanded) {
toggleFamilyList()
}
}
private fun updateDeviceList() {
val currentFamilyBean = familyBeanList.find { it.parentName == currentFamily }
val filteredDevices = if (currentFamilyBean != null) {
deviceBeanList.filter { it.parentId == currentFamilyBean.id }
val currentFamilyId = currentFamilyBean?.id
val filteredDevices = if (currentFamilyId != null) {
deviceBeanList.filter { it.parentId == currentFamilyId }
} else {
deviceBeanList
}
val deviceUiList = filteredDevices.map {
DeviceUi(it.deviceName, it.status == "online")
}
if (deviceUiList.isEmpty()) {
deviceAdapter.submitList(listOf(
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
startActivity(intent)
finish()
//1
} else {
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.LoginRequest
import bean.RegisterRequest
import com.example.defenseapplication.bean.SwitchFamilyRequest
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
@@ -46,7 +47,15 @@ interface ApiService {
/**
* 切换家庭
*/
//@POST("/app/switchFamily")
//suspend fun switchFamily(@Body request: com.example.defenseapplication.bean.SwitchFamilyRequest): GetCodeBean<Any>
@POST("/app/switchFamily")
suspend fun switchFamily(@Body request: SwitchFamilyRequest): GetCodeBean<Any>
/**
* 获取设备列表
*/
@GET("/app/getdeviceList")
suspend fun getDeviceList(): GetCodeBean<List<DeviceBean>>
}