Skip to content

Commit

Permalink
Merge pull request #64 from SlaVcE14/insets
Browse files Browse the repository at this point in the history
Insets
  • Loading branch information
SlaVcE14 authored May 6, 2023
2 parents 924f06d + 45e6b1c commit 6494da5
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 2 deletions.
4 changes: 2 additions & 2 deletions SJDialog/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {

defaultConfig {
minSdk 23
versionCode 20
versionName "1.6"
versionCode 21
versionName "1.6.1 (dev 1)"

buildConfigField 'int', 'VERSION_CODE', "${versionCode}"
buildConfigField 'String', 'VERSION_NAME', "\"${versionName}\""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,22 @@ public BasicDialog setDialogAnimations(int styleRes) {
return this;
}

/**{@inheritDoc}
* @since 1.6.1*/
@Override
public BasicDialog applyInsets(boolean applyInsets) {
super.applyInsets(applyInsets);
return this;
}

/**{@inheritDoc}
* @since 1.6.1*/
@Override
public BasicDialog applyInsets(int insets) {
super.applyInsets(insets);
return this;
}

@Override
protected void setButtons() {
setButton1(R.id.btn1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,22 @@ public CustomViewDialog swipeToDismiss(boolean isSwipeToDismiss) {
return this;
}

/**{@inheritDoc}
* @since 1.6.1*/
@Override
public CustomViewDialog applyInsets(boolean applyInsets) {
super.applyInsets(applyInsets);
return this;
}

/**{@inheritDoc}
* @since 1.6.1*/
@Override
public CustomViewDialog applyInsets(int insets) {
super.applyInsets(insets);
return this;
}

@Override
public void setButton2Visibility(int visibility) {
super.setButton2Visibility(visibility);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,22 @@ public ListDialog swipeToDismiss(boolean isSwipeToDismiss) {
return this;
}

/**{@inheritDoc}
* @since 1.6.1*/
@Override
public ListDialog applyInsets(boolean applyInsets) {
super.applyInsets(applyInsets);
return this;
}

/**{@inheritDoc}
* @since 1.6.1*/
@Override
public ListDialog applyInsets(int insets) {
super.applyInsets(insets);
return this;
}

/**
* Get selected items in a list
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,21 @@ public MessageDialog swipeToDismiss(boolean isSwipeToDismiss) {
super.swipeToDismiss(isSwipeToDismiss);
return this;
}


/**{@inheritDoc}
* @since 1.6.1*/
@Override
public MessageDialog applyInsets(boolean applyInsets) {
super.applyInsets(applyInsets);
return this;
}

/**{@inheritDoc}
* @since 1.6.1*/
@Override
public MessageDialog applyInsets(int insets) {
super.applyInsets(insets);
return this;
}
}
132 changes: 132 additions & 0 deletions SJDialog/src/main/java/com/sjapps/library/customdialog/SJDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import androidx.annotation.LayoutRes;
import androidx.annotation.StringDef;
import androidx.annotation.StyleRes;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.sjapps.library.R;

Expand Down Expand Up @@ -55,6 +58,45 @@ public abstract class SJDialog {
private boolean usesDefaultTheme = true;
private boolean isSwipeToDismiss = true;
private boolean isDefaultOnTouchListener = true;

private boolean isNotDefaultInsets = false;
private boolean setInsets = true;
private boolean leftInsets;
private boolean rightInsets;
private boolean bottomInsets;

/**
* Don't apply any {@link Insets}
* @since 1.6.1
* */
public static final int INSETS_NONE = 0;
/**
* Apply left {@link Insets}
* @since 1.6.1
* */
public static final int INSETS_LEFT = 1;
/**
* Apply right {@link Insets}
* @since 1.6.1
* */
public static final int INSETS_RIGHT = 4;
/**
* Apply bottom {@link Insets}
* @since 1.6.1
* */
public static final int INSETS_BOTTOM = 8;
/**
* Apply horizontal {@link Insets} ({@link #INSETS_LEFT} and {@link #INSETS_RIGHT})
* @since 1.6.1
* */
public static final int INSETS_HORIZONTAL = 5;
/**
* Apply all {@link Insets} ({@link #INSETS_LEFT}, {@link #INSETS_RIGHT} and {@link #INSETS_BOTTOM})
* @since 1.6.1
* */
public static final int INSETS_ALL = 13;


private View.OnTouchListener dialogOnTouchListener = null;

public Dialog dialog;
Expand All @@ -66,6 +108,24 @@ public abstract class SJDialog {
DialogButtonEvent dialogButtonEvent;
DialogButtonEvents dialogButtonEvents;


/**
* values for <b>DialogInsets</b>: {@link #INSETS_LEFT}, {@link #INSETS_RIGHT},
* {@link #INSETS_BOTTOM}, {@link #INSETS_HORIZONTAL}, {@link #INSETS_ALL} and {@link #INSETS_NONE}
*/
@IntDef(flag = true,
value = {
INSETS_LEFT,
INSETS_RIGHT,
INSETS_BOTTOM,
INSETS_HORIZONTAL,
INSETS_ALL,
INSETS_NONE
}
)

public @interface DialogInsets{}

@StringDef({RED_BUTTON, MATERIAL3_RED_BUTTON, OLD_BUTTON_COLOR})
public @interface ButtonColor {
}
Expand Down Expand Up @@ -521,10 +581,82 @@ protected SJDialog dialogWithTwoButtons() {
protected SJDialog show() {
setDialogTouchListener();
addOnClickListener();
if (!isNotDefaultInsets)
applyInsets(true);
if (setInsets)
applyWindowInsets();
dialog.show();
return this;
}

/**
* Apply default {@link Insets}
*
* @param applyInsets If it's true it will apply {@link Insets} to all sides otherwise it will remove all insets. Default value is <b>true</b>
* @return current class
* @since 1.6.1
* */
protected SJDialog applyInsets(boolean applyInsets){
if (applyInsets)
return applyInsets(INSETS_ALL);
return applyInsets(INSETS_NONE);
}

/**
* Apply {@link Insets} to a dialog <br>
* supported values: {@link #INSETS_LEFT}, {@link #INSETS_RIGHT},
* {@link #INSETS_BOTTOM}, {@link #INSETS_HORIZONTAL}, {@link #INSETS_ALL} or {@link #INSETS_NONE}.
* You can combine multiple values with bitwise-OR (<b> | </b>) operator
* @param insets bitwise-OR combination of {@link DialogInsets}
*
* @return current class
* @since 1.6.1
* */
protected SJDialog applyInsets(@DialogInsets int insets){
isNotDefaultInsets = true;

if (insets == INSETS_NONE){
setInsets = false;
return this;
}

setInsets = true;
if ((insets & INSETS_LEFT) == INSETS_LEFT)
leftInsets = true;
if ((insets & INSETS_RIGHT) == INSETS_RIGHT)
rightInsets = true;
if ((insets & INSETS_BOTTOM) == INSETS_BOTTOM)
bottomInsets = true;

return this;
}

private void applyWindowInsets() {

ViewCompat.setOnApplyWindowInsetsListener(dialog.getWindow().getDecorView(),(v, insets) -> {
Insets insetsSB = insets.getInsets(WindowInsetsCompat.Type.systemBars());
Insets insetsDC = insets.getInsets(WindowInsetsCompat.Type.displayCutout());

Insets insetsFinal = Insets.of(
insetsSB.left + insetsDC.left,
0,
insetsSB.right + insetsDC.right,
insetsSB.bottom + insetsDC.bottom
);

v.setPadding(
leftInsets ? insetsFinal.left : 0,
0,
rightInsets ? insetsFinal.right : 0,
bottomInsets ? insetsFinal.bottom : 0
);

return WindowInsetsCompat.CONSUMED;
});
}



private void addOnClickListener() {
if (dialogButtonEvents == null && dialogButtonEvent == null)
return;
Expand Down
5 changes: 5 additions & 0 deletions SJDialog/src/main/res/values-night/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
<item name="materialButtonStyle">@style/SJDialog.ButtonStyle</item>
</style>

<style name="Theme.SJDialog.NoInsets" parent="Theme.SJDialog">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>



</resources>
4 changes: 4 additions & 0 deletions SJDialog/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@
<item name="materialButtonStyle">@style/SJDialog.ButtonStyle</item>
</style>

<style name="Theme.SJDialog.NoInsets" parent="Theme.SJDialog">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
</resources>

0 comments on commit 6494da5

Please sign in to comment.