切换家庭联动
This commit is contained in:
@@ -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