diff --git a/app/src/main/java/io/legado/app/lib/dialogs/AndroidDialogs.kt b/app/src/main/java/io/legado/app/lib/dialogs/AndroidDialogs.kt index c178ef75d..e14a2730f 100644 --- a/app/src/main/java/io/legado/app/lib/dialogs/AndroidDialogs.kt +++ b/app/src/main/java/io/legado/app/lib/dialogs/AndroidDialogs.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.() -> 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.() -> 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.() -> Unit) = ctx.alert(init) -inline fun Fragment.alert(noinline init: AlertBuilder.() -> Unit) = activity?.alert(init) +inline fun Fragment.alert(noinline init: AlertBuilder.() -> Unit) = requireContext().alert(init) -fun Context.alert(init: AlertBuilder.() -> Unit): AlertBuilder = +fun Context.alert(init: AlertBuilder.() -> Unit): AlertBuilder = 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, diff --git a/app/src/main/java/io/legado/app/lib/dialogs/AndroidSelectors.kt b/app/src/main/java/io/legado/app/lib/dialogs/AndroidSelectors.kt index f4d63e1a8..17ed0dea8 100644 --- a/app/src/main/java/io/legado/app/lib/dialogs/AndroidSelectors.kt +++ b/app/src/main/java/io/legado/app/lib/dialogs/AndroidSelectors.kt @@ -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, - noinline onClick: (DialogInterface, Int) -> Unit -) = ctx.selector(title, items, onClick) inline fun Fragment.selector( - title: CharSequence? = null, - items: List, - noinline onClick: (DialogInterface, Int) -> Unit + title: CharSequence? = null, + items: List, + noinline onClick: (DialogInterface, Int) -> Unit ) = activity?.selector(title, items, onClick) fun Context.selector( - title: CharSequence? = null, - items: List, - onClick: (DialogInterface, Int) -> Unit + title: CharSequence? = null, + items: List, + onClick: (DialogInterface, Int) -> Unit ) { with(AndroidAlertBuilder(this)) { if (title != null) { diff --git a/app/src/main/java/io/legado/app/lib/dialogs/Dialogs.kt b/app/src/main/java/io/legado/app/lib/dialogs/Dialogs.kt index 9f4c0c77b..6a1e28428 100644 --- a/app/src/main/java/io/legado/app/lib/dialogs/Dialogs.kt +++ b/app/src/main/java/io/legado/app/lib/dialogs/Dialogs.kt @@ -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 = (Context) -> AlertBuilder -inline fun AnkoContext<*>.alert( - noinline factory: AlertBuilderFactory, - title: String? = null, - message: String? = null, - noinline init: (AlertBuilder.() -> Unit)? = null -) = ctx.alert(factory, title, message, init) - inline fun Fragment.alert( noinline factory: AlertBuilderFactory, title: String? = null, @@ -56,13 +48,6 @@ fun Context.alert( } } -inline fun AnkoContext<*>.alert( - noinline factory: AlertBuilderFactory, - titleResource: Int? = null, - messageResource: Int? = null, - noinline init: (AlertBuilder.() -> Unit)? = null -) = ctx.alert(factory, titleResource, messageResource, init) - inline fun Fragment.alert( noinline factory: AlertBuilderFactory, titleResource: Int? = null, @@ -87,11 +72,6 @@ fun Context.alert( } } -inline fun AnkoContext<*>.alert( - noinline factory: AlertBuilderFactory, - noinline init: AlertBuilder.() -> Unit -) = ctx.alert(factory, init) - inline fun Fragment.alert( noinline factory: AlertBuilderFactory, noinline init: AlertBuilder.() -> Unit diff --git a/app/src/main/java/io/legado/app/lib/dialogs/Selectors.kt b/app/src/main/java/io/legado/app/lib/dialogs/Selectors.kt index 485c6971c..b8fe1e1b4 100644 --- a/app/src/main/java/io/legado/app/lib/dialogs/Selectors.kt +++ b/app/src/main/java/io/legado/app/lib/dialogs/Selectors.kt @@ -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 AnkoContext<*>.selector( - noinline factory: AlertBuilderFactory, - title: CharSequence? = null, - items: List, - noinline onClick: (DialogInterface, CharSequence, Int) -> Unit -) = ctx.selector(factory, title, items, onClick) inline fun Fragment.selector( noinline factory: AlertBuilderFactory, diff --git a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt index 1b4bac585..7d9f4006a 100644 --- a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt @@ -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) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt b/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt index fd6b825d0..631356af0 100644 --- a/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt +++ b/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt @@ -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()) diff --git a/app/src/main/java/io/legado/app/utils/ViewExtensions.kt b/app/src/main/java/io/legado/app/utils/ViewExtensions.kt index 4300ea2ae..50944d224 100644 --- a/app/src/main/java/io/legado/app/utils/ViewExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ViewExtensions.kt @@ -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() } \ No newline at end of file