Browse Source

Move data binding to the view holder

Peter Cai 3 years ago
parent
commit
95155d953a
1 changed files with 20 additions and 13 deletions
  1. 20 13
      app/src/main/java/im/angry/openeuicc/ui/EuiccManagementFragment.kt

+ 20 - 13
app/src/main/java/im/angry/openeuicc/ui/EuiccManagementFragment.kt

@@ -76,7 +76,7 @@ class EuiccManagementFragment : Fragment(), EuiccFragmentMarker, EuiccProfilesCh
         }
     }
 
-    inner class ViewHolder(val binding: EuiccProfileBinding) : RecyclerView.ViewHolder(binding.root) {
+    inner class ViewHolder(private val binding: EuiccProfileBinding) : RecyclerView.ViewHolder(binding.root) {
         init {
             binding.iccid.setOnClickListener {
                 if (binding.iccid.transformationMethod == null) {
@@ -86,6 +86,24 @@ class EuiccManagementFragment : Fragment(), EuiccFragmentMarker, EuiccProfilesCh
                 }
             }
         }
+
+        private lateinit var profile: Map<String, String>
+
+        fun setProfile(profile: Map<String, String>) {
+            this.profile = profile
+            // TODO: The library is not exposing the nicknames. Expose them so that we can do something here.
+            binding.name.text = profile["NAME"]
+            binding.state.setText(
+                if (profile["STATE"]?.lowercase() == "enabled") {
+                    R.string.enabled
+                } else {
+                    R.string.disabled
+                }
+            )
+            binding.provider.text = profile["PROVIDER_NAME"]
+            binding.iccid.text = profile["ICCID"]
+            binding.iccid.transformationMethod = PasswordTransformationMethod.getInstance()
+        }
     }
 
     inner class EuiccProfileAdapter(var profiles: List<Map<String, String>>) : RecyclerView.Adapter<ViewHolder>() {
@@ -96,18 +114,7 @@ class EuiccManagementFragment : Fragment(), EuiccFragmentMarker, EuiccProfilesCh
         }
 
         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()
+            holder.setProfile(profiles[position])
         }
 
         override fun getItemCount(): Int = profiles.size