Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use view binding directly in BrowseGridFragment #1973

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -71,7 +69,6 @@
import org.jellyfin.apiclient.model.querying.ItemSortBy;
import org.jellyfin.sdk.model.api.BaseItemDto;
import org.jellyfin.sdk.model.api.BaseItemKind;
import org.koin.java.KoinJavaComponent;

import java.text.MessageFormat;
import java.util.HashMap;
Expand Down Expand Up @@ -106,12 +103,7 @@ public class BrowseGridFragment extends Fragment {
private BaseItemDto mFolder;
private LibraryPreferences libraryPreferences;

private TextView mTitleView;
private TextView mStatusText;
private TextView mCounter;
private ViewGroup mGridDock;
private LinearLayout mInfoRow;
private LinearLayout mToolBar;
private HorizontalGridBrowseBinding binding;
private ItemRowAdapter mAdapter;
private Presenter mGridPresenter;
private Presenter.ViewHolder mGridViewHolder;
Expand All @@ -128,6 +120,7 @@ public class BrowseGridFragment extends Fragment {
private final Lazy<MediaManager> mediaManager = inject(MediaManager.class);
private final Lazy<PreferencesRepository> preferencesRepository = inject(PreferencesRepository.class);
private final Lazy<UserViewsRepository> userViewsRepository = inject(UserViewsRepository.class);
private final Lazy<UserRepository> userRepository = inject(UserRepository.class);

private int mCardsScreenEst = 0;
private int mCardsScreenStride = 0;
Expand Down Expand Up @@ -179,7 +172,6 @@ public void onCreate(Bundle savedInstanceState) {

setDefaultGridRowCols(mPosterSizeSetting, mImageType);
setAutoCardGridValues();
mJumplistPopup = new JumplistPopup();
setupQueries();
setupEventListeners();
}
Expand All @@ -188,28 +180,22 @@ public void onCreate(Bundle savedInstanceState) {
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {

HorizontalGridBrowseBinding binding = HorizontalGridBrowseBinding.inflate(inflater, container, false);
mTitleView = binding.title;
mStatusText = binding.statusText;
mInfoRow = binding.infoRow;
mToolBar = binding.toolBar;
mCounter = binding.counter;
mGridDock = binding.rowsFragment;
binding = HorizontalGridBrowseBinding.inflate(inflater, container, false);

// Hide the description because we don't have room for it
binding.npBug.showDescription(false);

// NOTE: we only get the 100% correct grid size if we render it once, so hook into it here
mGridDock.post(() -> {
if (mGridDock.getHeight() > 0 && mGridDock.getWidth() > 0) {
binding.rowsFragment.post(() -> {
if (binding.rowsFragment.getHeight() > 0 && binding.rowsFragment.getWidth() > 0) {
if (mGridView == null) {
return;
}
// prevent adaption on minor size delta's
if (Math.abs(mGridHeight - mGridDock.getHeight()) > MIN_GRIDSIZE_CHANGE_DELTA || Math.abs(mGridWidth - mGridDock.getWidth()) > MIN_GRIDSIZE_CHANGE_DELTA) {
mGridHeight = mGridDock.getHeight();
mGridWidth = mGridDock.getWidth();
Timber.d("Auto-Adapting grid size to height <%s> width <%s>", mGridDock.getHeight(), mGridDock.getWidth());
if (Math.abs(mGridHeight - binding.rowsFragment.getHeight()) > MIN_GRIDSIZE_CHANGE_DELTA || Math.abs(mGridWidth - binding.rowsFragment.getWidth()) > MIN_GRIDSIZE_CHANGE_DELTA) {
mGridHeight = binding.rowsFragment.getHeight();
mGridWidth = binding.rowsFragment.getWidth();
Timber.d("Auto-Adapting grid size to height <%s> width <%s>", binding.rowsFragment.getHeight(), binding.rowsFragment.getWidth());
mDirty = true;
determiningPosterSize = true;
setAutoCardGridValues();
Expand Down Expand Up @@ -239,7 +225,7 @@ public void onDestroyView() {
}

private void createGrid() {
mGridViewHolder = mGridPresenter.onCreateViewHolder(mGridDock);
mGridViewHolder = mGridPresenter.onCreateViewHolder(binding.rowsFragment);
if (mGridViewHolder instanceof HorizontalGridPresenter.ViewHolder) {
mGridView = ((HorizontalGridPresenter.ViewHolder) mGridViewHolder).getGridView();
mGridView.setGravity(Gravity.CENTER_VERTICAL);
Expand All @@ -252,8 +238,8 @@ private void createGrid() {
mGridView.setHorizontalSpacing(mGridItemSpacingHorizontal);
mGridView.setVerticalSpacing(mGridItemSpacingVertical);
mGridView.setFocusable(true);
mGridDock.removeAllViews();
mGridDock.addView(mGridViewHolder.view);
binding.rowsFragment.removeAllViews();
binding.rowsFragment.addView(mGridViewHolder.view);

updateAdapter();
}
Expand Down Expand Up @@ -294,11 +280,11 @@ public void setGridPresenter(VerticalGridPresenter gridPresenter) {

public void setItem(BaseRowItem item) {
if (item != null) {
mTitleView.setText(item.getFullName(requireContext()));
InfoLayoutHelper.addInfoRow(requireContext(), item, mInfoRow, true, true);
binding.title.setText(item.getFullName(requireContext()));
InfoLayoutHelper.addInfoRow(requireContext(), item, binding.infoRow, true, true);
} else {
mTitleView.setText("");
mInfoRow.removeAllViews();
binding.title.setText("");
binding.infoRow.removeAllViews();
}
}

Expand Down Expand Up @@ -341,7 +327,7 @@ public void setStatusText(String folderName) {

text += " " + getString(R.string.lbl_from) + " '" + folderName + "' " + getString(R.string.lbl_sorted_by) + " " + getSortOption(mAdapter.getSortBy()).name;

mStatusText.setText(text);
binding.statusText.setText(text);
}

final private OnItemViewSelectedListener mRowSelectedListener =
Expand All @@ -364,7 +350,7 @@ public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,

public void updateCounter(int position) {
if (mAdapter != null) {
mCounter.setText(MessageFormat.format("{0} | {1}", position, mAdapter.getTotalItems()));
binding.counter.setText(MessageFormat.format("{0} | {1}", position, mAdapter.getTotalItems()));
}
}

Expand Down Expand Up @@ -594,7 +580,7 @@ private void setupQueries() {
String includeType = requireActivity().getIntent().getStringExtra(Extras.IncludeType);
if ("AlbumArtist".equals(includeType)) {
ArtistsQuery albumArtists = new ArtistsQuery();
albumArtists.setUserId(KoinJavaComponent.<UserRepository>get(UserRepository.class).getCurrentUser().getValue().getId().toString());
albumArtists.setUserId(userRepository.getValue().getCurrentUser().getValue().getId().toString());
albumArtists.setFields(new ItemFields[]{
ItemFields.PrimaryImageAspectRatio,
ItemFields.ItemCounts,
Expand Down Expand Up @@ -726,8 +712,8 @@ public void onResponse() {
}
mLetterButton.setVisibility(ItemSortBy.SortName.equals(mAdapter.getSortBy()) ? View.VISIBLE : View.GONE);
if (mAdapter.getTotalItems() == 0) {
mToolBar.requestFocus();
mHandler.postDelayed(() -> mTitleView.setText(mFolder.getName()), 500);
binding.toolBar.requestFocus();
mHandler.postDelayed(() -> binding.title.setText(mFolder.getName()), 500);
} else {
if (mGridView != null) mGridView.requestFocus();
}
Expand Down Expand Up @@ -773,7 +759,7 @@ private void addTools() {
@Override
public void onClick(View v) {
//Create sort menu
PopupMenu sortMenu = new PopupMenu(getActivity(), mToolBar, Gravity.END);
PopupMenu sortMenu = new PopupMenu(getActivity(), binding.toolBar, Gravity.END);
for (Integer key : sortOptions.keySet()) {
SortOption option = sortOptions.get(key);
if (option == null) option = sortOptions.get(0);
Expand All @@ -797,7 +783,7 @@ public boolean onMenuItemClick(MenuItem item) {
});
mSortButton.setContentDescription(getString(R.string.lbl_sort_by));

mToolBar.addView(mSortButton);
binding.toolBar.addView(mSortButton);

if (mRowDef.getQueryType() == QueryType.Items) {
mUnwatchedButton = new ImageButton(requireContext(), null, 0, R.style.Button_Icon);
Expand All @@ -819,7 +805,7 @@ public void onClick(View v) {
}
});
mUnwatchedButton.setContentDescription(getString(R.string.lbl_unwatched));
mToolBar.addView(mUnwatchedButton);
binding.toolBar.addView(mUnwatchedButton);
}

mFavoriteButton = new ImageButton(requireContext(), null, 0, R.style.Button_Icon);
Expand All @@ -841,8 +827,9 @@ public void onClick(View v) {
}
});
mFavoriteButton.setContentDescription(getString(R.string.lbl_favorite));
mToolBar.addView(mFavoriteButton);
binding.toolBar.addView(mFavoriteButton);

JumplistPopup jumplistPopup = new JumplistPopup();
mLetterButton = new ImageButton(requireContext(), null, 0, R.style.Button_Icon);
mLetterButton.setImageResource(R.drawable.ic_jump_letter);
mLetterButton.setMaxHeight(size);
Expand All @@ -851,11 +838,11 @@ public void onClick(View v) {
@Override
public void onClick(View v) {
//Open letter jump popup
mJumplistPopup.show();
jumplistPopup.show();
}
});
mLetterButton.setContentDescription(getString(R.string.lbl_by_letter));
mToolBar.addView(mLetterButton);
binding.toolBar.addView(mLetterButton);

mSettingsButton = new ImageButton(requireContext(), null, 0, R.style.Button_Icon);
mSettingsButton.setImageResource(R.drawable.ic_settings);
Expand All @@ -874,11 +861,9 @@ public void onClick(View v) {
}
});
mSettingsButton.setContentDescription(getString(R.string.lbl_settings));
mToolBar.addView(mSettingsButton);
binding.toolBar.addView(mSettingsButton);
}

private JumplistPopup mJumplistPopup;

class JumplistPopup {
private final int WIDTH = Utils.convertDpToPixel(requireContext(), 900);
private final int HEIGHT = Utils.convertDpToPixel(requireContext(), 55);
Expand All @@ -887,7 +872,7 @@ class JumplistPopup {
private final AlphaPickerView alphaPicker;

JumplistPopup() {
PopupEmptyBinding layout = PopupEmptyBinding.inflate(getLayoutInflater(), mGridDock, false);
PopupEmptyBinding layout = PopupEmptyBinding.inflate(getLayoutInflater(), binding.rowsFragment, false);
popupWindow = new PopupWindow(layout.emptyPopup, WIDTH, HEIGHT, true);
popupWindow.setOutsideTouchable(true);
popupWindow.setAnimationStyle(R.style.WindowAnimation_SlideTop);
Expand All @@ -904,7 +889,7 @@ class JumplistPopup {
}

public void show() {
popupWindow.showAtLocation(mGridDock, Gravity.TOP, mGridDock.getLeft(), mGridDock.getTop());
popupWindow.showAtLocation(binding.rowsFragment, Gravity.TOP, binding.rowsFragment.getLeft(), binding.rowsFragment.getTop());
if (mAdapter.getStartLetter() != null && !mAdapter.getStartLetter().isEmpty()) {
alphaPicker.focus(mAdapter.getStartLetter().charAt(0));
}
Expand Down Expand Up @@ -976,7 +961,7 @@ public void onResponse() {
if (mAdapter.getFilters() != null) {
if ((mAdapter.getFilters().isFavoriteOnly() && !mCurrentItem.isFavorite()) || (mAdapter.getFilters().isUnwatchedOnly() && mCurrentItem.isPlayed())) {
//if we are about to remove last item, throw focus to toolbar so framework doesn't crash
if (mAdapter.size() == 1) mToolBar.requestFocus();
if (mAdapter.size() == 1) binding.toolBar.requestFocus();
mAdapter.remove(mCurrentItem);
mAdapter.setTotalItems(mAdapter.getTotalItems() - 1);
updateCounter(mCurrentItem.getIndex());
Expand Down Expand Up @@ -1012,13 +997,13 @@ public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
mHandler.removeCallbacks(mDelayedSetItem);
if (!(item instanceof BaseRowItem)) {
mCurrentItem = null;
mTitleView.setText(mainTitle);
binding.title.setText(mainTitle);
//fill in default background
backgroundService.getValue().clearBackgrounds();
} else {
mCurrentItem = (BaseRowItem) item;
mTitleView.setText(mCurrentItem.getName(requireContext()));
mInfoRow.removeAllViews();
binding.title.setText(mCurrentItem.getName(requireContext()));
binding.infoRow.removeAllViews();
mHandler.postDelayed(mDelayedSetItem, VIEW_SELECT_UPDATE_DELAY);

if (!determiningPosterSize)
Expand Down