working track selectors

rework_peertube_api
Thomas 2 years ago
parent 0a9c5162ca
commit 327fac5f5a

@ -8,15 +8,12 @@ import android.util.SparseArray;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialog;
import androidx.fragment.app.DialogFragment; import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter; import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player;
@ -25,7 +22,7 @@ import com.google.android.exoplayer2.source.TrackGroup;
import com.google.android.exoplayer2.trackselection.TrackSelectionOverride; import com.google.android.exoplayer2.trackselection.TrackSelectionOverride;
import com.google.android.exoplayer2.trackselection.TrackSelectionParameters; import com.google.android.exoplayer2.trackselection.TrackSelectionParameters;
import com.google.android.exoplayer2.ui.TrackSelectionView; import com.google.android.exoplayer2.ui.TrackSelectionView;
import com.google.android.material.tabs.TabLayout; import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import java.util.ArrayList; import java.util.ArrayList;
@ -35,6 +32,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import app.fedilab.android.R; import app.fedilab.android.R;
import app.fedilab.android.databinding.TrackSelectionDialogBinding;
/** /**
* Dialog to select tracks. * Dialog to select tracks.
@ -48,6 +46,7 @@ public final class TrackSelectionDialog extends DialogFragment {
private int titleId; private int titleId;
private DialogInterface.OnClickListener onClickListener; private DialogInterface.OnClickListener onClickListener;
private DialogInterface.OnDismissListener onDismissListener; private DialogInterface.OnDismissListener onDismissListener;
private TrackSelectionDialogBinding binding;
public TrackSelectionDialog() { public TrackSelectionDialog() {
tabFragments = new SparseArray<>(); tabFragments = new SparseArray<>();
@ -214,11 +213,19 @@ public final class TrackSelectionDialog extends DialogFragment {
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
// We need to own the view to let tab layout work correctly on all API levels. We can't use MaterialAlertDialogBuilder materialAlertDialogBuilder = new MaterialAlertDialogBuilder(requireContext());
// AlertDialog because it owns the view itself, so we use AppCompatDialog instead, themed using binding = TrackSelectionDialogBinding.inflate(getLayoutInflater());
// the AlertDialog theme overlay with force-enabled title. materialAlertDialogBuilder.setView(binding.getRoot());
AppCompatDialog dialog = Dialog dialog = materialAlertDialogBuilder.create();
new AppCompatDialog(getActivity()); binding.trackSelectionDialogViewPager.setAdapter(new FragmentAdapter(getChildFragmentManager()));
binding.trackSelectionDialogTabLayout.setupWithViewPager(binding.trackSelectionDialogViewPager);
binding.trackSelectionDialogTabLayout.setVisibility(tabFragments.size() > 1 ? View.VISIBLE : View.GONE);
binding.trackSelectionDialogCancelButton.setOnClickListener(view -> dismiss());
binding.trackSelectionDialogOkButton.setOnClickListener(
view -> {
onClickListener.onClick(getDialog(), DialogInterface.BUTTON_POSITIVE);
dismiss();
});
dialog.setTitle(titleId); dialog.setTitle(titleId);
return dialog; return dialog;
} }
@ -229,25 +236,6 @@ public final class TrackSelectionDialog extends DialogFragment {
onDismissListener.onDismiss(dialog); onDismissListener.onDismiss(dialog);
} }
@Override
public View onCreateView(
LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View dialogView = inflater.inflate(R.layout.track_selection_dialog, container, false);
TabLayout tabLayout = dialogView.findViewById(R.id.track_selection_dialog_tab_layout);
ViewPager viewPager = dialogView.findViewById(R.id.track_selection_dialog_view_pager);
Button cancelButton = dialogView.findViewById(R.id.track_selection_dialog_cancel_button);
Button okButton = dialogView.findViewById(R.id.track_selection_dialog_ok_button);
viewPager.setAdapter(new FragmentAdapter(getChildFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
tabLayout.setVisibility(tabFragments.size() > 1 ? View.VISIBLE : View.GONE);
cancelButton.setOnClickListener(view -> dismiss());
okButton.setOnClickListener(
view -> {
onClickListener.onClick(getDialog(), DialogInterface.BUTTON_POSITIVE);
dismiss();
});
return dialogView;
}
/** /**
* Called when tracks are selected. * Called when tracks are selected.

@ -12,7 +12,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="vertical">
<androidx.viewpager.widget.ViewPager <androidx.viewpager.widget.ViewPager
@ -30,26 +30,28 @@
</androidx.viewpager.widget.ViewPager> </androidx.viewpager.widget.ViewPager>
<LinearLayout <androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="end" android:gravity="end"
android:layout_margin="10dp"
android:orientation="horizontal"> android:orientation="horizontal">
<Button <com.google.android.material.button.MaterialButton
android:id="@+id/track_selection_dialog_cancel_button" android:id="@+id/track_selection_dialog_cancel_button"
style="?android:attr/borderlessButtonStyle" style="@style/Widget.Material3.Button.OutlinedButton"
android:layout_marginEnd="5dp"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@android:string/cancel" /> android:text="@android:string/cancel" />
<Button <com.google.android.material.button.MaterialButton
android:id="@+id/track_selection_dialog_ok_button" android:id="@+id/track_selection_dialog_ok_button"
style="?android:attr/borderlessButtonStyle" style="@style/Widget.Material3.Button.OutlinedButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@android:string/ok" /> android:text="@android:string/ok" />
</LinearLayout> </androidx.appcompat.widget.LinearLayoutCompat>
</LinearLayout> </LinearLayout>
Loading…
Cancel
Save