Skip to content

Commit

Permalink
Allow to select Condition when editing Script on UI
Browse files Browse the repository at this point in the history
  • Loading branch information
renyuneyun committed May 9, 2018
1 parent 4799c93 commit 18585d5
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ JSONObject serialize_situation(EventData event) throws JSONException {
private JSONObject serialize_condition_trigger(ConditionStructure condition) throws JSONException {
JSONObject json_trigger = new JSONObject();
json_trigger.put(C.TYPE, C.TriggerType.T_CONDITION);
json_trigger.put(C.SCENARIO, condition.getName());
json_trigger.put(C.CONDITION, condition.getName());
return json_trigger;
}

Expand Down
119 changes: 84 additions & 35 deletions app/src/main/java/ryey/easer/core/ui/edit/EditScriptActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;

import java.util.List;
Expand All @@ -35,8 +36,10 @@
import ryey.easer.commons.C;
import ryey.easer.commons.plugindef.InvalidDataInputException;
import ryey.easer.commons.plugindef.eventplugin.EventData;
import ryey.easer.core.data.ConditionStructure;
import ryey.easer.core.data.ScenarioStructure;
import ryey.easer.core.data.ScriptStructure;
import ryey.easer.core.data.storage.ConditionDataStorage;
import ryey.easer.core.data.storage.ProfileDataStorage;
import ryey.easer.core.data.storage.ScenarioDataStorage;
import ryey.easer.core.data.storage.ScriptDataStorage;
Expand All @@ -50,7 +53,7 @@ public class EditScriptActivity extends AbstractEditDataActivity<ScriptStructure
TAG_DATA_TYPE = "script";
}

EventPluginViewPager mViewPager;
EventPluginViewPager mViewPager_edit_event;

EditText mEditText_name = null;
private static final String NON = ""; //TODO: more robust
Expand All @@ -59,15 +62,19 @@ public class EditScriptActivity extends AbstractEditDataActivity<ScriptStructure
Spinner mSpinner_profile = null;
List<String> mProfileList = null;
boolean isActive = true;
CompoundButton mSwitch_use_scenario;
RadioGroup rg_mode;
CompoundButton mSwitch_reverse;

ConstraintLayout layout_use_scenario;
Spinner mSpinner_scenario;
List<String> mScenarioList;
CompoundButton mSwitch_reverse;
CompoundButton mSwitch_repeatable;
CompoundButton mSwitch_persistent;

ConstraintLayout layout_use_scenario;
ConstraintLayout layout_use_condition;
Spinner mSpinner_condition;
List<String> mConditionList;


@Override
public boolean onCreateOptionsMenu(Menu menu) {
Expand Down Expand Up @@ -137,14 +144,12 @@ public void onNothingSelected(AdapterView<?> adapterView) {
}
});

mSwitch_use_scenario = findViewById(R.id.switch_use_scenario);
mSwitch_use_scenario.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
layout_use_scenario.setVisibility(checked ? View.VISIBLE : View.GONE);
mViewPager.setVisibility(!checked ? View.VISIBLE : View.GONE);
}
});
mSwitch_reverse = findViewById(R.id.switch_reverse);

mViewPager_edit_event = findViewById(R.id.pager);
mViewPager_edit_event.init(this);

layout_use_scenario = findViewById(R.id.layout_use_scenario);
mSpinner_scenario = findViewById(R.id.spinner_scenario);
mScenarioList = ScenarioDataStorage.getInstance(this).list();
ArrayAdapter<String> adapter_scenario = new ArrayAdapter<>(this, R.layout.spinner_simple, mScenarioList);
Expand All @@ -161,17 +166,48 @@ public void onNothingSelected(AdapterView<?> adapterView) {

}
});

mViewPager = findViewById(R.id.pager);
mViewPager.init(this);

layout_use_scenario = findViewById(R.id.layout_use_scenario);
mSwitch_reverse = findViewById(R.id.switch_reverse);
mSwitch_repeatable = findViewById(R.id.switch_repeatable);
mSwitch_persistent = findViewById(R.id.switch_persistent);

mSwitch_use_scenario.setChecked(true);
mSwitch_use_scenario.setChecked(false);
layout_use_condition = findViewById(R.id.layout_use_condition);
mSpinner_condition = findViewById(R.id.spinner_condition);
mConditionList = ConditionDataStorage.getInstance(this).list();
ArrayAdapter<String> adapter_condition = new ArrayAdapter<>(this, R.layout.spinner_simple, mConditionList);
adapter_condition.setDropDownViewResource(android.R.layout.simple_spinner_item);
mSpinner_condition.setAdapter(adapter_condition);
mSpinner_condition.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
adapterView.setSelection(i);
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
});

rg_mode = findViewById(R.id.rg_use_scenario);
rg_mode.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int id) {
int v_inline = View.GONE, v_scenario = View.GONE, v_condition = View.GONE;
if (id == R.id.radioButton_inline_scenario) {
v_inline = View.VISIBLE;
} else if (id == R.id.radioButton_scenario) {
v_scenario = View.VISIBLE;
} else if (id == R.id.radioButton_condition) {
v_condition = View.VISIBLE;
} else {
throw new IllegalAccessError();
}
mViewPager_edit_event.setVisibility(v_inline);
layout_use_scenario.setVisibility(v_scenario);
layout_use_condition.setVisibility(v_condition);
}
});
rg_mode.check(R.id.radioButton_condition);
rg_mode.check(R.id.radioButton_scenario);
}

