diff --git a/app/src/main/java/com/example/defenseapplication/adapter/FamilyAdapter.kt b/app/src/main/java/com/example/defenseapplication/adapter/FamilyAdapter.kt index c6ff813..f7807fd 100644 --- a/app/src/main/java/com/example/defenseapplication/adapter/FamilyAdapter.kt +++ b/app/src/main/java/com/example/defenseapplication/adapter/FamilyAdapter.kt @@ -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() { - private val items = mutableListOf() + private val items = mutableListOf() private var selectedFamily: String? = null - fun submitList(familyList: List, currentFamily: String) { + fun submitList(familyList: List, 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 + } } } } \ No newline at end of file diff --git a/app/src/main/java/com/example/defenseapplication/bean/LoginBean.kt b/app/src/main/java/com/example/defenseapplication/bean/LoginBean.kt index 948a85c..5755616 100644 --- a/app/src/main/java/com/example/defenseapplication/bean/LoginBean.kt +++ b/app/src/main/java/com/example/defenseapplication/bean/LoginBean.kt @@ -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, - ) \ No newline at end of file diff --git a/app/src/main/java/com/example/defenseapplication/bean/SwitchFamilyRequest.kt b/app/src/main/java/com/example/defenseapplication/bean/SwitchFamilyRequest.kt new file mode 100644 index 0000000..757e4ae --- /dev/null +++ b/app/src/main/java/com/example/defenseapplication/bean/SwitchFamilyRequest.kt @@ -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 +) \ No newline at end of file diff --git a/app/src/main/java/com/example/defenseapplication/home/HomeMenuFragment.kt b/app/src/main/java/com/example/defenseapplication/home/HomeMenuFragment.kt index d3a3852..3059f8e 100644 --- a/app/src/main/java/com/example/defenseapplication/home/HomeMenuFragment.kt +++ b/app/src/main/java/com/example/defenseapplication/home/HomeMenuFragment.kt @@ -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 = emptyList() private var familyBeanList: List = emptyList() private var deviceBeanList: List = 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) diff --git a/app/src/main/java/com/example/defenseapplication/login/LoginActivity.kt b/app/src/main/java/com/example/defenseapplication/login/LoginActivity.kt index 1555961..003af94 100644 --- a/app/src/main/java/com/example/defenseapplication/login/LoginActivity.kt +++ b/app/src/main/java/com/example/defenseapplication/login/LoginActivity.kt @@ -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() } diff --git a/app/src/main/java/com/example/defenseapplication/utils/ApiService.kt b/app/src/main/java/com/example/defenseapplication/utils/ApiService.kt index 98a8cd9..abc257f 100644 --- a/app/src/main/java/com/example/defenseapplication/utils/ApiService.kt +++ b/app/src/main/java/com/example/defenseapplication/utils/ApiService.kt @@ -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 + @POST("/app/switchFamily") + suspend fun switchFamily(@Body request: SwitchFamilyRequest): GetCodeBean + + /** + * 获取设备列表 + */ + @GET("/app/getdeviceList") + suspend fun getDeviceList(): GetCodeBean> + + } diff --git a/app/src/main/res/drawable/default_page.png b/app/src/main/res/drawable/default_page.png new file mode 100644 index 0000000..34bf5b7 Binary files /dev/null and b/app/src/main/res/drawable/default_page.png differ diff --git a/app/src/main/res/layout/home_menu_fragment.xml b/app/src/main/res/layout/home_menu_fragment.xml index 2afd8d2..b85c80a 100644 --- a/app/src/main/res/layout/home_menu_fragment.xml +++ b/app/src/main/res/layout/home_menu_fragment.xml @@ -36,17 +36,20 @@ @@ -144,6 +147,24 @@ android:nestedScrollingEnabled="false" android:paddingBottom="16dp" tools:listitem="@layout/item_device" /> + + + + + + + android:padding="4dp"> - + android:orientation="horizontal" + android:gravity="center_vertical" + android:paddingHorizontal="12dp" + android:paddingVertical="10dp" + android:background="?attr/selectableItemBackground" + android:clickable="true"> + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index f6a4675..f70fb18 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -60,6 +60,8 @@ 首页 个人 + 管理者 + 普通用户 我的设备 @@ -82,6 +84,8 @@ 绑定设备 解绑设备 暂无设备 + 暂无权限 + 您暂无权查看设备,请联系管理员 添加设备 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1d31e83..cedcb16 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -54,6 +54,9 @@ Home Profile + Administrator + Ordinary User + My Devices @@ -75,6 +78,8 @@ Unbind Device No devices found No device + No permission + You do not have permission to view devices Add Device