| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- package im.angry.openeuicc.ui
- import android.annotation.SuppressLint
- import android.os.Bundle
- import android.text.method.PasswordTransformationMethod
- import android.view.LayoutInflater
- import android.view.View
- import android.view.ViewGroup
- import androidx.fragment.app.Fragment
- import androidx.lifecycle.lifecycleScope
- import androidx.recyclerview.widget.LinearLayoutManager
- import androidx.recyclerview.widget.RecyclerView
- import im.angry.openeuicc.R
- import im.angry.openeuicc.core.EuiccChannel
- import im.angry.openeuicc.databinding.EuiccProfileBinding
- import im.angry.openeuicc.databinding.FragmentEuiccBinding
- import kotlinx.coroutines.Dispatchers
- import kotlinx.coroutines.launch
- import kotlinx.coroutines.withContext
- class EuiccManagementFragment(private val channel: EuiccChannel) : Fragment() {
- private var _binding: FragmentEuiccBinding? = null
- private val binding get() = _binding!!
- private val adapter = EuiccProfileAdapter(listOf())
- override fun onCreateView(
- inflater: LayoutInflater,
- container: ViewGroup?,
- savedInstanceState: Bundle?
- ): View {
- _binding = FragmentEuiccBinding.inflate(inflater, container, false)
- return binding.root
- }
- override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
- super.onViewCreated(view, savedInstanceState)
- binding.swipeRefresh.setOnRefreshListener { refresh() }
- binding.profileList.adapter = adapter
- binding.profileList.layoutManager =
- LinearLayoutManager(view.context, LinearLayoutManager.VERTICAL, false)
- binding.fab.setOnClickListener {
- ProfileDownloadFragment(channel).show(childFragmentManager, ProfileDownloadFragment.TAG)
- }
- }
- override fun onStart() {
- super.onStart()
- refresh()
- }
- @SuppressLint("NotifyDataSetChanged")
- private fun refresh() {
- binding.swipeRefresh.isRefreshing = true
- lifecycleScope.launch {
- val profiles = withContext(Dispatchers.IO) {
- channel.lpa.profiles
- }
- withContext(Dispatchers.Main) {
- adapter.profiles = profiles
- adapter.notifyDataSetChanged()
- binding.swipeRefresh.isRefreshing = false
- }
- }
- }
- }
- class EuiccProfileAdapter(var profiles: List<Map<String, String>>) :
- RecyclerView.Adapter<EuiccProfileAdapter.ViewHolder>() {
- data class ViewHolder(val binding: EuiccProfileBinding) : RecyclerView.ViewHolder(binding.root) {
- init {
- binding.iccid.setOnClickListener {
- if (binding.iccid.transformationMethod == null) {
- binding.iccid.transformationMethod = PasswordTransformationMethod.getInstance()
- } else {
- binding.iccid.transformationMethod = null
- }
- }
- }
- }
- override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
- val binding =
- EuiccProfileBinding.inflate(LayoutInflater.from(parent.context), parent, false)
- return ViewHolder(binding)
- }
- override fun onBindViewHolder(holder: ViewHolder, position: Int) {
- // TODO: The library is not exposing the nicknames. Expose them so that we can do something here.
- holder.binding.name.text = profiles[position]["NAME"]
- holder.binding.state.setText(
- if (profiles[position]["STATE"]?.lowercase() == "enabled") {
- R.string.enabled
- } else {
- R.string.disabled
- }
- )
- holder.binding.provider.text = profiles[position]["PROVIDER_NAME"]
- holder.binding.iccid.text = profiles[position]["ICCID"]
- holder.binding.iccid.transformationMethod = PasswordTransformationMethod.getInstance()
- }
- override fun getItemCount(): Int = profiles.size
- }
|