diff --git a/app/build.gradle b/app/build.gradle index 9ae26f82..410809ce 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -101,6 +101,7 @@ dependencies { implementation 'androidx.room:room-runtime:2.6.1' implementation 'androidx.core:core-splashscreen:1.0.1' implementation 'androidx.appcompat:appcompat:1.7.0' + implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.2.0" // Android Material implementation 'com.google.android.material:material:1.10.0' diff --git a/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/LibraryFragment.java b/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/LibraryFragment.java index daa5d412..b10e6402 100644 --- a/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/LibraryFragment.java +++ b/app/src/main/java/com/cappielloantonio/tempo/ui/fragment/LibraryFragment.java @@ -9,6 +9,8 @@ import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.ViewModelProvider; import androidx.media3.common.util.UnstableApi; import androidx.media3.session.MediaBrowser; @@ -16,8 +18,11 @@ import androidx.media3.session.SessionToken; import androidx.navigation.Navigation; import android.content.ComponentName; +import android.widget.Toast; + import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import com.cappielloantonio.tempo.R; import com.cappielloantonio.tempo.databinding.FragmentLibraryBinding; @@ -43,6 +48,7 @@ import java.util.Objects; @UnstableApi public class LibraryFragment extends Fragment implements ClickCallback { private static final String TAG = "LibraryFragment"; + private static final String TOAST_MSG = "Long press to refresh" ; private FragmentLibraryBinding bind; private MainActivity activity; @@ -81,6 +87,7 @@ public class LibraryFragment extends Fragment implements ClickCallback { initArtistView(); initGenreView(); initPlaylistView(); + initSwipeToRefresh(); } @Override @@ -112,22 +119,41 @@ public class LibraryFragment extends Fragment implements ClickCallback { activity.navController.navigate(R.id.action_libraryFragment_to_playlistCatalogueFragment, bundle); }); + // Album bind.albumCatalogueSampleTextViewRefreshable.setOnLongClickListener(view -> { libraryViewModel.refreshAlbumSample(getViewLifecycleOwner()); return true; }); + bind.albumCatalogueSampleTextViewRefreshable.setOnClickListener( v -> + Toast.makeText(requireContext(), TOAST_MSG, Toast.LENGTH_SHORT).show() + ); + + // Artist bind.artistCatalogueSampleTextViewRefreshable.setOnLongClickListener(view -> { libraryViewModel.refreshArtistSample(getViewLifecycleOwner()); return true; }); + bind.artistCatalogueSampleTextViewRefreshable.setOnClickListener( v -> + Toast.makeText(requireContext(), TOAST_MSG, Toast.LENGTH_SHORT).show() + ); + + // Genre bind.genreCatalogueSampleTextViewRefreshable.setOnLongClickListener(view -> { libraryViewModel.refreshGenreSample(getViewLifecycleOwner()); return true; }); + bind.genreCatalogueSampleTextViewRefreshable.setOnClickListener(v -> + Toast.makeText(requireContext(), TOAST_MSG, Toast.LENGTH_SHORT).show() + ); + + // Playlist bind.playlistCatalogueSampleTextViewRefreshable.setOnLongClickListener(view -> { libraryViewModel.refreshPlaylistSample(getViewLifecycleOwner()); return true; }); + bind.playlistCatalogueSampleTextViewRefreshable.setOnClickListener( v -> + Toast.makeText(requireContext(), TOAST_MSG, Toast.LENGTH_SHORT).show() + ); } private void initAppBar() { @@ -304,4 +330,20 @@ public class LibraryFragment extends Fragment implements ClickCallback { private void initializeMediaBrowser() { mediaBrowserListenableFuture = new MediaBrowser.Builder(requireContext(), new SessionToken(requireContext(), new ComponentName(requireContext(), MediaService.class))).buildAsync(); } + + public void initSwipeToRefresh() { + bind.swipeLibraryToRefresh.setOnRefreshListener(() -> { + pullToRefresh(); + bind.swipeLibraryToRefresh.setRefreshing(false); + }); + } + + private void pullToRefresh() { + LifecycleOwner lifecycleOwner = getViewLifecycleOwner(); + libraryViewModel.refreshAlbumSample(lifecycleOwner); + libraryViewModel.refreshGenreSample(lifecycleOwner); + libraryViewModel.refreshArtistSample(lifecycleOwner); + libraryViewModel.refreshPlaylistSample(lifecycleOwner); + + } } diff --git a/app/src/main/res/layout/fragment_library.xml b/app/src/main/res/layout/fragment_library.xml index c96fadaf..9c5f0419 100644 --- a/app/src/main/res/layout/fragment_library.xml +++ b/app/src/main/res/layout/fragment_library.xml @@ -11,6 +11,12 @@ android:layout_width="match_parent" android:layout_height="wrap_content" /> + + - + + + + + + + + + + android:layout_height="0dp" + android:layout_weight="1" /> @@ -130,22 +156,41 @@ android:paddingEnd="8dp" android:paddingBottom="8dp"> - + + android:layout_width="wrap_content" + android:layout_height="wrap_content" > + + + + + + + @@ -184,25 +229,45 @@ android:paddingEnd="8dp" android:paddingBottom="8dp"> - + + android:layout_width="wrap_content" + android:layout_height="wrap_content" > + + + + + + + + - + + + + + + + + + + android:layout_height="0dp" + android:layout_weight="1" /> @@ -270,4 +355,5 @@ + \ No newline at end of file