package com.vijay.jsonwizard.activities; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import com.vijay.jsonwizard.R; import com.vijay.jsonwizard.constants.JsonFormConstants; import com.vijay.jsonwizard.fragments.JsonFormFragment; import com.vijay.jsonwizard.interfaces.JsonApi; public class JsonFormActivity extends AppCompatActivity implements JsonApi { private static final String TAG = "JsonFormActivity"; private Toolbar mToolbar; private JSONObject mJSONObject; public void init(String json) { try { mJSONObject = new JSONObject(json); } catch (JSONException e) { Log.d(TAG, "Initialization error. Json passed is invalid : " + e.getMessage()); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_json_form); mToolbar = (Toolbar) findViewById(R.id.tb_top); setSupportActionBar(mToolbar); if (savedInstanceState == null) { init(getIntent().getStringExtra("json")); getSupportFragmentManager().beginTransaction() .add(R.id.container, JsonFormFragment.getFormFragment(JsonFormConstants.FIRST_STEP_NAME)).commit(); } else { init(savedInstanceState.getString("jsonState")); } } public Toolbar getToolbar() { return mToolbar; } @Override public synchronized JSONObject getStep(String name) { synchronized (mJSONObject) { try { return mJSONObject.getJSONObject(name); } catch (JSONException e) { e.printStackTrace(); } } return null; } @Override public void writeValue(String stepName, String key, String value) throws JSONException { synchronized (mJSONObject) { JSONObject jsonObject = mJSONObject.getJSONObject(stepName); JSONArray fields = jsonObject.getJSONArray("fields"); for (int i = 0; i < fields.length(); i++) { JSONObject item = fields.getJSONObject(i); String keyAtIndex = item.getString("key"); if (key.equals(keyAtIndex)) { item.put("value", value); return; } } } } @Override public void writeValue(String stepName, String parentKey, String childObjectKey, String childKey, String value) throws JSONException { synchronized (mJSONObject) { JSONObject jsonObject = mJSONObject.getJSONObject(stepName); JSONArray fields = jsonObject.getJSONArray("fields"); for (int i = 0; i < fields.length(); i++) { JSONObject item = fields.getJSONObject(i); String keyAtIndex = item.getString("key"); if (parentKey.equals(keyAtIndex)) { JSONArray jsonArray = item.getJSONArray(childObjectKey); for (int j = 0; j < jsonArray.length(); j++) { JSONObject innerItem = jsonArray.getJSONObject(j); String anotherKeyAtIndex = innerItem.getString("key"); if (childKey.equals(anotherKeyAtIndex)) { innerItem.put("value", value); return; } } } } } } @Override public String currentJsonState() { synchronized (mJSONObject) { return mJSONObject.toString(); } } @Override public String getCount() { synchronized (mJSONObject) { return mJSONObject.optString("count"); } } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("jsonState", mJSONObject.toString()); } }