-Merged bookmark buttons on playlist fragment into one.

-Fixed bookmark button flickering on visibility toggling.
-Removed toolbar up button control from local fragments, delegating functionality back to main fragment.
-Updated extractor to latest.
This commit is contained in:
John Zhen Mo 2018-02-08 19:53:04 -08:00
parent d0808ce159
commit f62ae930c7
5 changed files with 43 additions and 67 deletions

View File

@ -55,7 +55,7 @@ dependencies {
exclude module: 'support-annotations'
}
implementation 'com.github.TeamNewPipe:NewPipeExtractor:7fd21ec08581d'
implementation 'com.github.TeamNewPipe:NewPipeExtractor:4fb49d54b5'
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:1.10.19'

View File

@ -36,13 +36,16 @@ import org.schabi.newpipe.playlist.SinglePlayQueue;
import org.schabi.newpipe.report.UserAction;
import org.schabi.newpipe.util.ExtractorHelper;
import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.ThemeHelper;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.disposables.Disposables;
import static org.schabi.newpipe.util.AnimationUtils.animateView;
@ -50,6 +53,7 @@ public class PlaylistFragment extends BaseListInfoFragment<PlaylistInfo> {
private CompositeDisposable disposables;
private Subscription bookmarkReactor;
private AtomicBoolean isBookmarkButtonReady;
private RemotePlaylistManager remotePlaylistManager;
private PlaylistRemoteEntity playlistEntity;
@ -70,7 +74,6 @@ public class PlaylistFragment extends BaseListInfoFragment<PlaylistInfo> {
private View headerBackgroundButton;
private MenuItem playlistBookmarkButton;
private MenuItem playlistUnbookmarkButton;
public static PlaylistFragment getInstance(int serviceId, String url, String name) {
PlaylistFragment instance = new PlaylistFragment();
@ -86,6 +89,7 @@ public class PlaylistFragment extends BaseListInfoFragment<PlaylistInfo> {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
disposables = new CompositeDisposable();
isBookmarkButtonReady = new AtomicBoolean(false);
remotePlaylistManager = new RemotePlaylistManager(NewPipeDatabase.getInstance(getContext()));
}
@ -176,14 +180,14 @@ public class PlaylistFragment extends BaseListInfoFragment<PlaylistInfo> {
inflater.inflate(R.menu.menu_playlist, menu);
playlistBookmarkButton = menu.findItem(R.id.menu_item_bookmark);
playlistUnbookmarkButton = menu.findItem(R.id.menu_item_unbookmark);
updateBookmarkButtonsVisibility();
updateBookmarkButtons();
}
@Override
public void onDestroyView() {
super.onDestroyView();
if (isBookmarkButtonReady != null) isBookmarkButtonReady.set(false);
if (disposables != null) disposables.clear();
if (bookmarkReactor != null) bookmarkReactor.cancel();
@ -199,6 +203,7 @@ public class PlaylistFragment extends BaseListInfoFragment<PlaylistInfo> {
disposables = null;
remotePlaylistManager = null;
playlistEntity = null;
isBookmarkButtonReady = null;
}
/*//////////////////////////////////////////////////////////////////////////
@ -225,10 +230,7 @@ public class PlaylistFragment extends BaseListInfoFragment<PlaylistInfo> {
shareUrl(name, url);
break;
case R.id.menu_item_bookmark:
bookmarkPlaylist();
break;
case R.id.menu_item_unbookmark:
unbookmarkPlaylist();
onBookmarkClicked();
break;
default:
return super.onOptionsItemSelected(item);
@ -343,7 +345,9 @@ public class PlaylistFragment extends BaseListInfoFragment<PlaylistInfo> {
@Override
public void onNext(List<PlaylistRemoteEntity> playlist) {
playlistEntity = playlist.isEmpty() ? null : playlist.get(0);
updateBookmarkButtonsVisibility();
updateBookmarkButtons();
isBookmarkButtonReady.set(true);
if (bookmarkReactor != null) bookmarkReactor.request(1);
}
@ -366,35 +370,39 @@ public class PlaylistFragment extends BaseListInfoFragment<PlaylistInfo> {
headerTitleView.setText(title);
}
private void bookmarkPlaylist() {
if (remotePlaylistManager == null || currentInfo == null) return;
private void onBookmarkClicked() {
if (isBookmarkButtonReady == null || !isBookmarkButtonReady.get() ||
remotePlaylistManager == null)
return;
playlistBookmarkButton.setVisible(false);
playlistUnbookmarkButton.setVisible(false);
final Disposable action;
final Disposable disposable = remotePlaylistManager.onBookmark(currentInfo)
if (currentInfo != null && playlistEntity == null) {
action = remotePlaylistManager.onBookmark(currentInfo)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(ignored -> {/* Do nothing */}, this::onError);
disposables.add(disposable);
}
private void unbookmarkPlaylist() {
if (remotePlaylistManager == null || playlistEntity == null) return;
playlistBookmarkButton.setVisible(false);
playlistUnbookmarkButton.setVisible(false);
final Disposable disposable = remotePlaylistManager.deletePlaylist(playlistEntity.getUid())
} else if (playlistEntity != null) {
action = remotePlaylistManager.deletePlaylist(playlistEntity.getUid())
.observeOn(AndroidSchedulers.mainThread())
.doFinally(() -> playlistEntity = null)
.subscribe(ignored -> {/* Do nothing */}, this::onError);
disposables.add(disposable);
} else {
action = Disposables.empty();
}
private void updateBookmarkButtonsVisibility() {
if (playlistBookmarkButton == null || playlistUnbookmarkButton == null) return;
disposables.add(action);
}
playlistBookmarkButton.setVisible(playlistEntity == null);
playlistUnbookmarkButton.setVisible(playlistEntity != null);
private void updateBookmarkButtons() {
if (playlistBookmarkButton == null || activity == null) return;
final int iconAttr = playlistEntity == null ?
R.attr.ic_playlist_add : R.attr.ic_playlist_check;
final int titleRes = playlistEntity == null ?
R.string.bookmark_playlist : R.string.unbookmark_playlist;
playlistBookmarkButton.setIcon(ThemeHelper.resolveResourceIdFromAttr(activity, iconAttr));
playlistBookmarkButton.setTitle(titleRes);
}
}

