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);
boolean returnValue = XmlFileInterface.writeFile();
try
{
XmlFileInterface.readFile();
}
catch(Exception e)
{
Miscellaneous.logEvent("w", "Read file", Log.getStackTraceString(e), 3);
}
if(returnValue)
{
AutomationService service = AutomationService.getInstance();
@ -219,6 +228,19 @@ public class Rule implements Comparable<Rule>
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)
{
if(this.getName() == null || this.getName().length()==0)
@ -876,17 +898,6 @@ public class Rule implements Comparable<Rule>
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>
{
boolean wasActivated = false;

View File

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

View File

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

View File

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