Add sample js and android plugin

Summary:
The start of an example plugin.

My intention is for this to be a place that we keep up to date with the current best practice for doing things.

For example, with the introduction on persistedStateReducer, there are two ways to receive incoming messages, but only one of them works in the background. This should act as a guideline.

For this reason, don't hold back on reviewing it. I want it to be 👌

Reviewed By: priteshrnandgaonkar

Differential Revision: D10448592

fbshipit-source-id: d5fa978c14e47a7fa3c9a29d0929d5a6109267af
This commit is contained in:
John Knox
2018-10-19 09:42:00 -07:00
committed by Facebook Github Bot
parent 881d066369
commit 6cc7f60cde
8 changed files with 283 additions and 6 deletions

View File

@@ -1,11 +1,17 @@
// Copyright 2004-present Facebook. All Rights Reserved.
/*
* Copyright (c) 2004-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
package com.facebook.flipper.sample;
import android.app.Application;
import android.content.Context;
import com.facebook.flipper.android.AndroidFlipperClient;
import com.facebook.flipper.core.FlipperClient;
import com.facebook.flipper.plugins.example.ExampleFlipperPlugin;
import com.facebook.flipper.plugins.inspector.DescriptorMapping;
import com.facebook.flipper.plugins.inspector.InspectorFlipperPlugin;
import com.facebook.flipper.plugins.leakcanary.LeakCanaryFlipperPlugin;
@@ -56,6 +62,7 @@ public class FlipperSampleApplication extends Application {
new SharedPreferencesDescriptor("sample", Context.MODE_PRIVATE),
new SharedPreferencesDescriptor("other_sample", Context.MODE_PRIVATE))));
client.addPlugin(new LeakCanaryFlipperPlugin());
client.addPlugin(new ExampleFlipperPlugin());
client.start();
getSharedPreferences("sample", Context.MODE_PRIVATE).edit().putString("Hello", "world").apply();

View File

@@ -1,9 +1,18 @@
// Copyright 2004-present Facebook. All Rights Reserved.
/*
* Copyright (c) 2004-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
package com.facebook.flipper.sample;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.facebook.flipper.android.AndroidFlipperClient;
import com.facebook.flipper.core.FlipperClient;
import com.facebook.flipper.core.FlipperPlugin;
import com.facebook.flipper.plugins.example.ExampleFlipperPlugin;
import com.facebook.litho.ComponentContext;
import com.facebook.litho.LithoView;
@@ -14,5 +23,13 @@ public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
final ComponentContext c = new ComponentContext(this);
setContentView(LithoView.create(c, RootComponent.create(c).build()));
FlipperClient client = AndroidFlipperClient.getInstanceIfInitialized();
if (client != null) {
FlipperPlugin samplePlugin = client.getPlugin(ExampleFlipperPlugin.ID);
if (samplePlugin instanceof ExampleFlipperPlugin) {
((ExampleFlipperPlugin) samplePlugin).setActivity(this);
}
}
}
}

View File

@@ -1,10 +1,19 @@
// Copyright 2004-present Facebook. All Rights Reserved.
/*
* Copyright (c) 2004-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
package com.facebook.flipper.sample;
import android.content.Intent;
import android.util.Log;
import com.facebook.flipper.android.AndroidFlipperClient;
import com.facebook.flipper.android.diagnostics.FlipperDiagnosticActivity;
import com.facebook.flipper.core.FlipperClient;
import com.facebook.flipper.core.FlipperPlugin;
import com.facebook.flipper.plugins.example.ExampleFlipperPlugin;
import com.facebook.litho.ClickEvent;
import com.facebook.litho.Column;
import com.facebook.litho.Component;
@@ -39,6 +48,12 @@ public class RootComponentSpec {
.key("2")
.textSizeSp(20)
.clickHandler(RootComponent.hitPostRequest(c)))
.child(
Text.create(c)
.text("Trigger Notification")
.key("3")
.textSizeSp(20)
.clickHandler(RootComponent.triggerNotification(c)))
.child(
Text.create(c)
.text("Diagnose connection issues")
@@ -107,9 +122,21 @@ public class RootComponentSpec {
});
}
@OnEvent(ClickEvent.class)
static void triggerNotification(final ComponentContext c) {
FlipperClient client = AndroidFlipperClient.getInstanceIfInitialized();
if (client != null) {
FlipperPlugin plugin = client.getPlugin(ExampleFlipperPlugin.ID);
if (plugin instanceof ExampleFlipperPlugin) {
((ExampleFlipperPlugin) plugin).triggerNotification();
}
}
}
@OnEvent(ClickEvent.class)
static void openDiagnostics(final ComponentContext c) {
Intent intent = new Intent(c, FlipperDiagnosticActivity.class);
c.startActivity(intent);
}
}