diff --git a/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/LandingFragment.java b/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/LandingFragment.java index ea5abb2..3da2955 100644 --- a/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/LandingFragment.java +++ b/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/LandingFragment.java @@ -35,12 +35,9 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c binding = FragmentLandingBinding.inflate(inflater, container, false); final Fragment fragment = this; - binding.fab.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - NavController navController = NavHostFragment.findNavController(fragment); - navController.navigate(R.id.action_landingFragment_to_newAlarmFormFragment); - } + binding.fab.setOnClickListener(view -> { + NavController navController = NavHostFragment.findNavController(fragment); + navController.navigate(R.id.action_landingFragment_to_newAlarmFormFragment); }); return binding.getRoot(); @@ -54,25 +51,22 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat MutableLiveData liveData = navController.getCurrentBackStackEntry() .getSavedStateHandle() .getLiveData(NewAlarmFormFragment.ALARM_DATA_SERIALIZED_KEY); - liveData.observe(getViewLifecycleOwner(), new Observer() { - @Override - public void onChanged(String alarmSerialized) { - dataReceived = alarmSerialized != null && !alarmSerialized.isEmpty(); - if (dataReceived) { - Alarm alarm = Alarm.fromSerialized(alarmSerialized); - int hour = alarm.getTimeHour(); - int minutes = alarm.getTimeMinutes(); - String alertTime = ((hour > 9) ? hour : ("0" + hour)) + ":" + ((minutes > 9) ? minutes : ("0" + minutes)); - String alertInformationText = getResources().getString(R.string.main_activity_alarm_added_info, alarm.getTitle(), alertTime); - - binding.information.setText(alertInformationText); - binding.disclaimer.setVisibility(View.VISIBLE); - - Snackbar.make(binding.fab, getString(R.string.new_alarm_added), Snackbar.LENGTH_LONG).show(); - } else { - binding.information.setText(R.string.main_activity_explanation); - binding.disclaimer.setVisibility(View.GONE); - } + liveData.observe(getViewLifecycleOwner(), alarmSerialized -> { + dataReceived = alarmSerialized != null && !alarmSerialized.isEmpty(); + if (dataReceived) { + Alarm alarm = Alarm.fromSerialized(alarmSerialized); + int hour = alarm.getTimeHour(); + int minutes = alarm.getTimeMinutes(); + String alertTime = ((hour > 9) ? hour : ("0" + hour)) + ":" + ((minutes > 9) ? minutes : ("0" + minutes)); + String alertInformationText = getResources().getString(R.string.main_activity_alarm_added_info, alarm.getTitle(), alertTime); + + binding.information.setText(alertInformationText); + binding.disclaimer.setVisibility(View.VISIBLE); + + Snackbar.make(binding.fab, getString(R.string.new_alarm_added), Snackbar.LENGTH_LONG).show(); + } else { + binding.information.setText(R.string.main_activity_explanation); + binding.disclaimer.setVisibility(View.GONE); } }); } diff --git a/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/NewAlarmFormFragment.java b/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/NewAlarmFormFragment.java index 7bc6392..b3b19d9 100644 --- a/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/NewAlarmFormFragment.java +++ b/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/NewAlarmFormFragment.java @@ -88,16 +88,13 @@ public void onCompletedForm() { progressDialog.setCancelable(true); progressDialog.show(); progressDialog.setMessage(getString(R.string.form_sending_data_message)); - progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { - @Override - public void onCancel(DialogInterface dialogInterface) { - try { - dataSavingThread.interrupt(); - } catch (RuntimeException e) { - // No need to do anything here - } finally { - binding.stepperForm.cancelFormCompletionOrCancellationAttempt(); - } + progressDialog.setOnCancelListener(dialogInterface -> { + try { + dataSavingThread.interrupt(); + } catch (RuntimeException e) { + // No need to do anything here + } finally { + binding.stepperForm.cancelFormCompletionOrCancellationAttempt(); } }); } @@ -109,15 +106,12 @@ public void onCancelledForm() { private Thread saveData() { // Fake data saving effect - Thread thread = new Thread(new Runnable() { - @Override - public void run() { - try { - Thread.sleep(1000); - sendAlarmDataBack(); - } catch (InterruptedException e) { - e.printStackTrace(); - } + Thread thread = new Thread(() -> { + try { + Thread.sleep(1000); + sendAlarmDataBack(); + } catch (InterruptedException e) { + e.printStackTrace(); } }); thread.start(); @@ -127,17 +121,14 @@ public void run() { private void sendAlarmDataBack() { Activity activity = getActivity(); if (activity != null) { - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - Alarm alarm = new Alarm( - nameStep.getStepData(), - descriptionStep.getStepData(), - timeStep.getStepData().hour, - timeStep.getStepData().minutes, - daysStep.getStepData()); - goBack(alarm); - } + activity.runOnUiThread(() -> { + Alarm alarm = new Alarm( + nameStep.getStepData(), + descriptionStep.getStepData(), + timeStep.getStepData().hour, + timeStep.getStepData().minutes, + daysStep.getStepData()); + goBack(alarm); }); } } diff --git a/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/form/steps/AlarmDaysStep.java b/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/form/steps/AlarmDaysStep.java index bf4841c..c388d29 100644 --- a/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/form/steps/AlarmDaysStep.java +++ b/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/form/steps/AlarmDaysStep.java @@ -117,13 +117,10 @@ private void setupAlarmDays() { updateDayLayout(index, dayLayout, false); if (dayLayout != null) { - dayLayout.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - alarmDays[index] = !alarmDays[index]; - updateDayLayout(index, dayLayout, true); - markAsCompletedOrUncompleted(true); - } + dayLayout.setOnClickListener(v -> { + alarmDays[index] = !alarmDays[index]; + updateDayLayout(index, dayLayout, true); + markAsCompletedOrUncompleted(true); }); final TextView dayText = dayLayout.findViewById(R.id.day); diff --git a/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/form/steps/AlarmDescriptionStep.java b/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/form/steps/AlarmDescriptionStep.java index d381813..3dfeca4 100644 --- a/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/form/steps/AlarmDescriptionStep.java +++ b/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/form/steps/AlarmDescriptionStep.java @@ -31,12 +31,9 @@ protected View createStepContentLayout() { alarmDescriptionEditText = new TextInputEditText(getContext()); alarmDescriptionEditText.setHint(R.string.form_hint_description); alarmDescriptionEditText.setSingleLine(true); - alarmDescriptionEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { - @Override - public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { - getFormView().goToNextStep(true); - return false; - } + alarmDescriptionEditText.setOnEditorActionListener((v, actionId, event) -> { + getFormView().goToNextStep(true); + return false; }); return alarmDescriptionEditText; diff --git a/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/form/steps/AlarmNameStep.java b/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/form/steps/AlarmNameStep.java index 4365f2f..32ce5b2 100644 --- a/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/form/steps/AlarmNameStep.java +++ b/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/form/steps/AlarmNameStep.java @@ -47,12 +47,9 @@ public void onTextChanged(CharSequence s, int start, int before, int count) { @Override public void afterTextChanged(Editable s) {} }); - alarmNameEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { - @Override - public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { - getFormView().goToNextStep(true); - return false; - } + alarmNameEditText.setOnEditorActionListener((v, actionId, event) -> { + getFormView().goToNextStep(true); + return false; }); unformattedErrorString = getContext().getResources().getString(R.string.error_alarm_name_min_characters); diff --git a/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/form/steps/AlarmTimeStep.java b/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/form/steps/AlarmTimeStep.java index a77fe72..8558800 100644 --- a/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/form/steps/AlarmTimeStep.java +++ b/app/src/main/java/verticalstepperform/ernestoyaquello/com/verticalstepperform/form/steps/AlarmTimeStep.java @@ -48,26 +48,18 @@ protected View createStepContentLayout() { private void setupAlarmTime() { if (alarmTimePicker == null) { alarmTimePicker = new TimePickerDialog(getContext(), - new TimePickerDialog.OnTimeSetListener() { - @Override - public void onTimeSet(TimePicker view, int hourOfDay, int minute) { - alarmTimeHour = hourOfDay; - alarmTimeMinutes = minute; - - updatedAlarmTimeText(); - } + (view, hourOfDay, minute) -> { + alarmTimeHour = hourOfDay; + alarmTimeMinutes = minute; + + updatedAlarmTimeText(); }, alarmTimeHour, alarmTimeMinutes, true); } else { alarmTimePicker.updateTime(alarmTimeHour, alarmTimeMinutes); } if (alarmTimeTextView != null) { - alarmTimeTextView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - alarmTimePicker.show(); - } - }); + alarmTimeTextView.setOnClickListener(v -> alarmTimePicker.show()); } } diff --git a/vertical-stepper-form/src/main/java/ernestoyaquello/com/verticalstepperform/Builder.java b/vertical-stepper-form/src/main/java/ernestoyaquello/com/verticalstepperform/Builder.java index 7c3f7e7..086a871 100644 --- a/vertical-stepper-form/src/main/java/ernestoyaquello/com/verticalstepperform/Builder.java +++ b/vertical-stepper-form/src/main/java/ernestoyaquello/com/verticalstepperform/Builder.java @@ -13,7 +13,7 @@ public class Builder { private StepperFormListener listener; private StepHelper[] steps; - Builder(VerticalStepperFormView formView, StepperFormListener listener, Step[] steps) { + Builder(VerticalStepperFormView formView, StepperFormListener listener, Step[] steps) { this.formView = formView; this.listener = listener; this.steps = new StepHelper[steps.length]; diff --git a/vertical-stepper-form/src/main/java/ernestoyaquello/com/verticalstepperform/StepHelper.java b/vertical-stepper-form/src/main/java/ernestoyaquello/com/verticalstepperform/StepHelper.java index 6146a55..4354106 100644 --- a/vertical-stepper-form/src/main/java/ernestoyaquello/com/verticalstepperform/StepHelper.java +++ b/vertical-stepper-form/src/main/java/ernestoyaquello/com/verticalstepperform/StepHelper.java @@ -29,7 +29,7 @@ */ class StepHelper implements Step.InternalFormStepListener { - private Step step; + private Step step; private FormStyle formStyle; private View stepNumberCircleView; @@ -49,11 +49,11 @@ class StepHelper implements Step.InternalFormStepListener { private View titleAndSubtitleContainerView; private View errorContentAndButtonContainerView; - StepHelper(Step.InternalFormStepListener formListener, @NonNull Step step) { + StepHelper(Step.InternalFormStepListener formListener, @NonNull Step step) { this(formListener, step, false); } - StepHelper(Step.InternalFormStepListener formListener, Step step, boolean isConfirmationStep) { + StepHelper(Step.InternalFormStepListener formListener, Step step, boolean isConfirmationStep) { this.step = !isConfirmationStep ? step : new ConfirmationStep(); this.step.addListenerInternal(this); this.step.addListenerInternal(formListener); @@ -160,26 +160,13 @@ private void setupStepViews(final VerticalStepperFormView form, View stepLayout) subtitleView.setTextSize(TypedValue.COMPLEX_UNIT_PX, formStyle.stepSubtitleTextSizeInPx); errorMessageView.setTextSize(TypedValue.COMPLEX_UNIT_PX, formStyle.stepErrorMessageTextSizeInPx); - headerView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - if (formStyle.allowStepOpeningOnHeaderClick) { - form.goToStep(form.getStepPosition(step), true); - } - } - }); - nextButtonView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - form.goToStep(form.getStepPosition(step) + 1, true); - } - }); - cancelButtonView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - form.cancelForm(); + headerView.setOnClickListener(view -> { + if (formStyle.allowStepOpeningOnHeaderClick) { + form.goToStep(form.getStepPosition(step), true); } }); + nextButtonView.setOnClickListener(view -> form.goToStep(form.getStepPosition(step) + 1, true)); + cancelButtonView.setOnClickListener(view -> form.cancelForm()); int position = form.getStepPosition(step); boolean isLast = (position + 1) == form.getTotalNumberOfSteps(); @@ -246,7 +233,7 @@ void updateStepViewsAfterPositionChange(VerticalStepperFormView form) { onUpdatedStepVisibility(position, false); } - public Step getStepInstance() { + public Step getStepInstance() { return step; } diff --git a/vertical-stepper-form/src/main/java/ernestoyaquello/com/verticalstepperform/UIHelper.java b/vertical-stepper-form/src/main/java/ernestoyaquello/com/verticalstepperform/UIHelper.java index eeba6d6..88ff2ff 100644 --- a/vertical-stepper-form/src/main/java/ernestoyaquello/com/verticalstepperform/UIHelper.java +++ b/vertical-stepper-form/src/main/java/ernestoyaquello/com/verticalstepperform/UIHelper.java @@ -105,15 +105,12 @@ private static void performSlideAnimation(final View view, final boolean slideUp final ObjectAnimator animator = ObjectAnimator.ofFloat(view, View.ALPHA, correctedInitialValue, finalValue); animator.setDuration(durationMillis); - animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { - @Override - public void onAnimationUpdate(ValueAnimator valueAnimator) { - float scaleYCurrentValue = (float) valueAnimator.getAnimatedValue(); - int newHeight = (int) (expandedHeight * scaleYCurrentValue); + animator.addUpdateListener(valueAnimator -> { + float scaleYCurrentValue = (float) valueAnimator.getAnimatedValue(); + int newHeight = (int) (expandedHeight * scaleYCurrentValue); - setViewHeight(view, newHeight); - view.requestLayout(); - } + setViewHeight(view, newHeight); + view.requestLayout(); }); animator.addListener(new AnimatorListenerAdapter() { @Override diff --git a/vertical-stepper-form/src/main/java/ernestoyaquello/com/verticalstepperform/VerticalStepperFormView.java b/vertical-stepper-form/src/main/java/ernestoyaquello/com/verticalstepperform/VerticalStepperFormView.java index 06db3c8..8fb193c 100644 --- a/vertical-stepper-form/src/main/java/ernestoyaquello/com/verticalstepperform/VerticalStepperFormView.java +++ b/vertical-stepper-form/src/main/java/ernestoyaquello/com/verticalstepperform/VerticalStepperFormView.java @@ -75,7 +75,7 @@ public VerticalStepperFormView(Context context, AttributeSet attrs, int defStyle * @param steps An array with the steps that will be displayed in the form. * @return An instance of the stepper form builder. Use it to configure and initialize the form. */ - public Builder setup(StepperFormListener stepperFormListener, Step... steps) { + public Builder setup(StepperFormListener stepperFormListener, Step... steps) { return new Builder(this, stepperFormListener, steps); } @@ -86,8 +86,8 @@ public Builder setup(StepperFormListener stepperFormListener, Step... steps) { * @param steps A list with the steps that will be displayed in the form. * @return An instance of the stepper form builder. Use it to configure and initialize the form. */ - public Builder setup(StepperFormListener stepperFormListener, List steps) { - Step[] stepsArray = steps.toArray(new Step[0]); + public Builder setup(StepperFormListener stepperFormListener, List> steps) { + Step[] stepsArray = steps.toArray(new Step[0]); return new Builder(this, stepperFormListener, stepsArray); } @@ -319,7 +319,7 @@ public synchronized int getOpenStepPosition() { * * @return The currently open step, or null if not found. */ - public synchronized Step getOpenStep() { + public synchronized Step getOpenStep() { StepHelper openStepHelper = getOpenStepHelper(); return openStepHelper != null ? openStepHelper.getStepInstance() : null; } @@ -361,19 +361,17 @@ public void hideBottomNavigation() { */ public void scrollToStepIfNecessary(final int stepPosition, final boolean smoothScroll) { if (stepPosition >= 0 && stepPosition < stepHelpers.size()) { - stepsScrollView.post(new Runnable() { - public void run() { - Step stepInstance = stepHelpers.get(stepPosition).getStepInstance(); - View stepEntireLayout = stepInstance.getEntireStepLayout(); - View stepContentLayout = stepInstance.getContentLayout(); - Rect scrollBounds = new Rect(); - stepsScrollView.getDrawingRect(scrollBounds); - if (stepContentLayout == null || scrollBounds.top > stepContentLayout.getY()) { - if (smoothScroll) { - stepsScrollView.smoothScrollTo(0, stepEntireLayout.getTop()); - } else { - stepsScrollView.scrollTo(0, stepEntireLayout.getTop()); - } + stepsScrollView.post(() -> { + Step stepInstance = stepHelpers.get(stepPosition).getStepInstance(); + View stepEntireLayout = stepInstance.getEntireStepLayout(); + View stepContentLayout = stepInstance.getContentLayout(); + Rect scrollBounds = new Rect(); + stepsScrollView.getDrawingRect(scrollBounds); + if (stepContentLayout == null || scrollBounds.top > stepContentLayout.getY()) { + if (smoothScroll) { + stepsScrollView.smoothScrollTo(0, stepEntireLayout.getTop()); + } else { + stepsScrollView.scrollTo(0, stepEntireLayout.getTop()); } } }); @@ -427,7 +425,7 @@ public synchronized void cancelFormCompletionOrCancellationAttempt() { StepHelper stepHelper = stepHelpers.get(openedStepPosition); if (style.closeLastStepOnCompletion) { - Step step = stepHelper.getStepInstance(); + Step step = stepHelper.getStepInstance(); if (!step.isOpen()) { step.openInternal(true); } @@ -470,7 +468,7 @@ public int refreshFormProgress() { * @param stepToAdd The step to add. * @return True if the step was added successfully; false otherwise. */ - public boolean addStep(int index, Step stepToAdd) { + public boolean addStep(int index, Step stepToAdd) { StepHelper lastStep = stepHelpers.get(stepHelpers.size() - 1); int lastAllowedIndex = lastStep.isConfirmationStep() ? stepHelpers.size() - 1 : stepHelpers.size(); if (!initialized || formCompleted || index < 0 || index > lastAllowedIndex) { @@ -556,7 +554,7 @@ public int getTotalNumberOfSteps() { * @param step The step to find the position of. * @return The position of the step, or -1 if the step is not found. */ - public int getStepPosition(Step step) { + public int getStepPosition(Step step) { for (int i = 0; i < stepHelpers.size(); i++) { if (stepHelpers.get(i).getStepInstance() == step) return i; @@ -811,7 +809,7 @@ private void onConstructed(Context context, AttributeSet attrs, int defStyleAttr void initializeForm(StepperFormListener listener, StepHelper[] stepsArray) { this.listener = listener; this.originalStepHelpers = Arrays.asList(stepsArray); - this.stepHelpers = new ArrayList(originalStepHelpers); + this.stepHelpers = new ArrayList<>(originalStepHelpers); progressBar.setMax(stepHelpers.size()); @@ -942,7 +940,7 @@ private synchronized void attemptToCompleteForm(boolean isCancellation) { boolean markedConfirmationStepAsCompleted = false; String confirmationStepErrorMessage = ""; StepHelper lastStepHelper = stepHelpers.get(stepHelpers.size() - 1); - Step lastStep = lastStepHelper.getStepInstance(); + Step lastStep = lastStepHelper.getStepInstance(); if (!isCancellation) { if (!lastStep.isCompleted() && lastStepHelper.isConfirmationStep()) { confirmationStepErrorMessage = lastStep.getErrorMessage(); @@ -994,18 +992,8 @@ private void findViews() { } private void registerListeners() { - previousStepButton.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View view) { - goToPreviousStep(true); - } - }); - nextStepButton.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View view) { - goToNextStep(true); - } - }); + previousStepButton.setOnClickListener(view -> goToPreviousStep(true)); + nextStepButton.setOnClickListener(view -> goToNextStep(true)); addObserverForKeyboard(); } @@ -1047,7 +1035,7 @@ public Parcelable onSaveInstanceState() { String[] errorMessages = new String[originalStepHelpers.size()]; for (int i = 0; i < completedSteps.length; i++) { StepHelper stepHelper = originalStepHelpers.get(i); - Step step = stepHelper.getStepInstance(); + Step step = stepHelper.getStepInstance(); completedSteps[i] = step.isCompleted(); errorSteps[i] = step.hasError(); @@ -1116,7 +1104,7 @@ private void restoreFromState( for (int i = 0; i < completedSteps.length; i++) { StepHelper stepHelper = stepHelpers.get(i); - Step step = stepHelper.getStepInstance(); + Step step = stepHelper.getStepInstance(); step.restoreErrorState(errorSteps[i]); step.updateTitle(titles[i], false);