我的家庭切换
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,8 +36,3 @@ data class ForgotRequest(
|
||||
val smsCode: String,
|
||||
val password: String
|
||||
)
|
||||
data class LoginResponse(
|
||||
val clientid: String,
|
||||
val content_language: String,
|
||||
|
||||
)
|
||||
@@ -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
|
||||
)
|
||||
@@ -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,17 +229,17 @@ 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
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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>>
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
BIN
app/src/main/res/drawable/default_page.png
Normal file
BIN
app/src/main/res/drawable/default_page.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 16 KiB |
@@ -36,17 +36,20 @@
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvCurrentFamily"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="@string/my_family_home"
|
||||
android:textColor="@color/black"
|
||||
android:textSize="20dp"
|
||||
android:maxLines="1"
|
||||
android:ellipsize="end"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/ivFamilyArrow"
|
||||
android:layout_width="12dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_height="6dp"
|
||||
android:layout_marginStart="6dp"
|
||||
android:src="@drawable/triangle" />
|
||||
</LinearLayout>
|
||||
@@ -144,6 +147,24 @@
|
||||
android:nestedScrollingEnabled="false"
|
||||
android:paddingBottom="16dp"
|
||||
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>
|
||||
|
||||
<View
|
||||
@@ -157,15 +178,15 @@
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/familyPopupCard"
|
||||
android:layout_width="180dp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="16dp"
|
||||
android:layout_marginTop="62dp"
|
||||
android:background="@drawable/textinput_white_bg"
|
||||
android:elevation="8dp"
|
||||
android:layout_marginTop="92dp"
|
||||
android:background="@drawable/bg_family_popup"
|
||||
android:elevation="12dp"
|
||||
android:orientation="vertical"
|
||||
android:visibility="gone"
|
||||
android:padding="10dp">
|
||||
android:padding="4dp">
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/rvFamilyList"
|
||||
|
||||
@@ -1,12 +1,42 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/tvFamilyName"
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/layoutFamilyItem"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@drawable/textinput_white_bg"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:paddingHorizontal="14dp"
|
||||
android:paddingVertical="12dp"
|
||||
android:textColor="@color/black"
|
||||
android:text="张三"
|
||||
android:textSize="16sp" />
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center_vertical"
|
||||
android:paddingHorizontal="12dp"
|
||||
android:paddingVertical="10dp"
|
||||
android:background="?attr/selectableItemBackground"
|
||||
android:clickable="true">
|
||||
|
||||
<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>
|
||||
@@ -60,6 +60,8 @@
|
||||
<!-- Home Activity -->
|
||||
<string name="home">首页</string>
|
||||
<string name="profile">个人</string>
|
||||
<string name="admin_user">管理者</string>
|
||||
<string name="normal_user">普通用户</string>
|
||||
|
||||
<!-- Home Menu -->
|
||||
<string name="my_devices">我的设备</string>
|
||||
@@ -82,6 +84,8 @@
|
||||
<string name="bind_device">绑定设备</string>
|
||||
<string name="unbind_device">解绑设备</string>
|
||||
<string name="device_not_found">暂无设备</string>
|
||||
<string name="no_permission_title">暂无权限</string>
|
||||
<string name="no_permission_desc">您暂无权查看设备,请联系管理员</string>
|
||||
|
||||
<!-- Add Device -->
|
||||
<string name="add_device_title">添加设备</string>
|
||||
|
||||
@@ -54,6 +54,9 @@
|
||||
<!-- Home Activity -->
|
||||
<string name="home">Home</string>
|
||||
<string name="profile">Profile</string>
|
||||
<string name="admin_user">Administrator</string>
|
||||
<string name="normal_user">Ordinary User</string>
|
||||
|
||||
|
||||
<!-- Home Menu -->
|
||||
<string name="my_devices">My Devices</string>
|
||||
@@ -75,6 +78,8 @@
|
||||
<string name="unbind_device">Unbind Device</string>
|
||||
<string name="device_not_found">No devices found</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 -->
|
||||
<string name="add_device_title">Add Device</string>
|
||||
|
||||
Reference in New Issue
Block a user