[Android] Rewring insets handling logic for Downloader screen

Signed-off-by: Dzmitry Strekha <mr.choo96@gmail.com>
This commit is contained in:
Dzmitry Strekha 2024-10-05 22:07:05 +03:00 committed by Viktor Havaka
parent 558e0a05bd
commit ff61cebaf7
3 changed files with 102 additions and 37 deletions

View file

@ -1,15 +1,8 @@
package app.organicmaps.downloader;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.core.graphics.Insets;
import androidx.core.view.OnApplyWindowInsetsListener;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import app.organicmaps.util.WindowInsetUtils;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import app.organicmaps.R;
import app.organicmaps.util.StringUtils;
@ -62,36 +55,6 @@ class BottomPanel
});
mButton = frame.findViewById(R.id.action);
ViewCompat.setOnApplyWindowInsetsListener(frame, new OnApplyWindowInsetsListener()
{
@NonNull
@Override
public WindowInsetsCompat onApplyWindowInsets(@NonNull View v, @NonNull WindowInsetsCompat insets)
{
Insets safeInsets = insets.getInsets(WindowInsetUtils.TYPE_SAFE_DRAWING);
int baseMargin = UiUtils.dimen(v.getContext(), R.dimen.margin_base);
ViewGroup.MarginLayoutParams fabParams = (ViewGroup.MarginLayoutParams) mFab.getLayoutParams();
ViewGroup.MarginLayoutParams buttonParams = (ViewGroup.MarginLayoutParams) mButton.getLayoutParams();
final boolean isButtonVisible = UiUtils.isVisible(mButton);
buttonParams.bottomMargin = safeInsets.bottom;
mButton.setPadding(safeInsets.left, mButton.getPaddingTop(), safeInsets.right, mButton.getPaddingBottom());
fabParams.rightMargin = safeInsets.right + baseMargin;
if (isButtonVisible)
fabParams.bottomMargin = baseMargin;
else
fabParams.bottomMargin = safeInsets.bottom + baseMargin;
mFab.requestLayout();
mButton.requestLayout();
return insets;
}
});
}
private void setUpdateAllState(UpdateInfo info)
@ -161,5 +124,7 @@ class BottomPanel
}
UiUtils.showIf(show, mButton);
mFragment.requireView().requestApplyInsets();
}
}

View file

@ -9,6 +9,7 @@ import androidx.annotation.CallSuper;
import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.RecyclerView;
import app.organicmaps.R;
@ -125,6 +126,9 @@ public class DownloaderFragment extends BaseMwmRecyclerFragment<DownloaderAdapte
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
ViewCompat.setOnApplyWindowInsetsListener(view, new DownloaderInsetsListener(view));
mSubscriberSlot = MapManager.nativeSubscribe(new MapManager.StorageCallback()
{
@Override

View file

@ -0,0 +1,96 @@
package app.organicmaps.downloader;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.core.graphics.Insets;
import androidx.core.view.OnApplyWindowInsetsListener;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.RecyclerView;
import app.organicmaps.R;
import app.organicmaps.util.UiUtils;
import app.organicmaps.util.WindowInsetUtils;
final class DownloaderInsetsListener implements OnApplyWindowInsetsListener
{
@NonNull
private final Context mContext;
@NonNull
private final View mToolbar;
@NonNull
private final View mFab;
@NonNull
private final View mButton;
@NonNull
private final RecyclerView mRecyclerView;
DownloaderInsetsListener(@NonNull View fragmentView)
{
mContext = fragmentView.getContext();
mToolbar = fragmentView.findViewById(R.id.toolbar);
mFab = fragmentView.findViewById(R.id.fab);
mButton = fragmentView.findViewById(R.id.action);
mRecyclerView = fragmentView.findViewById(R.id.recycler);
mRecyclerView.setClipToPadding(false);
}
@NonNull
@Override
public WindowInsetsCompat onApplyWindowInsets(@NonNull View v, @NonNull WindowInsetsCompat insets)
{
final Insets safeInsets = insets.getInsets(WindowInsetUtils.TYPE_SAFE_DRAWING);
mToolbar.setPadding(safeInsets.left, safeInsets.top, safeInsets.right, mToolbar.getPaddingBottom());
boolean isAnyButtonVisible = UiUtils.isVisible(mFab) || UiUtils.isVisible(mButton);
if (isAnyButtonVisible)
applyInsetsToButtons(safeInsets);
applyInsetsToRecyclerView(safeInsets, isAnyButtonVisible);
// have to consume all insets here, so child views won't handle them again
return WindowInsetsCompat.CONSUMED;
}
private void applyInsetsToButtons(Insets insets)
{
int baseMargin = UiUtils.dimen(mContext, R.dimen.margin_base);
ViewGroup.MarginLayoutParams fabParams = (ViewGroup.MarginLayoutParams) mFab.getLayoutParams();
ViewGroup.MarginLayoutParams buttonParams = (ViewGroup.MarginLayoutParams) mButton.getLayoutParams();
final boolean isButtonVisible = UiUtils.isVisible(mButton);
buttonParams.bottomMargin = insets.bottom;
mButton.setPadding(insets.left, mButton.getPaddingTop(), insets.right, mButton.getPaddingBottom());
fabParams.rightMargin = insets.right + baseMargin;
if (isButtonVisible)
fabParams.bottomMargin = baseMargin;
else
fabParams.bottomMargin = insets.bottom + baseMargin;
mFab.requestLayout();
mButton.requestLayout();
}
private void applyInsetsToRecyclerView(Insets insets, boolean isAnyButtonVisible)
{
int bottomInset = isAnyButtonVisible ? 0 : insets.bottom;
mRecyclerView.setPadding(
mRecyclerView.getPaddingLeft(),
mRecyclerView.getPaddingTop(),
mRecyclerView.getPaddingRight(),
bottomInset);
final ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) mRecyclerView.getLayoutParams();
layoutParams.rightMargin = insets.right;
layoutParams.leftMargin = insets.left;
mRecyclerView.requestLayout();
}
}