|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.helloworld |
| 9 | + |
| 10 | +import android.content.Context |
| 11 | +import com.facebook.flipper.android.AndroidFlipperClient |
| 12 | +import com.facebook.flipper.android.utils.FlipperUtils |
| 13 | +import com.facebook.flipper.plugins.crashreporter.CrashReporterPlugin |
| 14 | +import com.facebook.flipper.plugins.databases.DatabasesFlipperPlugin |
| 15 | +import com.facebook.flipper.plugins.fresco.FrescoFlipperPlugin |
| 16 | +import com.facebook.flipper.plugins.inspector.DescriptorMapping |
| 17 | +import com.facebook.flipper.plugins.inspector.InspectorFlipperPlugin |
| 18 | +import com.facebook.flipper.plugins.network.FlipperOkhttpInterceptor |
| 19 | +import com.facebook.flipper.plugins.network.NetworkFlipperPlugin |
| 20 | +import com.facebook.flipper.plugins.sharedpreferences.SharedPreferencesFlipperPlugin |
| 21 | +import com.facebook.react.ReactInstanceEventListener |
| 22 | +import com.facebook.react.ReactInstanceManager |
| 23 | +import com.facebook.react.bridge.ReactContext |
| 24 | +import com.facebook.react.modules.network.NetworkingModule |
| 25 | + |
| 26 | +/** |
| 27 | + * Class responsible of loading Flipper inside your React Native application. This is the debug |
| 28 | + * flavor of it. Here you can add your own plugins and customize the Flipper setup. |
| 29 | + */ |
| 30 | +object ReactNativeFlipper { |
| 31 | + fun initializeFlipper(context: Context, reactInstanceManager: ReactInstanceManager) { |
| 32 | + if (FlipperUtils.shouldEnableFlipper(context)) { |
| 33 | + val client = AndroidFlipperClient.getInstance(context) |
| 34 | + client.addPlugin(InspectorFlipperPlugin(context, DescriptorMapping.withDefaults())) |
| 35 | + client.addPlugin(DatabasesFlipperPlugin(context)) |
| 36 | + client.addPlugin(SharedPreferencesFlipperPlugin(context)) |
| 37 | + client.addPlugin(CrashReporterPlugin.getInstance()) |
| 38 | + val networkFlipperPlugin = NetworkFlipperPlugin() |
| 39 | + NetworkingModule.setCustomClientBuilder { builder -> |
| 40 | + builder.addNetworkInterceptor(FlipperOkhttpInterceptor(networkFlipperPlugin)) |
| 41 | + } |
| 42 | + client.addPlugin(networkFlipperPlugin) |
| 43 | + client.start() |
| 44 | + |
| 45 | + // Fresco Plugin needs to ensure that ImagePipelineFactory is initialized |
| 46 | + // Hence we run if after all native modules have been initialized |
| 47 | + val currentReactContext = reactInstanceManager.currentReactContext |
| 48 | + if (currentReactContext == null) { |
| 49 | + reactInstanceManager.addReactInstanceEventListener( |
| 50 | + object : ReactInstanceEventListener { |
| 51 | + override fun onReactContextInitialized(context: ReactContext) { |
| 52 | + reactInstanceManager.removeReactInstanceEventListener(this) |
| 53 | + context.runOnNativeModulesQueueThread { client.addPlugin(FrescoFlipperPlugin()) } |
| 54 | + } |
| 55 | + }) |
| 56 | + } else { |
| 57 | + client.addPlugin(FrescoFlipperPlugin()) |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments