fix objectives import/export

This commit is contained in:
Milos Kozak 2016-08-13 11:30:43 +02:00
parent 40aada0eb4
commit 3fb84636db
3 changed files with 70 additions and 37 deletions

View file

@ -7,6 +7,7 @@ import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView; import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -37,6 +38,8 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener
RecyclerView recyclerView; RecyclerView recyclerView;
LinearLayoutManager llm; LinearLayoutManager llm;
CheckBox enableFake; CheckBox enableFake;
LinearLayout fake_layout;
TextView reset;
@Override @Override
public void onClick(View v) { public void onClick(View v) {
@ -82,7 +85,7 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener
ObjectivesPlugin.Objective o = (ObjectivesPlugin.Objective) v.getTag(); ObjectivesPlugin.Objective o = (ObjectivesPlugin.Objective) v.getTag();
o.started = new Date(); o.started = new Date();
updateGUI(); updateGUI();
objectivesPlugin.saveProgress(); ObjectivesPlugin.saveProgress();
} }
}); });
holder.verifyButton.setOnClickListener(new View.OnClickListener() { holder.verifyButton.setOnClickListener(new View.OnClickListener() {
@ -91,7 +94,7 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener
if (objectivesPlugin.requirementsMet(o.num).done || enableFake.isChecked()) { if (objectivesPlugin.requirementsMet(o.num).done || enableFake.isChecked()) {
o.accomplished = new Date(); o.accomplished = new Date();
updateGUI(); updateGUI();
objectivesPlugin.saveProgress(); ObjectivesPlugin.saveProgress();
} }
} }
}); });
@ -185,11 +188,20 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener
llm = new LinearLayoutManager(view.getContext()); llm = new LinearLayoutManager(view.getContext());
recyclerView.setLayoutManager(llm); recyclerView.setLayoutManager(llm);
enableFake = (CheckBox) view.findViewById(R.id.objectives_fake); enableFake = (CheckBox) view.findViewById(R.id.objectives_fake);
fake_layout = (LinearLayout) view.findViewById(R.id.objectives_fake_layout);
reset = (TextView) view.findViewById(R.id.objectives_reset);
enableFake.setOnClickListener(new View.OnClickListener() { enableFake.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
updateGUI(); updateGUI();
} }
}); });
reset.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
objectivesPlugin.initializeData();
objectivesPlugin.saveProgress();
updateGUI();
}
});
// Add correct translations to array after app is initialized // Add correct translations to array after app is initialized
objectivesPlugin.objectives.get(0).objective = MainApp.sResources.getString(R.string.objectives_0_objective); objectivesPlugin.objectives.get(0).objective = MainApp.sResources.getString(R.string.objectives_0_objective);
@ -216,7 +228,7 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
RecyclerViewAdapter adapter = new RecyclerViewAdapter(objectivesPlugin.objectives); RecyclerViewAdapter adapter = new RecyclerViewAdapter(ObjectivesPlugin.objectives);
recyclerView.setAdapter(adapter); recyclerView.setAdapter(adapter);
} }
}); });

View file

