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 b15403956..6406c18bf 100644 --- a/app/src/main/java/io/legado/app/base/BaseActivity.kt +++ b/app/src/main/java/io/legado/app/base/BaseActivity.kt @@ -38,7 +38,7 @@ abstract class BaseActivity( ) : AppCompatActivity(), CoroutineScope by MainScope() { - protected val binding: VB by lazy { getViewBinding() } + protected abstract val binding: VB val isInMultiWindow: Boolean get() { @@ -53,8 +53,6 @@ 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 2c669aacd..f7462712d 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 @@ -13,13 +13,12 @@ import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.accentColor import io.legado.app.utils.openUrl import io.legado.app.utils.share +import io.legado.app.utils.viewbindingdelegate.viewBinding class AboutActivity : BaseActivity() { - override fun getViewBinding(): ActivityAboutBinding { - return ActivityAboutBinding.inflate(layoutInflater) - } + override val binding by viewBinding(ActivityAboutBinding::inflate) override fun onActivityCreated(savedInstanceState: Bundle?) { binding.llAbout.background = ATH.getDialogBackground() 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 660ba79b2..831d25535 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 @@ -5,6 +5,7 @@ import android.os.Bundle import io.legado.app.R import io.legado.app.base.BaseActivity import io.legado.app.databinding.ActivityDonateBinding +import io.legado.app.utils.viewbindingdelegate.viewBinding /** * Created by GKF on 2018/1/13. @@ -13,9 +14,7 @@ import io.legado.app.databinding.ActivityDonateBinding class DonateActivity : BaseActivity() { - override fun getViewBinding(): ActivityDonateBinding { - return ActivityDonateBinding.inflate(layoutInflater) - } + override val binding by viewBinding(ActivityDonateBinding::inflate) override fun onActivityCreated(savedInstanceState: Bundle?) { val fTag = "donateFragment" 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 08de241e8..aea9f773d 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 @@ -18,6 +18,7 @@ import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.ui.book.search.SearchActivity import io.legado.app.utils.cnCompare import io.legado.app.utils.startActivity +import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.launch @@ -29,9 +30,7 @@ class ReadRecordActivity : BaseActivity() { lateinit var adapter: RecordAdapter private var sortMode = 0 - override fun getViewBinding(): ActivityReadRecordBinding { - return ActivityReadRecordBinding.inflate(layoutInflater) - } + override val binding by viewBinding(ActivityReadRecordBinding::inflate) override fun onActivityCreated(savedInstanceState: Bundle?) { initView() 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 8401830c6..0fa73e39e 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 @@ -6,15 +6,14 @@ import io.legado.app.base.VMBaseActivity import io.legado.app.constant.Theme import io.legado.app.databinding.ActivityTranslucenceBinding import io.legado.app.utils.toastOnUi +import io.legado.app.utils.viewbindingdelegate.viewBinding class FileAssociationActivity : VMBaseActivity( theme = Theme.Transparent ) { - override fun getViewBinding(): ActivityTranslucenceBinding { - return ActivityTranslucenceBinding.inflate(layoutInflater) - } + override val binding by viewBinding(ActivityTranslucenceBinding::inflate) override val viewModel: FileAssociationViewModel by viewModels() 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 c5a060d81..a4ee8af19 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 @@ -10,6 +10,7 @@ import io.legado.app.help.IntentDataHelp import io.legado.app.lib.dialogs.alert import io.legado.app.utils.toastOnUi +import io.legado.app.utils.viewbindingdelegate.viewBinding class ImportBookSourceActivity : @@ -17,9 +18,7 @@ class ImportBookSourceActivity : theme = Theme.Transparent ) { - override fun getViewBinding(): ActivityTranslucenceBinding { - return ActivityTranslucenceBinding.inflate(layoutInflater) - } + override val binding by viewBinding(ActivityTranslucenceBinding::inflate) override val viewModel: ImportBookSourceViewModel by viewModels() 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 9596b78c8..c2fa0d04d 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 @@ -10,15 +10,14 @@ import io.legado.app.databinding.ActivityTranslucenceBinding import io.legado.app.help.IntentDataHelp import io.legado.app.lib.dialogs.alert import io.legado.app.utils.toastOnUi +import io.legado.app.utils.viewbindingdelegate.viewBinding class ImportReplaceRuleActivity : VMBaseActivity( theme = Theme.Transparent ) { - override fun getViewBinding(): ActivityTranslucenceBinding { - return ActivityTranslucenceBinding.inflate(layoutInflater) - } + override val binding by viewBinding(ActivityTranslucenceBinding::inflate) override val viewModel: ImportReplaceRuleViewModel by viewModels() 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 a3fa20cee..724cd70be 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 @@ -10,15 +10,14 @@ import io.legado.app.help.IntentDataHelp import io.legado.app.lib.dialogs.alert import io.legado.app.utils.toastOnUi +import io.legado.app.utils.viewbindingdelegate.viewBinding class ImportRssSourceActivity : VMBaseActivity( theme = Theme.Transparent ) { - override fun getViewBinding(): ActivityTranslucenceBinding { - return ActivityTranslucenceBinding.inflate(layoutInflater) - } + override val binding by viewBinding(ActivityTranslucenceBinding::inflate) override val viewModel: ImportRssSourceViewModel by viewModels() 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 0f995ea8d..82b1915c4 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 @@ -26,6 +26,7 @@ import io.legado.app.ui.widget.recycler.ItemTouchCallback import io.legado.app.ui.widget.recycler.VerticalDivider import io.legado.app.utils.cnCompare import io.legado.app.utils.getPrefInt +import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -36,6 +37,8 @@ class ArrangeBookActivity : VMBaseActivity = arrayListOf() @@ -47,10 +50,6 @@ class ArrangeBookActivity : VMBaseActivity(toolBarTheme = Theme.Dark), ChangeSourceDialog.CallBack { + override val binding by viewBinding(ActivityAudioPlayBinding::inflate) override val viewModel: AudioPlayViewModel by viewModels() @@ -57,10 +59,6 @@ class AudioPlayActivity : } } - 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/cache/CacheActivity.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt index 7016927b5..89d79e307 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt @@ -28,6 +28,7 @@ import io.legado.app.ui.document.FilePicker import io.legado.app.ui.document.FilePickerParam import io.legado.app.ui.widget.dialog.TextListDialog import io.legado.app.utils.* +import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch @@ -58,13 +59,9 @@ class CacheActivity : VMBaseActivity() private var exportPosition = -1 private val groupList: ArrayList = arrayListOf() private var groupId: Long = -1 - + override val binding by viewBinding(ActivityCacheBookBinding::inflate) override val viewModel: CacheViewModel by viewModels() - override fun getViewBinding(): ActivityCacheBookBinding { - return ActivityCacheBookBinding.inflate(layoutInflater) - } - override fun onActivityCreated(savedInstanceState: Bundle?) { groupId = intent.getLongExtra("groupId", -1) launch { 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 95f1a12bc..760df2c71 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 @@ -13,20 +13,17 @@ import io.legado.app.ui.book.info.BookInfoActivity import io.legado.app.ui.widget.recycler.LoadMoreView import io.legado.app.ui.widget.recycler.VerticalDivider import io.legado.app.utils.startActivity +import io.legado.app.utils.viewbindingdelegate.viewBinding class ExploreShowActivity : VMBaseActivity(), ExploreShowAdapter.CallBack { - override val viewModel: ExploreShowViewModel - by viewModels() + override val binding by viewBinding(ActivityExploreShowBinding::inflate) + override val viewModel: ExploreShowViewModel by viewModels() private lateinit var adapter: ExploreShowAdapter 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() diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt index bb011df0b..7b1f1c5d6 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt @@ -38,6 +38,7 @@ import io.legado.app.ui.book.source.edit.BookSourceEditActivity import io.legado.app.ui.book.toc.TocActivityResult import io.legado.app.ui.widget.image.CoverImageView import io.legado.app.utils.* +import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -89,13 +90,9 @@ class BookInfoActivity : } } - + override val binding by viewBinding(ActivityBookInfoBinding::inflate) override val viewModel: BookInfoViewModel by viewModels() - override fun getViewBinding(): ActivityBookInfoBinding { - return ActivityBookInfoBinding.inflate(layoutInflater) - } - @SuppressLint("PrivateResource") override fun onActivityCreated(savedInstanceState: Bundle?) { binding.titleBar.transparent() diff --git a/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt index 366296bc8..df0757e4e 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt @@ -16,6 +16,7 @@ import io.legado.app.lib.permission.Permissions import io.legado.app.lib.permission.PermissionsCompat import io.legado.app.ui.book.changecover.ChangeCoverDialog import io.legado.app.utils.* +import io.legado.app.utils.viewbindingdelegate.viewBinding import java.io.File class BookInfoEditActivity : @@ -29,13 +30,10 @@ class BookInfoEditActivity : } } + override val binding by viewBinding(ActivityBookInfoEditBinding::inflate) override val viewModel: BookInfoEditViewModel by viewModels() - override fun getViewBinding(): ActivityBookInfoEditBinding { - return ActivityBookInfoEditBinding.inflate(layoutInflater) - } - override fun onActivityCreated(savedInstanceState: Bundle?) { viewModel.bookData.observe(this, { upView(it) }) if (viewModel.bookData.value == null) { diff --git a/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt index 724152c15..98889e85e 100644 --- a/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt @@ -17,13 +17,14 @@ import io.legado.app.data.appDb import io.legado.app.databinding.ActivityImportBookBinding import io.legado.app.databinding.DialogEditTextBinding import io.legado.app.help.AppConfig -import io.legado.app.lib.permission.Permissions import io.legado.app.lib.dialogs.alert +import io.legado.app.lib.permission.Permissions import io.legado.app.lib.permission.PermissionsCompat import io.legado.app.lib.theme.backgroundColor import io.legado.app.ui.document.FilePicker import io.legado.app.ui.widget.SelectActionBar import io.legado.app.utils.* +import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.launch @@ -57,13 +58,10 @@ class ImportBookActivity : VMBaseActivity(imageBg = false) { + override val binding by viewBinding(ActivityBookReadBinding::inflate) override val viewModel: ReadBookViewModel by viewModels() var bottomDialog = 0 - override fun getViewBinding(): ActivityBookReadBinding { - return ActivityBookReadBinding.inflate(layoutInflater) - } - override fun onCreate(savedInstanceState: Bundle?) { ReadBook.msg = null setOrientation() diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt index 1c7289729..ebee774f0 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt @@ -27,6 +27,7 @@ import io.legado.app.ui.book.info.BookInfoActivity import io.legado.app.ui.book.source.manage.BookSourceActivity import io.legado.app.ui.widget.recycler.LoadMoreView import io.legado.app.utils.* +import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -36,6 +37,7 @@ class SearchActivity : VMBaseActivity() - override fun getViewBinding(): ActivityBookSearchBinding { - return ActivityBookSearchBinding.inflate(layoutInflater) - } - override fun onActivityCreated(savedInstanceState: Bundle?) { binding.llHistory.setBackgroundColor(backgroundColor) searchView = 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 4d89a1c64..a164ab184 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 @@ -23,6 +23,7 @@ import io.legado.app.ui.widget.recycler.UpLinearLayoutManager import io.legado.app.ui.widget.recycler.VerticalDivider import io.legado.app.utils.ColorUtils import io.legado.app.utils.observeEvent +import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -32,6 +33,7 @@ class SearchContentActivity : VMBaseActivity(), SearchContentAdapter.Callback { + override val binding by viewBinding(ActivitySearchContentBinding::inflate) override val viewModel: SearchContentViewModel by viewModels() lateinit var adapter: SearchContentAdapter @@ -41,10 +43,6 @@ 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 42f85da7c..20ba8db96 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 @@ -14,10 +14,12 @@ import io.legado.app.lib.theme.accentColor import io.legado.app.ui.qrcode.QrCodeResult import io.legado.app.ui.widget.dialog.TextDialog import io.legado.app.utils.toastOnUi +import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.launch class BookSourceDebugActivity : VMBaseActivity() { + override val binding by viewBinding(ActivitySourceDebugBinding::inflate) override val viewModel: BookSourceDebugModel by viewModels() @@ -29,10 +31,6 @@ class BookSourceDebugActivity : VMBaseActivity(false), KeyboardToolPop.CallBack { - override val viewModel: BookSourceEditViewModel - by viewModels() + + override val binding by viewBinding(ActivityBookSourceEditBinding::inflate) + override val viewModel: BookSourceEditViewModel by viewModels() private val adapter = BookSourceEditAdapter() private val sourceEntities: ArrayList = ArrayList() @@ -65,10 +67,6 @@ 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) { diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index f76e4705a..f2312ce86 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -39,6 +39,7 @@ import io.legado.app.ui.widget.recycler.DragSelectTouchHelper import io.legado.app.ui.widget.recycler.ItemTouchCallback import io.legado.app.ui.widget.recycler.VerticalDivider import io.legado.app.utils.* +import io.legado.app.utils.viewbindingdelegate.viewBinding import java.io.File class BookSourceActivity : VMBaseActivity(), @@ -46,8 +47,8 @@ class BookSourceActivity : VMBaseActivity() { - override val viewModel: TocViewModel - by viewModels() + + override val binding by viewBinding(ActivityChapterListBinding::inflate) + override val viewModel: TocViewModel by viewModels() 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 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 2d7ab0cbd..6552a5553 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 @@ -8,14 +8,12 @@ import io.legado.app.constant.EventBus import io.legado.app.databinding.ActivityConfigBinding import io.legado.app.utils.observeEvent +import io.legado.app.utils.viewbindingdelegate.viewBinding class ConfigActivity : VMBaseActivity() { - override val viewModel: ConfigViewModel - by viewModels() - override fun getViewBinding(): ActivityConfigBinding { - return ActivityConfigBinding.inflate(layoutInflater) - } + override val binding by viewBinding(ActivityConfigBinding::inflate) + override val viewModel: ConfigViewModel by viewModels() override fun onActivityCreated(savedInstanceState: Bundle?) { intent.getIntExtra("configType", -1).let { diff --git a/app/src/main/java/io/legado/app/ui/document/FilePickerActivity.kt b/app/src/main/java/io/legado/app/ui/document/FilePickerActivity.kt index 1f7b56939..08bc51275 100644 --- a/app/src/main/java/io/legado/app/ui/document/FilePickerActivity.kt +++ b/app/src/main/java/io/legado/app/ui/document/FilePickerActivity.kt @@ -14,6 +14,7 @@ import io.legado.app.lib.dialogs.alert import io.legado.app.lib.permission.Permissions import io.legado.app.lib.permission.PermissionsCompat import io.legado.app.utils.isContentScheme +import io.legado.app.utils.viewbindingdelegate.viewBinding import java.io.File class FilePickerActivity : @@ -21,6 +22,8 @@ class FilePickerActivity : theme = Theme.Transparent ), FilePickerDialog.CallBack { + override val binding by viewBinding(ActivityTranslucenceBinding::inflate) + private val selectDocTree = registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) { it ?: let { @@ -41,10 +44,6 @@ class FilePickerActivity : onResult(Intent().setData(it)) } - override fun getViewBinding(): ActivityTranslucenceBinding { - return ActivityTranslucenceBinding.inflate(layoutInflater) - } - override fun onActivityCreated(savedInstanceState: Bundle?) { val mode = intent.getIntExtra("mode", 0) val allowExtensions = intent.getStringArrayExtra("allowExtensions") diff --git a/app/src/main/java/io/legado/app/ui/login/SourceLoginActivity.kt b/app/src/main/java/io/legado/app/ui/login/SourceLoginActivity.kt index d4ee0dd23..af9522ec7 100644 --- a/app/src/main/java/io/legado/app/ui/login/SourceLoginActivity.kt +++ b/app/src/main/java/io/legado/app/ui/login/SourceLoginActivity.kt @@ -13,19 +13,17 @@ import io.legado.app.base.BaseActivity import io.legado.app.databinding.ActivitySourceLoginBinding import io.legado.app.help.http.CookieStore import io.legado.app.utils.snackbar +import io.legado.app.utils.viewbindingdelegate.viewBinding class SourceLoginActivity : BaseActivity() { + override val binding by viewBinding(ActivitySourceLoginBinding::inflate) var sourceUrl: String? = null var loginUrl: String? = null var userAgent: 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 c9ea98bd0..349357aa6 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 @@ -36,11 +36,14 @@ import io.legado.app.ui.main.rss.RssFragment import io.legado.app.ui.widget.dialog.TextDialog import io.legado.app.utils.observeEvent import io.legado.app.utils.toastOnUi +import io.legado.app.utils.viewbindingdelegate.viewBinding class MainActivity : VMBaseActivity(), BottomNavigationView.OnNavigationItemSelectedListener, BottomNavigationView.OnNavigationItemReselectedListener { + + override val binding by viewBinding(ActivityMainBinding::inflate) override val viewModel: MainViewModel by viewModels() private var exitTime: Long = 0 private var bookshelfReselected: Long = 0 @@ -50,10 +53,6 @@ class MainActivity : VMBaseActivity(), private var bottomMenuCount = 2 private val realPositions = arrayOf(0, 1, 2, 3) - override fun getViewBinding(): ActivityMainBinding { - return ActivityMainBinding.inflate(layoutInflater) - } - override fun onActivityCreated(savedInstanceState: Bundle?) { upBottomMenu() binding.apply { 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 93680630e..dcabe5b8c 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 @@ -13,9 +13,12 @@ import io.legado.app.base.BaseActivity import io.legado.app.databinding.ActivityQrcodeCaptureBinding import io.legado.app.utils.QRCodeUtils import io.legado.app.utils.readBytes +import io.legado.app.utils.viewbindingdelegate.viewBinding class QrCodeActivity : BaseActivity(), OnScanResultCallback { + override val binding by viewBinding(ActivityQrcodeCaptureBinding::inflate) + private val selectQrImage = registerForActivityResult(ActivityResultContracts.GetContent()) { it.readBytes(this)?.let { bytes -> val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size) @@ -23,10 +26,6 @@ class QrCodeActivity : BaseActivity(), OnScanResul } } - override fun getViewBinding(): ActivityQrcodeCaptureBinding { - return ActivityQrcodeCaptureBinding.inflate(layoutInflater) - } - override fun onActivityCreated(savedInstanceState: Bundle?) { val fTag = "qrCodeFragment" val qrCodeFragment = QrCodeFragment() 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 f0097b62b..983d4480e 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 @@ -38,6 +38,7 @@ import io.legado.app.ui.widget.recycler.DragSelectTouchHelper import io.legado.app.ui.widget.recycler.ItemTouchCallback import io.legado.app.ui.widget.recycler.VerticalDivider import io.legado.app.utils.* +import io.legado.app.utils.viewbindingdelegate.viewBinding import java.io.File /** @@ -48,6 +49,7 @@ class ReplaceRuleActivity : VMBaseActivity() { + override val binding by viewBinding(ActivityRssArtivlesBinding::inflate) override val viewModel: RssSortViewModel by viewModels() private lateinit var adapter: TabFragmentPageAdapter @@ -33,10 +35,6 @@ class RssSortActivity : VMBaseActivity(), RssFavoritesAdapter.CallBack { + override val binding by viewBinding(ActivityRssFavoritesBinding::inflate) 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 95aaf8325..c4cde5610 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 @@ -26,6 +26,7 @@ import io.legado.app.ui.association.ImportRssSourceActivity import io.legado.app.ui.document.FilePicker import io.legado.app.ui.document.FilePickerParam import io.legado.app.utils.* +import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.launch import org.apache.commons.text.StringEscapeUtils import org.jsoup.Jsoup @@ -35,8 +36,8 @@ import splitties.systemservices.downloadManager class ReadRssActivity : VMBaseActivity(false), ReadRssViewModel.CallBack { - override val viewModel: ReadRssViewModel - by viewModels() + override val binding by viewBinding(ActivityRssReadBinding::inflate) + override val viewModel: ReadRssViewModel by viewModels() private val imagePathKey = "" private var starMenuItem: MenuItem? = null private var ttsMenuItem: MenuItem? = null @@ -47,10 +48,6 @@ class ReadRssActivity : VMBaseActivity viewModel.saveImage(webPic, it.toString()) } - 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 4b790e5c1..a03483a7d 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 @@ -14,20 +14,17 @@ import io.legado.app.ui.widget.dialog.TextDialog import io.legado.app.utils.gone import io.legado.app.utils.toastOnUi +import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.launch class RssSourceDebugActivity : VMBaseActivity() { - override val viewModel: RssSourceDebugModel - by viewModels() + override val binding by viewBinding(ActivitySourceDebugBinding::inflate) + override val viewModel: RssSourceDebugModel by viewModels() private lateinit var adapter: RssSourceDebugAdapter - override fun getViewBinding(): ActivitySourceDebugBinding { - return ActivitySourceDebugBinding.inflate(layoutInflater) - } - override fun onActivityCreated(savedInstanceState: Bundle?) { initRecyclerView() initSearchView() diff --git a/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt index a738a4633..97df39a12 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt @@ -25,6 +25,7 @@ import io.legado.app.ui.rss.source.debug.RssSourceDebugActivity import io.legado.app.ui.widget.KeyboardToolPop import io.legado.app.ui.widget.dialog.TextDialog import io.legado.app.utils.* +import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlin.math.abs class RssSourceEditActivity : @@ -32,6 +33,7 @@ class RssSourceEditActivity : ViewTreeObserver.OnGlobalLayoutListener, KeyboardToolPop.CallBack { + override val binding by viewBinding(ActivityRssSourceEditBinding::inflate) private var mSoftKeyboardTool: PopupWindow? = null private var mIsSoftKeyBoardShowing = false private val adapter = RssSourceEditAdapter() @@ -44,10 +46,6 @@ class RssSourceEditActivity : } } - override fun getViewBinding(): ActivityRssSourceEditBinding { - return ActivityRssSourceEditBinding.inflate(layoutInflater) - } - override val viewModel: RssSourceEditViewModel by viewModels() 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 6565ab242..abeef35b2 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 @@ -34,6 +34,7 @@ import io.legado.app.ui.widget.recycler.DragSelectTouchHelper import io.legado.app.ui.widget.recycler.ItemTouchCallback import io.legado.app.ui.widget.recycler.VerticalDivider import io.legado.app.utils.* +import io.legado.app.utils.viewbindingdelegate.viewBinding import java.io.File /** @@ -44,6 +45,7 @@ class RssSourceActivity : VMBaseActivity(), RuleSubAdapter.Callback { + override val binding by viewBinding(ActivityRuleSubBinding::inflate) private lateinit var adapter: RuleSubAdapter private var liveData: LiveData>? = null - override fun getViewBinding(): ActivityRuleSubBinding { - return ActivityRuleSubBinding.inflate(layoutInflater) - } - override fun onActivityCreated(savedInstanceState: Bundle?) { initView() initData() 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 11b55b24d..75e9adc12 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 @@ -15,13 +15,12 @@ import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.ui.main.MainActivity import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.startActivity +import io.legado.app.utils.viewbindingdelegate.viewBinding import java.util.concurrent.TimeUnit open class WelcomeActivity : BaseActivity() { - override fun getViewBinding(): ActivityWelcomeBinding { - return ActivityWelcomeBinding.inflate(layoutInflater) - } + override val binding by viewBinding(ActivityWelcomeBinding::inflate) override fun onActivityCreated(savedInstanceState: Bundle?) { binding.ivBook.setColorFilter(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 index fb42420b9..5ace190cc 100644 --- a/app/src/main/java/io/legado/app/utils/viewbindingdelegate/ActivityViewBindings.kt +++ b/app/src/main/java/io/legado/app/utils/viewbindingdelegate/ActivityViewBindings.kt @@ -2,59 +2,12 @@ package io.legado.app.utils.viewbindingdelegate -import android.view.View -import androidx.annotation.IdRes +import android.view.LayoutInflater 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 - ) - ) +inline fun ComponentActivity.viewBinding(crossinline bindingInflater: (LayoutInflater) -> T) = + lazy(LazyThreadSafetyMode.SYNCHRONIZED) { + val invoke = bindingInflater.invoke(layoutInflater) + invoke } -} \ No newline at end of file