Ejemplo n.º 1
0
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handles item selection in action bar
    if (item.getItemId() == R.id.action_save_profile) {
      /*
          Performs validation of input data
      */
      boolean isError = false;

      if (name.getText().length() == 0) {
        isError = true;
        name.setError("Name is required");
      }

      if (name.getText().toString().trim().length() > 50) {
        isError = true;
        name.setError("Name can't be longer than 50 characters");
      }

      if (textTimeMinutes.getText().equals("00") && textTimeSeconds.getText().equals("00")) {
        isError = true;
        timeError.setError("Time is required");
      }

      if (repeat.isChecked() && repeatNumber.getText().length() == 0) {
        isError = true;
        repeatNumber.setError("Number of laps is required");
      }

      /*
          Produces an error if:
          - name of profile is taken and adding a new profile
          - changed profile name is taken
      */
      if (dbHandler.profileExists(name.getText().toString().trim())) {
        if (!isEditingProfile || !name.getText().toString().trim().equals(profile.getName())) {
          isError = true;
          name.setError("Name already exists");
        }
      }

      if (!isError) {
        // Either adds or edits the profile depending on what's needed
        if (isEditingProfile) {
          setResult(PROFILE_EDITED);
          updateProfile();
        } else {
          setResult(PROFILE_ADDED);
          addProfile();
        }

        finish();
      }
    }

    return super.onOptionsItemSelected(item);
  }
Ejemplo n.º 2
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Sets the activity_profile as this activity's display
    setContentView(R.layout.activity_profile);

    // Initialising components
    // Input area components
    name = (EditText) findViewById(R.id.profile_text_name);
    repeatNumber = (EditText) findViewById(R.id.profile_text_repeat);
    textTimeMinutes = (TextView) findViewById(R.id.profile_text_time_minutes);
    textTimeSeconds = (TextView) findViewById(R.id.profile_text_time_seconds);
    timeError = (TextView) findViewById(R.id.profile_text_time_error);
    repeat = (CheckBox) findViewById(R.id.profile_checkbox_repeat);

    // Database
    dbHandler = new DBHandler(this, null, null, 1);

    // Handles click events for textTimeMinutes and textTimeSeconds
    textTimeMinutes.setOnClickListener(new TextTimeClickListener(true, this));
    textTimeSeconds.setOnClickListener(new TextTimeClickListener(false, this));

    if (getIntent().hasExtra("profileID")) {
      isEditingProfile = true;

      // Changes activity's title to @string/title_edit_profile
      setTitle(R.string.title_edit_profile);

      // Creates new chosen profile object
      profile = dbHandler.getProfile(getIntent().getIntExtra("profileID", 1));

      // Sets input box values for profile loaded from database
      name.setText(profile.getName());
      setTextTime(textTimeMinutes, profile.getMinutes());
      setTextTime(textTimeSeconds, profile.getSeconds());
      repeat.setChecked(profile.isRepeat());
      repeatNumber.setText(Integer.toString(profile.getRepeatNumber()));
    } else {
      // Changes activity's title to @string/title_new_profile
      setTitle(R.string.title_new_profile);
    }
  }