@ -16,6 +16,7 @@ import info.nightscout.androidaps.R;
import info.nightscout.androidaps.interfaces.ConstraintsInterface; import info.nightscout.androidaps.interfaces.ConstraintsInterface;
import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.Loop.LoopPlugin; import info.nightscout.androidaps.plugins.Loop.LoopPlugin;
import info.nightscout.utils.SafeParse;
/** /**
* Created by mike on 05.08.2016. * Created by mike on 05.08.2016.
@ -130,66 +131,68 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface {
} }
private void initializeData() { public void initializeData() {
objectives = new ArrayList<>(); objectives = new ArrayList<>();
objectives.add(new Objective(0, objectives.add(new Objective(0,
MainApp.sResources.getString(R.string.objectives_0_objective), MainApp.sResources.getString(R.string.objectives_0_objective),
MainApp.sResources.getString(R.string.objectives_0_gate), MainApp.sResources.getString(R.string.objectives_0_gate),
new Date(0, 0, 0), new Date(0),
1, // 1 day 1, // 1 day
new Date(0, 0, 0))); new Date(0)));
objectives.add(new Objective(1, objectives.add(new Objective(1,
MainApp.sResources.getString(R.string.objectives_1_objective), MainApp.sResources.getString(R.string.objectives_1_objective),
MainApp.sResources.getString(R.string.objectives_1_gate), MainApp.sResources.getString(R.string.objectives_1_gate),
new Date(0, 0, 0), new Date(0),
7, // 7 days 7, // 7 days
new Date(0, 0, 0))); new Date(0)));
objectives.add(new Objective(2, objectives.add(new Objective(2,
MainApp.sResources.getString(R.string.objectives_2_objective), MainApp.sResources.getString(R.string.objectives_2_objective),
MainApp.sResources.getString(R.string.objectives_2_gate), MainApp.sResources.getString(R.string.objectives_2_gate),
new Date(0, 0, 0), new Date(0),
0, // 0 days 0, // 0 days
new Date(0, 0, 0))); new Date(0)));
objectives.add(new Objective(3, objectives.add(new Objective(3,
MainApp.sResources.getString(R.string.objectives_3_objective), MainApp.sResources.getString(R.string.objectives_3_objective),
MainApp.sResources.getString(R.string.objectives_3_gate), MainApp.sResources.getString(R.string.objectives_3_gate),
new Date(0, 0, 0), new Date(0),
5, // 5 days 5, // 5 days
new Date(0, 0, 0))); new Date(0)));
objectives.add(new Objective(4, objectives.add(new Objective(4,
MainApp.sResources.getString(R.string.objectives_4_objective), MainApp.sResources.getString(R.string.objectives_4_objective),
MainApp.sResources.getString(R.string.objectives_4_gate), MainApp.sResources.getString(R.string.objectives_4_gate),
new Date(0, 0, 0), new Date(0),
1, 1,
new Date(0, 0, 0))); new Date(0)));
objectives.add(new Objective(5, objectives.add(new Objective(5,
MainApp.sResources.getString(R.string.objectives_5_objective), MainApp.sResources.getString(R.string.objectives_5_objective),
MainApp.sResources.getString(R.string.objectives_5_gate), MainApp.sResources.getString(R.string.objectives_5_gate),
new Date(0, 0, 0), new Date(0),
7, 7,
new Date(0, 0, 0))); new Date(0)));
objectives.add(new Objective(6, objectives.add(new Objective(6,
MainApp.sResources.getString(R.string.objectives_6_objective), MainApp.sResources.getString(R.string.objectives_6_objective),
"", "",
new Date(0, 0, 0), new Date(0),
1, 1,
new Date(0, 0, 0))); new Date(0)));
} }
public static void saveProgress() { public static void saveProgress() {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); if (objectives != null) {
SharedPreferences.Editor editor = settings.edit(); SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
for (int num = 0; num < objectives.size(); num++) { SharedPreferences.Editor editor = settings.edit();
Objective o = objectives.get(num); for (int num = 0; num < objectives.size(); num++) {
editor.putLong("Objectives" + num + "started", o.started.getTime()); Objective o = objectives.get(num);
editor.putLong("Objectives" + num + "accomplished", o.accomplished.getTime()); editor.putString("Objectives" + num + "started", Long.toString(o.started.getTime()));
editor.putString("Objectives" + num + "accomplished", Long.toString(o.accomplished.getTime()));
}
editor.putBoolean("Objectives" + "bgIsAvailableInNS", bgIsAvailableInNS);
editor.putBoolean("Objectives" + "pumpStatusIsAvailableInNS", pumpStatusIsAvailableInNS);
editor.putString("Objectives" + "manualEnacts", Integer.toString(manualEnacts));
editor.apply();
if (Config.logPrefsChange)
log.debug("Objectives stored");
} }
editor.putBoolean("Objectives" + "bgIsAvailableInNS", bgIsAvailableInNS);
editor.putBoolean("Objectives" + "pumpStatusIsAvailableInNS", pumpStatusIsAvailableInNS);
editor.putInt("Objectives" + "manualEnacts", manualEnacts);
editor.apply();
if (Config.logPrefsChange)
log.debug("Objectives stored");
} }
void loadProgress() { void loadProgress() {
@ -197,8 +200,8 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface {
for (int num = 0; num < objectives.size(); num++) { for (int num = 0; num < objectives.size(); num++) {
Objective o = objectives.get(num); Objective o = objectives.get(num);
try { try {
o.started = new Date(settings.getLong("Objectives" + num + "started", 0)); o.started = new Date(SafeParse.stringToLong(settings.getString("Objectives" + num + "started", "0")));
o.accomplished = new Date(settings.getLong("Objectives" + num + "accomplished", 0)); o.accomplished = new Date(SafeParse.stringToLong(settings.getString("Objectives" + num + "accomplished", "0")));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -206,7 +209,7 @@ public class ObjectivesPlugin implements PluginBase, ConstraintsInterface {
bgIsAvailableInNS = settings.getBoolean("Objectives" + "bgIsAvailableInNS", false); bgIsAvailableInNS = settings.getBoolean("Objectives" + "bgIsAvailableInNS", false);
pumpStatusIsAvailableInNS = settings.getBoolean("Objectives" + "pumpStatusIsAvailableInNS", false); pumpStatusIsAvailableInNS = settings.getBoolean("Objectives" + "pumpStatusIsAvailableInNS", false);
try { try {
manualEnacts = settings.getInt("Objectives" + "manualEnacts", 0); manualEnacts = SafeParse.stringToInt(settings.getString("Objectives" + "manualEnacts", "0"));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View file

@ -9,11 +9,29 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<CheckBox <LinearLayout
android:layout_width="wrap_content" android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Enable fake time and progress" android:id="@+id/objectives_fake_layout"
android:id="@+id/objectives_fake" /> android:visibility="gone">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enable fake time and progress"
android:id="@+id/objectives_fake" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:id="@+id/objectives_reset"
android:textColor="#fef900"
android:gravity="right"
android:layout_weight="0.5"
android:layout_marginRight="10dp" />
</LinearLayout>
<android.support.v7.widget.RecyclerView <android.support.v7.widget.RecyclerView
android:id="@+id/objectives_recyclerview" android:id="@+id/objectives_recyclerview"