ImagePreviewPagerAdapter.java 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* _____ _
  2. * |_ _| |_ _ _ ___ ___ _ __ __ _
  3. * | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. * |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. *
  6. * Threema for Android
  7. * Copyright (c) 2021-2024 Threema GmbH
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. package ch.threema.app.mediaattacher;
  22. import android.os.Bundle;
  23. import android.widget.Toast;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import androidx.annotation.NonNull;
  27. import androidx.annotation.Nullable;
  28. import androidx.annotation.OptIn;
  29. import androidx.fragment.app.Fragment;
  30. import androidx.fragment.app.FragmentActivity;
  31. import androidx.lifecycle.ViewModelProvider;
  32. import androidx.media3.common.util.UnstableApi;
  33. import androidx.viewpager2.adapter.FragmentStateAdapter;
  34. import ch.threema.app.ThreemaApplication;
  35. import ch.threema.app.activities.MediaViewerActivity;
  36. import ch.threema.app.ui.MediaItem;
  37. public class ImagePreviewPagerAdapter extends FragmentStateAdapter {
  38. private final MediaAttachViewModel mediaAttachViewModel;
  39. private List<MediaAttachItem> mediaAttachItems = new ArrayList<>();
  40. public ImagePreviewPagerAdapter(FragmentActivity mediaSelectionBaseActivity) {
  41. super(mediaSelectionBaseActivity);
  42. this.mediaAttachViewModel = new ViewModelProvider(mediaSelectionBaseActivity).get(MediaAttachViewModel.class);
  43. }
  44. @NonNull
  45. @Override
  46. public Fragment createFragment(int position) {
  47. MediaAttachItem mediaAttachItem = getItem(position);
  48. if (mediaAttachItem != null) {
  49. int mimeType = mediaAttachItem.getType();
  50. Bundle args = new Bundle();
  51. args.putBoolean(MediaViewerActivity.EXTRA_ID_IMMEDIATE_PLAY, true);
  52. PreviewFragment fragment = null;
  53. if (mimeType == MediaItem.TYPE_IMAGE || mimeType == MediaItem.TYPE_GIF || mimeType == MediaItem.TYPE_IMAGE_ANIMATED) {
  54. fragment = new ImagePreviewFragment(mediaAttachItem, mediaAttachViewModel);
  55. } else if (mimeType == MediaItem.TYPE_VIDEO) {
  56. fragment = new VideoPreviewFragment(mediaAttachItem, mediaAttachViewModel);
  57. }
  58. if (fragment != null) {
  59. fragment.setArguments(args);
  60. return fragment;
  61. } else {
  62. Toast.makeText(ThreemaApplication.getAppContext(), "Unrecognized Preview Format", Toast.LENGTH_SHORT).show();
  63. }
  64. }
  65. return new DummyPreviewFragment(mediaAttachItem, mediaAttachViewModel);
  66. }
  67. public void setMediaItems(List<MediaAttachItem> items) {
  68. mediaAttachItems = items;
  69. notifyDataSetChanged();
  70. }
  71. @Override
  72. public int getItemCount() {
  73. return mediaAttachItems.size();
  74. }
  75. @Nullable
  76. public MediaAttachItem getItem(int position) {
  77. if (position < mediaAttachItems.size() && position >= 0) {
  78. return mediaAttachItems.get(position);
  79. }
  80. return null;
  81. }
  82. }