ソースを参照

refactor: simplify profile operation (#129)

Co-authored-by: Peter Cai <peter@typeblog.net>
Reviewed-on: https://gitea.angry.im/PeterCxy/OpenEUICC/pulls/129
Co-authored-by: septs <github@septs.pw>
Co-committed-by: septs <github@septs.pw>
septs 1 年間 前
コミット
960f8855ad

+ 1 - 1
app-common/src/main/java/im/angry/openeuicc/ui/wizard/DownloadWizardSlotSelectFragment.kt

@@ -111,7 +111,7 @@ class DownloadWizardSlotSelectFragment : DownloadWizardActivity.DownloadWizardSt
                     } catch (e: Exception) {
                         ""
                     },
-                    channel.lpa.profiles.find { it.state == LocalProfileInfo.State.Enabled }?.displayName,
+                    channel.lpa.profiles.enabled?.displayName,
                     channel.intrinsicChannelName,
                 )
             }

+ 6 - 5
app-common/src/main/java/im/angry/openeuicc/util/LPAUtils.kt

@@ -16,9 +16,10 @@ val LocalProfileInfo.isEnabled: Boolean
     get() = state == LocalProfileInfo.State.Enabled
 
 val List<LocalProfileInfo>.operational: List<LocalProfileInfo>
-    get() = filter {
-        it.profileClass == LocalProfileInfo.Clazz.Operational
-    }
+    get() = filter { it.profileClass == LocalProfileInfo.Clazz.Operational }
+
+val List<LocalProfileInfo>.enabled: LocalProfileInfo?
+    get() = find { it.isEnabled }
 
 val List<EuiccChannel>.hasMultipleChips: Boolean
     get() = distinctBy { it.slotId }.size > 1
@@ -39,7 +40,7 @@ fun LocalProfileAssistant.switchProfile(
  * See EuiccManager.waitForReconnect()
  */
 fun LocalProfileAssistant.disableActiveProfile(refresh: Boolean): Boolean =
-    profiles.find { it.isEnabled }?.let {
+    profiles.enabled?.let {
         Log.i(TAG, "Disabling active profile ${it.iccid}")
         disableProfile(it.iccid, refresh)
     } ?: true
@@ -52,7 +53,7 @@ fun LocalProfileAssistant.disableActiveProfile(refresh: Boolean): Boolean =
  * disable.
  */
 fun LocalProfileAssistant.disableActiveProfileKeepIccId(refresh: Boolean): String? =
-    profiles.find { it.isEnabled }?.let {
+    profiles.enabled?.let {
         Log.i(TAG, "Disabling active profile ${it.iccid}")
         if (disableProfile(it.iccid, refresh)) {
             it.iccid

+ 7 - 14
app/src/main/java/im/angry/openeuicc/service/OpenEuiccService.kt

@@ -186,13 +186,10 @@ class OpenEuiccService : EuiccService(), OpenEuiccContextMarker {
             )
         }
 
-        try {
-            return@withEuiccChannelManager euiccChannelManager.withEuiccChannel(
-                slotId,
-                port
-            ) { channel ->
+        return@withEuiccChannelManager try {
+            euiccChannelManager.withEuiccChannel(slotId, port) { channel ->
                 val filteredProfiles =
-                    if (runBlocking { preferenceRepository.unfilteredProfileListFlow.first() })
+                    if (preferenceRepository.unfilteredProfileListFlow.first())
                         channel.lpa.profiles
                     else
                         channel.lpa.profiles.operational
@@ -224,7 +221,7 @@ class OpenEuiccService : EuiccService(), OpenEuiccContextMarker {
                 )
             }
         } catch (e: EuiccChannelManager.EuiccChannelNotFoundException) {
-            return@withEuiccChannelManager GetEuiccProfileInfoListResult(
+            GetEuiccProfileInfoListResult(
                 RESULT_FIRST_USER,
                 arrayOf(),
                 true
@@ -246,11 +243,7 @@ class OpenEuiccService : EuiccService(), OpenEuiccContextMarker {
         // Check that the profile has been disabled on all slots
         val enabledAnywhere = ports.any { port ->
             euiccChannelManager.withEuiccChannel(slotId, port) { channel ->
-                val profile = channel.lpa.profiles.find {
-                    it.iccid == iccid
-                } ?: return@withEuiccChannel false
-
-                profile.state == LocalProfileInfo.State.Enabled
+                channel.lpa.profiles.enabled?.iccid == iccid
             }
         }
 
@@ -354,8 +347,8 @@ class OpenEuiccService : EuiccService(), OpenEuiccContextMarker {
                 // iccid == null means disabling
                 val foundIccid =
                     euiccChannelManager.withEuiccChannel(foundSlotId, foundPortId) { channel ->
-                        channel.lpa.profiles.find { it.state == LocalProfileInfo.State.Enabled }
-                    }?.iccid ?: return@withEuiccChannelManager RESULT_FIRST_USER
+                        channel.lpa.profiles.enabled?.iccid
+                    } ?: return@withEuiccChannelManager RESULT_FIRST_USER
                 Pair(foundIccid, false)
             } else {
                 Pair(iccid, true)