Fixed cache problem after rule clone

This commit is contained in:
jens 2021-06-20 22:24:17 +02:00
parent 82156059fa
commit e445b787a9
4 changed files with 62 additions and 20 deletions

View File

@ -176,6 +176,15 @@ public class Rule implements Comparable<Rule>
ruleCollection.add(this); ruleCollection.add(this);
boolean returnValue = XmlFileInterface.writeFile(); boolean returnValue = XmlFileInterface.writeFile();
try
{
XmlFileInterface.readFile();
}
catch(Exception e)
{
Miscellaneous.logEvent("w", "Read file", Log.getStackTraceString(e), 3);
}
if(returnValue) if(returnValue)
{ {
AutomationService service = AutomationService.getInstance(); AutomationService service = AutomationService.getInstance();
@ -219,6 +228,19 @@ public class Rule implements Comparable<Rule>
return XmlFileInterface.writeFile(); return XmlFileInterface.writeFile();
} }
public boolean cloneRule(Context context)
{
Rule newRule = new Rule();
newRule.setName(this.getName() + " - clone");
newRule.setRuleActive(this.isRuleActive());
newRule.setRuleToggle(this.isRuleToggle());
newRule.setTriggerSet(this.getTriggerSet());
newRule.setActionSet(this.getActionSet());
return newRule.create(context);
}
private boolean checkBeforeSaving(Context context, boolean changeExistingRule) private boolean checkBeforeSaving(Context context, boolean changeExistingRule)
{ {
if(this.getName() == null || this.getName().length()==0) if(this.getName() == null || this.getName().length()==0)
@ -876,17 +898,6 @@ public class Rule implements Comparable<Rule>
return false; return false;
} }
public boolean cloneRule(Context context)
{
Rule newRule = new Rule();
newRule.setName(this.getName() + " - clone");
newRule.setRuleActive(this.isRuleActive());
newRule.setRuleToggle(this.isRuleToggle());
newRule.setTriggerSet(this.getTriggerSet());
newRule.setActionSet(this.getActionSet());
return newRule.create(context);
}
private class ActivateRuleTask extends AsyncTask<Object, String, Void> private class ActivateRuleTask extends AsyncTask<Object, String, Void>
{ {
boolean wasActivated = false; boolean wasActivated = false;

View File

@ -173,6 +173,15 @@ public class Rule implements Comparable<Rule>
ruleCollection.add(this); ruleCollection.add(this);
boolean returnValue = XmlFileInterface.writeFile(); boolean returnValue = XmlFileInterface.writeFile();
try
{
XmlFileInterface.readFile();
}
catch(Exception e)
{
Miscellaneous.logEvent("w", "Read file", Log.getStackTraceString(e), 3);
}
if(returnValue) if(returnValue)
{ {
AutomationService service = AutomationService.getInstance(); AutomationService service = AutomationService.getInstance();

View File

@ -175,6 +175,15 @@ public class Rule implements Comparable<Rule>
ruleCollection.add(this); ruleCollection.add(this);
boolean returnValue = XmlFileInterface.writeFile(); boolean returnValue = XmlFileInterface.writeFile();
try
{
XmlFileInterface.readFile();
}
catch(Exception e)
{
Miscellaneous.logEvent("w", "Read file", Log.getStackTraceString(e), 3);
}
if(returnValue) if(returnValue)
{ {
AutomationService service = AutomationService.getInstance(); AutomationService service = AutomationService.getInstance();

View File

@ -26,10 +26,14 @@ import java.util.ArrayList;
public class ActivityMainRules extends ActivityGeneric public class ActivityMainRules extends ActivityGeneric
{ {
private ListView ruleListView; private ListView ruleListView;
ArrayList<Rule> ruleList = new ArrayList<>();
private ArrayAdapter<Rule> ruleListViewAdapter; private ArrayAdapter<Rule> ruleListViewAdapter;
public static Rule ruleToEdit; public static Rule ruleToEdit;
protected static ActivityMainRules instance = null; protected static ActivityMainRules instance = null;
public static final int requestCodeCreateRule = 3000;
public static final int requestCodeChangeRule = 4000;
public static ActivityMainRules getInstance() public static ActivityMainRules getInstance()
{ {
if(instance == null) if(instance == null)
@ -54,13 +58,12 @@ public class ActivityMainRules extends ActivityGeneric
{ {
ruleToEdit = null; ruleToEdit = null;
Intent startAddRuleIntent = new Intent(ActivityMainRules.this, ActivityManageRule.class); Intent startAddRuleIntent = new Intent(ActivityMainRules.this, ActivityManageRule.class);
startActivityForResult(startAddRuleIntent, 3000); startActivityForResult(startAddRuleIntent, requestCodeCreateRule);
} }
}); });
ruleListViewAdapter = new RuleArrayAdapter(this, R.layout.view_for_rule_listview, ruleList);
ruleListView = (ListView)findViewById(R.id.lvRuleList); ruleListView = (ListView)findViewById(R.id.lvRuleList);
ruleListViewAdapter = new RuleArrayAdapter(this, R.layout.view_for_rule_listview, Rule.getRuleCollection());
ruleListView.setClickable(true); ruleListView.setClickable(true);
ruleListView.setOnItemLongClickListener(new OnItemLongClickListener() ruleListView.setOnItemLongClickListener(new OnItemLongClickListener()
@ -106,7 +109,6 @@ public class ActivityMainRules extends ActivityGeneric
private static class RuleArrayAdapter extends ArrayAdapter<Rule> private static class RuleArrayAdapter extends ArrayAdapter<Rule>
{ {
public RuleArrayAdapter(Context context, int resource, ArrayList<Rule> objects) public RuleArrayAdapter(Context context, int resource, ArrayList<Rule> objects)
{ {
super(context, resource, objects); super(context, resource, objects);
@ -157,13 +159,13 @@ public class ActivityMainRules extends ActivityGeneric
if(AutomationService.isMyServiceRunning(this)) if(AutomationService.isMyServiceRunning(this))
bindToService(); bindToService();
if(requestCode == 3000) //add Rule if(requestCode == requestCodeCreateRule) //add Rule
{ {
ruleToEdit = null; //clear cache ruleToEdit = null; //clear cache
updateListView(); updateListView();
} }
if(requestCode == 4000) //editRule if(requestCode == requestCodeChangeRule) //editRule
{ {
ruleToEdit = null; //clear cache ruleToEdit = null; //clear cache
updateListView(); updateListView();
@ -206,16 +208,22 @@ public class ActivityMainRules extends ActivityGeneric
case 1: case 1:
ruleToEdit = ruleThisIsAbout; ruleToEdit = ruleThisIsAbout;
Intent manageSpecificRuleIntent = new Intent (ActivityMainRules.this, ActivityManageRule.class); Intent manageSpecificRuleIntent = new Intent (ActivityMainRules.this, ActivityManageRule.class);
startActivityForResult(manageSpecificRuleIntent, 4000); startActivityForResult(manageSpecificRuleIntent, requestCodeChangeRule);
break; break;
case 2: case 2:
if(ruleThisIsAbout.delete()) if(ruleThisIsAbout.delete())
{
ruleToEdit = null; //clear cache
updateListView(); updateListView();
}
break; break;
case 3: case 3:
ruleToEdit = ruleThisIsAbout; ruleToEdit = ruleThisIsAbout;
if(ruleToEdit.cloneRule(ActivityMainRules.this)) if(ruleToEdit.cloneRule(ActivityMainRules.this))
{
ruleToEdit = null; //clear cache
updateListView(); updateListView();
}
break; break;
} }
} }
@ -228,6 +236,11 @@ public class ActivityMainRules extends ActivityGeneric
public void updateListView() public void updateListView()
{ {
Miscellaneous.logEvent("i", "ListView", "Attempting to update RuleListView", 4); Miscellaneous.logEvent("i", "ListView", "Attempting to update RuleListView", 4);
ruleList.clear();
for(Rule r : Rule.getRuleCollection())
ruleList.add(r);
try try
{ {
if(ruleListView.getAdapter() == null) if(ruleListView.getAdapter() == null)