pull/32/head
Administrator 5 years ago
parent 87f3bb9767
commit 9dfce08b58
  1. 41
      app/src/main/java/io/legado/app/lib/dialogs/AndroidDialogs.kt
  2. 19
      app/src/main/java/io/legado/app/lib/dialogs/AndroidSelectors.kt
  3. 20
      app/src/main/java/io/legado/app/lib/dialogs/Dialogs.kt
  4. 9
      app/src/main/java/io/legado/app/lib/dialogs/Selectors.kt
  5. 68
      app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt
  6. 16
      app/src/main/java/io/legado/app/ui/widget/TitleBar.kt
  7. 31
      app/src/main/java/io/legado/app/utils/ViewExtensions.kt

@ -25,12 +25,6 @@ import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import org.jetbrains.anko.AnkoContext
inline fun AnkoContext<*>.alert(
title: CharSequence? = null,
message: CharSequence? = null,
noinline init: (AlertBuilder<DialogInterface>.() -> Unit)? = null
) = ctx.alert(title, message, init)
inline fun Fragment.alert(
title: CharSequence? = null,
message: CharSequence? = null,
@ -53,12 +47,6 @@ fun Context.alert(
}
}
inline fun AnkoContext<*>.alert(
titleResource: Int? = null,
messageResource: Int? = null,
noinline init: (AlertBuilder<DialogInterface>.() -> Unit)? = null
) = ctx.alert(titleResource, messageResource, init)
inline fun Fragment.alert(
title: Int? = null,
message: Int? = null,
@ -83,17 +71,11 @@ fun Context.alert(
inline fun AnkoContext<*>.alert(noinline init: AlertBuilder<DialogInterface>.() -> Unit) = ctx.alert(init)
inline fun Fragment.alert(noinline init: AlertBuilder<DialogInterface>.() -> Unit) = activity?.alert(init)
inline fun Fragment.alert(noinline init: AlertBuilder<DialogInterface>.() -> Unit) = requireContext().alert(init)
fun Context.alert(init: AlertBuilder<DialogInterface>.() -> Unit): AlertBuilder<DialogInterface> =
fun Context.alert(init: AlertBuilder<AlertDialog>.() -> Unit): AlertBuilder<AlertDialog> =
AndroidAlertBuilder(this).apply { init() }
inline fun AnkoContext<*>.progressDialog(
title: Int? = null,
message: Int? = null,
noinline init: (ProgressDialog.() -> Unit)? = null
) = ctx.progressDialog(title, message, init)
inline fun Fragment.progressDialog(
title: Int? = null,
message: Int? = null,
@ -107,12 +89,6 @@ fun Context.progressDialog(
) = progressDialog(title?.let { getString(it) }, message?.let { getString(it) }, false, init)
inline fun AnkoContext<*>.indeterminateProgressDialog(
title: Int? = null,
message: Int? = null,
noinline init: (ProgressDialog.() -> Unit)? = null
) = ctx.indeterminateProgressDialog(title, message, init)
inline fun Fragment.indeterminateProgressDialog(
title: Int? = null,
message: Int? = null,
@ -125,13 +101,6 @@ fun Context.indeterminateProgressDialog(
init: (ProgressDialog.() -> Unit)? = null
) = progressDialog(title?.let { getString(it) }, message?.let { getString(it) }, true, init)
inline fun AnkoContext<*>.progressDialog(
title: CharSequence? = null,
message: CharSequence? = null,
noinline init: (ProgressDialog.() -> Unit)? = null
) = ctx.progressDialog(title, message, init)
inline fun Fragment.progressDialog(
title: CharSequence? = null,
message: CharSequence? = null,
@ -145,12 +114,6 @@ fun Context.progressDialog(
) = progressDialog(title, message, false, init)
inline fun AnkoContext<*>.indeterminateProgressDialog(
title: CharSequence? = null,
message: CharSequence? = null,
noinline init: (ProgressDialog.() -> Unit)? = null
) = ctx.indeterminateProgressDialog(title, message, init)
inline fun Fragment.indeterminateProgressDialog(
title: CharSequence? = null,
message: CharSequence? = null,

@ -21,24 +21,17 @@ package io.legado.app.lib.dialogs
import android.content.Context
import android.content.DialogInterface
import androidx.fragment.app.Fragment
import org.jetbrains.anko.AnkoContext
inline fun AnkoContext<*>.selector(
title: CharSequence? = null,
items: List<CharSequence>,
noinline onClick: (DialogInterface, Int) -> Unit
) = ctx.selector(title, items, onClick)
inline fun Fragment.selector(
title: CharSequence? = null,
items: List<CharSequence>,
noinline onClick: (DialogInterface, Int) -> Unit
title: CharSequence? = null,
items: List<CharSequence>,
noinline onClick: (DialogInterface, Int) -> Unit
) = activity?.selector(title, items, onClick)
fun Context.selector(
title: CharSequence? = null,
items: List<CharSequence>,
onClick: (DialogInterface, Int) -> Unit
title: CharSequence? = null,
items: List<CharSequence>,
onClick: (DialogInterface, Int) -> Unit
) {
with(AndroidAlertBuilder(this)) {
if (title != null) {

@ -21,17 +21,9 @@ package io.legado.app.lib.dialogs
import android.content.Context
import android.content.DialogInterface
import androidx.fragment.app.Fragment
import org.jetbrains.anko.AnkoContext
typealias AlertBuilderFactory<D> = (Context) -> AlertBuilder<D>
inline fun <D : DialogInterface> AnkoContext<*>.alert(
noinline factory: AlertBuilderFactory<D>,
title: String? = null,
message: String? = null,
noinline init: (AlertBuilder<D>.() -> Unit)? = null
) = ctx.alert(factory, title, message, init)
inline fun <D : DialogInterface> Fragment.alert(
noinline factory: AlertBuilderFactory<D>,
title: String? = null,
@ -56,13 +48,6 @@ fun <D : DialogInterface> Context.alert(
}
}
inline fun <D : DialogInterface> AnkoContext<*>.alert(
noinline factory: AlertBuilderFactory<D>,
titleResource: Int? = null,
messageResource: Int? = null,
noinline init: (AlertBuilder<D>.() -> Unit)? = null
) = ctx.alert(factory, titleResource, messageResource, init)
inline fun <D : DialogInterface> Fragment.alert(
noinline factory: AlertBuilderFactory<D>,
titleResource: Int? = null,
@ -87,11 +72,6 @@ fun <D : DialogInterface> Context.alert(
}
}
inline fun <D : DialogInterface> AnkoContext<*>.alert(
noinline factory: AlertBuilderFactory<D>,
noinline init: AlertBuilder<D>.() -> Unit
) = ctx.alert(factory, init)
inline fun <D : DialogInterface> Fragment.alert(
noinline factory: AlertBuilderFactory<D>,
noinline init: AlertBuilder<D>.() -> Unit

@ -15,19 +15,12 @@
*/
@file:Suppress("NOTHING_TO_INLINE", "unused")
package io.legado.app.lib.dialogs
import android.content.Context
import android.content.DialogInterface
import androidx.fragment.app.Fragment
import org.jetbrains.anko.AnkoContext
inline fun <D : DialogInterface> AnkoContext<*>.selector(
noinline factory: AlertBuilderFactory<D>,
title: CharSequence? = null,
items: List<CharSequence>,
noinline onClick: (DialogInterface, CharSequence, Int) -> Unit
) = ctx.selector(factory, title, items, onClick)
inline fun <D : DialogInterface> Fragment.selector(
noinline factory: AlertBuilderFactory<D>,

@ -10,11 +10,12 @@ import androidx.preference.PreferenceFragmentCompat
import io.legado.app.App
import io.legado.app.R
import io.legado.app.constant.Bus
import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.dialogs.noButton
import io.legado.app.lib.dialogs.yesButton
import io.legado.app.lib.theme.ATH
import io.legado.app.lib.theme.ColorUtils
import io.legado.app.utils.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener {
@ -46,40 +47,42 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar
}
"colorPrimary", "colorAccent", "colorBackground" -> {
if (backgroundIsDark(sharedPreferences)) {
activity?.let {
AlertDialog.Builder(it)
.setTitle("白天背景太暗")
.setMessage("将会恢复默认背景?")
.setPositiveButton(R.string.ok) { _, _ ->
putPrefInt(
"colorBackground",
getCompatColor(R.color.md_grey_100)
)
upTheme(false)
}
.setNegativeButton(R.string.cancel) { _, _ -> upTheme(false) }
.show().applyTint()
}
alert {
title = "白天背景太暗"
message = "将会恢复默认背景?"
yesButton {
putPrefInt(
"colorBackground",
getCompatColor(R.color.md_grey_100)
)
upTheme(false)
}
noButton {
upTheme(false)
}
}.show().applyTint()
} else {
upTheme(false)
}
}
"colorPrimaryNight", "colorAccentNight", "colorBackgroundNight" -> {
if (backgroundIsLight(sharedPreferences)) {
activity?.let {
AlertDialog.Builder(it)
.setTitle("夜间背景太亮")
.setMessage("将会恢复默认背景?")
.setPositiveButton(R.string.ok) { _, _ ->
putPrefInt(
"colorBackgroundNight",
getCompatColor(R.color.md_grey_800)
)
upTheme(true)
}
.setNegativeButton(R.string.cancel) { _, _ -> upTheme(true) }
.show().applyTint()
}
alert {
title = "夜间背景太亮"
message = "将会恢复默认背景?"
yesButton {
putPrefInt(
"colorBackgroundNight",
getCompatColor(R.color.md_grey_800)
)
upTheme(true)
}
noButton {
upTheme(true)
}
}.show().applyTint()
} else {
upTheme(true)
}
@ -142,10 +145,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar
private fun recreateActivities() {
postEvent(Bus.RECREATE, "")
runBlocking {
delay(100L)
activity?.recreate()
}
Handler().postDelayed({ activity?.recreate() }, 100L)
}
}

@ -18,6 +18,7 @@ import io.legado.app.R
import io.legado.app.lib.theme.DrawableUtils
import io.legado.app.lib.theme.primaryColor
import io.legado.app.lib.theme.primaryTextColor
import io.legado.app.utils.activity
import io.legado.app.utils.getNavigationBarHeight
import io.legado.app.utils.getStatusBarHeight
import org.jetbrains.anko.backgroundColor
@ -143,7 +144,7 @@ class TitleBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, a
override fun onAttachedToWindow() {
super.onAttachedToWindow()
if (attachToActivity) {
attachToActivity(context)
attachToActivity()
}
wrapAppTheme()
}
@ -188,24 +189,13 @@ class TitleBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, a
}
}
private fun attachToActivity(context: Context) {
val activity = getCompatActivity(context)
private fun attachToActivity() {
activity?.let {
it.setSupportActionBar(toolbar)
it.supportActionBar?.setDisplayHomeAsUpEnabled(displayHomeAsUp)
}
}
private fun getCompatActivity(context: Context?): AppCompatActivity? {
if (context == null) return null
return when (context) {
is AppCompatActivity -> context
is androidx.appcompat.view.ContextThemeWrapper -> getCompatActivity(context.baseContext)
is android.view.ContextThemeWrapper -> getCompatActivity(context.baseContext)
else -> null
}
}
private fun wrapDrawableTint(drawable: Drawable?, tintList: ColorStateList?, tintMode: Int) {
if (drawable == null || tintList == null) return
val wrappedDrawable = DrawableCompat.wrap(drawable.mutate())

@ -1,11 +1,32 @@
package io.legado.app.utils
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Rect
import android.os.Build
import android.os.Handler
import android.view.PixelCopy
import android.view.View
import android.view.View.*
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AppCompatActivity
import io.legado.app.App
import android.opengl.ETC1.getHeight
import android.opengl.ETC1.getWidth
private tailrec fun getCompatActivity(context: Context?): AppCompatActivity? {
return when (context) {
is AppCompatActivity -> context
is androidx.appcompat.view.ContextThemeWrapper -> getCompatActivity(context.baseContext)
is android.view.ContextThemeWrapper -> getCompatActivity(context.baseContext)
else -> null
}
}
val View.activity: AppCompatActivity?
get() = getCompatActivity(context)
fun View.hideSoftInput() = run {
val imm = App.INSTANCE.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
@ -30,4 +51,14 @@ fun View.invisible() {
fun View.visible() {
visibility = VISIBLE
}
fun View.screenshot(): Bitmap? {
return runCatching {
val screenshot = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565)
val c = Canvas(screenshot)
c.translate(-scrollX.toFloat(), -scrollY.toFloat())
draw(c)
screenshot
}.getOrNull()
}
Loading…
Cancel
Save