Skip to content

Commit 4e33f2d

Browse files
committed
Improve method order in DownloadDialog and add separator comments
1 parent dce874b commit 4e33f2d

1 file changed

Lines changed: 96 additions & 77 deletions

File tree

app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java

Lines changed: 96 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ public class DownloadDialog extends DialogFragment
132132
registerForActivityResult(
133133
new StartActivityForResult(), this::requestDownloadPickVideoFolderResult);
134134

135+
136+
/*//////////////////////////////////////////////////////////////////////////
137+
// Instance creation
138+
//////////////////////////////////////////////////////////////////////////*/
139+
135140
public static DownloadDialog newInstance(final StreamInfo info) {
136141
final DownloadDialog dialog = new DownloadDialog();
137142
dialog.setInfo(info);
@@ -153,6 +158,11 @@ public static DownloadDialog newInstance(final Context context, final StreamInfo
153158
return instance;
154159
}
155160

161+
162+
/*//////////////////////////////////////////////////////////////////////////
163+
// Setters
164+
//////////////////////////////////////////////////////////////////////////*/
165+
156166
private void setInfo(final StreamInfo info) {
157167
this.currentInfo = info;
158168
}
@@ -194,6 +204,11 @@ public void setSelectedSubtitleStream(final int ssi) {
194204
this.selectedSubtitleIndex = ssi;
195205
}
196206

207+
208+
/*//////////////////////////////////////////////////////////////////////////
209+
// Android lifecycle
210+
//////////////////////////////////////////////////////////////////////////*/
211+
197212
@Override
198213
public void onCreate(@Nullable final Bundle savedInstanceState) {
199214
super.onCreate(savedInstanceState);
@@ -263,10 +278,6 @@ public void onServiceDisconnected(final ComponentName name) {
263278
}, Context.BIND_AUTO_CREATE);
264279
}
265280

266-
/*//////////////////////////////////////////////////////////////////////////
267-
// Inits
268-
//////////////////////////////////////////////////////////////////////////*/
269-
270281
@Override
271282
public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup container,
272283
final Bundle savedInstanceState) {
@@ -322,6 +333,55 @@ public void onStopTrackingTouch(final SeekBar p1) { }
322333
fetchStreamsSize();
323334
}
324335

336+
private void initToolbar(final Toolbar toolbar) {
337+
if (DEBUG) {
338+
Log.d(TAG, "initToolbar() called with: toolbar = [" + toolbar + "]");
339+
}
340+
341+
toolbar.setTitle(R.string.download_dialog_title);
342+
toolbar.setNavigationIcon(R.drawable.ic_arrow_back);
343+
toolbar.inflateMenu(R.menu.dialog_url);
344+
toolbar.setNavigationOnClickListener(v -> requireDialog().dismiss());
345+
toolbar.setNavigationContentDescription(R.string.cancel);
346+
347+
okButton = toolbar.findViewById(R.id.okay);
348+
okButton.setEnabled(false); // disable until the download service connection is done
349+
350+
toolbar.setOnMenuItemClickListener(item -> {
351+
if (item.getItemId() == R.id.okay) {
352+
prepareSelectedDownload();
353+
if (getActivity() instanceof RouterActivity) {
354+
getActivity().finish();
355+
}
356+
return true;
357+
}
358+
return false;
359+
});
360+
}
361+
362+
@Override
363+
public void onDestroy() {
364+
super.onDestroy();
365+
disposables.clear();
366+
}
367+
368+
@Override
369+
public void onDestroyView() {
370+
dialogBinding = null;
371+
super.onDestroyView();
372+
}
373+
374+
@Override
375+
public void onSaveInstanceState(@NonNull final Bundle outState) {
376+
super.onSaveInstanceState(outState);
377+
Icepick.saveInstanceState(this, outState);
378+
}
379+
380+
381+
/*//////////////////////////////////////////////////////////////////////////
382+
// Video, audio and subtitle spinners
383+
//////////////////////////////////////////////////////////////////////////*/
384+
325385
private void fetchStreamsSize() {
326386
disposables.clear();
327387
disposables.add(StreamSizeWrapper.fetchSizeForWrapper(wrappedVideoStreams)
@@ -356,30 +416,39 @@ private void fetchStreamsSize() {
356416
currentInfo.getServiceId()))));
357417
}
358418

