mirror of
https://github.com/antebudimir/tempus.git
synced 2026-01-01 18:03:33 +00:00
Preparation to music streaming
This commit is contained in:
parent
f837bb14e2
commit
a28ad27288
23 changed files with 615 additions and 279 deletions
|
|
@ -3,7 +3,6 @@ package com.cappielloantonio.play.ui.activities;
|
|||
import android.content.IntentFilter;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
|
@ -17,6 +16,7 @@ import com.cappielloantonio.play.App;
|
|||
import com.cappielloantonio.play.R;
|
||||
import com.cappielloantonio.play.broadcast.receiver.ConnectivityStatusBroadcastReceiver;
|
||||
import com.cappielloantonio.play.databinding.ActivityMainBinding;
|
||||
import com.cappielloantonio.play.model.Song;
|
||||
import com.cappielloantonio.play.ui.activities.base.BaseActivity;
|
||||
import com.cappielloantonio.play.ui.fragment.PlayerBottomSheetFragment;
|
||||
import com.cappielloantonio.play.util.PreferenceUtil;
|
||||
|
|
@ -81,42 +81,6 @@ public class MainActivity extends BaseActivity {
|
|||
}
|
||||
}
|
||||
|
||||
private void initBottomSheet() {
|
||||
bottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.player_bottom_sheet));
|
||||
bottomSheetBehavior.addBottomSheetCallback(bottomSheetCallback);
|
||||
fragmentManager.beginTransaction().replace(R.id.player_bottom_sheet, new PlayerBottomSheetFragment(), "PlayerBottomSheet").commit();
|
||||
|
||||
isBottomSheetInPeek(mainViewModel.isQueueLoaded());
|
||||
}
|
||||
|
||||
public void isBottomSheetInPeek(Boolean isVisible) {
|
||||
|
||||
Log.d(TAG, "isBottomSheetInPeek: " + isVisible);
|
||||
|
||||
if (isVisible) {
|
||||
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
|
||||
} else {
|
||||
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
|
||||
}
|
||||
}
|
||||
|
||||
private void initNavigation() {
|
||||
bottomNavigationView = findViewById(R.id.bottom_navigation);
|
||||
navHostFragment = (NavHostFragment) fragmentManager.findFragmentById(R.id.nav_host_fragment);
|
||||
navController = navHostFragment.getNavController();
|
||||
navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
|
||||
if(bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED && (
|
||||
destination.getId() == R.id.homeFragment ||
|
||||
destination.getId() == R.id.libraryFragment ||
|
||||
destination.getId() == R.id.searchFragment ||
|
||||
destination.getId() == R.id.settingsFragment)
|
||||
) {
|
||||
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
|
||||
}
|
||||
});
|
||||
NavigationUI.setupWithNavController(bottomNavigationView, navController);
|
||||
}
|
||||
|
||||
private void checkPreviousSession() {
|
||||
App.getApiClientInstance(getApplicationContext()).ChangeServerLocation(PreferenceUtil.getInstance(this).getServer());
|
||||
App.getApiClientInstance(getApplicationContext()).SetAuthenticationInfo(PreferenceUtil.getInstance(this).getToken(), PreferenceUtil.getInstance(this).getUser());
|
||||
|
|
@ -140,6 +104,49 @@ public class MainActivity extends BaseActivity {
|
|||
});
|
||||
}
|
||||
|
||||
|
||||
// BOTTOM SHEET/NAVIGATION
|
||||
private void initBottomSheet() {
|
||||
bottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.player_bottom_sheet));
|
||||
bottomSheetBehavior.addBottomSheetCallback(bottomSheetCallback);
|
||||
fragmentManager.beginTransaction().replace(R.id.player_bottom_sheet, new PlayerBottomSheetFragment(), "PlayerBottomSheet").commit();
|
||||
|
||||
/*
|
||||
* All'apertura mostro il bottom sheet solo se in coda c'è qualcosa
|
||||
*/
|
||||
isBottomSheetInPeek(mainViewModel.isQueueLoaded());
|
||||
}
|
||||
|
||||
public void isBottomSheetInPeek(Boolean isVisible) {
|
||||
if (isVisible) {
|
||||
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
|
||||
} else {
|
||||
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
|
||||
}
|
||||
}
|
||||
|
||||
private void initNavigation() {
|
||||
bottomNavigationView = findViewById(R.id.bottom_navigation);
|
||||
navHostFragment = (NavHostFragment) fragmentManager.findFragmentById(R.id.nav_host_fragment);
|
||||
navController = navHostFragment.getNavController();
|
||||
|
||||
/*
|
||||
* In questo modo intercetto il cambio schermata tramite navbar e se il bottom sheet è aperto,
|
||||
* lo chiudo
|
||||
*/
|
||||
navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
|
||||
if(bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED && (
|
||||
destination.getId() == R.id.homeFragment ||
|
||||
destination.getId() == R.id.libraryFragment ||
|
||||
destination.getId() == R.id.searchFragment ||
|
||||
destination.getId() == R.id.settingsFragment)
|
||||
) {
|
||||
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
|
||||
}
|
||||
});
|
||||
NavigationUI.setupWithNavController(bottomNavigationView, navController);
|
||||
}
|
||||
|
||||
public void setBottomNavigationBarVisibility(boolean visibility) {
|
||||
if (visibility) {
|
||||
bottomNavigationView.setVisibility(View.VISIBLE);
|
||||
|
|
@ -161,7 +168,7 @@ public class MainActivity extends BaseActivity {
|
|||
@Override
|
||||
public void onStateChanged(@NonNull View view, int state) {
|
||||
switch (state) {
|
||||
case BottomSheetBehavior.STATE_COLLAPSED:
|
||||
case BottomSheetBehavior.STATE_SETTLING:
|
||||
PlayerBottomSheetFragment playerBottomSheetFragment = (PlayerBottomSheetFragment) getSupportFragmentManager().findFragmentByTag("PlayerBottomSheet");
|
||||
if(playerBottomSheetFragment == null) break;
|
||||
|
||||
|
|
@ -184,6 +191,18 @@ public class MainActivity extends BaseActivity {
|
|||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* Scroll on top del bottom sheet quando chiudo
|
||||
* In questo modo non mi ritrovo al posto dell'header una parte centrale del player
|
||||
*/
|
||||
public void setBottomSheetMusicInfo(Song song) {
|
||||
PlayerBottomSheetFragment playerBottomSheetFragment = (PlayerBottomSheetFragment) getSupportFragmentManager().findFragmentByTag("PlayerBottomSheet");
|
||||
if(playerBottomSheetFragment == null) return;
|
||||
|
||||
playerBottomSheetFragment.scrollPager(song, 0, false);
|
||||
}
|
||||
|
||||
// NAVIGATION
|
||||
public void goToLogin() {
|
||||
if (Objects.requireNonNull(navController.getCurrentDestination()).getId() == R.id.landingFragment)
|
||||
navController.navigate(R.id.action_landingFragment_to_loginFragment);
|
||||
|
|
@ -225,6 +244,15 @@ public class MainActivity extends BaseActivity {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
if(bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED)
|
||||
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
|
||||
else
|
||||
super.onBackPressed();
|
||||
}
|
||||
|
||||
// CONNECTION
|
||||
private void connectivityStatusReceiverManager(boolean isActive) {
|
||||
if (isActive) {
|
||||
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
|
||||
|
|
@ -233,12 +261,4 @@ public class MainActivity extends BaseActivity {
|
|||
unregisterReceiver(connectivityStatusBroadcastReceiver);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
if(bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED)
|
||||
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
|
||||
else
|
||||
super.onBackPressed();
|
||||
}
|
||||
}
|
||||
|
|
@ -5,10 +5,10 @@ import android.view.LayoutInflater;
|
|||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ScrollView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
|
|
@ -20,6 +20,7 @@ import com.cappielloantonio.play.adapter.PlayerSongQueueAdapter;
|
|||
import com.cappielloantonio.play.databinding.FragmentPlayerBottomSheetBinding;
|
||||
import com.cappielloantonio.play.model.Song;
|
||||
import com.cappielloantonio.play.ui.activities.MainActivity;
|
||||
import com.cappielloantonio.play.util.MusicUtil;
|
||||
import com.cappielloantonio.play.viewmodel.PlayerBottomSheetViewModel;
|
||||
|
||||
public class PlayerBottomSheetFragment extends Fragment {
|
||||
|
|
@ -32,6 +33,8 @@ public class PlayerBottomSheetFragment extends Fragment {
|
|||
private PlayerNowPlayingSongAdapter playerNowPlayingSongAdapter;
|
||||
private PlayerSongQueueAdapter playerSongQueueAdapter;
|
||||
|
||||
private boolean isNowPlaying = false;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
|
|
@ -43,64 +46,59 @@ public class PlayerBottomSheetFragment extends Fragment {
|
|||
|
||||
initQueueSlideView();
|
||||
initQueueRecyclerView();
|
||||
initFavoriteButtonClick();
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
private void initQueueSlideView() {
|
||||
bind.playerSongCoverViewPager.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
|
||||
bind.playerBodyLayout.playerSongCoverViewPager.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
|
||||
|
||||
playerNowPlayingSongAdapter = new PlayerNowPlayingSongAdapter(requireContext());
|
||||
bind.playerSongCoverViewPager.setAdapter(playerNowPlayingSongAdapter);
|
||||
bind.playerBodyLayout.playerSongCoverViewPager.setAdapter(playerNowPlayingSongAdapter);
|
||||
playerBottomSheetViewModel.getQueueSong().observe(requireActivity(), songs -> playerNowPlayingSongAdapter.setItems(songs));
|
||||
|
||||
bind.playerSongCoverViewPager.setOffscreenPageLimit(3);
|
||||
setDiscoverSongSlideViewOffset(40, 4);
|
||||
|
||||
bind.playerSongCoverViewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
|
||||
bind.playerBodyLayout.playerSongCoverViewPager.setOffscreenPageLimit(3);
|
||||
bind.playerBodyLayout.playerSongCoverViewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
|
||||
@Override
|
||||
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
|
||||
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
|
||||
public void onPageSelected(int position) {
|
||||
super.onPageSelected(position);
|
||||
|
||||
playerBottomSheetViewModel.setNowPlayingSong(position);
|
||||
Song song = playerNowPlayingSongAdapter.getItem(position);
|
||||
if (song != null && song != playerBottomSheetViewModel.getSong()) setSongInfo(song);
|
||||
}
|
||||
});
|
||||
|
||||
playerBottomSheetViewModel.getNowPlayingSong().observe(requireActivity(), song -> {
|
||||
if(song != null)
|
||||
setSongInfo(song);
|
||||
});
|
||||
}
|
||||
|
||||
private void initQueueRecyclerView() {
|
||||
bind.playerQueueRecyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
|
||||
bind.playerQueueRecyclerView.setHasFixedSize(true);
|
||||
bind.playerBodyLayout.playerQueueRecyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
|
||||
bind.playerBodyLayout.playerQueueRecyclerView.setHasFixedSize(true);
|
||||
|
||||
playerSongQueueAdapter = new PlayerSongQueueAdapter(activity, requireContext(), getChildFragmentManager());
|
||||
bind.playerQueueRecyclerView.setAdapter(playerSongQueueAdapter);
|
||||
playerSongQueueAdapter = new PlayerSongQueueAdapter(requireContext(), this);
|
||||
bind.playerBodyLayout.playerQueueRecyclerView.setAdapter(playerSongQueueAdapter);
|
||||
playerBottomSheetViewModel.getQueueSong().observe(requireActivity(), songs -> playerSongQueueAdapter.setItems(songs));
|
||||
}
|
||||
|
||||
private void setDiscoverSongSlideViewOffset(float pageOffset, float pageMargin) {
|
||||
bind.playerSongCoverViewPager.setPageTransformer((page, position) -> {
|
||||
float myOffset = position * -(2 * pageOffset + pageMargin);
|
||||
if (bind.playerSongCoverViewPager.getOrientation() == ViewPager2.ORIENTATION_HORIZONTAL) {
|
||||
if (ViewCompat.getLayoutDirection(bind.playerSongCoverViewPager) == ViewCompat.LAYOUT_DIRECTION_RTL) {
|
||||
page.setTranslationX(-myOffset);
|
||||
} else {
|
||||
page.setTranslationX(myOffset);
|
||||
}
|
||||
} else {
|
||||
page.setTranslationY(myOffset);
|
||||
}
|
||||
});
|
||||
private void initFavoriteButtonClick() {
|
||||
bind.playerBodyLayout.buttonFavorite.setOnClickListener(v -> playerBottomSheetViewModel.setFavorite());
|
||||
}
|
||||
|
||||
private void setSongInfo(Song song) {
|
||||
if(song != null) {
|
||||
bind.playerSongTitleLabel.setText(song.getTitle());
|
||||
bind.playerArtistNameLabel.setText(song.getArtistName());
|
||||
}
|
||||
playerBottomSheetViewModel.setNowPlayingSong(song);
|
||||
|
||||
bind.playerBodyLayout.playerSongTitleLabel.setText(song.getTitle());
|
||||
bind.playerBodyLayout.playerArtistNameLabel.setText(song.getArtistName());
|
||||
|
||||
bind.playerHeaderLayout.playerHeaderSongTitleLabel.setText(song.getTitle());
|
||||
bind.playerHeaderLayout.playerHeaderSongArtistLabel.setText(song.getArtistName());
|
||||
|
||||
bind.playerBodyLayout.buttonFavorite.setChecked(song.isFavorite());
|
||||
|
||||
playSong(song);
|
||||
}
|
||||
|
||||
private void playSong(Song song) {
|
||||
Toast.makeText(activity, MusicUtil.getSongFileUri(song), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
public View getPlayerHeader() {
|
||||
|
|
@ -110,4 +108,9 @@ public class PlayerBottomSheetFragment extends Fragment {
|
|||
public void scrollOnTop() {
|
||||
bind.playerNestedScrollView.fullScroll(ScrollView.FOCUS_UP);
|
||||
}
|
||||
|
||||
public void scrollPager(Song song, int page, boolean smoothScroll) {
|
||||
bind.playerBodyLayout.playerSongCoverViewPager.setCurrentItem(page, smoothScroll);
|
||||
setSongInfo(song);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue