Skip to content

Commit

Permalink
Merge pull request syrjs#118 from dmikey/derek-anderoid-1
Browse files Browse the repository at this point in the history
Derek anderoid 1
  • Loading branch information
dmikey committed Jan 9, 2018
2 parents 08515fe + 9622596 commit 6bc178d
Show file tree
Hide file tree
Showing 17 changed files with 348 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,39 @@
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// register NativeModules
List<SyrBaseModule> modules = new ArrayList<>();

// register NativeModules
modules.add(new SyrView());
modules.add(new SyrText());

// get the javascript bundle
SyrBundle bundle = new SyrBundleManager().setBundleAssetName("").build();

// create an instance of Syr
SyrInstance instance = new SyrInstanceManager().setJSBundleFile(bundle).build();

// expose the desired native modules to the instance
instance.setNativeModules(modules);

// create a new Rootview
SyrRootView rootview = new SyrRootView(this);

// start the Syr Application
rootview.startSyrApplication(instance, bundle);

// set the content of the to the Rootview
setContentView(rootview);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.dereanderson.syrnativeandroid;

/**
* Syr Project
* https://syr.js.org
* Created by Derek Anderson on 1/8/18.
*/
public class SyrAnimator {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.dereanderson.syrnativeandroid;

import android.content.Context;
import android.view.View;

import org.json.JSONObject;

/**
* Created by dereanderson on 1/9/18.
*/

public interface SyrBaseModule {
String getName();
View render(JSONObject component, Context context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import org.json.JSONObject;

Expand All @@ -17,8 +18,9 @@


public class SyrBridge {
Context mContext;
SyrRaster mRaster;
private Context mContext;
private SyrRaster mRaster;
private WebView mBridgedBrowser;

/** Instantiate the interface and set the context */
SyrBridge(Context c) {
Expand All @@ -32,24 +34,44 @@ public void setRaster(SyrRaster raster) {
@JavascriptInterface
public void message(String message) {
try {
JSONObject obj = new JSONObject(message);
mRaster.parseAST();
JSONObject jsonObject = new JSONObject(message);
String messageType = jsonObject.getString("type");

if(messageType.equals("gui")) {
mRaster.parseAST(jsonObject);
}

} catch (Throwable tx) {
Log.e("SyrBridge", "Could not parse malformed JSON: \"" + message + "\"");
}
}

public void loadBundle() {
WebView wv = new WebView(mContext);
wv.addJavascriptInterface(this, "SyrBridge");
mBridgedBrowser = new WebView(mContext);
mBridgedBrowser.addJavascriptInterface(this, "SyrBridge");

// if the url is changes from it's initial loadURL then cancel
mBridgedBrowser.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i("bridgebrowser", "navigating");
mRaster.clearRootView();
return false;
}
});

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
}

WebSettings webSettings = wv.getSettings();
WebSettings webSettings = mBridgedBrowser.getSettings();
webSettings.setJavaScriptEnabled(true);

wv.loadUrl("http://10.0.2.2:8080");
mBridgedBrowser.loadUrl("http://10.0.2.2:8080");
}

public void sendMessage() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
mBridgedBrowser.evaluateJavascript("SyrEvent({type:'foo'});", null);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.dereanderson.syrnativeandroid;

/**
* Created by dereanderson on 1/8/18.
* Syr Project
* https://syr.js.org
* Created by Derek Anderson on 1/8/18.
*/

public class SyrBundle {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.dereanderson.syrnativeandroid;

/**
* Created by dereanderson on 1/8/18.
* Syr Project
* https://syr.js.org
* Created by Derek Anderson on 1/8/18.
*/

public class SyrBundleManager {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.dereanderson.syrnativeandroid;

/**
* Syr Project
* https://syr.js.org
* Created by Derek Anderson on 1/8/18.
*/
public class SyrEvents {
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.example.dereanderson.syrnativeandroid;

import java.util.List;

/**
* Created by dereanderson on 1/8/18.
* Syr Project
* https://syr.js.org
* Created by Derek Anderson on 1/8/18.
*/

public class SyrInstance {
SyrBridge mBridge;
SyrRaster mRaster;
private SyrBridge mBridge;
private SyrRaster mRaster;
private List<SyrBaseModule> mModules;

public SyrInstance(SyrInstanceManager manager) {
// start syr root activity
Expand All @@ -17,8 +22,17 @@ public SyrInstance setBridge(SyrBridge bridge) {
return this;
}

public SyrInstance setNativeModules(List<SyrBaseModule> modules) {
mModules = modules;
return this;
}

public SyrInstance setRaster(SyrRaster raster) {
// set the raster for the bridge instance
mRaster = raster;

// pass down native modules
mRaster.setModules(mModules);
mBridge.setRaster(mRaster);
return this;
}
Expand All @@ -27,5 +41,4 @@ public void loadBundle() {
mBridge.loadBundle();
}


}
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package com.example.dereanderson.syrnativeandroid;


/**
* Syr Project
* https://syr.js.org
* Created by Derek Anderson on 1/8/18.
*/
public class SyrInstanceManager {
private SyrBundle bundle;
private SyrBundle mBundle;

public SyrInstanceManager() {
// does this need the (additional) config param?
// if not do we need this constructor?
}

public SyrInstanceManager setJSBundleFile(SyrBundle bundle) {
this.bundle = bundle;
mBundle = bundle;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.dereanderson.syrnativeandroid;

/**
* Syr Project
* https://syr.js.org
* Created by Derek Anderson on 1/8/18.
*/

import java.lang.annotation.Retention;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Retention(RUNTIME)
public @interface SyrMethod {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.text.Layout;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;


/**
* Syr Project
Expand All @@ -17,39 +27,86 @@ public class SyrRaster {
private Context mContext;
private SyrRootView mRootview;
private Handler uiHandler;
private List<SyrBaseModule> mModules;
private HashMap<String, Object> mModuleMap;
private HashMap<String, Object> mModuleInstances;

/** Instantiate the interface and set the context */
SyrRaster(Context c) {
mModuleMap = new HashMap<String, Object>();
mContext = c;
}

public void setRootview(SyrRootView rootview) {
mRootview = rootview;

// main thread looper for UI updates
uiHandler = new Handler(Looper.getMainLooper());
}

public void setModules(List<SyrBaseModule> modules) {
mModules = modules;

// map module names, these will be the JSX tags
for (int i = 0; i < modules.size(); i++) {
SyrBaseModule module = modules.get(i);
String moduleName = module.getName();
mModuleMap.put(moduleName, module);
}
}

public void parseAST() {
uiHandler.post(new Runnable() {
@Override
public void run() {
addText();
public void parseAST(JSONObject jsonObject) {
try {
final JSONObject ast = new JSONObject(jsonObject.getString("ast"));
final View component = createComponent(ast);
final String elementName = ast.getString("elementName");
JSONArray children = ast.getJSONArray("children");
String guid = ast.getString("guid");

if(children.length() > 0) {
buildChildren(children, (ViewGroup) component);
}

uiHandler.post(new Runnable() {
@Override
public void run() {
mRootview.addView(component);
}
});

} catch (JSONException e) {
e.printStackTrace();
}
}

public void clearRootView() {
mRootview.removeAllViews();
}

private void buildChildren(JSONArray children, ViewGroup viewParent) {
try {
for (int i = 0; i < children.length(); i++) {
JSONObject child = children.getJSONObject(i);
View component = createComponent(child);
JSONArray childChildren = child.getJSONArray("children");
viewParent.addView(component);

if(component instanceof ViewGroup) {
buildChildren(childChildren, (ViewGroup) component);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
});
}

public void addText() {
TextView tv = new TextView(mContext);
tv.setText("Test");
private View createComponent(JSONObject child) throws JSONException {
String className = child.getString("elementName");
SyrBaseModule componentModule = (SyrBaseModule) mModuleMap.get(className);
View returnView = null;

// Defining the layout parameters of the TextView
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
returnView = componentModule.render(child, mContext);

// Setting the parameters on the TextView
tv.setLayoutParams(lp);
mRootview.addView(tv);
return returnView;
}
}

0 comments on commit 6bc178d

Please sign in to comment.