Initial commit 🎉
fbshipit-source-id: b6fc29740c6875d2e78953b8a7123890a67930f2 Co-authored-by: Sebastian McKenzie <sebmck@fb.com> Co-authored-by: John Knox <jknox@fb.com> Co-authored-by: Emil Sjölander <emilsj@fb.com> Co-authored-by: Pritesh Nandgaonkar <prit91@fb.com>
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
|
||||
package com.facebook.sonar.sample;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import com.facebook.litho.ComponentContext;
|
||||
import com.facebook.litho.LithoView;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
final ComponentContext c = new ComponentContext(this);
|
||||
setContentView(
|
||||
LithoView.create(
|
||||
c,
|
||||
RootComponent.create(c).build()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
|
||||
package com.facebook.sonar.sample;
|
||||
|
||||
import com.facebook.litho.Column;
|
||||
import com.facebook.litho.Component;
|
||||
import com.facebook.litho.ComponentContext;
|
||||
import com.facebook.litho.annotations.LayoutSpec;
|
||||
import com.facebook.litho.annotations.OnCreateLayout;
|
||||
import com.facebook.litho.widget.Text;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.FormBody;
|
||||
import okhttp3.FormBody.Builder;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Call;
|
||||
import okhttp3.Callback;
|
||||
import com.facebook.litho.ClickEvent;
|
||||
import android.util.Log;
|
||||
import java.io.IOException;
|
||||
import com.facebook.litho.annotations.OnEvent;
|
||||
|
||||
@LayoutSpec
|
||||
public class RootComponentSpec {
|
||||
|
||||
@OnCreateLayout
|
||||
static Component onCreateLayout(ComponentContext c) {
|
||||
return Column.create(c)
|
||||
.child(
|
||||
Text.create(c)
|
||||
.text("Tap to hit get request")
|
||||
.key("1")
|
||||
.textSizeSp(20)
|
||||
.clickHandler(RootComponent.hitGetRequest(c)))
|
||||
.child(
|
||||
Text.create(c)
|
||||
.text("Tap to hit post request")
|
||||
.key("2")
|
||||
.textSizeSp(20)
|
||||
.clickHandler(RootComponent.hitPostRequest(c)))
|
||||
.child(
|
||||
Text.create(c)
|
||||
.text("I m just a text")
|
||||
.key("3")
|
||||
.textSizeSp(20))
|
||||
.build();
|
||||
}
|
||||
|
||||
@OnEvent(ClickEvent.class)
|
||||
static void hitGetRequest(ComponentContext c) {
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url("https://api.github.com/repos/facebook/yoga")
|
||||
.get()
|
||||
.build();
|
||||
SonarSampleApplication.okhttpClient.newCall(request).enqueue(new Callback() {
|
||||
@Override
|
||||
public void onFailure(Call call, IOException e) {
|
||||
e.printStackTrace();
|
||||
Log.d("Sonar", e.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResponse(Call call, Response response) throws IOException {
|
||||
if (response.isSuccessful()) {
|
||||
Log.d("Sonar", response.body().string());
|
||||
} else {
|
||||
Log.d("Sonar", "not successful");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@OnEvent(ClickEvent.class)
|
||||
static void hitPostRequest(ComponentContext c) {
|
||||
|
||||
RequestBody formBody = new FormBody.Builder()
|
||||
.add("app", "Sonar")
|
||||
.add("remarks", "Its awesome")
|
||||
.build();
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url("https://demo9512366.mockable.io/SonarPost")
|
||||
.post(formBody)
|
||||
.build();
|
||||
|
||||
SonarSampleApplication.okhttpClient.newCall(request).enqueue(new Callback() {
|
||||
@Override
|
||||
public void onFailure(Call call, IOException e) {
|
||||
e.printStackTrace();
|
||||
Log.d("Sonar", e.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResponse(Call call, Response response) throws IOException {
|
||||
if (response.isSuccessful()) {
|
||||
Log.d("Sonar", response.body().string());
|
||||
} else {
|
||||
Log.d("Sonar", "not successful");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
|
||||
package com.facebook.sonar.sample;
|
||||
|
||||
import android.app.Application;
|
||||
import android.net.Network;
|
||||
import android.support.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import com.facebook.litho.sonar.LithoSonarDescriptors;
|
||||
import com.facebook.soloader.SoLoader;
|
||||
import com.facebook.sonar.android.utils.SonarUtils;
|
||||
import com.facebook.sonar.android.AndroidSonarClient;
|
||||
import com.facebook.sonar.core.SonarClient;
|
||||
import com.facebook.sonar.plugins.inspector.DescriptorMapping;
|
||||
import com.facebook.sonar.plugins.inspector.InspectorSonarPlugin;
|
||||
import com.facebook.sonar.plugins.network.NetworkSonarPlugin;
|
||||
import com.facebook.sonar.plugins.network.SonarOkhttpInterceptor;
|
||||
import com.facebook.sonar.plugins.network.NetworkResponseFormatter;
|
||||
import okhttp3.OkHttpClient;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class SonarSampleApplication extends Application {
|
||||
|
||||
static public OkHttpClient okhttpClient;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
SoLoader.init(this, false);
|
||||
|
||||
final SonarClient client = AndroidSonarClient.getInstance(this);
|
||||
final DescriptorMapping descriptorMapping = DescriptorMapping.withDefaults();
|
||||
|
||||
NetworkSonarPlugin networkPlugin = new NetworkSonarPlugin();
|
||||
SonarOkhttpInterceptor interceptor = new SonarOkhttpInterceptor(networkPlugin);
|
||||
|
||||
okhttpClient = new OkHttpClient.Builder()
|
||||
.addNetworkInterceptor(interceptor)
|
||||
.connectTimeout(60, TimeUnit.SECONDS)
|
||||
.readTimeout(60, TimeUnit.SECONDS)
|
||||
.writeTimeout(10, TimeUnit.MINUTES)
|
||||
.build();
|
||||
|
||||
LithoSonarDescriptors.add(descriptorMapping);
|
||||
client.addPlugin(new InspectorSonarPlugin(this, descriptorMapping));
|
||||
client.addPlugin(networkPlugin);
|
||||
client.start();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user