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 eb297383c..05539ad26 100644 --- a/app/src/main/java/io/legado/app/base/BaseActivity.kt +++ b/app/src/main/java/io/legado/app/base/BaseActivity.kt @@ -5,7 +5,6 @@ import android.content.res.Configuration import android.os.Build import android.os.Bundle import android.util.AttributeSet -import android.view.LayoutInflater import android.view.Menu import android.view.MenuItem import android.view.View @@ -24,7 +23,6 @@ import io.legado.app.utils.* import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.MainScope import kotlinx.coroutines.cancel -import java.lang.reflect.ParameterizedType abstract class BaseActivity( @@ -35,15 +33,7 @@ abstract class BaseActivity( ) : AppCompatActivity(), CoroutineScope by MainScope() { - protected val binding: VB by lazy { - //使用反射得到viewBinding的class - val type = javaClass.genericSuperclass as ParameterizedType - val aClass = type.actualTypeArguments[0] as Class<*> - val method = aClass.getDeclaredMethod("inflate", LayoutInflater::class.java) - @Suppress("UNCHECKED_CAST") - method.invoke(null, layoutInflater) as VB - } - + protected val binding: VB by lazy { getViewBinding() } val isInMultiWindow: Boolean get() { @@ -58,6 +48,8 @@ abstract class BaseActivity( super.attachBaseContext(LanguageUtils.setConfiguration(newBase)) } + protected abstract fun getViewBinding(): VB + override fun onCreateView( parent: View?, name: String, 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 fc7d91783..abfa31b03 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 @@ -17,6 +17,10 @@ import org.jetbrains.anko.share class AboutActivity : BaseActivity() { + override fun getViewBinding(): ActivityAboutBinding { + return ActivityAboutBinding.inflate(layoutInflater) + } + override fun onActivityCreated(savedInstanceState: Bundle?) { binding.llAbout.background = ATH.getDialogBackground() val fTag = "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 958ba1494..660ba79b2 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 @@ -13,6 +13,10 @@ import io.legado.app.databinding.ActivityDonateBinding class DonateActivity : BaseActivity() { + override fun getViewBinding(): ActivityDonateBinding { + return ActivityDonateBinding.inflate(layoutInflater) + } + override fun onActivityCreated(savedInstanceState: Bundle?) { val fTag = "donateFragment" var donateFragment = supportFragmentManager.findFragmentByTag(fTag) diff --git a/app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.kt b/app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.kt index 22d27d925..dc4233464 100644 --- a/app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.kt +++ b/app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.kt @@ -22,6 +22,10 @@ class ReadRecordActivity : BaseActivity() { lateinit var adapter: RecordAdapter + override fun getViewBinding(): ActivityReadRecordBinding { + return ActivityReadRecordBinding.inflate(layoutInflater) + } + override fun onActivityCreated(savedInstanceState: Bundle?) { initView() initData() diff --git a/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt b/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt index 18c18c43b..a9f79def1 100644 --- a/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt +++ b/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt @@ -16,6 +16,10 @@ class FileAssociationActivity : theme = Theme.Transparent ) { + override fun getViewBinding(): ActivityTranslucenceBinding { + return ActivityTranslucenceBinding.inflate(layoutInflater) + } + override val viewModel: FileAssociationViewModel get() = getViewModel(FileAssociationViewModel::class.java) diff --git a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt index f17b893f0..f6e11de63 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt @@ -16,6 +16,10 @@ class ImportBookSourceActivity : theme = Theme.Transparent ) { + override fun getViewBinding(): ActivityTranslucenceBinding { + return ActivityTranslucenceBinding.inflate(layoutInflater) + } + override val viewModel: ImportBookSourceViewModel get() = getViewModel(ImportBookSourceViewModel::class.java) diff --git a/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleActivity.kt b/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleActivity.kt index 88d721b17..e0fe869f7 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleActivity.kt @@ -16,6 +16,10 @@ class ImportReplaceRuleActivity : theme = Theme.Transparent ) { + override fun getViewBinding(): ActivityTranslucenceBinding { + return ActivityTranslucenceBinding.inflate(layoutInflater) + } + override val viewModel: ImportReplaceRuleViewModel get() = getViewModel(ImportReplaceRuleViewModel::class.java) diff --git a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceActivity.kt b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceActivity.kt index f0a50f239..76e7bb2c7 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceActivity.kt @@ -15,6 +15,10 @@ class ImportRssSourceActivity : theme = Theme.Transparent ) { + override fun getViewBinding(): ActivityTranslucenceBinding { + return ActivityTranslucenceBinding.inflate(layoutInflater) + } + override val viewModel: ImportRssSourceViewModel get() = getViewModel(ImportRssSourceViewModel::class.java) diff --git a/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt b/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt index e839b3a46..9d03b4e31 100644 --- a/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt +++ b/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt @@ -42,6 +42,10 @@ class AudioPlayActivity : private var requestCodeChapter = 8461 private var adjustProgress = false + override fun getViewBinding(): ActivityAudioPlayBinding { + return ActivityAudioPlayBinding.inflate(layoutInflater) + } + override fun onActivityCreated(savedInstanceState: Bundle?) { binding.titleBar.transparent() AudioPlay.titleData.observe(this, { binding.titleBar.title = it }) diff --git a/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt index 1286edd76..6e51b69e3 100644 --- a/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt @@ -43,6 +43,10 @@ class ArrangeBookActivity : VMBaseActivity() override val viewModel: CacheViewModel get() = getViewModel(CacheViewModel::class.java) + override fun getViewBinding(): ActivityCacheBookBinding { + return ActivityCacheBookBinding.inflate(layoutInflater) + } + override fun onActivityCreated(savedInstanceState: Bundle?) { groupId = intent.getLongExtra("groupId", -1) binding.titleBar.subtitle = intent.getStringExtra("groupName") ?: getString(R.string.all) diff --git a/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowActivity.kt b/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowActivity.kt index d909ddd18..31d3f3828 100644 --- a/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowActivity.kt @@ -24,6 +24,10 @@ class ExploreShowActivity : VMBaseActivity() + override fun getViewBinding(): ActivityBookSearchBinding { + return ActivityBookSearchBinding.inflate(layoutInflater) + } + override fun onActivityCreated(savedInstanceState: Bundle?) { binding.llHistory.setBackgroundColor(backgroundColor) serchView = binding.titleBar.findViewById(R.id.search_view) diff --git a/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt b/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt index 8aed05780..affc59e9d 100644 --- a/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt @@ -42,6 +42,10 @@ class SearchContentActivity : private var durChapterIndex = 0 private var searchResultList: MutableList = mutableListOf() + override fun getViewBinding(): ActivitySearchContentBinding { + return ActivitySearchContentBinding.inflate(layoutInflater) + } + override fun onActivityCreated(savedInstanceState: Bundle?) { searchView = binding.titleBar.findViewById(R.id.search_view) val bbg = bottomBackground diff --git a/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugActivity.kt index b7be0ce7b..9c8fc9fc5 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugActivity.kt @@ -25,6 +25,10 @@ class BookSourceDebugActivity : VMBaseActivity() override val viewModel: ConfigViewModel get() = getViewModel(ConfigViewModel::class.java) + override fun getViewBinding(): ActivityConfigBinding { + return ActivityConfigBinding.inflate(layoutInflater) + } + 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/login/SourceLogin.kt b/app/src/main/java/io/legado/app/ui/login/SourceLogin.kt index 1057a8a3d..d357e9c53 100644 --- a/app/src/main/java/io/legado/app/ui/login/SourceLogin.kt +++ b/app/src/main/java/io/legado/app/ui/login/SourceLogin.kt @@ -21,6 +21,10 @@ class SourceLogin : BaseActivity() { var loginUrl: String? = null var checking = false + override fun getViewBinding(): ActivitySourceLoginBinding { + return ActivitySourceLoginBinding.inflate(layoutInflater) + } + override fun onActivityCreated(savedInstanceState: Bundle?) { sourceUrl = intent.getStringExtra("sourceUrl") loginUrl = intent.getStringExtra("loginUrl") 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 341c33428..af453d8e2 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 @@ -47,6 +47,10 @@ class MainActivity : VMBaseActivity(), private var pagePosition = 0 private val fragmentMap = hashMapOf() + override fun getViewBinding(): ActivityMainBinding { + return ActivityMainBinding.inflate(layoutInflater) + } + override fun onActivityCreated(savedInstanceState: Bundle?) { ATH.applyEdgeEffectColor(binding.viewPagerMain) ATH.applyBottomNavigationColor(binding.bottomNavigationView) 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 979677a5c..2d0ed31e6 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 @@ -21,6 +21,10 @@ class QrCodeActivity : BaseActivity(), QRCodeView. private val requestQrImage = 202 private var flashlightIsOpen: Boolean = false + override fun getViewBinding(): ActivityQrcodeCaptureBinding { + return ActivityQrcodeCaptureBinding.inflate(layoutInflater) + } + override fun onActivityCreated(savedInstanceState: Bundle?) { binding.zXingView.setDelegate(this) binding.fabFlashlight.setOnClickListener { diff --git a/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt b/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt index ba8cfcc14..ed2f5e932 100644 --- a/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt @@ -60,6 +60,10 @@ class ReplaceRuleActivity : VMBaseActivity>? = null private var dataInit = false + override fun getViewBinding(): ActivityReplaceRuleBinding { + return ActivityReplaceRuleBinding.inflate(layoutInflater) + } + override fun onActivityCreated(savedInstanceState: Bundle?) { initRecyclerView() initSearchView() diff --git a/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditActivity.kt b/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditActivity.kt index 336ec08e6..9d56d65e4 100644 --- a/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditActivity.kt @@ -52,6 +52,10 @@ class ReplaceEditActivity : } } + override fun getViewBinding(): ActivityReplaceEditBinding { + return ActivityReplaceEditBinding.inflate(layoutInflater) + } + override val viewModel: ReplaceEditViewModel get() = getViewModel(ReplaceEditViewModel::class.java) diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt index 719d2662e..705276072 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt @@ -25,6 +25,10 @@ class RssSortActivity : VMBaseActivity() private lateinit var adapter: TabFragmentPageAdapter + override fun getViewBinding(): ActivityRssArtivlesBinding { + return ActivityRssArtivlesBinding.inflate(layoutInflater) + } + override fun onActivityCreated(savedInstanceState: Bundle?) { adapter = TabFragmentPageAdapter(supportFragmentManager) binding.tabLayout.setupWithViewPager(binding.viewPager) diff --git a/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesActivity.kt b/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesActivity.kt index e4c1901d9..2bca25e4e 100644 --- a/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesActivity.kt @@ -18,6 +18,10 @@ class RssFavoritesActivity : BaseActivity(), private var liveData: LiveData>? = null private lateinit var adapter: RssFavoritesAdapter + override fun getViewBinding(): ActivityRssFavoritesBinding { + return ActivityRssFavoritesBinding.inflate(layoutInflater) + } + override fun onActivityCreated(savedInstanceState: Bundle?) { initView() initData() diff --git a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt index 701e63cc6..ce3e2515c 100644 --- a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt @@ -43,6 +43,10 @@ class ReadRssActivity : VMBaseActivity private var customWebViewCallback: WebChromeClient.CustomViewCallback? = null private var webPic: String? = null + override fun getViewBinding(): ActivityRssReadBinding { + return ActivityRssReadBinding.inflate(layoutInflater) + } + override fun onActivityCreated(savedInstanceState: Bundle?) { viewModel.callBack = this binding.titleBar.title = intent.getStringExtra("title") diff --git a/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugActivity.kt index 5ada47696..87d429d7c 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugActivity.kt @@ -18,6 +18,10 @@ class RssSourceDebugActivity : VMBaseActivity = ArrayList() + override fun getViewBinding(): ActivityRssSourceEditBinding { + return ActivityRssSourceEditBinding.inflate(layoutInflater) + } + override val viewModel: RssSourceEditViewModel get() = getViewModel(RssSourceEditViewModel::class.java) diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt index 7579aa351..6179493b9 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt @@ -60,6 +60,10 @@ class RssSourceActivity : VMBaseActivity() private var groupMenu: SubMenu? = null + override fun getViewBinding(): ActivityRssSourceBinding { + return ActivityRssSourceBinding.inflate(layoutInflater) + } + override fun onActivityCreated(savedInstanceState: Bundle?) { initRecyclerView() initSearchView() 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 87672771b..e76d1ff95 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 @@ -20,6 +20,10 @@ import java.util.concurrent.TimeUnit open class WelcomeActivity : BaseActivity() { + override fun getViewBinding(): ActivityWelcomeBinding { + return ActivityWelcomeBinding.inflate(layoutInflater) + } + override fun onActivityCreated(savedInstanceState: Bundle?) { binding.ivBook.setColorFilter(accentColor) binding.vwTitleLine.setBackgroundColor(accentColor) diff --git a/app/src/main/java/io/legado/app/utils/viewbindingdelegate/ActivityViewBindings.kt b/app/src/main/java/io/legado/app/utils/viewbindingdelegate/ActivityViewBindings.kt new file mode 100644 index 000000000..fb42420b9 --- /dev/null +++ b/app/src/main/java/io/legado/app/utils/viewbindingdelegate/ActivityViewBindings.kt @@ -0,0 +1,60 @@ +@file:Suppress("RedundantVisibilityModifier", "unused") + +package io.legado.app.utils.viewbindingdelegate + +import android.view.View +import androidx.annotation.IdRes +import androidx.core.app.ComponentActivity +import androidx.viewbinding.ViewBinding + +private class ActivityViewBindingProperty( + viewBinder: (A) -> T +) : ViewBindingProperty(viewBinder) { + + override fun getLifecycleOwner(thisRef: A) = thisRef +} + +/** + * Create new [ViewBinding] associated with the [Activity][ComponentActivity] and allow customize how + * a [View] will be bounded to the view binding. + */ +@JvmName("viewBindingActivity") +public fun ComponentActivity.viewBinding( + viewBinder: (A) -> T +): ViewBindingProperty { + return ActivityViewBindingProperty(viewBinder) +} + +/** + * Create new [ViewBinding] associated with the [Activity][ComponentActivity] and allow customize how + * a [View] will be bounded to the view binding. + */ +@JvmName("viewBindingActivity") +public inline fun ComponentActivity.viewBinding( + crossinline vbFactory: (View) -> T, + crossinline viewProvider: (A) -> View +): ViewBindingProperty { + return viewBinding { activity: A -> vbFactory(viewProvider(activity)) } +} + +/** + * Create new [ViewBinding] associated with the [Activity][this] and allow customize how + * a [View] will be bounded to the view binding. + * + * @param vbFactory Function that create new instance of [ViewBinding]. `MyViewBinding::bind` can be used + * @param viewBindingRootId Root view's id that will be used as root for the view binding + */ +@Suppress("unused") +@JvmName("viewBindingActivity") +public inline fun ComponentActivity.viewBinding( + crossinline vbFactory: (View) -> T, + @IdRes viewBindingRootId: Int +): ViewBindingProperty { + return viewBinding { activity: ComponentActivity -> + vbFactory( + activity.findViewById( + viewBindingRootId + ) + ) + } +} \ No newline at end of file