View File

@ -87,13 +87,6 @@ public abstract class BaseLocalListFragment<I, N> extends BaseStateFragment<I>
// Lifecycle - Menu
//////////////////////////////////////////////////////////////////////////*/
/** Determines if the fragment is part of the main fragment view pager.
* If so, then this method must be overriden to return true
* in order to show the hamburger menu. */
protected boolean isPartOfFrontPager() {
return false;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
@ -104,14 +97,6 @@ public abstract class BaseLocalListFragment<I, N> extends BaseStateFragment<I>
if (supportActionBar == null) return;
supportActionBar.setDisplayShowTitleEnabled(true);
// Show up arrow icon if the fragment is not used as front page or part of the front pager
if (!useAsFrontPage && !isPartOfFrontPager()) {
// If set true, an up arrow icon will be displayed.
// If set false, no icon will be shown.
// If unset, show hamburger menu
supportActionBar.setDisplayHomeAsUpEnabled(true);
}
}
/*//////////////////////////////////////////////////////////////////////////

View File

@ -147,15 +147,6 @@ public final class BookmarkFragment
});
}
/*//////////////////////////////////////////////////////////////////////////
// Fragment Lifecycle - Menu
//////////////////////////////////////////////////////////////////////////*/
@Override
protected boolean isPartOfFrontPager() {
return true;
}
///////////////////////////////////////////////////////////////////////////
// Fragment LifeCycle - Loading
///////////////////////////////////////////////////////////////////////////

View File

@ -18,15 +18,7 @@
android:id="@+id/menu_item_bookmark"
android:icon="?attr/ic_playlist_add"
android:title="@string/bookmark_playlist"
android:visible="false"
app:showAsAction="always"
tools:visible="true"/>
<item
android:id="@+id/menu_item_unbookmark"
android:icon="?attr/ic_playlist_check"
android:title="@string/unbookmark_playlist"
android:visible="false"
app:showAsAction="always"
android:visible="true"
app:showAsAction="ifRoom"
tools:visible="true"/>
</menu>