Last active
February 27, 2024 02:18
-
-
Save kilbot/253e4bfb0b931f9dfc23a45f71564bf7 to your computer and use it in GitHub Desktop.
WatermelonDB Inspector for Flipper (extensible RN debugger)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (c) Facebook, Inc. and its affiliates. | |
* | |
* <p>This source code is licensed under the MIT license found in the LICENSE file in the root | |
* directory of this source tree. | |
*/ | |
package ***CHANGE_ME***; | |
import android.content.Context; | |
import com.facebook.flipper.android.AndroidFlipperClient; | |
import com.facebook.flipper.android.utils.FlipperUtils; | |
import com.facebook.flipper.core.FlipperClient; | |
import com.facebook.flipper.plugins.crashreporter.CrashReporterPlugin; | |
import com.facebook.flipper.plugins.databases.DatabasesFlipperPlugin; | |
import com.facebook.flipper.plugins.databases.impl.SqliteDatabaseDriver; | |
import com.facebook.flipper.plugins.databases.impl.SqliteDatabaseProvider; | |
import com.facebook.flipper.plugins.fresco.FrescoFlipperPlugin; | |
import com.facebook.flipper.plugins.inspector.DescriptorMapping; | |
import com.facebook.flipper.plugins.inspector.InspectorFlipperPlugin; | |
import com.facebook.flipper.plugins.network.FlipperOkhttpInterceptor; | |
import com.facebook.flipper.plugins.network.NetworkFlipperPlugin; | |
import com.facebook.flipper.plugins.react.ReactFlipperPlugin; | |
import com.facebook.flipper.plugins.sharedpreferences.SharedPreferencesFlipperPlugin; | |
import com.facebook.react.ReactInstanceManager; | |
import com.facebook.react.bridge.ReactContext; | |
import com.facebook.react.modules.network.NetworkingModule; | |
import okhttp3.OkHttpClient; | |
import java.io.File; | |
import java.util.List; | |
import java.util.ArrayList; | |
public class ReactNativeFlipper { | |
public static void initializeFlipper(Context context, ReactInstanceManager reactInstanceManager) { | |
if (FlipperUtils.shouldEnableFlipper(context)) { | |
final FlipperClient client = AndroidFlipperClient.getInstance(context); | |
client.addPlugin(new InspectorFlipperPlugin(context, DescriptorMapping.withDefaults())); | |
client.addPlugin(new ReactFlipperPlugin()); | |
client.addPlugin(new DatabasesFlipperPlugin(new SqliteDatabaseDriver(context, new SqliteDatabaseProvider() { | |
@Override | |
public List<File> getDatabaseFiles() { | |
List<File> databaseFiles = new ArrayList<>(); | |
for (String databaseName : context.databaseList()) { | |
databaseFiles.add(context.getDatabasePath(databaseName)); | |
} | |
// String watermelondb = context.getDatabasePath("***CHANGE_ME***.db").getPath().replace("/databases", ""); | |
// databaseFiles.add(new File(watermelondb)); | |
File dir = context.getDataDir(); | |
File[] files = dir.listFiles((d, name) -> name.endsWith(".db")); | |
for (int i = 0; i < files.length; i++) { | |
databaseFiles.add(files[i]); | |
} | |
return databaseFiles; | |
} | |
}))); | |
client.addPlugin(new SharedPreferencesFlipperPlugin(context)); | |
client.addPlugin(CrashReporterPlugin.getInstance()); | |
NetworkFlipperPlugin networkFlipperPlugin = new NetworkFlipperPlugin(); | |
NetworkingModule.setCustomClientBuilder( | |
new NetworkingModule.CustomClientBuilder() { | |
@Override | |
public void apply(OkHttpClient.Builder builder) { | |
builder.addNetworkInterceptor(new FlipperOkhttpInterceptor(networkFlipperPlugin)); | |
} | |
}); | |
client.addPlugin(networkFlipperPlugin); | |
client.start(); | |
// Fresco Plugin needs to ensure that ImagePipelineFactory is initialized | |
// Hence we run if after all native modules have been initialized | |
ReactContext reactContext = reactInstanceManager.getCurrentReactContext(); | |
if (reactContext == null) { | |
reactInstanceManager.addReactInstanceEventListener( | |
new ReactInstanceManager.ReactInstanceEventListener() { | |
@Override | |
public void onReactContextInitialized(ReactContext reactContext) { | |
reactInstanceManager.removeReactInstanceEventListener(this); | |
reactContext.runOnNativeModulesQueueThread( | |
new Runnable() { | |
@Override | |
public void run() { | |
client.addPlugin(new FrescoFlipperPlugin()); | |
} | |
}); | |
} | |
}); | |
} else { | |
client.addPlugin(new FrescoFlipperPlugin()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment