切换家庭联动
This commit is contained in:
@@ -5,6 +5,7 @@ import android.graphics.Color
|
||||
import android.graphics.Rect
|
||||
import android.graphics.drawable.GradientDrawable
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.util.TypedValue
|
||||
import android.widget.Toast
|
||||
import androidx.fragment.app.Fragment
|
||||
@@ -22,6 +23,8 @@ 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.FamilySwitchEvent
|
||||
import com.example.defenseapplication.utils.MessageStatusManager
|
||||
import com.example.defenseapplication.utils.RetrofitNetwork
|
||||
import com.example.defenseapplication.utils.UserinfoUtil
|
||||
import com.example.defenseapplication.view.DefenseDialogs
|
||||
@@ -42,6 +45,33 @@ class HomeMenuFragment : Fragment() {
|
||||
private var isFamilyExpanded = false
|
||||
private lateinit var currentFamily: String
|
||||
|
||||
private val unreadCountChangeListener = object : MessageStatusManager.UnreadCountChangeListener {
|
||||
override fun onUnreadCountChanged(count: Int) {
|
||||
updateUnreadCountUI(count)
|
||||
}
|
||||
}
|
||||
|
||||
private val familySwitchListener = object : FamilySwitchEvent.FamilySwitchListener {
|
||||
override fun onFamilySwitched(familyName: String) {
|
||||
if (familyName != currentFamily) {
|
||||
currentFamily = familyName
|
||||
updateFamilyDisplayText(familyName)
|
||||
familyAdapter.submitList(familyBeanList, currentFamily)
|
||||
switchToFamily(familyName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateFamilyDisplayText(familyName: String) {
|
||||
val familyBean = familyBeanList.find { it.parentName == familyName }
|
||||
val isAdmin = familyBean?.role == "appadmin"
|
||||
binding.tvCurrentFamily.text = if (isAdmin) {
|
||||
getString(R.string.my_family)
|
||||
} else {
|
||||
"${familyName}${getString(R.string.family_with_normal)}"
|
||||
}
|
||||
}
|
||||
|
||||
private var familyList: List<String> = emptyList()
|
||||
private var familyBeanList: List<FamilyBean> = emptyList()
|
||||
private var deviceBeanList: List<DeviceBean> = emptyList()
|
||||
@@ -87,6 +117,11 @@ class HomeMenuFragment : Fragment() {
|
||||
}
|
||||
|
||||
fetchFamilyList()
|
||||
fetchUnreadMessageCount()
|
||||
|
||||
MessageStatusManager.addListener(unreadCountChangeListener)
|
||||
FamilySwitchEvent.addListener(familySwitchListener)
|
||||
updateUnreadCountUI(MessageStatusManager.getUnreadCount())
|
||||
}
|
||||
|
||||
private fun initView() {
|
||||
@@ -111,17 +146,49 @@ class HomeMenuFragment : Fragment() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun fetchUnreadMessageCount() {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
val response = RetrofitNetwork.getNetworkService().getMessage()
|
||||
withContext(Dispatchers.Main) {
|
||||
if (response.code == 200 && response.data != null) {
|
||||
val messageGroups = response.data as List<*>
|
||||
var unreadCount = 0
|
||||
messageGroups.forEach { group ->
|
||||
if (group is Map<*, *>) {
|
||||
val list = group["list"] as? List<*>
|
||||
list?.let { unreadCount += it.size }
|
||||
}
|
||||
}
|
||||
MessageStatusManager.setUnreadCount(unreadCount)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateUnreadCountUI(count: Int) {
|
||||
if (count > 0) {
|
||||
binding.tvUnreadCount.visibility = View.VISIBLE
|
||||
binding.tvUnreadCount.text = if (count > 99) "99+" else count.toString()
|
||||
} else {
|
||||
binding.tvUnreadCount.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
|
||||
private fun fetchFamilyList() {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
val response = RetrofitNetwork.getNetworkService().getFamilyList()
|
||||
withContext(Dispatchers.Main) {
|
||||
if (response.code == 200 && response.data != null) {
|
||||
familyBeanList = response.data
|
||||
familyList = response.data.map { it.parentName }
|
||||
familyBeanList = response.data.reversed()
|
||||
familyList = response.data.map { it.parentName }.reversed()
|
||||
if (familyList.isNotEmpty()) {
|
||||
currentFamily = familyList.first()
|
||||
binding.tvCurrentFamily.text = currentFamily
|
||||
updateFamilyDisplayText(currentFamily)
|
||||
familyAdapter.submitList(familyBeanList, currentFamily)
|
||||
|
||||
val userInfo = UserinfoUtil.getUserInfo()
|
||||
@@ -144,6 +211,7 @@ class HomeMenuFragment : Fragment() {
|
||||
}
|
||||
|
||||
private fun updateAdminUI() {
|
||||
Log.d("HomeMenuFragment", "canViewDevice = $canViewDevice")
|
||||
if (canViewDevice) {
|
||||
binding.tvDeviceTitle.visibility = View.VISIBLE
|
||||
binding.rvDeviceList.visibility = View.VISIBLE
|
||||
@@ -180,7 +248,9 @@ class HomeMenuFragment : Fragment() {
|
||||
val userInfo = UserinfoUtil.getUserInfo()
|
||||
canViewDevice = userInfo?.role == "appadmin" && familyBean.role != "member"
|
||||
updateAdminUI()
|
||||
updateFamilyDisplayText(familyName)
|
||||
fetchDeviceList()
|
||||
FamilySwitchEvent.notifyFamilySwitched(familyName)
|
||||
} else {
|
||||
Toast.makeText(requireContext(), response.msg, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
@@ -200,8 +270,10 @@ class HomeMenuFragment : Fragment() {
|
||||
try {
|
||||
val response = RetrofitNetwork.getNetworkService().getDeviceList()
|
||||
withContext(Dispatchers.Main) {
|
||||
Log.d("DeviceList", "code = ${response.code}, data = ${response.data}")
|
||||
if (response.code == 200 && response.data != null) {
|
||||
deviceBeanList = response.data
|
||||
Log.d("DeviceList", "deviceBeanList size = ${deviceBeanList.size}")
|
||||
updateDeviceList()
|
||||
} else {
|
||||
Toast.makeText(requireContext(), response.msg, Toast.LENGTH_SHORT).show()
|
||||
@@ -237,15 +309,8 @@ class HomeMenuFragment : Fragment() {
|
||||
}
|
||||
|
||||
private fun updateDeviceList() {
|
||||
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")
|
||||
val deviceUiList = deviceBeanList.map {
|
||||
DeviceUi(it.deviceName, it.status == "0")
|
||||
}
|
||||
|
||||
if (deviceUiList.isEmpty()) {
|
||||
@@ -307,6 +372,8 @@ class HomeMenuFragment : Fragment() {
|
||||
super.onDestroyView()
|
||||
binding.rvFamilyList.adapter = null
|
||||
binding.rvDeviceList.adapter = null
|
||||
MessageStatusManager.removeListener(unreadCountChangeListener)
|
||||
FamilySwitchEvent.removeListener(familySwitchListener)
|
||||
_binding = null // 避免内存泄漏
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.example.defenseapplication.home
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.view.ViewCompat
|
||||
@@ -8,8 +9,16 @@ import androidx.core.view.WindowInsetsCompat
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.example.defenseapplication.adapter.MessageListAdapter
|
||||
import com.example.defenseapplication.adapter.MessageListItem
|
||||
import com.example.defenseapplication.bean.MessageGroup
|
||||
import com.example.defenseapplication.bean.MessageItem
|
||||
import com.example.defenseapplication.databinding.MessageActivityBinding
|
||||
import com.example.defenseapplication.utils.MessageStatusManager
|
||||
import com.example.defenseapplication.utils.RetrofitNetwork
|
||||
import com.gyf.immersionbar.ImmersionBar
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
//消息通知页面
|
||||
class MessageActivity : AppCompatActivity() {
|
||||
private lateinit var binding: MessageActivityBinding
|
||||
@@ -17,80 +26,94 @@ class MessageActivity : AppCompatActivity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
binding = MessageActivityBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
ImmersionBar.with(this)
|
||||
.statusBarDarkFont(true)
|
||||
.titleBar(binding.topBar)
|
||||
.init()
|
||||
ViewCompat.setOnApplyWindowInsetsListener(binding.main) { v, insets ->
|
||||
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
|
||||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
|
||||
insets
|
||||
}
|
||||
initView()
|
||||
MessageStatusManager.clearUnreadCount()
|
||||
loadMessageList()
|
||||
}
|
||||
|
||||
private fun initView() {
|
||||
binding.ivBack.setOnClickListener { finish() }
|
||||
binding.rvMessageList.layoutManager = LinearLayoutManager(this)
|
||||
binding.rvMessageList.adapter = messageAdapter
|
||||
messageAdapter.submitList(buildMockData())
|
||||
}
|
||||
|
||||
private fun buildMockData(): List<MessageListItem> {
|
||||
return listOf(
|
||||
MessageListItem.Header("今天"),
|
||||
MessageListItem.Message(
|
||||
time = "15:54",
|
||||
title = "有人入侵",
|
||||
dateTime = "2026-05-06 15:54:25",
|
||||
unread = true
|
||||
),
|
||||
MessageListItem.Message(
|
||||
time = "12:54",
|
||||
title = "有人入侵",
|
||||
dateTime = "2026-05-06 15:54:25",
|
||||
unread = true
|
||||
),
|
||||
MessageListItem.Message(
|
||||
time = "11:54",
|
||||
title = "有人入侵",
|
||||
dateTime = "2026-05-06 15:54:25",
|
||||
unread = true
|
||||
),
|
||||
MessageListItem.Header("05-05"),
|
||||
MessageListItem.Message(
|
||||
time = "15:54",
|
||||
title = "设备故障",
|
||||
dateTime = "2026-05-06 15:54:25",
|
||||
unread = false
|
||||
),
|
||||
MessageListItem.Message(
|
||||
time = "08:07",
|
||||
title = "设备离线",
|
||||
dateTime = "2026-05-06 15:54:25",
|
||||
unread = false
|
||||
),
|
||||
MessageListItem.Message(
|
||||
time = "08:07",
|
||||
title = "到期提醒",
|
||||
dateTime = "2026-05-06 15:54:25",
|
||||
unread = false
|
||||
),
|
||||
MessageListItem.Message(
|
||||
time = "11:54",
|
||||
title = "有人入侵",
|
||||
dateTime = "2026-05-06 15:54:25",
|
||||
unread = false
|
||||
),
|
||||
MessageListItem.Message(
|
||||
time = "11:54",
|
||||
title = "有人入侵",
|
||||
dateTime = "2026-05-06 15:54:25",
|
||||
unread = false
|
||||
)
|
||||
)
|
||||
private fun loadMessageList() {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
val response = RetrofitNetwork.getNetworkService().getMessage()
|
||||
if (response.code == 0 && response.data != null) {
|
||||
val messageGroups = response.data as List<MessageGroup>
|
||||
val items = convertToMessageListItems(messageGroups)
|
||||
withContext(Dispatchers.Main) {
|
||||
if (items.isEmpty()) {
|
||||
showEmptyState()
|
||||
} else {
|
||||
hideEmptyState()
|
||||
messageAdapter.submitList(items)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
withContext(Dispatchers.Main) {
|
||||
showEmptyState()
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
withContext(Dispatchers.Main) {
|
||||
showEmptyState()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun showEmptyState() {
|
||||
binding.contentLayout.visibility = View.GONE
|
||||
binding.emptyLayout.visibility = View.VISIBLE
|
||||
}
|
||||
|
||||
private fun hideEmptyState() {
|
||||
binding.contentLayout.visibility = View.VISIBLE
|
||||
binding.emptyLayout.visibility = View.GONE
|
||||
}
|
||||
|
||||
private fun convertToMessageListItems(groups: List<MessageGroup>): List<MessageListItem> {
|
||||
val items = mutableListOf<MessageListItem>()
|
||||
groups.forEach { group ->
|
||||
val headerTitle = formatDate(group.time)
|
||||
items.add(MessageListItem.Header(headerTitle))
|
||||
group.list.forEach { message ->
|
||||
items.add(
|
||||
MessageListItem.Message(
|
||||
time = extractTime(message.createTime),
|
||||
title = message.categoryName,
|
||||
dateTime = message.createTime,
|
||||
unread = false
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
private fun formatDate(dateStr: String): String {
|
||||
return if (dateStr == "今天" || dateStr.contains("今天")) {
|
||||
"今天"
|
||||
} else {
|
||||
dateStr
|
||||
}
|
||||
}
|
||||
|
||||
private fun extractTime(dateTime: String): String {
|
||||
return if (dateTime.contains(" ")) {
|
||||
dateTime.split(" ")[1].substring(0, 5)
|
||||
} else {
|
||||
dateTime
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import androidx.fragment.app.Fragment
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Toast
|
||||
import com.bumptech.glide.Glide
|
||||
import com.example.defenseapplication.R
|
||||
import com.example.defenseapplication.databinding.PersonalFragmentBinding
|
||||
@@ -13,9 +14,15 @@ import com.example.defenseapplication.liveVideo.LinkedUserActivity
|
||||
import com.example.defenseapplication.personal.PasswordManagementActivity
|
||||
import com.example.defenseapplication.personal.PersonalProfileActivity
|
||||
import com.example.defenseapplication.utils.LanguageUtil
|
||||
import com.example.defenseapplication.utils.RetrofitNetwork
|
||||
import com.example.defenseapplication.utils.UserinfoUtil
|
||||
import com.example.defenseapplication.view.FamilySwitchDialog
|
||||
import com.example.defenseapplication.view.LanguageDialog
|
||||
import com.gyf.immersionbar.ImmersionBar
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
//我的碎片
|
||||
class PersonalFragment : Fragment() {
|
||||
// ViewBinding
|
||||
@@ -61,9 +68,36 @@ class PersonalFragment : Fragment() {
|
||||
val intent= Intent(requireContext(), PasswordManagementActivity::class.java)
|
||||
startActivity(intent)
|
||||
}
|
||||
fetchCurrentFamily()
|
||||
return binding.root
|
||||
}
|
||||
|
||||
private fun fetchCurrentFamily() {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
val response = RetrofitNetwork.getNetworkService().getFamilyList()
|
||||
withContext(Dispatchers.Main) {
|
||||
if (response.code == 200 && response.data != null) {
|
||||
val userInfo = UserinfoUtil.getUserInfo()
|
||||
val currentFamily = response.data.find {
|
||||
it.role == userInfo?.role && it.role != "member"
|
||||
} ?: response.data.firstOrNull()
|
||||
currentFamily?.let {
|
||||
val isAdmin = it.role == "appadmin"
|
||||
if (isAdmin) {
|
||||
binding.tvCurrentFamily.text = getString(R.string.my_family)
|
||||
} else {
|
||||
binding.tvCurrentFamily.text = "${it.parentName}${getString(R.string.family_with_normal)}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 更新当前语言显示
|
||||
@@ -90,11 +124,39 @@ class PersonalFragment : Fragment() {
|
||||
*/
|
||||
private fun showFamilySwitchDialog() {
|
||||
val dialog = FamilySwitchDialog(requireContext()) { familyName ->
|
||||
binding.tvCurrentFamily.text = familyName
|
||||
updateFamilyDisplay(familyName)
|
||||
}
|
||||
dialog.show()
|
||||
}
|
||||
|
||||
private fun updateFamilyDisplay(familyName: String) {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
val response = RetrofitNetwork.getNetworkService().getFamilyList()
|
||||
withContext(Dispatchers.Main) {
|
||||
if (response.code == 200 && response.data != null) {
|
||||
val family = response.data.find { it.parentName == familyName }
|
||||
family?.let {
|
||||
val isAdmin = it.role == "appadmin"
|
||||
if (isAdmin) {
|
||||
binding.tvCurrentFamily.text = getString(R.string.my_family)
|
||||
} else {
|
||||
binding.tvCurrentFamily.text = "${it.parentName}${getString(R.string.family_with_normal)}"
|
||||
}
|
||||
} ?: run {
|
||||
binding.tvCurrentFamily.text = familyName
|
||||
}
|
||||
} else {
|
||||
binding.tvCurrentFamily.text = familyName
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
binding.tvCurrentFamily.text = familyName
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
_binding = null // 避免内存泄漏
|
||||
|
||||
Reference in New Issue
Block a user