@Override
Expand All @@ -185,17 +221,24 @@ protected void loadFromData(ScriptStructure script) {
String parent = script.getParentName();
mSpinner_parent.setSelection(mScriptList.indexOf(parent));

ScenarioStructure scenario = script.getScenario();
if (scenario.isTmpScenario()) {
mSwitch_use_scenario.setChecked(false);
EventData eventData = scenario.getEventData();
mViewPager.setEventData(eventData);
mSwitch_reverse.setChecked(script.isReverse());

if (script.isEvent()) {
ScenarioStructure scenario = script.getScenario();
if (scenario.isTmpScenario()) {
rg_mode.check(R.id.radioButton_inline_scenario);
EventData eventData = scenario.getEventData();
mViewPager_edit_event.setEventData(eventData);
} else {
rg_mode.check(R.id.radioButton_scenario);
mSpinner_scenario.setSelection(mScenarioList.indexOf(scenario.getName()));
mSwitch_repeatable.setChecked(script.isRepeatable());
mSwitch_persistent.setChecked(script.isPersistent());
}
} else {
mSwitch_use_scenario.setChecked(true);
mSpinner_scenario.setSelection(mScenarioList.indexOf(scenario.getName()));
mSwitch_reverse.setChecked(script.isReverse());
mSwitch_repeatable.setChecked(script.isRepeatable());
mSwitch_persistent.setChecked(script.isPersistent());
rg_mode.check(R.id.radioButton_condition);
ConditionStructure condition = script.getCondition();
mSpinner_condition.setSelection(mConditionList.indexOf(condition.getName()));
}

isActive = script.isActive();
Expand All @@ -212,15 +255,21 @@ protected ScriptStructure saveToData() throws InvalidDataInputException {
if (!parent.equals(NON))
script.setParentName(parent);

ScenarioDataStorage scenarioDataStorage = ScenarioDataStorage.getInstance(this);
if (mSwitch_use_scenario.isChecked()) {
script.setReverse(mSwitch_reverse.isChecked());

if (rg_mode.getCheckedRadioButtonId() == R.id.radioButton_inline_scenario) {
ScenarioDataStorage scenarioDataStorage = ScenarioDataStorage.getInstance(this);
script.setEventData(mViewPager_edit_event.getEventData());
} else if (rg_mode.getCheckedRadioButtonId() == R.id.radioButton_scenario) {
ScenarioDataStorage scenarioDataStorage = ScenarioDataStorage.getInstance(this);
String scenario_name = (String) mSpinner_scenario.getSelectedItem();
script.setScenario(scenarioDataStorage.get(scenario_name));
script.setReverse(mSwitch_reverse.isChecked());
script.setRepeatable(mSwitch_repeatable.isChecked());
script.setPersistent(mSwitch_persistent.isChecked());
} else {
script.setEventData(mViewPager.getEventData());
} else if (rg_mode.getCheckedRadioButtonId() == R.id.radioButton_condition) {
ConditionDataStorage conditionDataStorage = ConditionDataStorage.getInstance(this);
String condition_name = (String) mSpinner_condition.getSelectedItem();
script.setCondition(conditionDataStorage.get(condition_name));
}
return script;
}
Expand Down
93 changes: 65 additions & 28 deletions app/src/main/res/layout/activity_edit_script.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,14 @@

</GridLayout>

<android.support.v7.widget.SwitchCompat
android:id="@+id/switch_use_scenario"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:showText="true"
android:textOff="@string/text_inline_scenario"
android:textOn="@string/text_use_scenario"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/layout_top" />

<ryey.easer.core.ui.edit.EventPluginViewPager
android:id="@+id/pager"
style="@style/AppTheme.PluginViewPager"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/switch_use_scenario">
app:layout_constraintTop_toBottomOf="@+id/switch_reverse">

<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
Expand All @@ -109,7 +95,7 @@
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/switch_use_scenario">
app:layout_constraintTop_toBottomOf="@+id/switch_reverse">

<Spinner
android:id="@+id/spinner_scenario"
Expand All @@ -131,18 +117,6 @@
android:text="@string/text_repeatable_desc"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/switch_reverse" />

<android.support.v7.widget.SwitchCompat
android:id="@+id/switch_reverse"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/text_reverse_desc"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/spinner_scenario" />

<android.support.v7.widget.SwitchCompat
Expand All @@ -160,4 +134,67 @@

</android.support.constraint.ConstraintLayout>

<android.support.constraint.ConstraintLayout
android:id="@+id/layout_use_condition"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/switch_reverse">

<Spinner
android:id="@+id/spinner_condition"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

<RadioGroup
android:id="@+id/rg_use_scenario"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:checkedButton="@+id/radioButton_inline_scenario"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/layout_top">

<RadioButton
android:id="@+id/radioButton_inline_scenario"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/text_inline_scenario" />

<RadioButton
android:id="@+id/radioButton_scenario"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/text_use_scenario" />

<RadioButton
android:id="@+id/radioButton_condition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/text_use_condition" />
</RadioGroup>

<android.support.v7.widget.SwitchCompat
android:id="@+id/switch_reverse"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="@string/text_reverse_desc"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/rg_use_scenario" />

</android.support.constraint.ConstraintLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,6 @@
<string name="title_condition">狀況</string>
<string name="title_edit_condition">編輯狀況</string>
<string name="hint_condition_title">狀況名</string>
<string name="text_use_condition">使用狀況</string>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<string name="text_profile">Profile: </string>
<string name="text_use_scenario">Use Scenario</string>
<string name="text_inline_scenario">Inline Scenario</string>
<string name="text_use_condition">Use Condition</string>
<string name="text_repeatable_desc">Repeatable when satisfied</string>
<string name="text_reverse_desc">Reverse Scenario</string>
<string name="text_volatile">Volatile</string>
Expand Down

0 comments on commit 18585d5

Please sign in to comment.