diff --git a/app/build.gradle b/app/build.gradle index 578212db5..4771d0825 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -135,9 +135,4 @@ dependencies { //颜色选择 implementation 'com.jaredrummler:colorpicker:1.1.0' - - //对话框 - implementation 'com.afollestad.material-dialogs:core:3.0.0-rc3' - implementation 'com.afollestad.material-dialogs:input:3.0.0-rc3' - implementation 'com.afollestad.material-dialogs:files:3.0.0-rc3' } diff --git a/app/src/main/java/io/legado/app/base/BaseActivity.kt b/app/src/main/java/io/legado/app/base/BaseActivity.kt index 4dfe09fb4..51f960788 100644 --- a/app/src/main/java/io/legado/app/base/BaseActivity.kt +++ b/app/src/main/java/io/legado/app/base/BaseActivity.kt @@ -7,7 +7,6 @@ import android.view.MenuItem import android.view.View import android.view.WindowManager import androidx.appcompat.app.AppCompatActivity -import androidx.lifecycle.ViewModel import io.legado.app.R import io.legado.app.lib.theme.ColorUtils import io.legado.app.lib.theme.ThemeStore @@ -17,20 +16,16 @@ import kotlinx.coroutines.MainScope import kotlinx.coroutines.cancel -abstract class BaseActivity(private val fullScreen: Boolean = true) : AppCompatActivity(), +abstract class BaseActivity(private val layoutID: Int, private val fullScreen: Boolean = true) : AppCompatActivity(), CoroutineScope by MainScope() { - protected abstract val viewModel: VM - - protected abstract val layoutID: Int - override fun onCreate(savedInstanceState: Bundle?) { window.decorView.disableAutoFill() initTheme() setupSystemBar() super.onCreate(savedInstanceState) setContentView(layoutID) - onActivityCreated(viewModel, savedInstanceState) + onActivityCreated(savedInstanceState) observeLiveBus() } @@ -39,7 +34,7 @@ abstract class BaseActivity(private val fullScreen: Boolean = tr cancel() } - abstract fun onActivityCreated(viewModel: VM, savedInstanceState: Bundle?) + abstract fun onActivityCreated(savedInstanceState: Bundle?) final override fun onCreateOptionsMenu(menu: Menu?): Boolean { return menu?.let { diff --git a/app/src/main/java/io/legado/app/base/BaseFragment.kt b/app/src/main/java/io/legado/app/base/BaseFragment.kt index 7c02f1bd3..f8314c44c 100644 --- a/app/src/main/java/io/legado/app/base/BaseFragment.kt +++ b/app/src/main/java/io/legado/app/base/BaseFragment.kt @@ -11,7 +11,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.MainScope import kotlinx.coroutines.cancel -abstract class BaseFragment(contentLayoutId: Int = 0) : Fragment(contentLayoutId), +abstract class BaseFragment(layoutID: Int) : Fragment(layoutID), CoroutineScope by MainScope() { var supportToolbar: Toolbar? = null @@ -20,6 +20,7 @@ abstract class BaseFragment(contentLayoutId: Int = 0) : Fragment(contentLayoutId val menuInflater: MenuInflater get() = SupportMenuInflater(requireContext()) + override fun onDestroy() { super.onDestroy() cancel() diff --git a/app/src/main/java/io/legado/app/base/VMBaseActivity.kt b/app/src/main/java/io/legado/app/base/VMBaseActivity.kt new file mode 100644 index 000000000..8267a2c8b --- /dev/null +++ b/app/src/main/java/io/legado/app/base/VMBaseActivity.kt @@ -0,0 +1,10 @@ +package io.legado.app.base + +import androidx.lifecycle.ViewModel + +abstract class VMBaseActivity(layoutID: Int, fullScreen: Boolean = true) : + BaseActivity(layoutID, fullScreen) { + + protected abstract val viewModel: VM + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/base/VMBaseFragment.kt b/app/src/main/java/io/legado/app/base/VMBaseFragment.kt new file mode 100644 index 000000000..cc78d19dd --- /dev/null +++ b/app/src/main/java/io/legado/app/base/VMBaseFragment.kt @@ -0,0 +1,9 @@ +package io.legado.app.base + +import androidx.lifecycle.ViewModel + +abstract class VMBaseFragment(layoutID: Int) : BaseFragment(layoutID) { + + protected abstract val viewModel: VM + +} diff --git a/app/src/main/java/io/legado/app/lib/dialogs/AlertBuilder.kt b/app/src/main/java/io/legado/app/lib/dialogs/AlertBuilder.kt new file mode 100644 index 000000000..4617e99f1 --- /dev/null +++ b/app/src/main/java/io/legado/app/lib/dialogs/AlertBuilder.kt @@ -0,0 +1,103 @@ +/* + * Copyright 2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@file:Suppress("NOTHING_TO_INLINE", "unused") +package io.legado.app.lib.dialogs + +import android.annotation.SuppressLint +import android.content.Context +import android.content.DialogInterface +import android.graphics.drawable.Drawable +import android.view.KeyEvent +import android.view.View +import android.view.ViewManager +import androidx.annotation.DrawableRes +import androidx.annotation.StringRes +import org.jetbrains.anko.UI +import org.jetbrains.anko.internals.AnkoInternals.NO_GETTER +import kotlin.DeprecationLevel.ERROR + +@SuppressLint("SupportAnnotationUsage") +interface AlertBuilder { + val ctx: Context + + var title: CharSequence + @Deprecated(NO_GETTER, level = ERROR) get + + var titleResource: Int + @Deprecated(NO_GETTER, level = ERROR) get + + var message: CharSequence + @Deprecated(NO_GETTER, level = ERROR) get + + var messageResource: Int + @Deprecated(NO_GETTER, level = ERROR) get + + var icon: Drawable + @Deprecated(NO_GETTER, level = ERROR) get + + @setparam:DrawableRes + var iconResource: Int + @Deprecated(NO_GETTER, level = ERROR) get + + var customTitle: View + @Deprecated(NO_GETTER, level = ERROR) get + + var customView: View + @Deprecated(NO_GETTER, level = ERROR) get + + var isCancelable: Boolean + @Deprecated(NO_GETTER, level = ERROR) get + + fun onCancelled(handler: (dialog: DialogInterface) -> Unit) + + fun onKeyPressed(handler: (dialog: DialogInterface, keyCode: Int, e: KeyEvent) -> Boolean) + + fun positiveButton(buttonText: String, onClicked: (dialog: DialogInterface) -> Unit) + fun positiveButton(@StringRes buttonTextResource: Int, onClicked: (dialog: DialogInterface) -> Unit) + + fun negativeButton(buttonText: String, onClicked: (dialog: DialogInterface) -> Unit) + fun negativeButton(@StringRes buttonTextResource: Int, onClicked: (dialog: DialogInterface) -> Unit) + + fun neutralPressed(buttonText: String, onClicked: (dialog: DialogInterface) -> Unit) + fun neutralPressed(@StringRes buttonTextResource: Int, onClicked: (dialog: DialogInterface) -> Unit) + + fun items(items: List, onItemSelected: (dialog: DialogInterface, index: Int) -> Unit) + fun items(items: List, onItemSelected: (dialog: DialogInterface, item: T, index: Int) -> Unit) + + fun build(): D + fun show(): D +} + +fun AlertBuilder<*>.customTitle(view: () -> View) { + customTitle = view() +} + +fun AlertBuilder<*>.customView(view: () -> View) { + customView = view() +} + +inline fun AlertBuilder<*>.okButton(noinline handler: (dialog: DialogInterface) -> Unit) = + positiveButton(android.R.string.ok, handler) + +inline fun AlertBuilder<*>.cancelButton(noinline handler: (dialog: DialogInterface) -> Unit) = + negativeButton(android.R.string.cancel, handler) + +inline fun AlertBuilder<*>.yesButton(noinline handler: (dialog: DialogInterface) -> Unit) = + positiveButton(android.R.string.yes, handler) + +inline fun AlertBuilder<*>.noButton(noinline handler: (dialog: DialogInterface) -> Unit) = + negativeButton(android.R.string.no, handler) \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/lib/dialogs/AndroidAlertBuilder.kt b/app/src/main/java/io/legado/app/lib/dialogs/AndroidAlertBuilder.kt new file mode 100644 index 000000000..38e03d573 --- /dev/null +++ b/app/src/main/java/io/legado/app/lib/dialogs/AndroidAlertBuilder.kt @@ -0,0 +1,117 @@ +/* + * Copyright 2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.legado.app.lib.dialogs + +import android.content.Context +import android.content.DialogInterface +import android.graphics.drawable.Drawable +import android.view.KeyEvent +import android.view.View +import androidx.appcompat.app.AlertDialog +import org.jetbrains.anko.internals.AnkoInternals +import org.jetbrains.anko.internals.AnkoInternals.NO_GETTER +import kotlin.DeprecationLevel.ERROR + +val Android: AlertBuilderFactory = ::AndroidAlertBuilder + +internal class AndroidAlertBuilder(override val ctx: Context) : AlertBuilder { + private val builder = AlertDialog.Builder(ctx) + + override var title: CharSequence + @Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter() + set(value) { builder.setTitle(value) } + + override var titleResource: Int + @Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter() + set(value) { builder.setTitle(value) } + + override var message: CharSequence + @Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter() + set(value) { builder.setMessage(value) } + + override var messageResource: Int + @Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter() + set(value) { builder.setMessage(value) } + + override var icon: Drawable + @Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter() + set(value) { builder.setIcon(value) } + + override var iconResource: Int + @Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter() + set(value) { builder.setIcon(value) } + + override var customTitle: View + @Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter() + set(value) { builder.setCustomTitle(value) } + + override var customView: View + @Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter() + set(value) { builder.setView(value) } + + override var isCancelable: Boolean + @Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter() + set(value) { builder.setCancelable(value) } + + override fun onCancelled(handler: (DialogInterface) -> Unit) { + builder.setOnCancelListener(handler) + } + + override fun onKeyPressed(handler: (dialog: DialogInterface, keyCode: Int, e: KeyEvent) -> Boolean) { + builder.setOnKeyListener(handler) + } + + override fun positiveButton(buttonText: String, onClicked: (dialog: DialogInterface) -> Unit) { + builder.setPositiveButton(buttonText) { dialog, _ -> onClicked(dialog) } + } + + override fun positiveButton(buttonTextResource: Int, onClicked: (dialog: DialogInterface) -> Unit) { + builder.setPositiveButton(buttonTextResource) { dialog, _ -> onClicked(dialog) } + } + + override fun negativeButton(buttonText: String, onClicked: (dialog: DialogInterface) -> Unit) { + builder.setNegativeButton(buttonText) { dialog, _ -> onClicked(dialog) } + } + + override fun negativeButton(buttonTextResource: Int, onClicked: (dialog: DialogInterface) -> Unit) { + builder.setNegativeButton(buttonTextResource) { dialog, _ -> onClicked(dialog) } + } + + override fun neutralPressed(buttonText: String, onClicked: (dialog: DialogInterface) -> Unit) { + builder.setNeutralButton(buttonText) { dialog, _ -> onClicked(dialog) } + } + + override fun neutralPressed(buttonTextResource: Int, onClicked: (dialog: DialogInterface) -> Unit) { + builder.setNeutralButton(buttonTextResource) { dialog, _ -> onClicked(dialog) } + } + + override fun items(items: List, onItemSelected: (dialog: DialogInterface, index: Int) -> Unit) { + builder.setItems(Array(items.size) { i -> items[i].toString() }) { dialog, which -> + onItemSelected(dialog, which) + } + } + + override fun items(items: List, onItemSelected: (dialog: DialogInterface, item: T, index: Int) -> Unit) { + builder.setItems(Array(items.size) { i -> items[i].toString() }) { dialog, which -> + onItemSelected(dialog, items[which], which) + } + } + + override fun build(): AlertDialog = builder.create() + + override fun show(): AlertDialog = builder.show() +} \ No newline at end of file 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 new file mode 100644 index 000000000..c178ef75d --- /dev/null +++ b/app/src/main/java/io/legado/app/lib/dialogs/AndroidDialogs.kt @@ -0,0 +1,179 @@ +/* + * Copyright 2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@file:Suppress("NOTHING_TO_INLINE", "unused") + +package io.legado.app.lib.dialogs + +import android.app.ProgressDialog +import android.content.Context +import android.content.DialogInterface +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, + noinline init: (AlertBuilder.() -> Unit)? = null +) = requireActivity().alert(title, message, init) + +fun Context.alert( + title: CharSequence? = null, + message: CharSequence? = null, + init: (AlertBuilder.() -> Unit)? = null +): AlertBuilder { + return AndroidAlertBuilder(this).apply { + if (title != null) { + this.title = title + } + if (message != null) { + this.message = message + } + if (init != null) init() + } +} + +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, + noinline init: (AlertBuilder.() -> Unit)? = null +) = requireActivity().alert(title, message, init) + +fun Context.alert( + titleResource: Int? = null, + messageResource: Int? = null, + init: (AlertBuilder.() -> Unit)? = null +): AlertBuilder { + return AndroidAlertBuilder(this).apply { + if (titleResource != null) { + this.titleResource = titleResource + } + if (messageResource != null) { + this.messageResource = messageResource + } + if (init != null) init() + } +} + + +inline fun AnkoContext<*>.alert(noinline init: AlertBuilder.() -> Unit) = ctx.alert(init) +inline fun Fragment.alert(noinline init: AlertBuilder.() -> Unit) = activity?.alert(init) + +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, + noinline init: (ProgressDialog.() -> Unit)? = null +) = requireActivity().progressDialog(title, message, init) + +fun Context.progressDialog( + title: Int? = null, + message: Int? = null, + init: (ProgressDialog.() -> Unit)? = null +) = 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, + noinline init: (ProgressDialog.() -> Unit)? = null +) = requireActivity().indeterminateProgressDialog(title, message, init) + +fun Context.indeterminateProgressDialog( + title: Int? = null, + message: Int? = null, + 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, + noinline init: (ProgressDialog.() -> Unit)? = null +) = requireActivity().progressDialog(title, message, init) + +fun Context.progressDialog( + title: CharSequence? = null, + message: CharSequence? = null, + init: (ProgressDialog.() -> Unit)? = null +) = 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, + noinline init: (ProgressDialog.() -> Unit)? = null +) = requireActivity().indeterminateProgressDialog(title, message, init) + +fun Context.indeterminateProgressDialog( + title: CharSequence? = null, + message: CharSequence? = null, + init: (ProgressDialog.() -> Unit)? = null +) = progressDialog(title, message, true, init) + + +private fun Context.progressDialog( + title: CharSequence? = null, + message: CharSequence? = null, + indeterminate: Boolean, + init: (ProgressDialog.() -> Unit)? = null +) = ProgressDialog(this).apply { + isIndeterminate = indeterminate + if (!indeterminate) setProgressStyle(ProgressDialog.STYLE_HORIZONTAL) + if (message != null) setMessage(message) + if (title != null) setTitle(title) + if (init != null) init() + show() +} 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 new file mode 100644 index 000000000..f4d63e1a8 --- /dev/null +++ b/app/src/main/java/io/legado/app/lib/dialogs/AndroidSelectors.kt @@ -0,0 +1,50 @@ +/* + * Copyright 2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@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( + 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 +) = activity?.selector(title, items, onClick) + +fun Context.selector( + title: CharSequence? = null, + items: List, + onClick: (DialogInterface, Int) -> Unit +) { + with(AndroidAlertBuilder(this)) { + if (title != null) { + this.title = title + } + items(items, onClick) + show() + } +} 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 new file mode 100644 index 000000000..9f4c0c77b --- /dev/null +++ b/app/src/main/java/io/legado/app/lib/dialogs/Dialogs.kt @@ -0,0 +1,103 @@ +/* + * Copyright 2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@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 + +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, + message: String? = null, + noinline init: (AlertBuilder.() -> Unit)? = null +) = activity?.alert(factory, title, message, init) + +fun Context.alert( + factory: AlertBuilderFactory, + title: String? = null, + message: String? = null, + init: (AlertBuilder.() -> Unit)? = null +): AlertBuilder { + return factory(this).apply { + if (title != null) { + this.title = title + } + if (message != null) { + this.message = message + } + if (init != null) init() + } +} + +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, + messageResource: Int? = null, + noinline init: (AlertBuilder.() -> Unit)? = null +) = requireActivity().alert(factory, titleResource, messageResource, init) + +fun Context.alert( + factory: AlertBuilderFactory, + titleResource: Int? = null, + messageResource: Int? = null, + init: (AlertBuilder.() -> Unit)? = null +): AlertBuilder { + return factory(this).apply { + if (titleResource != null) { + this.titleResource = titleResource + } + if (messageResource != null) { + this.messageResource = messageResource + } + if (init != null) init() + } +} + +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 +) = requireActivity().alert(factory, init) + +fun Context.alert( + factory: AlertBuilderFactory, + init: AlertBuilder.() -> Unit +): AlertBuilder = factory(this).apply { init() } 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 new file mode 100644 index 000000000..485c6971c --- /dev/null +++ b/app/src/main/java/io/legado/app/lib/dialogs/Selectors.kt @@ -0,0 +1,52 @@ +/* + * Copyright 2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@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, + title: CharSequence? = null, + items: List, + noinline onClick: (DialogInterface, CharSequence, Int) -> Unit +) = requireActivity().selector(factory, title, items, onClick) + +fun Context.selector( + factory: AlertBuilderFactory, + title: CharSequence? = null, + items: List, + onClick: (DialogInterface, CharSequence, Int) -> Unit +) { + with(factory(this)) { + if (title != null) { + this.title = title + } + items(items, onClick) + show() + } +} diff --git a/app/src/main/java/io/legado/app/lib/theme/prefs/ATEEditTextPreference.kt b/app/src/main/java/io/legado/app/lib/theme/prefs/ATEEditTextPreference.kt index 639ccab86..935aa6289 100644 --- a/app/src/main/java/io/legado/app/lib/theme/prefs/ATEEditTextPreference.kt +++ b/app/src/main/java/io/legado/app/lib/theme/prefs/ATEEditTextPreference.kt @@ -18,7 +18,7 @@ import androidx.appcompat.app.AlertDialog import androidx.preference.EditTextPreference import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ThemeStore -import io.legado.app.utils.upTint +import io.legado.app.utils.applyTint class ATEEditTextPreference(context: Context?, attrs: AttributeSet?) : EditTextPreference( context, @@ -72,7 +72,7 @@ class ATEEditTextPreference(context: Context?, attrs: AttributeSet?) : EditTextP requestInputMethod(dialog) dialog.setOnDismissListener(this) dialog.show() - dialog.upTint() + dialog.applyTint() } protected fun onCreateDialogView(): View? { diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSonPath.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSonPath.kt index e1f697acd..298c1d2fa 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSonPath.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSonPath.kt @@ -98,12 +98,12 @@ class AnalyzeByJSonPath { if (rules.size == 1) { if (!rule.contains("{$.")) { try { - val `object` = ctx!!.read(rule) ?: return result - if (`object` is List<*>) { - for (o in `object`) + val obj = ctx!!.read(rule) ?: return result + if (obj is List<*>) { + for (o in obj) result.add(o.toString()) } else { - result.add(`object`.toString()) + result.add(obj.toString()) } } catch (ignored: Exception) { } diff --git a/app/src/main/java/io/legado/app/ui/about/AboutActivity.kt b/app/src/main/java/io/legado/app/ui/about/AboutActivity.kt index 05eef3ad7..303e9de4b 100644 --- a/app/src/main/java/io/legado/app/ui/about/AboutActivity.kt +++ b/app/src/main/java/io/legado/app/ui/about/AboutActivity.kt @@ -8,16 +8,13 @@ import android.view.MenuItem import androidx.lifecycle.AndroidViewModel import io.legado.app.R import io.legado.app.base.BaseActivity +import io.legado.app.base.VMBaseActivity import io.legado.app.utils.getViewModel import org.jetbrains.anko.toast -class AboutActivity : BaseActivity() { - override val viewModel: AndroidViewModel - get() = getViewModel(AndroidViewModel::class.java) - override val layoutID: Int - get() = R.layout.activity_about +class AboutActivity : BaseActivity(R.layout.activity_about) { - override fun onActivityCreated(viewModel: AndroidViewModel, savedInstanceState: Bundle?) { + override fun onActivityCreated(savedInstanceState: Bundle?) { val fTag = "aboutFragment" var aboutFragment = supportFragmentManager.findFragmentByTag(fTag) if (aboutFragment == null) aboutFragment = AboutFragment() diff --git a/app/src/main/java/io/legado/app/ui/about/DonateActivity.kt b/app/src/main/java/io/legado/app/ui/about/DonateActivity.kt index c5bb90704..313a6bec6 100644 --- a/app/src/main/java/io/legado/app/ui/about/DonateActivity.kt +++ b/app/src/main/java/io/legado/app/ui/about/DonateActivity.kt @@ -23,15 +23,9 @@ import java.net.URLEncoder * 捐赠页面 */ -class DonateActivity : BaseActivity() { +class DonateActivity : BaseActivity(R.layout.activity_donate) { - override val viewModel: AndroidViewModel - get() = getViewModel(AndroidViewModel::class.java) - - override val layoutID: Int - get() = R.layout.activity_donate - - override fun onActivityCreated(viewModel: AndroidViewModel, savedInstanceState: Bundle?) { + override fun onActivityCreated(savedInstanceState: Bundle?) { setSupportActionBar(toolbar) ATH.applyEdgeEffectColor(scroll_view) vw_zfb_tz.setOnClickListener { aliDonate(this) } diff --git a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoActivity.kt index 39adfaa01..314a76cf4 100644 --- a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoActivity.kt @@ -3,15 +3,10 @@ package io.legado.app.ui.bookinfo import android.os.Bundle import io.legado.app.R import io.legado.app.base.BaseActivity -import io.legado.app.utils.getViewModel -class BookInfoActivity : BaseActivity() { - override val viewModel: BookInfoViewModel - get() = getViewModel(BookInfoViewModel::class.java) - override val layoutID: Int - get() = R.layout.activity_book_info +class BookInfoActivity : BaseActivity(R.layout.activity_book_info) { - override fun onActivityCreated(viewModel: BookInfoViewModel, savedInstanceState: Bundle?) { + override fun onActivityCreated(savedInstanceState: Bundle?) { } diff --git a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoEditActivity.kt b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoEditActivity.kt index 8e3ed92c5..689398fdc 100644 --- a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoEditActivity.kt @@ -2,16 +2,14 @@ package io.legado.app.ui.bookinfo import android.os.Bundle import io.legado.app.R -import io.legado.app.base.BaseActivity +import io.legado.app.base.VMBaseActivity import io.legado.app.utils.getViewModel -class BookInfoEditActivity : BaseActivity() { +class BookInfoEditActivity : VMBaseActivity(R.layout.activity_book_info_edit) { override val viewModel: BookInfoViewModel get() = getViewModel(BookInfoViewModel::class.java) - override val layoutID: Int - get() = R.layout.activity_book_info_edit - override fun onActivityCreated(viewModel: BookInfoViewModel, savedInstanceState: Bundle?) { + override fun onActivityCreated(savedInstanceState: Bundle?) { } diff --git a/app/src/main/java/io/legado/app/ui/bookshelf/BookshelfActivity.kt b/app/src/main/java/io/legado/app/ui/bookshelf/BookshelfActivity.kt index a6cda3975..590699652 100644 --- a/app/src/main/java/io/legado/app/ui/bookshelf/BookshelfActivity.kt +++ b/app/src/main/java/io/legado/app/ui/bookshelf/BookshelfActivity.kt @@ -10,22 +10,20 @@ import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.App import io.legado.app.R -import io.legado.app.base.BaseActivity +import io.legado.app.base.VMBaseActivity import io.legado.app.data.entities.Book import io.legado.app.lib.theme.ATH import io.legado.app.utils.getViewModel import kotlinx.android.synthetic.main.activity_bookshelf.* -class BookshelfActivity : BaseActivity() { +class BookshelfActivity : VMBaseActivity(R.layout.activity_bookshelf) { override val viewModel: BookshelfViewModel get() = getViewModel(BookshelfViewModel::class.java) - override val layoutID: Int - get() = R.layout.activity_bookshelf private lateinit var bookshelfAdapter: BookshelfAdapter private var bookshelfLiveData: LiveData>? = null - override fun onActivityCreated(viewModel: BookshelfViewModel, savedInstanceState: Bundle?) { + override fun onActivityCreated(savedInstanceState: Bundle?) { if (viewModel.bookGroup == null) { viewModel.bookGroup = intent.getParcelableExtra("data") } diff --git a/app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt index 25690f258..66ff91a1d 100644 --- a/app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt @@ -14,7 +14,7 @@ import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.App import io.legado.app.R -import io.legado.app.base.BaseActivity +import io.legado.app.base.VMBaseActivity import io.legado.app.data.entities.BookSource import io.legado.app.help.ItemTouchCallback import io.legado.app.lib.theme.ATH @@ -28,17 +28,16 @@ import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch import org.jetbrains.anko.startActivity -class BookSourceActivity : BaseActivity(), BookSourceAdapter.CallBack, +class BookSourceActivity : VMBaseActivity(R.layout.activity_book_source), + BookSourceAdapter.CallBack, SearchView.OnQueryTextListener { override val viewModel: BookshelfViewModel get() = getViewModel(BookshelfViewModel::class.java) - override val layoutID: Int - get() = R.layout.activity_book_source private lateinit var adapter: BookSourceAdapter private var bookSourceLiveDate: LiveData>? = null - override fun onActivityCreated(viewModel: BookshelfViewModel, savedInstanceState: Bundle?) { + override fun onActivityCreated(savedInstanceState: Bundle?) { setSupportActionBar(toolbar) initRecyclerView() initDataObserve() diff --git a/app/src/main/java/io/legado/app/ui/config/ConfigActivity.kt b/app/src/main/java/io/legado/app/ui/config/ConfigActivity.kt index a8449be35..6329cb2e3 100644 --- a/app/src/main/java/io/legado/app/ui/config/ConfigActivity.kt +++ b/app/src/main/java/io/legado/app/ui/config/ConfigActivity.kt @@ -2,18 +2,16 @@ package io.legado.app.ui.config import android.os.Bundle import io.legado.app.R -import io.legado.app.base.BaseActivity +import io.legado.app.base.VMBaseActivity import io.legado.app.utils.getViewModel import kotlinx.android.synthetic.main.activity_config.* import kotlinx.android.synthetic.main.view_title_bar.* -class ConfigActivity : BaseActivity() { +class ConfigActivity : VMBaseActivity(R.layout.activity_config) { override val viewModel: ConfigViewModel get() = getViewModel(ConfigViewModel::class.java) - override val layoutID: Int - get() = R.layout.activity_config - override fun onActivityCreated(viewModel: ConfigViewModel, savedInstanceState: Bundle?) { + override fun onActivityCreated(savedInstanceState: Bundle?) { intent.getIntExtra("configType", -1).let { if (it != -1) viewModel.configType = it } 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 89a698b54..778e6e15e 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 @@ -56,7 +56,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar upTheme(false) } .setNegativeButton(R.string.cancel) { _, _ -> upTheme(false) } - .show().upTint() + .show().applyTint() } } else { upTheme(false) @@ -76,7 +76,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar upTheme(true) } .setNegativeButton(R.string.cancel) { _, _ -> upTheme(true) } - .show().upTint() + .show().applyTint() } } else { upTheme(true) @@ -106,7 +106,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar recreateActivities() } .setNegativeButton(R.string.cancel, null) - .show().upTint() + .show().applyTint() } } } diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index b92d71a03..bcb1d2fe3 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -9,7 +9,7 @@ import androidx.viewpager.widget.ViewPager import com.google.android.material.bottomnavigation.BottomNavigationView import io.legado.app.App import io.legado.app.R -import io.legado.app.base.BaseActivity +import io.legado.app.base.VMBaseActivity import io.legado.app.constant.Bus import io.legado.app.help.permission.Permissions import io.legado.app.help.permission.PermissionsCompat @@ -26,16 +26,14 @@ import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -class MainActivity : BaseActivity(), BottomNavigationView.OnNavigationItemSelectedListener, +class MainActivity : VMBaseActivity(R.layout.activity_main), + BottomNavigationView.OnNavigationItemSelectedListener, ViewPager.OnPageChangeListener { override val viewModel: MainViewModel get() = getViewModel(MainViewModel::class.java) - override val layoutID: Int - get() = R.layout.activity_main - - override fun onActivityCreated(viewModel: MainViewModel, savedInstanceState: Bundle?) { + override fun onActivityCreated(savedInstanceState: Bundle?) { ATH.applyEdgeEffectColor(view_pager_main) bottom_navigation_view.setBackgroundColor(ThemeStore.backgroundColor(this)) val colorStateList = Selector.colorBuild() diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt index 6bd0a54df..fece79af1 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt @@ -3,6 +3,7 @@ package io.legado.app.ui.main.bookshelf import android.os.Bundle import android.view.Menu import android.view.View +import android.widget.EditText import androidx.appcompat.widget.SearchView import androidx.lifecycle.LiveData import androidx.lifecycle.Observer @@ -10,28 +11,32 @@ import androidx.paging.LivePagedListBuilder import androidx.paging.PagedList import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager -import com.afollestad.materialdialogs.MaterialDialog -import com.afollestad.materialdialogs.input.input import io.legado.app.App import io.legado.app.R -import io.legado.app.base.BaseFragment +import io.legado.app.base.VMBaseFragment import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookGroup +import io.legado.app.lib.dialogs.* import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ThemeStore import io.legado.app.ui.bookshelf.BookshelfActivity import io.legado.app.ui.search.SearchActivity -import io.legado.app.utils.disableAutoFill +import io.legado.app.utils.applyTint +import io.legado.app.utils.getViewModel +import io.legado.app.utils.requestInputMethod +import kotlinx.android.synthetic.main.dialog_edittext.view.* import kotlinx.android.synthetic.main.fragment_bookshelf.* import kotlinx.android.synthetic.main.view_title_bar.* -import kotlinx.coroutines.Dispatchers.IO -import kotlinx.coroutines.launch import org.jetbrains.anko.startActivity import org.jetbrains.anko.textColor -class BookshelfFragment : BaseFragment(R.layout.fragment_bookshelf), SearchView.OnQueryTextListener, +class BookshelfFragment : VMBaseFragment(R.layout.fragment_bookshelf), + SearchView.OnQueryTextListener, BookGroupAdapter.CallBack { + override val viewModel: BookshelfViewModel + get() = getViewModel(BookshelfViewModel::class.java) + private lateinit var bookshelfAdapter: BookshelfAdapter private lateinit var bookGroupAdapter: BookGroupAdapter private var bookGroupLiveData: LiveData>? = null @@ -89,25 +94,7 @@ class BookshelfFragment : BaseFragment(R.layout.fragment_bookshelf), SearchView. override fun open(bookGroup: BookGroup) { when (bookGroup.groupId) { - -10 -> context?.let { - MaterialDialog(it).show { - window?.decorView?.disableAutoFill() - title(text = "新建分组") - input(hint = "分组名称") { _, charSequence -> - run { - launch(IO) { - App.db.bookGroupDao().insert( - BookGroup( - App.db.bookGroupDao().maxId + 1, - charSequence.toString() - ) - ) - } - } - } - positiveButton(R.string.ok) - } - } + -10 -> showGroupInputDialog() else -> context?.startActivity(Pair("data", bookGroup)) } } @@ -121,4 +108,26 @@ class BookshelfFragment : BaseFragment(R.layout.fragment_bookshelf), SearchView. return false } + private fun showGroupInputDialog() { + alert(title = "新建分组") { + var editText: EditText? = null + + customView { + layoutInflater.inflate(R.layout.dialog_edittext, null).apply { + editText = edit_view.apply { + ATH.setTint(this, ThemeStore.accentColor(ctx)) + hint = "分组名称" + } + } + } + + yesButton { + viewModel.saveBookGroup(editText?.text?.toString()) + } + + noButton { } + + }.show().applyTint().requestInputMethod() + } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt new file mode 100644 index 000000000..d47080cb4 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt @@ -0,0 +1,24 @@ +package io.legado.app.ui.main.bookshelf + +import android.app.Application +import io.legado.app.App +import io.legado.app.base.BaseViewModel +import io.legado.app.data.entities.BookGroup + +class BookshelfViewModel(application: Application) : BaseViewModel(application) { + + + fun saveBookGroup(group: String?) { + if (!group.isNullOrBlank()) { + execute { + App.db.bookGroupDao().insert( + BookGroup( + App.db.bookGroupDao().maxId + 1, + group + ) + ) + } + } + } + +} diff --git a/app/src/main/java/io/legado/app/ui/qrcode/QrCodeActivity.kt b/app/src/main/java/io/legado/app/ui/qrcode/QrCodeActivity.kt index deb50d3a9..dac723182 100644 --- a/app/src/main/java/io/legado/app/ui/qrcode/QrCodeActivity.kt +++ b/app/src/main/java/io/legado/app/ui/qrcode/QrCodeActivity.kt @@ -6,27 +6,21 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.view.View -import androidx.lifecycle.AndroidViewModel import cn.bingoogolapple.qrcode.core.QRCodeView import io.legado.app.R import io.legado.app.base.BaseActivity import io.legado.app.help.permission.Permissions import io.legado.app.help.permission.PermissionsCompat import io.legado.app.utils.FileUtils -import io.legado.app.utils.getViewModel import kotlinx.android.synthetic.main.activity_qrcode_capture.* import kotlinx.android.synthetic.main.view_title_bar.* -class QrCodeActivity : BaseActivity(), QRCodeView.Delegate { - override val viewModel: AndroidViewModel - get() = getViewModel(AndroidViewModel::class.java) - override val layoutID: Int - get() = R.layout.activity_qrcode_capture +class QrCodeActivity : BaseActivity(R.layout.activity_qrcode_capture), QRCodeView.Delegate { private val requestQrImage = 202 private var flashlightIsOpen: Boolean = false - override fun onActivityCreated(viewModel: AndroidViewModel, savedInstanceState: Bundle?) { + override fun onActivityCreated(savedInstanceState: Bundle?) { setSupportActionBar(toolbar) zxingview.setDelegate(this) fab_flashlight.setOnClickListener { diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt index a6d206d36..6e82de8df 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt @@ -13,7 +13,7 @@ import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import io.legado.app.App import io.legado.app.R -import io.legado.app.base.BaseActivity +import io.legado.app.base.VMBaseActivity import io.legado.app.constant.AppConst.APP_TAG import io.legado.app.data.entities.ReplaceRule import io.legado.app.lib.theme.ATH @@ -23,16 +23,14 @@ import org.jetbrains.anko.doAsync import org.jetbrains.anko.toast -class ReplaceRuleActivity : BaseActivity() { +class ReplaceRuleActivity : VMBaseActivity(R.layout.activity_replace_rule) { override val viewModel: ReplaceRuleViewModel get() = getViewModel(ReplaceRuleViewModel::class.java) - override val layoutID: Int - get() = R.layout.activity_replace_rule private lateinit var adapter: ReplaceRuleAdapter private var rulesLiveData: LiveData>? = null private var allEnabled = false - override fun onActivityCreated(viewModel: ReplaceRuleViewModel, savedInstanceState: Bundle?) { + override fun onActivityCreated(savedInstanceState: Bundle?) { initRecyclerView() initDataObservers() initSwipeToDelete() diff --git a/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt index c2a420c6f..cc9b7a76b 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt @@ -3,21 +3,18 @@ package io.legado.app.ui.search import android.os.Bundle import android.view.View import io.legado.app.R -import io.legado.app.base.BaseActivity +import io.legado.app.base.VMBaseActivity import io.legado.app.lib.theme.ATH import io.legado.app.utils.getViewModel import kotlinx.android.synthetic.main.activity_search.* import kotlinx.android.synthetic.main.view_title_bar.* -class SearchActivity : BaseActivity() { +class SearchActivity : VMBaseActivity(R.layout.activity_search) { override val viewModel: SearchViewModel get() = getViewModel(SearchViewModel::class.java) - override val layoutID: Int - get() = R.layout.activity_search - - override fun onActivityCreated(viewModel: SearchViewModel, savedInstanceState: Bundle?) { + override fun onActivityCreated(savedInstanceState: Bundle?) { initSearchView() initRecyclerView() viewModel.search() diff --git a/app/src/main/java/io/legado/app/ui/sourcedebug/SourceDebugActivity.kt b/app/src/main/java/io/legado/app/ui/sourcedebug/SourceDebugActivity.kt index eda5f392d..82df34f83 100644 --- a/app/src/main/java/io/legado/app/ui/sourcedebug/SourceDebugActivity.kt +++ b/app/src/main/java/io/legado/app/ui/sourcedebug/SourceDebugActivity.kt @@ -3,10 +3,9 @@ package io.legado.app.ui.sourcedebug import android.os.Bundle import android.view.View import androidx.appcompat.widget.SearchView -import androidx.lifecycle.Observer import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.R -import io.legado.app.base.BaseActivity +import io.legado.app.base.VMBaseActivity import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ThemeStore import io.legado.app.utils.getViewModel @@ -14,20 +13,18 @@ import kotlinx.android.synthetic.main.activity_source_debug.* import kotlinx.android.synthetic.main.view_title_bar.* import org.jetbrains.anko.toast -class SourceDebugActivity : BaseActivity() { +class SourceDebugActivity : VMBaseActivity(R.layout.activity_source_debug) { override val viewModel: SourceDebugModel get() = getViewModel(SourceDebugModel::class.java) - override val layoutID: Int - get() = R.layout.activity_source_debug private lateinit var adapter: SourceDebugAdapter - override fun onActivityCreated(viewModel: SourceDebugModel, savedInstanceState: Bundle?) { + override fun onActivityCreated(savedInstanceState: Bundle?) { viewModel.init(intent.getStringExtra("key")) initRecyclerView() initSearchView() - viewModel.observeLogs(this){ + viewModel.observeLogs(this) { adapter.addItem(it.obj as String) if (it.what == -1 || it.what == 1000) { rotate_loading.hide() diff --git a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt index 4f1e8a344..cb9c3ed0f 100644 --- a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt @@ -12,7 +12,7 @@ import androidx.lifecycle.Observer import androidx.recyclerview.widget.LinearLayoutManager import com.google.android.material.tabs.TabLayout import io.legado.app.R -import io.legado.app.base.BaseActivity +import io.legado.app.base.VMBaseActivity import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.rule.* import io.legado.app.lib.theme.ATH @@ -26,11 +26,10 @@ import org.jetbrains.anko.startActivity import org.jetbrains.anko.toast import kotlin.math.abs -class SourceEditActivity : BaseActivity(false), KeyboardToolPop.OnClickListener { +class SourceEditActivity : VMBaseActivity(R.layout.activity_source_edit, false), + KeyboardToolPop.OnClickListener { override val viewModel: SourceEditViewModel get() = getViewModel(SourceEditViewModel::class.java) - override val layoutID: Int - get() = R.layout.activity_source_edit private val adapter = SourceEditAdapter() private val sourceEditList: ArrayList = ArrayList() @@ -43,7 +42,7 @@ class SourceEditActivity : BaseActivity(false), KeyboardToo private var mSoftKeyboardTool: PopupWindow? = null private var mIsSoftKeyBoardShowing = false - override fun onActivityCreated(viewModel: SourceEditViewModel, savedInstanceState: Bundle?) { + override fun onActivityCreated(savedInstanceState: Bundle?) { initView() viewModel.sourceLiveData.observe(this, Observer { upRecyclerView(it) diff --git a/app/src/main/java/io/legado/app/ui/welcome/WelcomeActivity.kt b/app/src/main/java/io/legado/app/ui/welcome/WelcomeActivity.kt index f225bcd80..3295cb766 100644 --- a/app/src/main/java/io/legado/app/ui/welcome/WelcomeActivity.kt +++ b/app/src/main/java/io/legado/app/ui/welcome/WelcomeActivity.kt @@ -3,22 +3,16 @@ package io.legado.app.ui.welcome import android.animation.Animator import android.animation.ValueAnimator import android.os.Bundle -import androidx.lifecycle.AndroidViewModel import io.legado.app.R import io.legado.app.base.BaseActivity import io.legado.app.lib.theme.ThemeStore import io.legado.app.ui.main.MainActivity -import io.legado.app.utils.getViewModel import kotlinx.android.synthetic.main.activity_welcome.* import org.jetbrains.anko.startActivity -class WelcomeActivity : BaseActivity() { - override val viewModel: AndroidViewModel - get() = getViewModel(AndroidViewModel::class.java) - override val layoutID: Int - get() = R.layout.activity_welcome +class WelcomeActivity : BaseActivity(R.layout.activity_welcome) { - override fun onActivityCreated(viewModel: AndroidViewModel, savedInstanceState: Bundle?) { + override fun onActivityCreated(savedInstanceState: Bundle?) { iv_bg.setColorFilter(ThemeStore.accentColor(this)) val welAnimator = ValueAnimator.ofFloat(1f, 0f).setDuration(800) welAnimator.startDelay = 100 diff --git a/app/src/main/java/io/legado/app/utils/AlertDialogExtensions.kt b/app/src/main/java/io/legado/app/utils/AlertDialogExtensions.kt index e956f1368..2f261a2e7 100644 --- a/app/src/main/java/io/legado/app/utils/AlertDialogExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/AlertDialogExtensions.kt @@ -1,8 +1,13 @@ package io.legado.app.utils +import android.view.WindowManager import androidx.appcompat.app.AlertDialog import io.legado.app.lib.theme.ATH -fun AlertDialog.upTint(): AlertDialog { +fun AlertDialog.applyTint(): AlertDialog { return ATH.setAlertDialogTint(this) } + +fun AlertDialog.requestInputMethod(){ + window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE) +} 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 0275e057e..4300ea2ae 100644 --- a/app/src/main/java/io/legado/app/utils/ViewExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ViewExtensions.kt @@ -3,6 +3,7 @@ package io.legado.app.utils import android.content.Context import android.os.Build import android.view.View +import android.view.View.* import android.view.inputmethod.InputMethodManager import io.legado.app.App @@ -15,6 +16,18 @@ fun View.hideSoftInput() = run { fun View.disableAutoFill() = run { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - this.importantForAutofill = View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS + this.importantForAutofill = IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS } +} + +fun View.gone() { + visibility = GONE +} + +fun View.invisible() { + visibility = INVISIBLE +} + +fun View.visible() { + visibility = VISIBLE } \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_edittext.xml b/app/src/main/res/layout/dialog_edittext.xml new file mode 100644 index 000000000..04dd314d0 --- /dev/null +++ b/app/src/main/res/layout/dialog_edittext.xml @@ -0,0 +1,18 @@ + + + + + \ No newline at end of file