EuiccManagementFragment.kt 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package im.angry.openeuicc.ui
  2. import android.annotation.SuppressLint
  3. import android.os.Bundle
  4. import android.text.method.PasswordTransformationMethod
  5. import android.view.LayoutInflater
  6. import android.view.View
  7. import android.view.ViewGroup
  8. import androidx.fragment.app.Fragment
  9. import androidx.lifecycle.lifecycleScope
  10. import androidx.recyclerview.widget.LinearLayoutManager
  11. import androidx.recyclerview.widget.RecyclerView
  12. import im.angry.openeuicc.R
  13. import im.angry.openeuicc.core.EuiccChannel
  14. import im.angry.openeuicc.databinding.EuiccProfileBinding
  15. import im.angry.openeuicc.databinding.FragmentEuiccBinding
  16. import kotlinx.coroutines.Dispatchers
  17. import kotlinx.coroutines.launch
  18. import kotlinx.coroutines.withContext
  19. class EuiccManagementFragment(private val channel: EuiccChannel) : Fragment() {
  20. private var _binding: FragmentEuiccBinding? = null
  21. private val binding get() = _binding!!
  22. private val adapter = EuiccProfileAdapter(listOf())
  23. override fun onCreateView(
  24. inflater: LayoutInflater,
  25. container: ViewGroup?,
  26. savedInstanceState: Bundle?
  27. ): View {
  28. _binding = FragmentEuiccBinding.inflate(inflater, container, false)
  29. return binding.root
  30. }
  31. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  32. super.onViewCreated(view, savedInstanceState)
  33. binding.swipeRefresh.setOnRefreshListener { refresh() }
  34. binding.profileList.adapter = adapter
  35. binding.profileList.layoutManager =
  36. LinearLayoutManager(view.context, LinearLayoutManager.VERTICAL, false)
  37. }
  38. override fun onStart() {
  39. super.onStart()
  40. refresh()
  41. }
  42. @SuppressLint("NotifyDataSetChanged")
  43. private fun refresh() {
  44. binding.swipeRefresh.isRefreshing = true
  45. lifecycleScope.launch {
  46. val profiles = withContext(Dispatchers.IO) {
  47. channel.lpa.profiles
  48. }
  49. withContext(Dispatchers.Main) {
  50. adapter.profiles = profiles
  51. adapter.notifyDataSetChanged()
  52. binding.swipeRefresh.isRefreshing = false
  53. }
  54. }
  55. }
  56. }
  57. class EuiccProfileAdapter(var profiles: List<Map<String, String>>) :
  58. RecyclerView.Adapter<EuiccProfileAdapter.ViewHolder>() {
  59. data class ViewHolder(val binding: EuiccProfileBinding) : RecyclerView.ViewHolder(binding.root)
  60. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
  61. val binding =
  62. EuiccProfileBinding.inflate(LayoutInflater.from(parent.context), parent, false)
  63. return ViewHolder(binding)
  64. }
  65. override fun onBindViewHolder(holder: ViewHolder, position: Int) {
  66. // TODO: The library is not exposing the nicknames. Expose them so that we can do something here.
  67. holder.binding.name.text = profiles[position]["NAME"]
  68. holder.binding.state.setText(
  69. if (profiles[position]["STATE"]?.lowercase() == "enabled") {
  70. R.string.enabled
  71. } else {
  72. R.string.disabled
  73. }
  74. )
  75. holder.binding.provider.text = profiles[position]["PROVIDER_NAME"]
  76. holder.binding.iccid.text = profiles[position]["ICCID"]
  77. holder.binding.iccid.transformationMethod = PasswordTransformationMethod()
  78. }
  79. override fun getItemCount(): Int = profiles.size
  80. }