Skip to content

Commit

Permalink
增加预览注解#8
Browse files Browse the repository at this point in the history
  • Loading branch information
Halifax authored and Halifax committed Nov 18, 2024
1 parent 5ae2723 commit fbd1ec1
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Android CI
on:
push:
branches:
- 'v**'
- 'main'
- '!master'
- '!dev_**'

Expand Down
10 changes: 5 additions & 5 deletions any_pop_dialog_library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ afterEvaluate {

android {
namespace = "com.melody.dialog.any_pop"
compileSdk = 33
compileSdk = 35

defaultConfig {
minSdk = 21
Expand Down Expand Up @@ -49,10 +49,10 @@ android {
}

dependencies {
implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.activity:activity-compose:1.7.2")
implementation("com.google.android.material:material:1.8.0")
implementation(platform("androidx.compose:compose-bom:2023.03.00"))
implementation("androidx.core:core-ktx:1.13.1")
implementation("androidx.activity:activity-compose:1.8.2")
implementation("com.google.android.material:material:1.12.0")
implementation(platform("androidx.compose:compose-bom:2024.10.01"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.material3:material3")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import androidx.compose.ui.window.DialogWindowProvider
import androidx.compose.ui.window.SecureFlagPolicy
import androidx.core.view.WindowCompat
import kotlinx.coroutines.delay
import kotlin.math.roundToInt

@Composable
private fun getDialogWindow(): Window? = (LocalView.current.parent as? DialogWindowProvider)?.window
Expand All @@ -56,6 +59,16 @@ private tailrec fun Context.getActivityWindow(): Window? = when (this) {
else -> null
}

private fun Context.getDisplayWidth(): Int{
val density = resources.displayMetrics.density
return (resources.configuration.screenWidthDp * density).roundToInt()
}

private fun Context.getDisplayHeight(): Int{
val density = resources.displayMetrics.density
return (resources.configuration.screenHeightDp * density).roundToInt()
}

private const val DefaultDurationMillis: Int = 250

@Composable
Expand All @@ -65,6 +78,7 @@ private fun DialogFullScreen(
properties: AnyPopDialogProperties,
content: @Composable () -> Unit
) {
val isPreview = LocalInspectionMode.current
var isAnimateLayout by remember {
mutableStateOf(false)
}
Expand Down Expand Up @@ -111,31 +125,37 @@ private fun DialogFullScreen(
}
}
val activityWindow = getActivityWindow()

val dialogWindow = getDialogWindow()
val parentView = LocalView.current.parent as View
// 处理预览模式,宽高问题
val displayWidth = activityWindow?.decorView?.width ?: LocalContext.current.getDisplayWidth()
val displayHeight = activityWindow?.decorView?.height ?: LocalContext.current.getDisplayHeight()
SideEffect {
if (activityWindow == null || dialogWindow == null || isBackPress || isAnimateLayout)
if (
(isPreview && (isBackPress || isAnimateLayout)) ||
(!isPreview && (activityWindow == null || dialogWindow == null || isBackPress || isAnimateLayout))
) {
return@SideEffect
}
if(dialogWindow != null) {
val attributes = WindowManager.LayoutParams()
if (activityWindow != null) { // 判空忽略预览模式
attributes.copyFrom(activityWindow.attributes)
}
attributes.type = dialogWindow.attributes.type
dialogWindow.attributes = attributes
// 修复Android10 - Android11出现背景全黑的情况
dialogWindow.setBackgroundDrawableResource(android.R.color.transparent)
// 禁止Dialog跟随软键盘高度变化,用Compose提供的imePadding替代
dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING)

val attributes = WindowManager.LayoutParams()
attributes.copyFrom(activityWindow.attributes)
attributes.type = dialogWindow.attributes.type
dialogWindow.attributes = attributes
// 修复Android10 - Android11出现背景全黑的情况
dialogWindow.setBackgroundDrawableResource(android.R.color.transparent)
// 禁止Dialog跟随软键盘高度变化,用Compose提供的imePadding替代
dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING)
dialogWindow.setLayout(displayWidth, displayHeight)
dialogWindow.statusBarColor = Color.Transparent.toArgb()
dialogWindow.navigationBarColor = Color.Transparent.toArgb()

dialogWindow.setLayout(
activityWindow.decorView.width,
activityWindow.decorView.height
)
dialogWindow.statusBarColor = Color.Transparent.toArgb()
dialogWindow.navigationBarColor = Color.Transparent.toArgb()

WindowCompat.getInsetsController(dialogWindow, parentView)
.isAppearanceLightNavigationBars = properties.isAppearanceLightNavigationBars
WindowCompat.getInsetsController(dialogWindow, parentView)
.isAppearanceLightNavigationBars = properties.isAppearanceLightNavigationBars
}
isAnimateLayout = true
}
Box(
Expand All @@ -158,7 +178,8 @@ private fun DialogFullScreen(
)
AnimatedVisibility(
modifier = Modifier.pointerInput(Unit) {},
visible = isAnimateLayout,
// 预览还想直接显示UI的情况,配置其他方向需要点击:Start interactive mode这个手势按钮
visible = if(isPreview && properties.direction == DirectionState.NONE) true else isAnimateLayout,
enter = when (properties.direction) {
DirectionState.TOP -> slideInVertically(initialOffsetY = { -it })
DirectionState.LEFT -> slideInHorizontally(initialOffsetX = { -it })
Expand Down
18 changes: 9 additions & 9 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ plugins {

android {
namespace = "com.example.myapplication"
compileSdk = 33
compileSdk = 35

defaultConfig {
applicationId = "com.example.myapplication"
minSdk = 21
targetSdk = 33
versionCode = 102
versionName = "1.0.2"
targetSdk = 35
versionCode = 104
versionName = "1.0.4"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down Expand Up @@ -51,10 +51,10 @@ android {

dependencies {

implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
implementation("androidx.activity:activity-compose:1.7.2")
implementation(platform("androidx.compose:compose-bom:2023.03.00"))
implementation("androidx.core:core-ktx:1.13.1")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.3")
implementation("androidx.activity:activity-compose:1.8.2")
implementation(platform("androidx.compose:compose-bom:2024.10.01"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
Expand All @@ -67,7 +67,7 @@ dependencies {
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
androidTestImplementation(platform("androidx.compose:compose-bom:2024.10.01"))
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
Expand Down
36 changes: 33 additions & 3 deletions app/src/main/java/com/example/myapplication/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.example.myapplication

import android.content.res.Configuration
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
Expand All @@ -17,13 +19,19 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.Wallpapers
import androidx.compose.ui.unit.dp
import com.example.myapplication.ui.theme.MyApplicationTheme
import com.example.myapplication.widget.LRListDialog
import com.example.myapplication.widget.LRListSB1Dialog
import com.example.myapplication.widget.NoNetWorkHelpDialog
import com.example.myapplication.widget.NoNetWorkHelpNavBarDialog1
import com.example.myapplication.widget.TopToastDialog
import com.melody.dialog.any_pop.AnyPopDialog
import com.melody.dialog.any_pop.AnyPopDialogProperties
import com.melody.dialog.any_pop.DirectionState

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -85,10 +93,32 @@ fun TestContent(modifier: Modifier = Modifier) {
}
}

@Preview(showBackground = true)
@Preview(
showBackground = true,
device = "id:Nexus 5"
)
@Composable
fun GreetingPreview() {
MyApplicationTheme {
TestContent()
TestContent(modifier = Modifier.fillMaxSize())
}
}
}

@Preview(
showBackground = true,
device = "id:Nexus 5",
wallpaper = Wallpapers.YELLOW_DOMINATED_EXAMPLE,
uiMode = Configuration.UI_MODE_NIGHT_YES or Configuration.UI_MODE_TYPE_NORMAL
)
@Composable
fun PreviewAnyPopDialog() {
MyApplicationTheme(dynamicColor = true, darkTheme = false) {
AnyPopDialog(
onDismiss = {},
properties = AnyPopDialogProperties(direction = DirectionState.NONE),
content = {
Text("我是预览模式下的内容", color = Color.White, modifier = Modifier.padding(16.dp))
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.material3.Text
import androidx.compose.material3.ripple
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand Down Expand Up @@ -58,7 +59,7 @@ internal fun NoNetWorkHelpDialog(showDialog: Boolean, onDismiss: () -> Unit) {
content = {
NetSettingContent(
modifier = Modifier
.padding(start = 20.dp, end = 20.dp, bottom = 20.dp)
.padding(20.dp)
.fillMaxWidth()
.wrapContentHeight(),
onDismiss = {
Expand Down Expand Up @@ -131,7 +132,7 @@ private fun TitleAndClose(onDismiss: () -> Unit) {
.align(Alignment.TopEnd)
.clip(CircleShape)
.clickable(
indication = rememberRipple(),
indication = ripple(),
interactionSource = remember { MutableInteractionSource() },
onClick = currentOnDismiss
)
Expand Down Expand Up @@ -166,7 +167,7 @@ private fun ColumnScope.RoundCornerOkButton(onDismiss: () -> Unit) {
)
.clip(RoundedCornerShape(23.dp))
.clickable(
indication = rememberRipple(),
indication = ripple(),
interactionSource = remember { MutableInteractionSource() },
onClick = currentOnDismiss
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.material3.Text
import androidx.compose.material3.ripple
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -36,7 +37,7 @@ internal fun TopToastDialog(showDialog: Boolean, onDismiss: () -> Unit) {
.requiredHeightIn(max = 160.dp, min = 80.dp)
.background(color = colorResource(id = R.color.dialog_toast_background))
.clickable(
indication = rememberRipple(),
indication = ripple(),
interactionSource = remember { MutableInteractionSource() },
onClick = {
isActiveClose = true
Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ android.nonTransitiveRClass=true
GROUP_ID = io.github.TheMelody
LIB_ARTIFACT_ID = any_pop_dialog_compose

COMPILE_SDK_VERSION = 33
TARGET_SDK_VERSION = 33
COMPILE_SDK_VERSION = 35
TARGET_SDK_VERSION = 35
MIN_SDK_VERSION = 21

LIB_VERSION_NAME = 1.0.3
LIB_VERSION_NAME = 1.0.4

0 comments on commit fbd1ec1

Please sign in to comment.