Added Android code
[wl-app.git] / Android / app / src / main / java / com / moiseum / wolnelektury / view / player / PlayerFragment.java
1 package com.moiseum.wolnelektury.view.player;
2
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;
14
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;
21
22 import org.parceler.Parcels;
23
24 import butterknife.BindView;
25 import butterknife.OnClick;
26
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;
29
30
31 /**
32  * Created by Piotr Ostrowski on 22.05.2018.
33  */
34 public class PlayerFragment extends PresenterFragment<PlayerPresenter> implements PlayerView {
35
36         private static final String TAG = PlayerFragment.class.getSimpleName();
37         private AlertDialog errorDialog;
38
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;
46         }
47
48         private static final String HEADER_FRAGMENT_TAG = "HeaderFragmentTag";
49         private static final String LIST_FRAGMENT_TAG = "ListFragmentTag";
50
51         int userSelectedPosition = 0;
52         private boolean mUserIsSeeking = false;
53
54         private PlayerHeaderFragment headerFragment;
55         private PlayerPlaylistFragment playlistFragment;
56
57         private SeekBar.OnSeekBarChangeListener listener = new SeekBar.OnSeekBarChangeListener() {
58                 @Override
59                 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
60                         tvCurrentProgress.setText(getPresenter().getCurrentTimerText(progress));
61                         if (fromUser) {
62                                 userSelectedPosition = progress;
63                         }
64                 }
65
66                 @Override
67                 public void onStartTrackingTouch(SeekBar seekBar) {
68                         mUserIsSeeking = true;
69                         getPresenter().playOrPause(true);
70                 }
71
72                 @Override
73                 public void onStopTrackingTouch(SeekBar seekBar) {
74                         mUserIsSeeking = false;
75                         getPresenter().seekTo(userSelectedPosition);
76                         getPresenter().playOrPause(false);
77                 }
78         };
79
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)
89         TextView 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)
97         ImageButton ibNext;
98
99         @Override
100         protected PlayerPresenter createPresenter() {
101                 if (getArguments() == null || getArguments().getParcelable(BOOK_KEY) == null) {
102                         throw new IllegalStateException("Book object is required at this point.");
103                 }
104                 return new PlayerPresenter(
105                                 Parcels.unwrap(getArguments().getParcelable(BOOK_KEY)),
106                                 getArguments().getString(BOOK_SLUG_KEY),
107                                 this,
108                                 getContext()
109                 );
110         }
111
112         @Override
113         public int getLayoutResourceId() {
114                 return R.layout.fragment_player;
115         }
116
117         @Override
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.");
121                 }
122                 BookDetailsModel book = Parcels.unwrap(getArguments().getParcelable(BOOK_KEY));
123                 initializeHeaderAndPlaylistFragments(book);
124                 sbPlayerProgress.setOnSeekBarChangeListener(listener);
125
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);
131         }
132
133         @OnClick(R.id.ibToggleList)
134         public void onToggleListClicked() {
135                 FragmentManager fragmentManager = getFragmentManager();
136                 if (fragmentManager == null) {
137                         return;
138                 }
139
140                 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
141                 if (playlistFragment.isHidden()) {
142                         fragmentTransaction.show(playlistFragment);
143                         fragmentTransaction.hide(headerFragment);
144                 } else {
145                         fragmentTransaction.hide(playlistFragment);
146                         fragmentTransaction.show(headerFragment);
147                 }
148                 fragmentTransaction.commit();
149         }
150
151         @OnClick(R.id.ibPrevious)
152         public void onPreviousClicked() {
153                 getPresenter().changeChapter(false);
154         }
155
156         @OnClick(R.id.ibNext)
157         public void onNextClicked() {
158                 getPresenter().changeChapter(true);
159         }
160
161         @OnClick(R.id.ibRewind)
162         public void onRewindClicked() {
163                 getPresenter().seekToButton(false);
164         }
165
166         @OnClick(R.id.ibPlayPause)
167         public void onPauseClicked() {
168                 getPresenter().playOrPause(false);
169         }
170
171         @OnClick(R.id.ibFastForward)
172         public void onFastForwardClicked() {
173                 getPresenter().seekToButton(true);
174         }
175
176         @Override
177         public void setTrackDuration(int trackDuration, String totalProgress) {
178                 sbPlayerProgress.setMax(trackDuration);
179                 tvTotalProgress.setText(totalProgress);
180         }
181
182         @Override
183         public void setTrackPosition(int position, String currentProgress) {
184                 if (!mUserIsSeeking) {
185                         sbPlayerProgress.setProgress(position);
186                         tvCurrentProgress.setText(currentProgress);
187                 }
188         }
189
190         @Override
191         public void setTrackTexts(String title, int chapter) {
192                 tvArtist.setText(getString(R.string.player_chapter_number, (chapter + 1)));
193                 tvChapterTitle.setText(title);
194         }
195
196         @Override
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);
201                 }
202         }
203
204         @Override
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();
212                                 dialog.dismiss();
213                         });
214                         dialogBuilder.setOnDismissListener(dialog -> errorDialog = null);
215                         errorDialog = dialogBuilder.create();
216                         errorDialog.show();
217                 }
218         }
219
220         private void initializeHeaderAndPlaylistFragments(BookDetailsModel book) {
221                 FragmentManager fragmentManager = getFragmentManager();
222                 if (fragmentManager == null) {
223                         return;
224                 }
225
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);
231                 }
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);
236                 }
237                 fragmentTransaction.hide(playlistFragment);
238                 fragmentTransaction.commit();
239         }
240 }
241
242
243