androidsharedpreferencesflipperfbflipper

Adding multiple shared preferences keys to the flipper shared preferences plugin


I was wondering how to show multiple shared preferences keys in the Flipper Shared Preferences Viewer Plugin. KEY_FOO, KEY_BAR, KEY_BAZ are string constants for shared preference files.

Something like

class App: Application() {

     override fun onCreate() {
        super.onCreate()
        setupFlipper()
     }
    
     private fun setupFlipper() {
         if (BuildConfig.DEBUG && FlipperUtils.shouldEnableFlipper(this)) {
            val client = AndroidFlipperClient.getInstance(this)
            client.addPlugin(InspectorFlipperPlugin(this, DescriptorMapping.withDefaults()))

         
            client.addPlugin(
                SharedPreferencesFlipperPlugin(applicationContext, KEY_FOO)
            )

            client.addPlugin(
                SharedPreferencesFlipperPlugin(applicationContext, KEY_BAR)
            )

            client.addPlugin(
                SharedPreferencesFlipperPlugin(applicationContext, KEY_BAZ)
            )

            client.start()
         }
     }

}


Solution

  • Upon inspection of the constructor for SharedPreferencesFlipperPlugin. A second option exists where it takes a list of SharedPreferencesDescriptor's.

    Solution below.

    class App: Application() {
    
         override fun onCreate() {
            super.onCreate()
            setupFlipper()
         }
        
         private fun setupFlipper() {
             if (BuildConfig.DEBUG && FlipperUtils.shouldEnableFlipper(this)) {
                val client = AndroidFlipperClient.getInstance(this)
                client.addPlugin(InspectorFlipperPlugin(this, DescriptorMapping.withDefaults()))
    
                val keys = mutableListOf(
                    KEY_FOO,
                    KEY_BAR,
                    KEY_BAZ,
                )
    
                var descriptors: List<SharedPreferencesFlipperPlugin.SharedPreferencesDescriptor> = keys.map {
                    SharedPreferencesFlipperPlugin.SharedPreferencesDescriptor(it, MODE_PRIVATE)
                }
    
                client.addPlugin(
                    SharedPreferencesFlipperPlugin(applicationContext, descriptors)
                )
    
                client.start()
             }
         }
    
    }