359-
@Override
360-
public void onDestroy() {
361-
super.onDestroy();
362-
disposables.clear();
419+
private void setupAudioSpinner() {
420+
if (getContext() == null) {
421+
return;
422+
}
423+
424+
dialogBinding.qualitySpinner.setAdapter(audioStreamsAdapter);
425+
dialogBinding.qualitySpinner.setSelection(selectedAudioIndex);
426+
setRadioButtonsState(true);
363427
}
364428

365-
@Override
366-
public void onDestroyView() {
367-
dialogBinding = null;
368-
super.onDestroyView();
429+
private void setupVideoSpinner() {
430+
if (getContext() == null) {
431+
return;
432+
}
433+
434+
dialogBinding.qualitySpinner.setAdapter(videoStreamsAdapter);
435+
dialogBinding.qualitySpinner.setSelection(selectedVideoIndex);
436+
setRadioButtonsState(true);
369437
}
370438

371-
/*//////////////////////////////////////////////////////////////////////////
372-
// Radio group Video&Audio options - Listener
373-
//////////////////////////////////////////////////////////////////////////*/
439+
private void setupSubtitleSpinner() {
440+
if (getContext() == null) {
441+
return;
442+
}
374443

375-
@Override
376-
public void onSaveInstanceState(@NonNull final Bundle outState) {
377-
super.onSaveInstanceState(outState);
378-
Icepick.saveInstanceState(this, outState);
444+
dialogBinding.qualitySpinner.setAdapter(subtitleStreamsAdapter);
445+
dialogBinding.qualitySpinner.setSelection(selectedSubtitleIndex);
446+
setRadioButtonsState(true);
379447
}
380448

449+
381450
/*//////////////////////////////////////////////////////////////////////////
382-
// Streams Spinner Listener
451+
// Activity results
383452
//////////////////////////////////////////////////////////////////////////*/
384453

385454
private void requestDownloadPickAudioFolderResult(final ActivityResult result) {
@@ -454,66 +523,11 @@ private void requestDownloadPickFolderResult(final ActivityResult result,
454523
}
455524
}
456525

457-
private void initToolbar(final Toolbar toolbar) {
458-
if (DEBUG) {
459-
Log.d(TAG, "initToolbar() called with: toolbar = [" + toolbar + "]");
460-
}
461-
462-
toolbar.setTitle(R.string.download_dialog_title);
463-
toolbar.setNavigationIcon(R.drawable.ic_arrow_back);
464-
toolbar.inflateMenu(R.menu.dialog_url);
465-
toolbar.setNavigationOnClickListener(v -> requireDialog().dismiss());
466-
toolbar.setNavigationContentDescription(R.string.cancel);
467-
468-
okButton = toolbar.findViewById(R.id.okay);
469-
okButton.setEnabled(false); // disable until the download service connection is done
470-
471-
toolbar.setOnMenuItemClickListener(item -> {
472-
if (item.getItemId() == R.id.okay) {
473-
prepareSelectedDownload();
474-
if (getActivity() instanceof RouterActivity) {
475-
getActivity().finish();
476-
}
477-
return true;
478-
}
479-
return false;
480-
});
481-
}
482526

483527
/*//////////////////////////////////////////////////////////////////////////
484-
// Utils
528+
// Listeners
485529
//////////////////////////////////////////////////////////////////////////*/
486530

487-
private void setupAudioSpinner() {
488-
if (getContext() == null) {
489-
return;
490-
}
491-
492-
dialogBinding.qualitySpinner.setAdapter(audioStreamsAdapter);
493-
dialogBinding.qualitySpinner.setSelection(selectedAudioIndex);
494-
setRadioButtonsState(true);
495-
}
496-
497-
private void setupVideoSpinner() {
498-
if (getContext() == null) {
499-
return;
500-
}
501-
502-
dialogBinding.qualitySpinner.setAdapter(videoStreamsAdapter);
503-
dialogBinding.qualitySpinner.setSelection(selectedVideoIndex);
504-
setRadioButtonsState(true);
505-
}
506-
507-
private void setupSubtitleSpinner() {
508-
if (getContext() == null) {
509-
return;
510-
}
511-
512-
dialogBinding.qualitySpinner.setAdapter(subtitleStreamsAdapter);
513-
dialogBinding.qualitySpinner.setSelection(selectedSubtitleIndex);
514-
setRadioButtonsState(true);
515-
}
516-
517531
@Override
518532
public void onCheckedChanged(final RadioGroup group, @IdRes final int checkedId) {
519533
if (DEBUG) {
@@ -563,6 +577,11 @@ public void onItemSelected(final AdapterView<?> parent, final View view,
563577
public void onNothingSelected(final AdapterView<?> parent) {
564578
}
565579

580+
581+
/*//////////////////////////////////////////////////////////////////////////
582+
// Download
583+
//////////////////////////////////////////////////////////////////////////*/
584+
566585
protected void setupDownloadOptions() {
567586
setRadioButtonsState(false);
568587

@@ -575,7 +594,7 @@ protected void setupDownloadOptions() {
575594
dialogBinding.subtitleButton.setVisibility(isSubtitleStreamsAvailable
576595
? View.VISIBLE : View.GONE);
577596

578-
prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
597+
prefs = PreferenceManager.getDefaultSharedPreferences(requireContext());
579598
final String defaultMedia = prefs.getString(getString(R.string.last_used_download_type),
580599
getString(R.string.last_download_type_video_key));
581600

0 commit comments

Comments
 (0)