瀏覽代碼

Add withEuiccChannel helper for EuiccChannelFragment

Peter Cai 1 年之前
父節點
當前提交
95b24e6151

+ 4 - 2
app-common/src/main/java/im/angry/openeuicc/core/DefaultEuiccChannelManager.kt

@@ -162,13 +162,15 @@ open class DefaultEuiccChannelManager(
     override suspend fun <R> withEuiccChannel(
     override suspend fun <R> withEuiccChannel(
         physicalSlotId: Int,
         physicalSlotId: Int,
         portId: Int,
         portId: Int,
-        fn: (EuiccChannel) -> R
+        fn: suspend (EuiccChannel) -> R
     ): R {
     ): R {
         val channel = findEuiccChannelByPortBlocking(physicalSlotId, portId)
         val channel = findEuiccChannelByPortBlocking(physicalSlotId, portId)
             ?: throw EuiccChannelManager.EuiccChannelNotFoundException()
             ?: throw EuiccChannelManager.EuiccChannelNotFoundException()
         val wrapper = EuiccChannelWrapper(channel)
         val wrapper = EuiccChannelWrapper(channel)
         try {
         try {
-            return fn(wrapper)
+            return withContext(Dispatchers.IO) {
+                fn(wrapper)
+            }
         } finally {
         } finally {
             wrapper.invalidateWrapper()
             wrapper.invalidateWrapper()
         }
         }

+ 7 - 1
app-common/src/main/java/im/angry/openeuicc/core/EuiccChannelManager.kt

@@ -71,9 +71,15 @@ interface EuiccChannelManager {
      * The reference is not supposed to be held outside of the callback. This is enforced via
      * The reference is not supposed to be held outside of the callback. This is enforced via
      * a wrapper object.
      * a wrapper object.
      *
      *
+     * The callback is run on Dispatchers.IO by default.
+     *
      * If a channel for that slot / port is not found, EuiccChannelNotFoundException is thrown
      * If a channel for that slot / port is not found, EuiccChannelNotFoundException is thrown
      */
      */
-    suspend fun <R> withEuiccChannel(physicalSlotId: Int, portId: Int, fn: (EuiccChannel) -> R): R
+    suspend fun <R> withEuiccChannel(
+        physicalSlotId: Int,
+        portId: Int,
+        fn: suspend (EuiccChannel) -> R
+    ): R
 
 
     /**
     /**
      * Invalidate all EuiccChannels previously cached by this Manager
      * Invalidate all EuiccChannels previously cached by this Manager

+ 5 - 0
app-common/src/main/java/im/angry/openeuicc/util/EuiccChannelFragmentUtils.kt

@@ -40,6 +40,11 @@ val <T> T.channel: EuiccChannel where T: Fragment, T: EuiccChannelFragmentMarker
     get() =
     get() =
         euiccChannelManager.findEuiccChannelByPortBlocking(slotId, portId)!!
         euiccChannelManager.findEuiccChannelByPortBlocking(slotId, portId)!!
 
 
+suspend fun <T, R> T.withEuiccChannel(fn: suspend (EuiccChannel) -> R): R where T : Fragment, T : EuiccChannelFragmentMarker {
+    ensureEuiccChannelManager()
+    return euiccChannelManager.withEuiccChannel(slotId, portId, fn)
+}
+
 suspend fun <T> T.ensureEuiccChannelManager() where T: Fragment, T: EuiccChannelFragmentMarker =
 suspend fun <T> T.ensureEuiccChannelManager() where T: Fragment, T: EuiccChannelFragmentMarker =
     (requireActivity() as BaseEuiccAccessActivity).euiccChannelManagerLoaded.await()
     (requireActivity() as BaseEuiccAccessActivity).euiccChannelManagerLoaded.await()