Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Dec 1, 2024
1 parent e389185 commit 06d247b
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 43 deletions.
30 changes: 12 additions & 18 deletions app/src/main/java/io/legado/app/base/BaseDialogFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import io.legado.app.R
import io.legado.app.constant.AppLog
import io.legado.app.help.config.AppConfig
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.lib.theme.ThemeStore
import io.legado.app.utils.dpToPx
import io.legado.app.utils.setBackgroundKeepPadding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlin.coroutines.CoroutineContext
Expand All @@ -40,31 +40,25 @@ abstract class BaseDialogFragment(
super.onStart()
if (adaptationSoftKeyboard) {
dialog?.window?.setBackgroundDrawableResource(R.color.transparent)
}
if (AppConfig.isEInkMode) {
} else if (AppConfig.isEInkMode) {
dialog?.window?.let {
it.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
val attr = it.attributes
attr.dimAmount = 0.0f
attr.windowAnimations = 0
it.attributes = attr
it.setBackgroundDrawableResource(R.color.transparent)
it.decorView.setBackgroundKeepPadding(R.color.transparent)
}
// 修改gravity的时机一般在子类的onStart方法中, 因此需要在onStart之后执行.
this.lifecycle.addObserver(object : LifecycleEventObserver {
override fun onStateChanged(
source: LifecycleOwner,
event: Lifecycle.Event
) {
if (event == Lifecycle.Event.ON_START) {
when (dialog?.window?.attributes?.gravity) {
Gravity.TOP -> view?.setBackgroundResource(R.drawable.bg_eink_border_bottom)
Gravity.BOTTOM -> view?.setBackgroundResource(R.drawable.bg_eink_border_top)
else -> {
val padding = 2.dpToPx();
view?.setPadding(padding, padding, padding, padding)
view?.setBackgroundResource(R.drawable.bg_eink_border_dialog)
}
lifecycle.addObserver(LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_START) {
when (dialog?.window?.attributes?.gravity) {
Gravity.TOP -> view?.setBackgroundResource(R.drawable.bg_eink_border_bottom)
Gravity.BOTTOM -> view?.setBackgroundResource(R.drawable.bg_eink_border_top)
else -> {
val padding = 2.dpToPx();
view?.setPadding(padding, padding, padding, padding)
view?.setBackgroundResource(R.drawable.bg_eink_border_dialog)
}
}
}
Expand Down
24 changes: 9 additions & 15 deletions app/src/main/java/io/legado/app/base/BasePrefDialogFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import io.legado.app.R
import io.legado.app.help.config.AppConfig
import io.legado.app.utils.dpToPx
Expand All @@ -27,20 +26,15 @@ abstract class BasePrefDialogFragment(
}

// 修改gravity的时机一般在子类的onStart方法中, 因此需要在onStart之后执行.
this.lifecycle.addObserver(object : LifecycleEventObserver {
override fun onStateChanged(
source: LifecycleOwner,
event: Lifecycle.Event
) {
if (event == Lifecycle.Event.ON_START) {
when (dialog?.window?.attributes?.gravity) {
Gravity.TOP -> view?.setBackgroundResource(R.drawable.bg_eink_border_bottom)
Gravity.BOTTOM -> view?.setBackgroundResource(R.drawable.bg_eink_border_top)
else -> {
val padding = 2.dpToPx();
view?.setPadding(padding, padding, padding, padding)
view?.setBackgroundResource(R.drawable.bg_eink_border_dialog)
}
lifecycle.addObserver(LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_START) {
when (dialog?.window?.attributes?.gravity) {
Gravity.TOP -> view?.setBackgroundResource(R.drawable.bg_eink_border_bottom)
Gravity.BOTTOM -> view?.setBackgroundResource(R.drawable.bg_eink_border_top)
else -> {
val padding = 2.dpToPx();
view?.setPadding(padding, padding, padding, padding)
view?.setBackgroundResource(R.drawable.bg_eink_border_dialog)
}
}
}
Expand Down
20 changes: 15 additions & 5 deletions app/src/main/java/io/legado/app/lib/dialogs/AndroidAlertBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import android.graphics.drawable.Drawable
import android.view.KeyEvent
import android.view.View
import androidx.appcompat.app.AlertDialog
import io.legado.app.utils.applyTint
import io.legado.app.R
import io.legado.app.help.config.AppConfig
import io.legado.app.utils.applyTint

internal class AndroidAlertBuilder(override val ctx: Context) : AlertBuilder<AlertDialog> {
private val builder = AlertDialog.Builder(ctx)
Expand Down Expand Up @@ -144,17 +144,27 @@ internal class AndroidAlertBuilder(override val ctx: Context) : AlertBuilder<Ale
override fun build(): AlertDialog {
var dialog = builder.create()
if (AppConfig.isEInkMode) {
dialog.window?.setDimAmount(0f)
dialog.window?.decorView?.setBackgroundResource(R.drawable.bg_eink_border_dialog)
dialog.window?.run {
val attr = attributes
attr.dimAmount = 0f
attr.windowAnimations = 0
attributes = attr
setBackgroundDrawableResource(R.drawable.bg_eink_border_dialog)
}
}
return dialog
}

override fun show(): AlertDialog {
var dialog = builder.show().applyTint()
if (AppConfig.isEInkMode) {
dialog.window?.setDimAmount(0f)
dialog.window?.decorView?.setBackgroundResource(R.drawable.bg_eink_border_dialog)
dialog.window?.run {
val attr = attributes
attr.dimAmount = 0f
attr.windowAnimations = 0
attributes = attr
setBackgroundDrawableResource(R.drawable.bg_eink_border_dialog)
}
}
return dialog
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import io.legado.app.data.entities.SearchBook
import io.legado.app.databinding.DialogBookChangeSourceBinding
import io.legado.app.help.config.AppConfig
import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.theme.elevation
import io.legado.app.lib.theme.getPrimaryTextColor
import io.legado.app.lib.theme.primaryColor
import io.legado.app.ui.book.read.ReadBookActivity
Expand Down Expand Up @@ -123,7 +124,7 @@ class ChangeBookSourceDialog() : BaseDialogFragment(R.layout.dialog_book_change_
binding.toolBar.subtitle = viewModel.author
binding.toolBar.navigationIcon =
getCompatDrawable(androidx.appcompat.R.drawable.abc_ic_ab_back_material)
binding.toolBar.elevation = AppConfig.elevation.toFloat()
binding.toolBar.elevation = requireContext().elevation
}

private fun initMenu() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import io.legado.app.help.config.ReadBookConfig
import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.dialogs.selector
import io.legado.app.lib.theme.ThemeStore
import io.legado.app.lib.theme.backgroundColor
import io.legado.app.lib.theme.bottomBackground
import io.legado.app.model.CacheBook
import io.legado.app.model.ReadBook
Expand Down Expand Up @@ -320,7 +319,6 @@ abstract class BaseReadBookActivity :
ReadBook.book?.let { book ->
alert(titleResource = R.string.simulated_reading) {
val alertBinding = DialogSimulatedReadingBinding.inflate(layoutInflater).apply {
root.setBackgroundColor(root.context.backgroundColor)
srEnabled.isChecked = book.getReadSimulating()
editStart.setText(book.getStartChapter().toString())
editNum.setText(book.getDailyChapters().toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TipConfigDialog : BaseDialogFragment(R.layout.dialog_tip_config) {

override fun onStart() {
super.onStart()
setLayout(0.9f, ViewGroup.LayoutParams.WRAP_CONTENT)
setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
}

override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/io/legado/app/ui/widget/TitleBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.core.view.WindowInsetsCompat
import androidx.core.view.children
import com.google.android.material.appbar.AppBarLayout
import io.legado.app.R
import io.legado.app.help.config.AppConfig
import io.legado.app.lib.theme.elevation
import io.legado.app.lib.theme.primaryColor
import io.legado.app.utils.activity
Expand Down Expand Up @@ -176,7 +177,11 @@ class TitleBar @JvmOverloads constructor(
}
}

setBackgroundColor(context.primaryColor)
if (AppConfig.isEInkMode) {
setBackgroundResource(R.drawable.bg_eink_border_bottom)
} else {
setBackgroundColor(context.primaryColor)
}

stateListAnimator = null
elevation = context.elevation
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/io/legado/app/utils/ViewExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import android.widget.RadioGroup
import android.widget.SeekBar
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.view.menu.MenuPopupHelper
import androidx.appcompat.widget.PopupMenu
Expand Down Expand Up @@ -277,3 +278,12 @@ fun View.applyNavigationBarPadding(withInitialPadding: Boolean = false) {
windowInsets
}
}

fun View.setBackgroundKeepPadding(@DrawableRes backgroundResId: Int) {
val paddingLeft = paddingLeft
val paddingTop = paddingTop
val paddingRight = paddingRight
val paddingBottom = paddingBottom
setBackgroundResource(backgroundResId)
setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom)
}

0 comments on commit 06d247b

Please sign in to comment.