我的家庭切换
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user