Make drawer's corners round (#6478)

This commit is contained in:
ebraminio 2023-05-28 12:48:56 +03:30 committed by GitHub
parent 7d1259a39a
commit 10c70dd5f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 4 deletions

View File

@ -1,9 +1,13 @@
package de.danoeh.antennapod.fragment;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
@ -17,12 +21,17 @@ import android.widget.ProgressBar;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.core.graphics.Insets;
import androidx.core.util.Pair;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.shape.MaterialShapeDrawable;
import com.google.android.material.shape.ShapeAppearanceModel;
import org.apache.commons.lang3.StringUtils;
import org.greenrobot.eventbus.EventBus;
@ -55,6 +64,7 @@ import de.danoeh.antennapod.event.UnreadItemsUpdateEvent;
import de.danoeh.antennapod.model.feed.Feed;
import de.danoeh.antennapod.storage.preferences.UserPreferences;
import de.danoeh.antennapod.ui.appstartintent.MainActivityStarter;
import de.danoeh.antennapod.ui.common.ThemeUtils;
import de.danoeh.antennapod.ui.home.HomeFragment;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
@ -94,6 +104,20 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS
@Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View root = inflater.inflate(R.layout.nav_list, container, false);
setupDrawerRoundBackground(root);
ViewCompat.setOnApplyWindowInsetsListener(root, (view, insets) -> {
Insets bars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
view.setPadding(bars.left, bars.top, bars.right, 0);
float navigationBarHeight = 0;
Activity activity = getActivity();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && activity != null) {
navigationBarHeight = getActivity().getWindow().getNavigationBarDividerColor() == Color.TRANSPARENT
? 0 : 1 * getResources().getDisplayMetrics().density; // Assuming the divider is 1dp in height
}
float bottomInset = Math.max(0f, Math.round(bars.bottom - navigationBarHeight));
((ViewGroup.MarginLayoutParams) view.getLayoutParams()).bottomMargin = (int) bottomInset;
return insets;
});
SharedPreferences preferences = getContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
openFolders = new HashSet<>(preferences.getStringSet(PREF_OPEN_FOLDERS, new HashSet<>())); // Must not modify
@ -112,6 +136,23 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS
return root;
}
private void setupDrawerRoundBackground(View root) {
// Akin to this logic:
// https://github.com/material-components/material-components-android/blob/8938da8c/lib/java/com/google/android/material/navigation/NavigationView.java#L405
ShapeAppearanceModel.Builder shapeBuilder = ShapeAppearanceModel.builder();
float cornerSize = getResources().getDimension(R.dimen.drawer_corner_size);
boolean isRtl = getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
if (isRtl) {
shapeBuilder.setTopLeftCornerSize(cornerSize).setBottomLeftCornerSize(cornerSize);
} else {
shapeBuilder.setTopRightCornerSize(cornerSize).setBottomRightCornerSize(cornerSize);
}
MaterialShapeDrawable drawable = new MaterialShapeDrawable(shapeBuilder.build());
int themeColor = ThemeUtils.getColorFromAttr(root.getContext(), android.R.attr.windowBackground);
drawable.setFillColor(ColorStateList.valueOf(themeColor));
root.setBackground(drawable);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

View File

@ -5,15 +5,12 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="?android:attr/windowBackground">
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/nav_settings"
android:layout_width="match_parent"
android:layout_height="@dimen/listitem_iconwithtext_height"
android:fitsSystemWindows="true"
android:layout_alignParentBottom="true"
android:background="?attr/selectableItemBackground"
android:contentDescription="@string/settings_label"

View File

@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="additional_horizontal_spacing">0dp</dimen>
<!-- Should match with @dimen/m3_navigation_drawer_layout_corner_size -->
<dimen name="drawer_corner_size">16dp</dimen>
</resources>