AndroidAPS/app/src/main/java/info/nightscout/androidaps/startupwizard/SetupWizardActivity.java

183 lines
6.1 KiB
Java
Raw Normal View History

2018-04-20 17:27:31 +02:00
package info.nightscout.androidaps.startupwizard;
2018-04-23 16:03:22 +02:00
import android.content.Intent;
2018-04-20 17:27:31 +02:00
import android.os.Bundle;
2018-05-03 16:04:56 +02:00
import android.support.v7.app.AppCompatActivity;
2018-04-20 17:27:31 +02:00
import android.view.View;
2018-04-23 14:58:27 +02:00
import android.widget.LinearLayout;
import android.widget.TextView;
2018-05-03 20:06:13 +02:00
import com.squareup.otto.Subscribe;
2018-04-25 10:03:33 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2018-04-23 14:58:27 +02:00
import java.util.List;
2018-04-20 17:27:31 +02:00
2018-04-26 15:59:56 +02:00
import info.nightscout.androidaps.MainActivity;
2018-04-30 15:41:40 +02:00
import info.nightscout.androidaps.MainApp;
2018-04-20 17:27:31 +02:00
import info.nightscout.androidaps.R;
2018-05-09 18:49:42 +02:00
import info.nightscout.androidaps.events.EventProfileStoreChanged;
2018-05-10 23:59:20 +02:00
import info.nightscout.androidaps.events.EventProfileSwitchChange;
2018-05-09 13:38:17 +02:00
import info.nightscout.androidaps.events.EventPumpStatusChanged;
2018-05-11 08:30:21 +02:00
import info.nightscout.androidaps.plugins.ConstraintsObjectives.events.EventObjectivesSaved;
2018-05-03 20:06:13 +02:00
import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientStatus;
import info.nightscout.androidaps.startupwizard.events.EventSWUpdate;
2018-04-30 15:41:40 +02:00
import info.nightscout.utils.LocaleHelper;
2018-05-09 19:24:20 +02:00
import info.nightscout.utils.OKDialog;
2018-05-11 13:14:59 +02:00
import info.nightscout.utils.SP;
2018-04-20 17:27:31 +02:00
public class SetupWizardActivity extends AppCompatActivity {
2018-05-03 16:04:56 +02:00
//logging
2018-04-25 10:03:33 +02:00
private static Logger log = LoggerFactory.getLogger(SetupWizardActivity.class);
2018-05-03 16:04:56 +02:00
2018-05-09 15:10:47 +02:00
private SWDefinition swDefinition = new SWDefinition();
private List<SWScreen> screens = swDefinition.getScreens();
2018-04-25 14:29:20 +02:00
private int currentWizardPage = 0;
2018-04-23 16:03:22 +02:00
public static final String INTENT_MESSAGE = "WIZZARDPAGE";
2018-04-20 17:27:31 +02:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2018-05-03 20:06:13 +02:00
LocaleHelper.onCreate(this, "en");
2018-04-20 17:27:31 +02:00
setContentView(R.layout.activity_setupwizard);
2018-04-23 14:58:27 +02:00
2018-04-23 16:03:22 +02:00
Intent intent = getIntent();
2018-05-03 20:06:13 +02:00
currentWizardPage = intent.getIntExtra(SetupWizardActivity.INTENT_MESSAGE, 0);
if (screens.size() > 0 && currentWizardPage < screens.size()) {
SWScreen currentScreen = screens.get(currentWizardPage);
2018-04-23 14:58:27 +02:00
//Set screen name
2018-05-09 15:10:47 +02:00
TextView screenName = (TextView) findViewById(R.id.sw_content);
2018-04-23 14:58:27 +02:00
screenName.setText(currentScreen.getHeader());
2018-05-09 18:49:42 +02:00
swDefinition.setActivity(this);
2018-04-27 12:30:45 +02:00
//Generate layout first
2018-05-04 10:17:50 +02:00
generateLayout();
2018-05-03 20:06:13 +02:00
updateButtons();
2018-04-23 14:58:27 +02:00
}
2018-05-03 20:06:13 +02:00
}
2018-04-23 14:58:27 +02:00
2018-05-09 19:24:20 +02:00
@Override
public void onBackPressed() {
OKDialog.showConfirmation(this, MainApp.gs(R.string.exitwizard), this::finish);
}
2018-05-03 20:06:13 +02:00
@Override
public void onPause() {
super.onPause();
MainApp.bus().unregister(this);
2018-04-20 17:27:31 +02:00
}
2018-04-30 15:41:40 +02:00
@Override
protected void onResume() {
super.onResume();
2018-05-03 20:06:13 +02:00
MainApp.bus().register(this);
2018-05-09 15:10:47 +02:00
swDefinition.setActivity(this);
2018-04-30 15:41:40 +02:00
}
2018-05-03 20:06:13 +02:00
@Subscribe
public void onContentUpdate(EventSWUpdate ev) {
2018-05-04 10:17:50 +02:00
if (ev.redraw)
generateLayout();
2018-05-03 20:06:13 +02:00
updateButtons();
}
@Subscribe
2018-05-09 13:38:17 +02:00
public void onEventNSClientStatus(EventNSClientStatus ignored) {
updateButtons();
}
@Subscribe
public void onEventPumpStatusChanged(EventPumpStatusChanged ignored) {
2018-05-03 20:06:13 +02:00
updateButtons();
}
2018-05-09 18:49:42 +02:00
@Subscribe
public void onEventProfileStoreChanged(EventProfileStoreChanged ignored) {
updateButtons();
}
2018-05-10 23:59:20 +02:00
@Subscribe
2018-05-11 08:30:21 +02:00
public void onEventProfileSwitchChange(EventProfileSwitchChange ignored) {
updateButtons();
}
@Subscribe
public void onEventObjectivesSaved(EventObjectivesSaved ignored) {
2018-05-10 23:59:20 +02:00
updateButtons();
}
2018-05-04 10:17:50 +02:00
private void generateLayout() {
SWScreen currentScreen = screens.get(currentWizardPage);
LinearLayout layout = SWItem.generateLayout(this.findViewById(R.id.sw_content_fields));
for (int i = 0; i < currentScreen.items.size(); i++) {
SWItem currentItem = currentScreen.items.get(i);
currentItem.generateDialog(this.findViewById(R.id.sw_content_fields), layout);
}
}
2018-05-03 20:06:13 +02:00
private void updateButtons() {
2018-05-09 18:49:42 +02:00
runOnUiThread(() -> {
SWScreen currentScreen = screens.get(currentWizardPage);
if (currentScreen.validator == null || currentScreen.validator.isValid() || currentScreen.skippable) {
if (currentWizardPage == nextPage()) {
findViewById(R.id.finish_button).setVisibility(View.VISIBLE);
findViewById(R.id.next_button).setVisibility(View.GONE);
} else {
findViewById(R.id.finish_button).setVisibility(View.GONE);
findViewById(R.id.next_button).setVisibility(View.VISIBLE);
}
2018-05-03 20:06:13 +02:00
} else {
findViewById(R.id.finish_button).setVisibility(View.GONE);
2018-05-09 18:49:42 +02:00
findViewById(R.id.next_button).setVisibility(View.GONE);
2018-05-03 20:06:13 +02:00
}
2018-05-09 18:49:42 +02:00
if (currentWizardPage == 0)
findViewById(R.id.previous_button).setVisibility(View.GONE);
else
findViewById(R.id.previous_button).setVisibility(View.VISIBLE);
currentScreen.processVisibility();
});
2018-04-26 15:59:56 +02:00
}
2018-04-23 14:58:27 +02:00
2018-04-23 16:03:22 +02:00
public void showNextPage(View view) {
2018-05-03 20:06:13 +02:00
this.finish();
2018-04-23 16:03:22 +02:00
Intent intent = new Intent(this, SetupWizardActivity.class);
2018-05-09 18:49:42 +02:00
intent.putExtra(INTENT_MESSAGE, nextPage());
2018-04-23 16:03:22 +02:00
startActivity(intent);
}
public void showPreviousPage(View view) {
2018-05-03 20:06:13 +02:00
this.finish();
2018-04-23 16:03:22 +02:00
Intent intent = new Intent(this, SetupWizardActivity.class);
2018-05-09 18:49:42 +02:00
intent.putExtra(INTENT_MESSAGE, previousPage());
2018-04-23 16:03:22 +02:00
startActivity(intent);
}
2018-04-26 15:59:56 +02:00
// Go back to overview
2018-05-03 16:04:56 +02:00
public void finishSetupWizard(View view) {
2018-05-11 13:14:59 +02:00
SP.putBoolean(R.string.key_setupwizard_processed, true);
2018-05-03 16:04:56 +02:00
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
2018-04-26 15:59:56 +02:00
}
2018-04-30 15:41:40 +02:00
2018-05-09 18:49:42 +02:00
private int nextPage() {
int page = currentWizardPage + 1;
while (page < screens.size()) {
if (screens.get(page).visibility == null || screens.get(page).visibility.isValid())
return page;
page++;
}
return currentWizardPage;
}
private int previousPage() {
int page = currentWizardPage - 1;
while (page > 0) {
if (screens.get(page).visibility == null || screens.get(page).visibility.isValid())
return page;
page--;
}
return currentWizardPage;
}
2018-05-03 16:04:56 +02:00
}