Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ You can parse any xml through the json2view library to create a JSON that will b

<img alt="how it works schematic" src="https://github.com/Avocarrot/json2view/blob/master/example_assets/how_it_works.png" width="700px"/>

Note: Runtime creation of a view without the precompiled version of xml in apk (res/layout), especially for highly complex layouts, can be a potential latency issue.
Note: Runtime creation of a view without the precompiled version of xml in apk (res/layout), especially for highly complex layouts, can be a potential latency issue. In order to reduce the workload in the UI thread, use the createViewAsync methods.

## Examples

Expand Down
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
buildscript {
repositories {
jcenter()
mavenCentral()
mavenLocal()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -15,5 +18,6 @@ buildscript {
allprojects {
repositories {
jcenter()
mavenLocal()
}
}
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Oct 06 01:23:45 PDT 2016
#Sat Aug 05 01:28:17 CLT 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
5 changes: 5 additions & 0 deletions json2view/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'

version '1.0'
group 'com.avocarrot'

android {
compileSdkVersion 25
Expand All @@ -24,4 +28,5 @@ android {
dependencies {
androidTestCompile 'junit:junit:4.12'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-annotations:25.3.1'
}
49 changes: 49 additions & 0 deletions json2view/src/main/java/com/avocarrot/json2view/DynamicView.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.avocarrot.json2view;

import android.content.Context;
import android.support.annotation.UiThread;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -20,6 +21,38 @@
*/
public class DynamicView {

public interface OnJsonParsedAsView{
void onBackgroundChanges(View parsedView);
void onSuccess(View parsedView);
void onFailure();
}

static class AsynchronousParser implements Runnable{
public Context context;
public JSONObject jsonObject;
public Class viewHolderClass;
public OnJsonParsedAsView onJsonParsedAsView;
View sampleView;

@Override
public void run() {
sampleView = DynamicView.createView(context, jsonObject, viewHolderClass);
if(onJsonParsedAsView != null) onJsonParsedAsView.onBackgroundChanges(sampleView);
finished();
}

@UiThread
private void finished(){
if(onJsonParsedAsView != null){
if(sampleView != null){
onJsonParsedAsView.onSuccess(sampleView);
}else{
onJsonParsedAsView.onFailure();
}
}
}
}

static int mCurrentId = 13;
static int INTERNAL_TAG_ID = 0x7f020000;

Expand All @@ -32,6 +65,22 @@ public static View createView (Context context, JSONObject jsonObject, Class hol
return createView(context, jsonObject, null, holderClass);
}

/**
* @param jsonObject : json object
* @param holderClass : class that will be created as an holder and attached as a tag in the View
* @return the view that created
*/
public static void createViewAsync (Context context, JSONObject jsonObject, Class holderClass, OnJsonParsedAsView onJsonParsedAsView) {

AsynchronousParser parser = new AsynchronousParser();
parser.context = context;
parser.jsonObject = jsonObject;
parser.viewHolderClass = holderClass;
parser.onJsonParsedAsView = onJsonParsedAsView;

ParserExecutor.getExecutor().execute(parser);
}

/**
* @param jsonObject : json object
* @param parent : parent viewGroup
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.avocarrot.json2view;

import android.content.Context;
import android.support.annotation.UiThread;
import android.view.View;

import org.json.JSONObject;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* Created by fco on 13-08-17.
*/

public class ParserExecutor {

class AsynchronousParser implements Runnable{
public Context context;
public JSONObject jsonObject;
public Class viewHolderClass;
public DynamicView.OnJsonParsedAsView onJsonParsedAsView;
View sampleView;

@Override
public void run() {
sampleView = DynamicView.createView(context, jsonObject, viewHolderClass);
if(onJsonParsedAsView != null) onJsonParsedAsView.onBackgroundChanges(sampleView);
finished();
}

@UiThread
private void finished(){
if(onJsonParsedAsView != null){
if(sampleView != null){
onJsonParsedAsView.onSuccess(sampleView);
}else{
onJsonParsedAsView.onFailure();
}
}
}
}

private static BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();
private static ThreadPoolExecutor executor;

public static ThreadPoolExecutor getExecutor(){
if(executor == null){
int processors = 1;
executor = new ThreadPoolExecutor(processors, processors, 1, TimeUnit.SECONDS, workQueue);
}
return executor;
}
}
Loading