Skip to content

Commit 4cdf6ed

Browse files
committed
Use viewbinding
1 parent 652d501 commit 4cdf6ed

1 file changed

Lines changed: 42 additions & 82 deletions

File tree

app/src/main/java/org/schabi/newpipe/player/helper/PlaybackParameterDialog.java

Lines changed: 42 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@
77
import android.content.Context;
88
import android.os.Bundle;
99
import android.util.Log;
10-
import android.view.View;
11-
import android.widget.CheckBox;
10+
import android.view.LayoutInflater;
1211
import android.widget.SeekBar;
1312
import android.widget.TextView;
1413

15-
import androidx.annotation.IdRes;
1614
import androidx.annotation.NonNull;
1715
import androidx.annotation.Nullable;
1816
import androidx.appcompat.app.AlertDialog;
1917
import androidx.fragment.app.DialogFragment;
2018
import androidx.preference.PreferenceManager;
2119

2220
import org.schabi.newpipe.R;
21+
import org.schabi.newpipe.databinding.DialogPlaybackParameterBinding;
2322
import org.schabi.newpipe.util.SliderStrategy;
2423

2524
import java.util.Objects;
@@ -72,18 +71,7 @@ public class PlaybackParameterDialog extends DialogFragment {
7271
@State
7372
boolean skipSilence = DEFAULT_SKIP_SILENCE;
7473

75-
private SeekBar tempoSlider;
76-
private TextView tempoCurrentText;
77-
private TextView tempoStepDownText;
78-
private TextView tempoStepUpText;
79-
80-
private SeekBar pitchSlider;
81-
private TextView pitchCurrentText;
82-
private TextView pitchStepDownText;
83-
private TextView pitchStepUpText;
84-
85-
private CheckBox unhookingCheckbox;
86-
private CheckBox skipSilenceCheckbox;
74+
private DialogPlaybackParameterBinding binding;
8775

8876
public static PlaybackParameterDialog newInstance(
8977
final double playbackTempo,
@@ -110,7 +98,7 @@ public static PlaybackParameterDialog newInstance(
11098
//////////////////////////////////////////////////////////////////////////*/
11199

112100
@Override
113-
public void onAttach(final Context context) {
101+
public void onAttach(@NonNull final Context context) {
114102
super.onAttach(context);
115103
if (context instanceof Callback) {
116104
callback = (Callback) context;
@@ -120,7 +108,7 @@ public void onAttach(final Context context) {
120108
}
121109

122110
@Override
123-
public void onSaveInstanceState(final Bundle outState) {
111+
public void onSaveInstanceState(@NonNull final Bundle outState) {
124112
super.onSaveInstanceState(outState);
125113
Icepick.saveInstanceState(this, outState);
126114
}
@@ -135,12 +123,12 @@ public Dialog onCreateDialog(@Nullable final Bundle savedInstanceState) {
135123
assureCorrectAppLanguage(getContext());
136124
Icepick.restoreInstanceState(this, savedInstanceState);
137125

138-
final View view = View.inflate(getContext(), R.layout.dialog_playback_parameter, null);
139-
initUI(view);
126+
binding = DialogPlaybackParameterBinding.inflate(LayoutInflater.from(getContext()));
127+
initUI();
140128
initUIData();
141129

142130
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(requireActivity())
143-
.setView(view)
131+
.setView(binding.getRoot())
144132
.setCancelable(true)
145133
.setNegativeButton(R.string.cancel, (dialogInterface, i) -> {
146134
setAndUpdateTempo(initialTempo);
@@ -163,37 +151,21 @@ public Dialog onCreateDialog(@Nullable final Bundle savedInstanceState) {
163151
// Control Views
164152
//////////////////////////////////////////////////////////////////////////*/
165153

166-
private void initUI(@NonNull final View rootView) {
154+
private void initUI() {
167155
// Tempo
168-
tempoSlider = Objects.requireNonNull(rootView.findViewById(R.id.tempoSeekbar));
169-
tempoCurrentText = Objects.requireNonNull(rootView.findViewById(R.id.tempoCurrentText));
170-
tempoStepUpText = Objects.requireNonNull(rootView.findViewById(R.id.tempoStepUp));
171-
tempoStepDownText = Objects.requireNonNull(rootView.findViewById(R.id.tempoStepDown));
172-
173-
setText(rootView, R.id.tempoMinimumText, PlayerHelper::formatSpeed, MINIMUM_PLAYBACK_VALUE);
174-
setText(rootView, R.id.tempoMaximumText, PlayerHelper::formatSpeed, MAXIMUM_PLAYBACK_VALUE);
156+
setText(binding.tempoMinimumText, PlayerHelper::formatSpeed, MINIMUM_PLAYBACK_VALUE);
157+
setText(binding.tempoMaximumText, PlayerHelper::formatSpeed, MAXIMUM_PLAYBACK_VALUE);
175158

176159
// Pitch
177-
pitchSlider = Objects.requireNonNull(rootView.findViewById(R.id.pitchSeekbar));
178-
pitchCurrentText = Objects.requireNonNull(rootView.findViewById(R.id.pitchCurrentText));
179-
pitchStepUpText = Objects.requireNonNull(rootView.findViewById(R.id.pitchStepUp));
180-
pitchStepDownText = Objects.requireNonNull(rootView.findViewById(R.id.pitchStepDown));
181-
182-
setText(rootView, R.id.pitchMinimumText, PlayerHelper::formatPitch, MINIMUM_PLAYBACK_VALUE);
183-
setText(rootView, R.id.pitchMaximumText, PlayerHelper::formatPitch, MAXIMUM_PLAYBACK_VALUE);
160+
setText(binding.pitchMinimumText, PlayerHelper::formatPitch, MINIMUM_PLAYBACK_VALUE);
161+
setText(binding.pitchMaximumText, PlayerHelper::formatPitch, MAXIMUM_PLAYBACK_VALUE);
184162

185163
// Steps
186-
setupStepTextView(rootView, R.id.stepSizeOnePercent, STEP_1_PERCENT_VALUE);
187-
setupStepTextView(rootView, R.id.stepSizeFivePercent, STEP_5_PERCENT_VALUE);
188-
setupStepTextView(rootView, R.id.stepSizeTenPercent, STEP_10_PERCENT_VALUE);
189-
setupStepTextView(rootView, R.id.stepSizeTwentyFivePercent, STEP_25_PERCENT_VALUE);
190-
setupStepTextView(rootView, R.id.stepSizeOneHundredPercent, STEP_100_PERCENT_VALUE);
191-
192-
// Bottom controls
193-
unhookingCheckbox =
194-
Objects.requireNonNull(rootView.findViewById(R.id.unhookCheckbox));
195-
skipSilenceCheckbox =
196-
Objects.requireNonNull(rootView.findViewById(R.id.skipSilenceCheckbox));
164+
setupStepTextView(binding.stepSizeOnePercent, STEP_1_PERCENT_VALUE);
165+
setupStepTextView(binding.stepSizeFivePercent, STEP_5_PERCENT_VALUE);
166+
setupStepTextView(binding.stepSizeTenPercent, STEP_10_PERCENT_VALUE);
167+
setupStepTextView(binding.stepSizeTwentyFivePercent, STEP_25_PERCENT_VALUE);
168+
setupStepTextView(binding.stepSizeOneHundredPercent, STEP_100_PERCENT_VALUE);
197169
}
198170

199171
private TextView setText(
@@ -205,59 +177,47 @@ private TextView setText(
205177
return textView;
206178
}
207179

208-
private TextView setText(
209-
final View rootView,
210-
@IdRes final int idRes,
211-
final DoubleFunction<String> formatter,
212-
final double value
213-
) {
214-
final TextView textView = rootView.findViewById(idRes);
215-
setText(textView, formatter, value);
216-
return textView;
217-
}
218-
219180
private void setupStepTextView(
220-
final View rootView,
221-
@IdRes final int idRes,
181+
final TextView textView,
222182
final double stepSizeValue
223183
) {
224-
setText(rootView, idRes, PlaybackParameterDialog::getPercentString, stepSizeValue)
184+
setText(textView, PlaybackParameterDialog::getPercentString, stepSizeValue)
225185
.setOnClickListener(view -> setAndUpdateStepSize(stepSizeValue));
226186
}
227187

228188
private void initUIData() {
229189
// Tempo
230-
tempoSlider.setMax(QUADRATIC_STRATEGY.progressOf(MAXIMUM_PLAYBACK_VALUE));
190+
binding.tempoSeekbar.setMax(QUADRATIC_STRATEGY.progressOf(MAXIMUM_PLAYBACK_VALUE));
231191
setAndUpdateTempo(tempo);
232-
tempoSlider.setOnSeekBarChangeListener(
192+
binding.tempoSeekbar.setOnSeekBarChangeListener(
233193
getTempoOrPitchSeekbarChangeListener(this::onTempoSliderUpdated));
234194

235195
registerOnStepClickListener(
236-
tempoStepDownText, tempo, -1, this::onTempoSliderUpdated);
196+
binding.tempoStepDown, tempo, -1, this::onTempoSliderUpdated);
237197
registerOnStepClickListener(
238-
tempoStepUpText, tempo, 1, this::onTempoSliderUpdated);
198+
binding.tempoStepUp, tempo, 1, this::onTempoSliderUpdated);
239199

240200
// Pitch
241-
pitchSlider.setMax(QUADRATIC_STRATEGY.progressOf(MAXIMUM_PLAYBACK_VALUE));
201+
binding.pitchSeekbar.setMax(QUADRATIC_STRATEGY.progressOf(MAXIMUM_PLAYBACK_VALUE));
242202
setAndUpdatePitch(pitch);
243-
pitchSlider.setOnSeekBarChangeListener(
203+
binding.pitchSeekbar.setOnSeekBarChangeListener(
244204
getTempoOrPitchSeekbarChangeListener(this::onPitchSliderUpdated));
245205

246206
registerOnStepClickListener(
247-
pitchStepDownText, pitch, -1, this::onPitchSliderUpdated);
207+
binding.pitchStepDown, pitch, -1, this::onPitchSliderUpdated);
248208
registerOnStepClickListener(
249-
pitchStepUpText, pitch, 1, this::onPitchSliderUpdated);
209+
binding.pitchStepUp, pitch, 1, this::onPitchSliderUpdated);
250210

251211
// Steps
252212
setAndUpdateStepSize(stepSize);
253213

254214
// Bottom controls
255215
// restore whether pitch and tempo are unhooked or not
256-
unhookingCheckbox.setChecked(PreferenceManager
216+
binding.unhookCheckbox.setChecked(PreferenceManager
257217
.getDefaultSharedPreferences(requireContext())
258218
.getBoolean(getString(R.string.playback_unhook_key), true));
259219

260-
unhookingCheckbox.setOnCheckedChangeListener((compoundButton, isChecked) -> {
220+
binding.unhookCheckbox.setOnCheckedChangeListener((compoundButton, isChecked) -> {
261221
// save whether pitch and tempo are unhooked or not
262222
PreferenceManager.getDefaultSharedPreferences(requireContext())
263223
.edit()
@@ -271,7 +231,7 @@ private void initUIData() {
271231
});
272232

273233
setAndUpdateSkipSilence(skipSilence);
274-
skipSilenceCheckbox.setOnCheckedChangeListener((compoundButton, isChecked) -> {
234+
binding.skipSilenceCheckbox.setOnCheckedChangeListener((compoundButton, isChecked) -> {
275235
skipSilence = isChecked;
276236
updateCallback();
277237
});
@@ -291,16 +251,16 @@ private void registerOnStepClickListener(
291251
private void setAndUpdateStepSize(final double newStepSize) {
292252
this.stepSize = newStepSize;
293253

294-
tempoStepUpText.setText(getStepUpPercentString(newStepSize));
295-
tempoStepDownText.setText(getStepDownPercentString(newStepSize));
254+
binding.tempoStepUp.setText(getStepUpPercentString(newStepSize));
255+
binding.tempoStepDown.setText(getStepDownPercentString(newStepSize));
296256

297-
pitchStepUpText.setText(getStepUpPercentString(newStepSize));
298-
pitchStepDownText.setText(getStepDownPercentString(newStepSize));
257+
binding.pitchStepUp.setText(getStepUpPercentString(newStepSize));
258+
binding.pitchStepDown.setText(getStepDownPercentString(newStepSize));
299259
}
300260

301261
private void setAndUpdateSkipSilence(final boolean newSkipSilence) {
302262
this.skipSilence = newSkipSilence;
303-
skipSilenceCheckbox.setChecked(newSkipSilence);
263+
binding.skipSilenceCheckbox.setChecked(newSkipSilence);
304264
}
305265

306266
/*//////////////////////////////////////////////////////////////////////////
@@ -333,15 +293,15 @@ public void onStopTrackingTouch(final SeekBar seekBar) {
333293
}
334294

335295
private void onTempoSliderUpdated(final double newTempo) {
336-
if (!unhookingCheckbox.isChecked()) {
296+
if (!binding.unhookCheckbox.isChecked()) {
337297
setSliders(newTempo);
338298
} else {
339299
setAndUpdateTempo(newTempo);
340300
}
341301
}
342302

343303
private void onPitchSliderUpdated(final double newPitch) {
344-
if (!unhookingCheckbox.isChecked()) {
304+
if (!binding.unhookCheckbox.isChecked()) {
345305
setSliders(newPitch);
346306
} else {
347307
setAndUpdatePitch(newPitch);
@@ -355,14 +315,14 @@ private void setSliders(final double newValue) {
355315

356316
private void setAndUpdateTempo(final double newTempo) {
357317
this.tempo = newTempo;
358-
tempoSlider.setProgress(QUADRATIC_STRATEGY.progressOf(tempo));
359-
setText(tempoCurrentText, PlayerHelper::formatSpeed, tempo);
318+
binding.tempoSeekbar.setProgress(QUADRATIC_STRATEGY.progressOf(tempo));
319+
setText(binding.tempoCurrentText, PlayerHelper::formatSpeed, tempo);
360320
}
361321

362322
private void setAndUpdatePitch(final double newPitch) {
363323
this.pitch = newPitch;
364-
pitchSlider.setProgress(QUADRATIC_STRATEGY.progressOf(pitch));
365-
setText(pitchCurrentText, PlayerHelper::formatPitch, pitch);
324+
binding.pitchSeekbar.setProgress(QUADRATIC_STRATEGY.progressOf(pitch));
325+
setText(binding.pitchCurrentText, PlayerHelper::formatPitch, pitch);
366326
}
367327

368328
/*//////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)