Skip to content

Refactor Degree Parsing and Updating Logic into a Helper Method #2665

Closed
@sourcery-ai

Description

@sourcery-ai

Description

The current implementation contains duplicated logic for parsing and updating degree values across multiple listeners. This redundancy can be reduced by extracting the common logic into a helper method. This refactoring will improve code maintainability and readability.

Suggested Solution

  1. Extract a Helper Method

    Create a method processDegreeInput that handles the degree input parsing and updates the associated seek arc. This method should:

    • Parse the degree from the TextView.
    • Update the degree value and the SeekArc progress.
    • Handle invalid degree values by resetting the TextView and SeekArc and displaying a toast message.
    private void processDegreeInput(TextView degreeText, SeekArc seekArc) {
        int previousDegree = degree;
        String degreeStr = degreeText.getText().toString().trim();
        if (degreeStr.isEmpty()) {
            degree = (degree == 0) ? (int) (seekArc.getProgress() * 3.6) : previousDegree;
        } else {
            degree = Integer.parseInt(degreeStr);
        }
    
        if (degree > 360 || degree < 0) {
            degreeText.setText(getResources().getString(R.string.zero));
            seekArc.setProgress(0);
            toastInvalidValueMessage();
        } else {
            seekArc.setProgress((int) (degree / 3.6));
            editEnter = true;
        }
    }
  2. Refactor Each Listener

    Update each OnEditorActionListener to utilize the new helper method, passing the appropriate TextView and SeekArc as parameters.

    degreeText1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            removeStatusBar();
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                processDegreeInput(degreeText1, seekArc1);
            }
            return false;
        }
    });

    Repeat similarly for degreeText2, degreeText3, and degreeText4, passing the corresponding seek arc.

Action Items

  • Implement the processDegreeInput helper method.
  • Refactor existing listeners to use the new helper method.
  • Test to ensure functionality remains consistent and no regressions are introduced.

Notes

This refactoring task is currently out of scope for the ongoing pull request but should be addressed in a future update to enhance code quality.


This issue was identified by the sourcery-ai[bot] and acknowledged by contributor marcnause as a future improvement task.


I created this issue for @marcnause from #2652 (comment).

Tips and commands

Interacting with Sourcery

  • Generate a plan of action: Comment @sourcery-ai plan on this issue.
  • Generate a pull request for this issue: Comment @sourcery-ai develop to
    generate a PR that addresses this issue.

Getting Help

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions