Skip to content

Commit

Permalink
Very minor code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestoyaquello committed Jun 8, 2021
1 parent c64b4d9 commit 5c0b8f7
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -54,25 +51,22 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
MutableLiveData<String> liveData = navController.getCurrentBackStackEntry()
.getSavedStateHandle()
.getLiveData(NewAlarmFormFragment.ALARM_DATA_SERIALIZED_KEY);
liveData.observe(getViewLifecycleOwner(), new Observer<String>() {
@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);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
}
Expand All @@ -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();
Expand All @@ -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);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/
class StepHelper implements Step.InternalFormStepListener {

private Step step;
private Step<?> step;
private FormStyle formStyle;

private View stepNumberCircleView;
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -246,7 +233,7 @@ void updateStepViewsAfterPositionChange(VerticalStepperFormView form) {
onUpdatedStepVisibility(position, false);
}

public Step getStepInstance() {
public Step<?> getStepInstance() {
return step;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 5c0b8f7

Please sign in to comment.