登录注册
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
package com.example.defenseapplication.home
|
||||
|
||||
import android.content.Intent
|
||||
import android.content.res.Resources
|
||||
import android.graphics.Color
|
||||
import android.graphics.Rect
|
||||
import android.graphics.drawable.GradientDrawable
|
||||
import android.os.Bundle
|
||||
import android.util.TypedValue
|
||||
import android.widget.Toast
|
||||
import androidx.fragment.app.Fragment
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
@@ -18,10 +18,17 @@ 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.databinding.HomeMenuFragmentBinding
|
||||
import com.example.defenseapplication.utils.RetrofitNetwork
|
||||
import com.example.defenseapplication.view.DefenseDialogs
|
||||
import com.example.defenseapplication.view.showDefenseDialog
|
||||
import com.gyf.immersionbar.ImmersionBar
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
//主页碎片
|
||||
class HomeMenuFragment : Fragment() {
|
||||
// ViewBinding
|
||||
@@ -33,30 +40,9 @@ class HomeMenuFragment : Fragment() {
|
||||
private var isFamilyExpanded = false
|
||||
private lateinit var currentFamily: String
|
||||
|
||||
private val familyList: List<String> by lazy {
|
||||
listOf(
|
||||
getString(R.string.my_family_home),
|
||||
getString(R.string.workshop),
|
||||
getString(R.string.parents_home)
|
||||
)
|
||||
}
|
||||
private val familyDeviceMap: Map<String, List<DeviceUi>> by lazy {
|
||||
mapOf(
|
||||
getString(R.string.my_family_home) to listOf(
|
||||
DeviceUi(getString(R.string.smart_hanging_defense), true),
|
||||
DeviceUi(getString(R.string.smart_defense_device), false),
|
||||
DeviceUi(getString(R.string.smart_rail_defense), false)
|
||||
),
|
||||
getString(R.string.workshop) to listOf(
|
||||
DeviceUi(getString(R.string.corridor_defense), true),
|
||||
DeviceUi(getString(R.string.door_defense), true)
|
||||
),
|
||||
getString(R.string.parents_home) to listOf(
|
||||
DeviceUi(getString(R.string.living_room_defense), false),
|
||||
DeviceUi(getString(R.string.balcony_defense), true)
|
||||
)
|
||||
)
|
||||
}
|
||||
private var familyList: List<String> = emptyList()
|
||||
private var familyBeanList: List<FamilyBean> = emptyList()
|
||||
private var deviceBeanList: List<DeviceBean> = emptyList()
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
@@ -93,13 +79,13 @@ class HomeMenuFragment : Fragment() {
|
||||
val intent = Intent(requireContext(), ScanActivity::class.java)
|
||||
startActivity(intent)
|
||||
}
|
||||
|
||||
fetchFamilyList()
|
||||
}
|
||||
|
||||
private fun initView() {
|
||||
currentFamily = familyList.first()
|
||||
binding.rvFamilyList.layoutManager = LinearLayoutManager(requireContext())
|
||||
binding.rvFamilyList.adapter = familyAdapter
|
||||
familyAdapter.submitList(familyList, currentFamily)
|
||||
|
||||
binding.rvDeviceList.layoutManager = GridLayoutManager(requireContext(), 2)
|
||||
binding.rvDeviceList.adapter = deviceAdapter
|
||||
@@ -108,9 +94,7 @@ class HomeMenuFragment : Fragment() {
|
||||
GridHorizontalSpaceItemDecoration(horizontalSpace = dpToPx(8))
|
||||
)
|
||||
}
|
||||
updateDeviceList()
|
||||
|
||||
binding.tvCurrentFamily.text = currentFamily
|
||||
binding.familyHeaderLayout.setOnClickListener {
|
||||
toggleFamilyList()
|
||||
}
|
||||
@@ -121,6 +105,52 @@ class HomeMenuFragment : Fragment() {
|
||||
}
|
||||
}
|
||||
|
||||
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 }
|
||||
if (familyList.isNotEmpty()) {
|
||||
currentFamily = familyList.first()
|
||||
binding.tvCurrentFamily.text = currentFamily
|
||||
familyAdapter.submitList(familyList, currentFamily)
|
||||
}
|
||||
} 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()
|
||||
}
|
||||
}
|
||||
fetchDeviceList()
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
private fun toggleFamilyList() {
|
||||
isFamilyExpanded = !isFamilyExpanded
|
||||
val visibility = if (isFamilyExpanded) View.VISIBLE else View.GONE
|
||||
@@ -143,7 +173,24 @@ class HomeMenuFragment : Fragment() {
|
||||
}
|
||||
|
||||
private fun updateDeviceList() {
|
||||
deviceAdapter.submitList(familyDeviceMap[currentFamily].orEmpty())
|
||||
val currentFamilyBean = familyBeanList.find { it.parentName == currentFamily }
|
||||
val filteredDevices = if (currentFamilyBean != null) {
|
||||
deviceBeanList.filter { it.parentId == currentFamilyBean.id }
|
||||
} 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)
|
||||
))
|
||||
} else {
|
||||
deviceAdapter.submitList(deviceUiList)
|
||||
}
|
||||
}
|
||||
|
||||
private fun onDeviceClicked(device: DeviceUi) {
|
||||
|
||||
Reference in New Issue
Block a user