Feat, wip[UI]: conditionnal visibility, reduced layout shifting, full screen scroll

This commit is contained in:
Mathias Boulay 2023-08-17 00:15:37 +02:00 committed by ArtDev
parent 17cce29c00
commit 04d6900bc4
7 changed files with 137 additions and 62 deletions

View File

@ -33,6 +33,7 @@ import android.view.View;
import android.view.WindowManager;
import android.webkit.MimeTypeMap;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
@ -1029,4 +1030,11 @@ public final class Tools {
Intent sendIntent = Intent.createChooser(shareIntent, "latestlog.txt");
context.startActivity(sendIntent);
}
public static int mesureTextviewHeight(TextView t) {
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(t.getWidth(), View.MeasureSpec.AT_MOST);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
t.measure(widthMeasureSpec, heightMeasureSpec);
return t.getMeasuredHeight();
}
}

View File

@ -2,6 +2,7 @@ package net.kdt.pojavlaunch.fragments;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
@ -12,6 +13,7 @@ import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.math.MathUtils;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
@ -35,6 +37,16 @@ import java.util.concurrent.Future;
public class SearchModFragment extends Fragment {
public static final String TAG = "SearchModFragment";
private View mOverlay;
private float mOverlayTopCache; // Padding cache reduce resource lookup
private final RecyclerView.OnScrollListener mOverlayPositionListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
mOverlay.setY(MathUtils.clamp(mOverlay.getY() - dy, -mOverlay.getHeight(), mOverlayTopCache));
}
};
private TextView mSelectedVersion;
private Button mSelectVersionButton;
private EditText mSearchEditText;
@ -49,8 +61,6 @@ public class SearchModFragment extends Fragment {
private SearchFilters mSearchFilters;
public SearchModFragment(){
super(R.layout.fragment_mod_search);
modpackApi = new CommonApi();
@ -62,7 +72,9 @@ public class SearchModFragment extends Fragment {
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
// You can only access resources after attaching to current context
mModItemAdapter = new ModItemAdapter(getResources(), modpackApi);
mOverlayTopCache = getResources().getDimension(R.dimen.fragment_padding_medium);
mOverlay = view.findViewById(R.id.search_mod_overlay);
mSearchEditText = view.findViewById(R.id.search_mod_edittext);
mSearchProgressBar = view.findViewById(R.id.search_mod_progressbar);
mSelectedVersion = view.findViewById(R.id.search_mod_selected_mc_version_textview);
@ -75,6 +87,8 @@ public class SearchModFragment extends Fragment {
mRecyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
mRecyclerview.setAdapter(mModItemAdapter);
mRecyclerview.addOnScrollListener(mOverlayPositionListener);
// Setup the expendable list behavior
mSelectVersionButton.setOnClickListener(v -> VersionSelectorDialog.open(v.getContext(), true, (id, snapshot)->{
mSelectedVersion.setText(id);
@ -93,6 +107,12 @@ public class SearchModFragment extends Fragment {
});
}
@Override
public void onDestroyView() {
super.onDestroyView();
mRecyclerview.removeOnScrollListener(mOverlayPositionListener);
}
class SearchModTask implements SelfReferencingFuture.FutureInterface {
private final SearchFilters mTaskFilters;

View File

@ -31,10 +31,14 @@ import net.kdt.pojavlaunch.modloaders.modpacks.models.ModDetail;
import net.kdt.pojavlaunch.modloaders.modpacks.models.ModItem;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.Future;
public class ModItemAdapter extends RecyclerView.Adapter<ModItemAdapter.ViewHolder> {
private static final ModItem[] MOD_ITEMS_EMPTY = new ModItem[0];
/* Used when versions haven't loaded yet, default text to reduce layout shifting */
private final SimpleArrayAdapter<String> mLoadingAdapter = new SimpleArrayAdapter<>(Collections.singletonList("Loading"));
private final ModIconCache mIconCache = new ModIconCache();
private ModItem[] mModItems;
private final ModpackApi mModpackApi;
@ -60,7 +64,7 @@ public class ModItemAdapter extends RecyclerView.Adapter<ModItemAdapter.ViewHold
private ImageReceiver mImageReceiver;
/* Used to display available versions of the mod(pack) */
private SimpleArrayAdapter<String> mVersionAdapter = new SimpleArrayAdapter<>(null);
private final SimpleArrayAdapter<String> mVersionAdapter = new SimpleArrayAdapter<>(null);
public ViewHolder(View view) {
super(view);
@ -77,7 +81,7 @@ public class ModItemAdapter extends RecyclerView.Adapter<ModItemAdapter.ViewHold
mExtendedButton.getContext().getApplicationContext(),
mModDetail,
mExtendedSpinner.getSelectedItemPosition()));
mExtendedSpinner.setAdapter(mVersionAdapter);
mExtendedSpinner.setAdapter(mLoadingAdapter);
} else {
if(isExtended()) closeDetailedView();
else openDetailedView();
@ -184,12 +188,12 @@ public class ModItemAdapter extends RecyclerView.Adapter<ModItemAdapter.ViewHold
private void openDetailedView(){
mExtendedLayout.setVisibility(View.VISIBLE);
mDescription.setMaxLines(99);
mExtendedLayout.post(() -> {
// We need to align to the longer section
int futureBottom = mDescription.getBottom() + Tools.mesureTextviewHeight(mDescription) - mDescription.getHeight();
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) mExtendedLayout.getLayoutParams();
params.topToBottom = mDescription.getBottom() > mIconView.getBottom() ? R.id.mod_body_textview : R.id.mod_thumbnail_imageview;
params.topToBottom = futureBottom > mIconView.getBottom() ? R.id.mod_body_textview : R.id.mod_thumbnail_imageview;
mExtendedLayout.setLayoutParams(params);
});
}
private void closeDetailedView(){
@ -199,7 +203,7 @@ public class ModItemAdapter extends RecyclerView.Adapter<ModItemAdapter.ViewHold
private void setDetailedStateDefault() {
mExtendedButton.setEnabled(false);
mExtendedSpinner.setAdapter(null);
mExtendedSpinner.setAdapter(mLoadingAdapter);
mExtendedErrorTextView.setVisibility(View.GONE);
openDetailedView();
}

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="@dimen/_2sdp" />
<padding
android:bottom="@dimen/padding_small"
android:left="@dimen/padding_small"
android:right="@dimen/padding_small"
android:top="@dimen/padding_small" />
<solid android:color="@color/background_overlay" />
</shape>

View File

@ -6,15 +6,36 @@
android:background="@color/background_app"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingHorizontal="@dimen/fragment_padding_medium"
android:paddingVertical="@dimen/fragment_padding_medium">
>
<!--
Cosmetic layout to have a better scrolling separation
-->
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/search_mod_overlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:translationZ="10dp"
android:background="@drawable/background_overlay"
android:paddingHorizontal="@dimen/padding_medium"
android:paddingVertical="@dimen/padding_medium"
android:layout_marginTop="@dimen/fragment_padding_medium"
android:layout_marginHorizontal="@dimen/fragment_padding_medium"
app:layout_constraintTop_toTopOf="parent"
>
<!-- Search text -->
<EditText
android:id="@+id/search_mod_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_line"
android:textSize="@dimen/_13ssp"
android:layout_marginBottom="@dimen/padding_large"
android:translationZ="2dp"
android:ems="10"
android:hint="@string/hint_search_modpack"
@ -33,6 +54,7 @@
app:layout_constraintTop_toBottomOf="@+id/search_mod_edittext"
tools:layout_editor_absoluteX="13dp" />
<!-- Version filter -->
<TextView
android:id="@+id/search_mod_mc_version_textview"
android:layout_width="wrap_content"
@ -68,13 +90,22 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/search_mod_selected_mc_version_textview" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/search_mod_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_height="match_parent"
android:paddingTop="@dimen/_65sdp"
android:clipToPadding="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/search_mod_selected_mc_version_textview"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView

View File

@ -76,7 +76,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout="@layout/view_mod_extended"
app:layout_constraintTop_toBottomOf="@id/mod_body_textview"/>
app:layout_constraintTop_toBottomOf="@id/mod_thumbnail_imageview"/>

View File

@ -5,6 +5,7 @@
<color name="minebutton_color">#57CC33</color>
<color name="background_app">#181818</color>
<color name="background_overlay">#464646</color>
<color name="background_status_bar">#242424</color>
<color name="background_bottom_bar">#232323</color>