1 package com.moiseum.wolnelektury.view.player;
3 import android.content.DialogInterface;
4 import android.graphics.drawable.Drawable;
5 import android.os.Bundle;
6 import android.support.v4.app.FragmentManager;
7 import android.support.v4.app.FragmentTransaction;
8 import android.support.v4.content.ContextCompat;
9 import android.support.v7.app.AlertDialog;
10 import android.view.View;
11 import android.widget.ImageButton;
12 import android.widget.SeekBar;
13 import android.widget.TextView;
15 import com.moiseum.wolnelektury.R;
16 import com.moiseum.wolnelektury.base.mvp.PresenterFragment;
17 import com.moiseum.wolnelektury.connection.models.BookDetailsModel;
18 import com.moiseum.wolnelektury.utils.StringUtils;
19 import com.moiseum.wolnelektury.view.player.header.PlayerHeaderFragment;
20 import com.moiseum.wolnelektury.view.player.playlist.PlayerPlaylistFragment;
22 import org.parceler.Parcels;
24 import butterknife.BindView;
25 import butterknife.OnClick;
27 import static com.moiseum.wolnelektury.view.player.PlayerActivity.PlayerIntent.BOOK_KEY;
28 import static com.moiseum.wolnelektury.view.player.PlayerActivity.PlayerIntent.BOOK_SLUG_KEY;
32 * Created by Piotr Ostrowski on 22.05.2018.
34 public class PlayerFragment extends PresenterFragment<PlayerPresenter> implements PlayerView {
36 private static final String TAG = PlayerFragment.class.getSimpleName();
37 private AlertDialog errorDialog;
39 public static PlayerFragment newInstance(BookDetailsModel book, String slug) {
40 PlayerFragment playerFragment = new PlayerFragment();
41 Bundle args = new Bundle();
42 args.putParcelable(BOOK_KEY, Parcels.wrap(book));
43 args.putString(BOOK_SLUG_KEY, slug);
44 playerFragment.setArguments(args);
45 return playerFragment;
48 private static final String HEADER_FRAGMENT_TAG = "HeaderFragmentTag";
49 private static final String LIST_FRAGMENT_TAG = "ListFragmentTag";
51 int userSelectedPosition = 0;
52 private boolean mUserIsSeeking = false;
54 private PlayerHeaderFragment headerFragment;
55 private PlayerPlaylistFragment playlistFragment;
57 private SeekBar.OnSeekBarChangeListener listener = new SeekBar.OnSeekBarChangeListener() {
59 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
60 tvCurrentProgress.setText(getPresenter().getCurrentTimerText(progress));
62 userSelectedPosition = progress;
67 public void onStartTrackingTouch(SeekBar seekBar) {
68 mUserIsSeeking = true;
69 getPresenter().playOrPause(true);
73 public void onStopTrackingTouch(SeekBar seekBar) {
74 mUserIsSeeking = false;
75 getPresenter().seekTo(userSelectedPosition);
76 getPresenter().playOrPause(false);
80 @BindView(R.id.sbPlayerProgress)
81 SeekBar sbPlayerProgress;
82 @BindView(R.id.tvCurrentProgress)
83 TextView tvCurrentProgress;
84 @BindView(R.id.tvTotalProgress)
85 TextView tvTotalProgress;
86 @BindView(R.id.tvChapterTitle)
87 TextView tvChapterTitle;
88 @BindView(R.id.tvArtist)
90 @BindView(R.id.ibToggleList)
91 ImageButton ibToggleList;
92 @BindView(R.id.ibPlayPause)
93 ImageButton ibPlayPause;
94 @BindView(R.id.ibPrevious)
95 ImageButton ibPrevious;
96 @BindView(R.id.ibNext)
100 protected PlayerPresenter createPresenter() {
101 if (getArguments() == null || getArguments().getParcelable(BOOK_KEY) == null) {
102 throw new IllegalStateException("Book object is required at this point.");
104 return new PlayerPresenter(
105 Parcels.unwrap(getArguments().getParcelable(BOOK_KEY)),
106 getArguments().getString(BOOK_SLUG_KEY),
113 public int getLayoutResourceId() {
114 return R.layout.fragment_player;
118 public void prepareView(View view, Bundle savedInstanceState) {
119 if (getArguments() == null || getArguments().getParcelable(BOOK_KEY) == null) {
120 throw new IllegalStateException("Book object is required at this point.");
122 BookDetailsModel book = Parcels.unwrap(getArguments().getParcelable(BOOK_KEY));
123 initializeHeaderAndPlaylistFragments(book);
124 sbPlayerProgress.setOnSeekBarChangeListener(listener);
126 int visibility = book.getAudiobookMediaModels().size() > 1 ? View.VISIBLE : View.GONE;
127 tvArtist.setVisibility(visibility);
128 ibToggleList.setVisibility(visibility);
129 ibPrevious.setVisibility(visibility);
130 ibNext.setVisibility(visibility);
133 @OnClick(R.id.ibToggleList)
134 public void onToggleListClicked() {
135 FragmentManager fragmentManager = getFragmentManager();
136 if (fragmentManager == null) {
140 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
141 if (playlistFragment.isHidden()) {
142 fragmentTransaction.show(playlistFragment);
143 fragmentTransaction.hide(headerFragment);
145 fragmentTransaction.hide(playlistFragment);
146 fragmentTransaction.show(headerFragment);
148 fragmentTransaction.commit();
151 @OnClick(R.id.ibPrevious)
152 public void onPreviousClicked() {
153 getPresenter().changeChapter(false);
156 @OnClick(R.id.ibNext)
157 public void onNextClicked() {
158 getPresenter().changeChapter(true);
161 @OnClick(R.id.ibRewind)
162 public void onRewindClicked() {
163 getPresenter().seekToButton(false);
166 @OnClick(R.id.ibPlayPause)
167 public void onPauseClicked() {
168 getPresenter().playOrPause(false);
171 @OnClick(R.id.ibFastForward)
172 public void onFastForwardClicked() {
173 getPresenter().seekToButton(true);
177 public void setTrackDuration(int trackDuration, String totalProgress) {
178 sbPlayerProgress.setMax(trackDuration);
179 tvTotalProgress.setText(totalProgress);
183 public void setTrackPosition(int position, String currentProgress) {
184 if (!mUserIsSeeking) {
185 sbPlayerProgress.setProgress(position);
186 tvCurrentProgress.setText(currentProgress);
191 public void setTrackTexts(String title, int chapter) {
192 tvArtist.setText(getString(R.string.player_chapter_number, (chapter + 1)));
193 tvChapterTitle.setText(title);
197 public void setPlayButtonState(boolean playing) {
198 if (getContext() != null) {
199 Drawable drawable = ContextCompat.getDrawable(getContext(), playing ? R.drawable.pause_selector : R.drawable.play_selector);
200 ibPlayPause.setImageDrawable(drawable);
205 public void onPlayerError() {
206 if (getActivity() != null && errorDialog == null) {
207 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
208 dialogBuilder.setMessage(getString(R.string.load_player_failed));
209 dialogBuilder.setCancelable(false);
210 dialogBuilder.setPositiveButton(getString(R.string.close), (dialog, id) -> {
211 getActivity().finish();
214 dialogBuilder.setOnDismissListener(dialog -> errorDialog = null);
215 errorDialog = dialogBuilder.create();
220 private void initializeHeaderAndPlaylistFragments(BookDetailsModel book) {
221 FragmentManager fragmentManager = getFragmentManager();
222 if (fragmentManager == null) {
226 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
227 headerFragment = (PlayerHeaderFragment) fragmentManager.findFragmentByTag(HEADER_FRAGMENT_TAG);
228 if (headerFragment == null) {
229 headerFragment = PlayerHeaderFragment.newInstance(StringUtils.joinCategory(book.getAuthors(), ", "), book.getTitle(), book.getCoverThumb());
230 fragmentTransaction.add(R.id.flPlayerFragmentContainer, headerFragment, HEADER_FRAGMENT_TAG);
232 playlistFragment = (PlayerPlaylistFragment) fragmentManager.findFragmentByTag(LIST_FRAGMENT_TAG);
233 if (playlistFragment == null) {
234 playlistFragment = PlayerPlaylistFragment.newInstance(book.getAudiobookMediaModels());
235 fragmentTransaction.add(R.id.flPlayerFragmentContainer, playlistFragment, LIST_FRAGMENT_TAG);
237 fragmentTransaction.hide(playlistFragment);
238 fragmentTransaction.commit();