Skip to content

Commit

Permalink
formtting
Browse files Browse the repository at this point in the history
  • Loading branch information
ened committed Apr 8, 2024
1 parent b792830 commit 957fba5
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.google.common.util.concurrent.ListenableFuture
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.dart.DartExecutor
import io.flutter.embedding.engine.loader.FlutterLoader
import io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.view.FlutterCallbackInformation
Expand All @@ -24,9 +23,8 @@ import java.util.Random
*/
class BackgroundWorker(
applicationContext: Context,
private val workerParams: WorkerParameters
private val workerParams: WorkerParameters,
) : ListenableWorker(applicationContext, workerParams), MethodChannel.MethodCallHandler {

private lateinit var backgroundChannel: MethodChannel

companion object {
Expand Down Expand Up @@ -59,10 +57,11 @@ class BackgroundWorker(

private var completer: CallbackToFutureAdapter.Completer<Result>? = null

private var resolvableFuture = CallbackToFutureAdapter.getFuture { completer ->
this.completer = completer
null
}
private var resolvableFuture =
CallbackToFutureAdapter.getFuture { completer ->
this.completer = completer
null
}

override fun startWork(): ListenableFuture<Result> {
startTime = System.currentTimeMillis()
Expand All @@ -76,7 +75,7 @@ class BackgroundWorker(
flutterLoader.ensureInitializationCompleteAsync(
applicationContext,
null,
Handler(Looper.getMainLooper())
Handler(Looper.getMainLooper()),
) {
val callbackHandle = SharedPreferenceHelper.getCallbackHandle(applicationContext)
val callbackInfo = FlutterCallbackInformation.lookupCallbackInformation(callbackHandle)
Expand All @@ -90,7 +89,7 @@ class BackgroundWorker(
payload,
callbackHandle,
callbackInfo,
dartBundlePath
dartBundlePath,
)
}

Expand All @@ -102,8 +101,8 @@ class BackgroundWorker(
DartExecutor.DartCallback(
applicationContext.assets,
dartBundlePath,
callbackInfo
)
callbackInfo,
),
)
}
}
Expand All @@ -125,7 +124,7 @@ class BackgroundWorker(
dartTask,
payload,
fetchDuration,
result ?: Result.failure()
result ?: Result.failure(),
)
}

Expand All @@ -142,7 +141,10 @@ class BackgroundWorker(
}
}

override fun onMethodCall(call: MethodCall, r: MethodChannel.Result) {
override fun onMethodCall(
call: MethodCall,
r: MethodChannel.Result,
) {
when (call.method) {
BACKGROUND_CHANNEL_INITIALIZED ->
backgroundChannel.invokeMethod(
Expand All @@ -156,7 +158,7 @@ class BackgroundWorker(
override fun error(
errorCode: String,
errorMessage: String?,
errorDetails: Any?
errorDetails: Any?,
) {
Log.e(TAG, "errorCode: $errorCode, errorMessage: $errorMessage")
stopEngine(Result.failure())
Expand All @@ -166,7 +168,7 @@ class BackgroundWorker(
val wasSuccessFul = receivedResult?.let { it as Boolean? } == true
stopEngine(if (wasSuccessFul) Result.success() else Result.retry())
}
}
},
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,33 @@ object ThumbnailGenerator {
}

object DebugHelper {
private const val debugChannelId = "WorkmanagerDebugChannelId"
private const val debugChannelName = "A helper channel to debug your background tasks."
private const val DEBUG_CHANNEL_ID = "WorkmanagerDebugChannelId"
private const val DEBUG_CHANNEL_NAME = "A helper channel to debug your background tasks."
private val debugDateFormatter =
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM)

private val currentTime get() = debugDateFormatter.format(Date())

private fun mapMillisToSeconds(milliseconds: Long) =
"${MILLISECONDS.toSeconds(milliseconds)} seconds."
private fun mapMillisToSeconds(milliseconds: Long) = "${MILLISECONDS.toSeconds(milliseconds)} seconds."

fun postTaskCompleteNotification(
ctx: Context,
threadIdentifier: Int,
dartTask: String,
payload: String? = null,
fetchDuration: Long,
result: ListenableWorker.Result
result: ListenableWorker.Result,
) {
postNotification(
ctx,
threadIdentifier,
"${ThumbnailGenerator.workEmoji} $currentTime",
"""
• Result: ${ThumbnailGenerator.mapResultToEmoji(result)} ${result.javaClass.simpleName}
• dartTask: $dartTask
• inputData: ${payload ?: "not found"}
• Elapsed time: ${mapMillisToSeconds(fetchDuration)}
""".trimIndent()
• Result: ${ThumbnailGenerator.mapResultToEmoji(result)} ${result.javaClass.simpleName}
• dartTask: $dartTask
• inputData: ${payload ?: "not found"}
• Elapsed time: ${mapMillisToSeconds(fetchDuration)}
""".trimIndent(),
)
}

Expand All @@ -60,40 +59,44 @@ object DebugHelper {
payload: String? = null,
callbackHandle: Long,
callbackInfo: FlutterCallbackInformation?,
dartBundlePath: String?
dartBundlePath: String?,
) {
postNotification(
ctx,
threadIdentifier,
"${ThumbnailGenerator.workEmoji} $currentTime",
"""
• dartTask: $dartTask
• inputData: ${payload ?: "not found"}
• callbackHandle: $callbackHandle
• callBackName: ${callbackInfo?.callbackName ?: "not found"}
• callbackClassName: ${callbackInfo?.callbackClassName ?: "not found"}
• callbackLibraryPath: ${callbackInfo?.callbackLibraryPath ?: "not found"}
• dartBundlePath: $dartBundlePath"
""".trimIndent()

• dartTask: $dartTask
• inputData: ${payload ?: "not found"}
• callbackHandle: $callbackHandle
• callBackName: ${callbackInfo?.callbackName ?: "not found"}
• callbackClassName: ${callbackInfo?.callbackClassName ?: "not found"}
• callbackLibraryPath: ${callbackInfo?.callbackLibraryPath ?: "not found"}
• dartBundlePath: $dartBundlePath"
""".trimIndent(),
)
}

private fun postNotification(ctx: Context, messageId: Int, title: String, contentText: String) {
private fun postNotification(
ctx: Context,
messageId: Int,
title: String,
contentText: String,
) {
(ctx.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).apply {
createNotificationChannel()

notify(
messageId,
NotificationCompat.Builder(ctx, debugChannelId)
NotificationCompat.Builder(ctx, DEBUG_CHANNEL_ID)
.setContentTitle(title)
.setContentText(contentText)
.setStyle(
NotificationCompat.BigTextStyle()
.bigText(contentText)
.bigText(contentText),
)
.setSmallIcon(android.R.drawable.stat_notify_sync)
.build()
.build(),
)
}
}
Expand All @@ -102,10 +105,10 @@ object DebugHelper {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel(
NotificationChannel(
debugChannelId,
debugChannelName,
NotificationManager.IMPORTANCE_DEFAULT
)
DEBUG_CHANNEL_ID,
DEBUG_CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT,
),
)
}
}
Expand Down
Loading

0 comments on commit 957fba5

Please sign in to comment.