viewBinding

pull/517/head
gedoor 4 years ago
parent 5801607f4c
commit 95bf1d8330
  1. 14
      app/src/main/java/io/legado/app/base/BaseActivity.kt
  2. 4
      app/src/main/java/io/legado/app/ui/about/AboutActivity.kt
  3. 4
      app/src/main/java/io/legado/app/ui/about/DonateActivity.kt
  4. 4
      app/src/main/java/io/legado/app/ui/about/ReadRecordActivity.kt
  5. 4
      app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt
  6. 4
      app/src/main/java/io/legado/app/ui/association/ImportBookSourceActivity.kt
  7. 4
      app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleActivity.kt
  8. 4
      app/src/main/java/io/legado/app/ui/association/ImportRssSourceActivity.kt
  9. 4
      app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt
  10. 4
      app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt
  11. 4
      app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt
  12. 4
      app/src/main/java/io/legado/app/ui/book/explore/ExploreShowActivity.kt
  13. 4
      app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt
  14. 4
      app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt
  15. 4
      app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt
  16. 4
      app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt
  17. 4
      app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt
  18. 4
      app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt
  19. 4
      app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugActivity.kt
  20. 4
      app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt
  21. 4
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt
  22. 4
      app/src/main/java/io/legado/app/ui/book/toc/ChapterListActivity.kt
  23. 4
      app/src/main/java/io/legado/app/ui/config/ConfigActivity.kt
  24. 4
      app/src/main/java/io/legado/app/ui/login/SourceLogin.kt
  25. 4
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt
  26. 4
      app/src/main/java/io/legado/app/ui/qrcode/QrCodeActivity.kt
  27. 4
      app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt
  28. 4
      app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditActivity.kt
  29. 4
      app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt
  30. 4
      app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesActivity.kt
  31. 4
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt
  32. 4
      app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugActivity.kt
  33. 4
      app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt
  34. 4
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt
  35. 4
      app/src/main/java/io/legado/app/ui/welcome/WelcomeActivity.kt
  36. 60
      app/src/main/java/io/legado/app/utils/viewbindingdelegate/ActivityViewBindings.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<VB : ViewBinding>(
@ -35,15 +33,7 @@ abstract class BaseActivity<VB : ViewBinding>(
) : 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<VB : ViewBinding>(
super.attachBaseContext(LanguageUtils.setConfiguration(newBase))
}
protected abstract fun getViewBinding(): VB
override fun onCreateView(
parent: View?,
name: String,

@ -17,6 +17,10 @@ import org.jetbrains.anko.share
class AboutActivity : BaseActivity<ActivityAboutBinding>() {
override fun getViewBinding(): ActivityAboutBinding {
return ActivityAboutBinding.inflate(layoutInflater)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
binding.llAbout.background = ATH.getDialogBackground()
val fTag = "aboutFragment"

@ -13,6 +13,10 @@ import io.legado.app.databinding.ActivityDonateBinding
class DonateActivity : BaseActivity<ActivityDonateBinding>() {
override fun getViewBinding(): ActivityDonateBinding {
return ActivityDonateBinding.inflate(layoutInflater)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
val fTag = "donateFragment"
var donateFragment = supportFragmentManager.findFragmentByTag(fTag)

@ -22,6 +22,10 @@ class ReadRecordActivity : BaseActivity<ActivityReadRecordBinding>() {
lateinit var adapter: RecordAdapter
override fun getViewBinding(): ActivityReadRecordBinding {
return ActivityReadRecordBinding.inflate(layoutInflater)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
initView()
initData()

@ -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)

@ -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)

@ -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)

@ -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)

@ -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 })

@ -43,6 +43,10 @@ class ArrangeBookActivity : VMBaseActivity<ActivityArrangeBookBinding, ArrangeBo
private var menu: Menu? = null
private var groupId: Long = -1
override fun getViewBinding(): ActivityArrangeBookBinding {
return ActivityArrangeBookBinding.inflate(layoutInflater)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
groupId = intent.getLongExtra("groupId", -1)
binding.titleBar.subtitle = intent.getStringExtra("groupName") ?: getString(R.string.all)

@ -48,6 +48,10 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
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)

@ -24,6 +24,10 @@ class ExploreShowActivity : VMBaseActivity<ActivityExploreShowBinding, ExploreSh
private lateinit var loadMoreView: LoadMoreView
private var isLoading = true
override fun getViewBinding(): ActivityExploreShowBinding {
return ActivityExploreShowBinding.inflate(layoutInflater)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
binding.titleBar.title = intent.getStringExtra("exploreName")
initRecyclerView()

@ -57,6 +57,10 @@ class BookInfoActivity :
override val viewModel: BookInfoViewModel
get() = getViewModel(BookInfoViewModel::class.java)
override fun getViewBinding(): ActivityBookInfoBinding {
return ActivityBookInfoBinding.inflate(layoutInflater)
}
@SuppressLint("PrivateResource")
override fun onActivityCreated(savedInstanceState: Bundle?) {
binding.titleBar.transparent()

@ -28,6 +28,10 @@ class BookInfoEditActivity :
override val viewModel: BookInfoEditViewModel
get() = getViewModel(BookInfoEditViewModel::class.java)
override fun getViewBinding(): ActivityBookInfoEditBinding {
return ActivityBookInfoEditBinding.inflate(layoutInflater)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
viewModel.bookData.observe(this, { upView(it) })
if (viewModel.bookData.value == null) {

@ -52,6 +52,10 @@ class ImportBookActivity : VMBaseActivity<ActivityImportBookBinding, ImportBookV
override val viewModel: ImportBookViewModel
get() = getViewModel(ImportBookViewModel::class.java)
override fun getViewBinding(): ActivityImportBookBinding {
return ActivityImportBookBinding.inflate(layoutInflater)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
initView()
initEvent()

@ -45,6 +45,10 @@ abstract class ReadBookBaseActivity :
get() = getViewModel(ReadBookViewModel::class.java)
var bottomDialog = 0
override fun getViewBinding(): ActivityBookReadBinding {
return ActivityBookReadBinding.inflate(layoutInflater)
}
override fun onCreate(savedInstanceState: Bundle?) {
ReadBook.msg = null
setOrientation()

@ -52,6 +52,10 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
private var precisionSearchMenuItem: MenuItem? = null
private var groups = linkedSetOf<String>()
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)

@ -42,6 +42,10 @@ class SearchContentActivity :
private var durChapterIndex = 0
private var searchResultList: MutableList<SearchResult> = 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

@ -25,6 +25,10 @@ class BookSourceDebugActivity : VMBaseActivity<ActivitySourceDebugBinding, BookS
private lateinit var searchView: SearchView
private val qrRequestCode = 101
override fun getViewBinding(): ActivitySourceDebugBinding {
return ActivitySourceDebugBinding.inflate(layoutInflater)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
searchView = binding.titleBar.findViewById(R.id.search_view)
viewModel.init(intent.getStringExtra("key"))

@ -53,6 +53,10 @@ class BookSourceEditActivity :
private var mSoftKeyboardTool: PopupWindow? = null
private var mIsSoftKeyBoardShowing = false
override fun getViewBinding(): ActivityBookSourceEditBinding {
return ActivityBookSourceEditBinding.inflate(layoutInflater)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
initView()
viewModel.initData(intent) {

@ -67,6 +67,10 @@ class BookSourceActivity : VMBaseActivity<ActivityBookSourceBinding, BookSourceV
private var sortAscending = 0
private var snackBar: Snackbar? = null
override fun getViewBinding(): ActivityBookSourceBinding {
return ActivityBookSourceBinding.inflate(layoutInflater)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
searchView = binding.titleBar.findViewById(R.id.search_view)
initRecyclerView()

@ -26,6 +26,10 @@ class ChapterListActivity : VMBaseActivity<ActivityChapterListBinding, ChapterLi
private lateinit var tabLayout: TabLayout
private var searchView: SearchView? = null
override fun getViewBinding(): ActivityChapterListBinding {
return ActivityChapterListBinding.inflate(layoutInflater)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
tabLayout = binding.titleBar.findViewById(R.id.tab_layout)
tabLayout.isTabIndicatorFullWidth = false

@ -12,6 +12,10 @@ class ConfigActivity : VMBaseActivity<ActivityConfigBinding, ConfigViewModel>()
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

@ -21,6 +21,10 @@ class SourceLogin : BaseActivity<ActivitySourceLoginBinding>() {
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")

@ -47,6 +47,10 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
private var pagePosition = 0
private val fragmentMap = hashMapOf<Int, Fragment>()
override fun getViewBinding(): ActivityMainBinding {
return ActivityMainBinding.inflate(layoutInflater)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
ATH.applyEdgeEffectColor(binding.viewPagerMain)
ATH.applyBottomNavigationColor(binding.bottomNavigationView)

@ -21,6 +21,10 @@ class QrCodeActivity : BaseActivity<ActivityQrcodeCaptureBinding>(), 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 {

@ -60,6 +60,10 @@ class ReplaceRuleActivity : VMBaseActivity<ActivityReplaceRuleBinding, ReplaceRu
private var replaceRuleLiveData: LiveData<List<ReplaceRule>>? = null
private var dataInit = false
override fun getViewBinding(): ActivityReplaceRuleBinding {
return ActivityReplaceRuleBinding.inflate(layoutInflater)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
initRecyclerView()
initSearchView()

@ -52,6 +52,10 @@ class ReplaceEditActivity :
}
}
override fun getViewBinding(): ActivityReplaceEditBinding {
return ActivityReplaceEditBinding.inflate(layoutInflater)
}
override val viewModel: ReplaceEditViewModel
get() = getViewModel(ReplaceEditViewModel::class.java)

@ -25,6 +25,10 @@ class RssSortActivity : VMBaseActivity<ActivityRssArtivlesBinding, RssSortViewMo
private val fragments = linkedMapOf<String, RssArticlesFragment>()
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)

@ -18,6 +18,10 @@ class RssFavoritesActivity : BaseActivity<ActivityRssFavoritesBinding>(),
private var liveData: LiveData<List<RssStar>>? = null
private lateinit var adapter: RssFavoritesAdapter
override fun getViewBinding(): ActivityRssFavoritesBinding {
return ActivityRssFavoritesBinding.inflate(layoutInflater)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
initView()
initData()

@ -43,6 +43,10 @@ class ReadRssActivity : VMBaseActivity<ActivityRssReadBinding, ReadRssViewModel>
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")

@ -18,6 +18,10 @@ class RssSourceDebugActivity : VMBaseActivity<ActivitySourceDebugBinding, RssSou
private lateinit var adapter: RssSourceDebugAdapter
override fun getViewBinding(): ActivitySourceDebugBinding {
return ActivitySourceDebugBinding.inflate(layoutInflater)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
initRecyclerView()
initSearchView()

@ -40,6 +40,10 @@ class RssSourceEditActivity :
private val adapter = RssSourceEditAdapter()
private val sourceEntities: ArrayList<EditEntity> = ArrayList()
override fun getViewBinding(): ActivityRssSourceEditBinding {
return ActivityRssSourceEditBinding.inflate(layoutInflater)
}
override val viewModel: RssSourceEditViewModel
get() = getViewModel(RssSourceEditViewModel::class.java)

@ -60,6 +60,10 @@ class RssSourceActivity : VMBaseActivity<ActivityRssSourceBinding, RssSourceView
private var groups = hashSetOf<String>()
private var groupMenu: SubMenu? = null
override fun getViewBinding(): ActivityRssSourceBinding {
return ActivityRssSourceBinding.inflate(layoutInflater)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
initRecyclerView()
initSearchView()

@ -20,6 +20,10 @@ import java.util.concurrent.TimeUnit
open class WelcomeActivity : BaseActivity<ActivityWelcomeBinding>() {
override fun getViewBinding(): ActivityWelcomeBinding {
return ActivityWelcomeBinding.inflate(layoutInflater)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
binding.ivBook.setColorFilter(accentColor)
binding.vwTitleLine.setBackgroundColor(accentColor)

@ -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<A : ComponentActivity, T : ViewBinding>(
viewBinder: (A) -> T
) : ViewBindingProperty<A, T>(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 <A : ComponentActivity, T : ViewBinding> ComponentActivity.viewBinding(
viewBinder: (A) -> T
): ViewBindingProperty<A, T> {
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 <A : ComponentActivity, T : ViewBinding> ComponentActivity.viewBinding(
crossinline vbFactory: (View) -> T,
crossinline viewProvider: (A) -> View
): ViewBindingProperty<A, T> {
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 <T : ViewBinding> ComponentActivity.viewBinding(
crossinline vbFactory: (View) -> T,
@IdRes viewBindingRootId: Int
): ViewBindingProperty<ComponentActivity, T> {
return viewBinding { activity: ComponentActivity ->
vbFactory(
activity.findViewById(
viewBindingRootId
)
)
}
}
Loading…
Cancel
Save