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 446d0c042..02506cccc 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 @@ -2,11 +2,12 @@ package io.legado.app.ui.association import android.content.Intent import android.os.Bundle +import androidx.activity.viewModels import io.legado.app.base.VMBaseActivity import io.legado.app.constant.Theme import io.legado.app.databinding.ActivityTranslucenceBinding import io.legado.app.ui.main.MainActivity -import io.legado.app.utils.getViewModel + import io.legado.app.utils.toastOnUi import org.jetbrains.anko.startActivity @@ -20,8 +21,7 @@ class FileAssociationActivity : return ActivityTranslucenceBinding.inflate(layoutInflater) } - override val viewModel: FileAssociationViewModel - get() = getViewModel(FileAssociationViewModel::class.java) + override val viewModel: FileAssociationViewModel by viewModels() override fun onActivityCreated(savedInstanceState: Bundle?) { binding.rotateLoading.show() 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 e9a638456..09007b795 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 @@ -1,13 +1,14 @@ package io.legado.app.ui.association import android.os.Bundle +import androidx.activity.viewModels import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.Theme import io.legado.app.databinding.ActivityTranslucenceBinding import io.legado.app.help.IntentDataHelp import io.legado.app.lib.dialogs.alert -import io.legado.app.utils.getViewModel + import io.legado.app.utils.toastOnUi @@ -21,7 +22,7 @@ class ImportBookSourceActivity : } override val viewModel: ImportBookSourceViewModel - get() = getViewModel(ImportBookSourceViewModel::class.java) + by viewModels() override fun onActivityCreated(savedInstanceState: Bundle?) { binding.rotateLoading.show() diff --git a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt index 7899f5416..27c90b97f 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt @@ -9,6 +9,7 @@ import android.view.MenuItem import android.view.View import android.view.ViewGroup import androidx.appcompat.widget.Toolbar +import androidx.fragment.app.viewModels import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.App import io.legado.app.R @@ -23,8 +24,11 @@ import io.legado.app.databinding.DialogRecyclerViewBinding import io.legado.app.databinding.ItemSourceImportBinding import io.legado.app.help.AppConfig import io.legado.app.lib.dialogs.alert -import io.legado.app.utils.* +import io.legado.app.utils.dp +import io.legado.app.utils.putPrefBoolean +import io.legado.app.utils.splitNotBlank import io.legado.app.utils.viewbindingdelegate.viewBinding +import io.legado.app.utils.visible /** @@ -34,8 +38,7 @@ class ImportBookSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickList private val binding by viewBinding(DialogRecyclerViewBinding::bind) - val viewModel: ImportBookSourceViewModel - get() = getViewModelOfActivity(ImportBookSourceViewModel::class.java) + val viewModel: ImportBookSourceViewModel by viewModels() lateinit var adapter: SourcesAdapter override fun onStart() { 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 bff45583b..05c5bd9b6 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 @@ -1,6 +1,7 @@ package io.legado.app.ui.association import android.os.Bundle +import androidx.activity.viewModels import io.legado.app.App import io.legado.app.base.VMBaseActivity import io.legado.app.constant.Theme @@ -8,7 +9,7 @@ import io.legado.app.data.entities.ReplaceRule import io.legado.app.databinding.ActivityTranslucenceBinding import io.legado.app.help.IntentDataHelp import io.legado.app.lib.dialogs.alert -import io.legado.app.utils.getViewModel + import io.legado.app.utils.toastOnUi class ImportReplaceRuleActivity : @@ -21,7 +22,7 @@ class ImportReplaceRuleActivity : } override val viewModel: ImportReplaceRuleViewModel - get() = getViewModel(ImportReplaceRuleViewModel::class.java) + by viewModels() override fun onActivityCreated(savedInstanceState: Bundle?) { binding.rotateLoading.show() 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 69105cfba..3a8efd37b 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 @@ -1,13 +1,14 @@ package io.legado.app.ui.association import android.os.Bundle +import androidx.activity.viewModels import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.Theme import io.legado.app.databinding.ActivityTranslucenceBinding import io.legado.app.help.IntentDataHelp import io.legado.app.lib.dialogs.alert -import io.legado.app.utils.getViewModel + import io.legado.app.utils.toastOnUi class ImportRssSourceActivity : @@ -20,7 +21,7 @@ class ImportRssSourceActivity : } override val viewModel: ImportRssSourceViewModel - get() = getViewModel(ImportRssSourceViewModel::class.java) + by viewModels() override fun onActivityCreated(savedInstanceState: Bundle?) { binding.rotateLoading.show() diff --git a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt index 6b7c211bb..38d4ecfcf 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt @@ -9,6 +9,7 @@ import android.view.MenuItem import android.view.View import android.view.ViewGroup import androidx.appcompat.widget.Toolbar +import androidx.fragment.app.activityViewModels import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.App import io.legado.app.R @@ -23,8 +24,11 @@ import io.legado.app.databinding.DialogRecyclerViewBinding import io.legado.app.databinding.ItemSourceImportBinding import io.legado.app.help.AppConfig import io.legado.app.lib.dialogs.alert -import io.legado.app.utils.* +import io.legado.app.utils.dp +import io.legado.app.utils.putPrefBoolean +import io.legado.app.utils.splitNotBlank import io.legado.app.utils.viewbindingdelegate.viewBinding +import io.legado.app.utils.visible /** * 导入rss源弹出窗口 @@ -34,7 +38,7 @@ class ImportRssSourceDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListe private val binding by viewBinding(DialogRecyclerViewBinding::bind) val viewModel: ImportRssSourceViewModel - get() = getViewModelOfActivity(ImportRssSourceViewModel::class.java) + by activityViewModels() lateinit var adapter: SourcesAdapter override fun onStart() { 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 f271c87ee..38f9e338c 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 @@ -9,6 +9,7 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.widget.SeekBar +import androidx.activity.viewModels import com.bumptech.glide.RequestBuilder import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions import com.bumptech.glide.request.RequestOptions.bitmapTransform @@ -39,7 +40,7 @@ class AudioPlayActivity : ChangeSourceDialog.CallBack { override val viewModel: AudioPlayViewModel - get() = getViewModel(AudioPlayViewModel::class.java) + by viewModels() private var requestCodeChapter = 8461 private var adjustProgress = false 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 216fc076a..1981e5736 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 @@ -3,6 +3,7 @@ package io.legado.app.ui.book.arrange import android.os.Bundle import android.view.Menu import android.view.MenuItem +import androidx.activity.viewModels import androidx.appcompat.widget.PopupMenu import androidx.lifecycle.LiveData import androidx.recyclerview.widget.ItemTouchHelper @@ -25,7 +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.getViewModel + class ArrangeBookActivity : VMBaseActivity(), @@ -34,7 +35,7 @@ class ArrangeBookActivity : VMBaseActivity = arrayListOf() private val groupRequestCode = 22 private val addToGroupRequestCode = 34 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 1e417a1a0..69234986d 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 @@ -5,6 +5,7 @@ import android.content.Intent import android.os.Bundle import android.view.Menu import android.view.MenuItem +import androidx.activity.viewModels import androidx.lifecycle.LiveData import androidx.recyclerview.widget.LinearLayoutManager import com.google.android.material.snackbar.Snackbar @@ -46,7 +47,7 @@ class CacheActivity : VMBaseActivity() private var groupId: Long = -1 override val viewModel: CacheViewModel - get() = getViewModel(CacheViewModel::class.java) + by viewModels() override fun getViewBinding(): ActivityCacheBookBinding { return ActivityCacheBookBinding.inflate(layoutInflater) diff --git a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt index 483fbbb75..cf3eb8d1f 100644 --- a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt @@ -7,6 +7,7 @@ import android.view.View import android.view.ViewGroup import androidx.appcompat.widget.Toolbar import androidx.fragment.app.FragmentManager +import androidx.fragment.app.viewModels import androidx.recyclerview.widget.GridLayoutManager import io.legado.app.R import io.legado.app.base.BaseDialogFragment @@ -14,7 +15,7 @@ import io.legado.app.databinding.DialogChangeCoverBinding import io.legado.app.lib.theme.primaryColor import io.legado.app.utils.applyTint import io.legado.app.utils.getSize -import io.legado.app.utils.getViewModel + import io.legado.app.utils.viewbindingdelegate.viewBinding @@ -39,7 +40,7 @@ class ChangeCoverDialog : BaseDialogFragment(), private val binding by viewBinding(DialogChangeCoverBinding::bind) private var callBack: CallBack? = null - private lateinit var viewModel: ChangeCoverViewModel + private val viewModel: ChangeCoverViewModel by viewModels() lateinit var adapter: CoverAdapter override fun onStart() { @@ -54,7 +55,6 @@ class ChangeCoverDialog : BaseDialogFragment(), savedInstanceState: Bundle? ): View? { callBack = activity as? CallBack - viewModel = getViewModel(ChangeCoverViewModel::class.java) return inflater.inflate(R.layout.dialog_change_cover, container) } diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt index fe26451fb..918d8f26a 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceDialog.kt @@ -5,6 +5,7 @@ import android.view.* import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.Toolbar import androidx.fragment.app.FragmentManager +import androidx.fragment.app.viewModels import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import io.legado.app.App @@ -45,7 +46,7 @@ class ChangeSourceDialog : BaseDialogFragment(), private val binding by viewBinding(DialogChangeSourceBinding::bind) private val groups = linkedSetOf() private var callBack: CallBack? = null - private lateinit var viewModel: ChangeSourceViewModel + private val viewModel: ChangeSourceViewModel by viewModels() lateinit var adapter: ChangeSourceAdapter override fun onStart() { @@ -60,7 +61,6 @@ class ChangeSourceDialog : BaseDialogFragment(), savedInstanceState: Bundle? ): View? { callBack = activity as? CallBack - viewModel = getViewModel(ChangeSourceViewModel::class.java) return inflater.inflate(R.layout.dialog_change_source, container) } 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 136717903..f4ce03bd3 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 @@ -1,6 +1,7 @@ package io.legado.app.ui.book.explore import android.os.Bundle +import androidx.activity.viewModels import androidx.recyclerview.widget.RecyclerView import io.legado.app.R import io.legado.app.base.VMBaseActivity @@ -11,13 +12,13 @@ import io.legado.app.databinding.ViewLoadMoreBinding 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.getViewModel + import org.jetbrains.anko.startActivity class ExploreShowActivity : VMBaseActivity(), ExploreShowAdapter.CallBack { override val viewModel: ExploreShowViewModel - get() = getViewModel(ExploreShowViewModel::class.java) + by viewModels() private lateinit var adapter: ExploreShowAdapter private lateinit var loadMoreView: LoadMoreView diff --git a/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt index 2c64e7fe1..84ef201bf 100644 --- a/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt @@ -8,6 +8,7 @@ import android.view.MenuItem import android.view.View import android.view.ViewGroup import androidx.appcompat.widget.Toolbar +import androidx.fragment.app.viewModels import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -26,12 +27,15 @@ import io.legado.app.lib.theme.backgroundColor import io.legado.app.lib.theme.primaryColor 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.applyTint +import io.legado.app.utils.getSize +import io.legado.app.utils.requestInputMethod import io.legado.app.utils.viewbindingdelegate.viewBinding +import io.legado.app.utils.visible class GroupManageDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { - private lateinit var viewModel: GroupViewModel + private val viewModel: GroupViewModel by viewModels() private lateinit var adapter: GroupAdapter private val binding by viewBinding(DialogRecyclerViewBinding::bind) @@ -46,7 +50,6 @@ class GroupManageDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener container: ViewGroup?, savedInstanceState: Bundle? ): View? { - viewModel = getViewModel(GroupViewModel::class.java) return inflater.inflate(R.layout.dialog_recycler_view, container) } diff --git a/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt b/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt index 9802f7aef..40aa03b6b 100644 --- a/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt @@ -9,6 +9,7 @@ import android.view.View import android.view.ViewGroup import androidx.appcompat.widget.Toolbar import androidx.fragment.app.FragmentManager +import androidx.fragment.app.viewModels import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -29,7 +30,7 @@ import io.legado.app.ui.widget.recycler.ItemTouchCallback import io.legado.app.ui.widget.recycler.VerticalDivider import io.legado.app.utils.applyTint import io.legado.app.utils.getSize -import io.legado.app.utils.getViewModel + import io.legado.app.utils.requestInputMethod import io.legado.app.utils.viewbindingdelegate.viewBinding @@ -52,7 +53,7 @@ class GroupSelectDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener private val binding by viewBinding(DialogBookGroupPickerBinding::bind) private var requestCode: Int = -1 - private lateinit var viewModel: GroupViewModel + private val viewModel: GroupViewModel by viewModels() private lateinit var adapter: GroupAdapter private var callBack: CallBack? = null private var groupId: Long = 0 @@ -68,7 +69,6 @@ class GroupSelectDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener container: ViewGroup?, savedInstanceState: Bundle? ): View? { - viewModel = getViewModel(GroupViewModel::class.java) return inflater.inflate(R.layout.dialog_book_group_picker, container) } 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 13c2c8dd1..6018f4531 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 @@ -9,6 +9,7 @@ import android.view.Menu import android.view.MenuItem import android.widget.CheckBox import android.widget.LinearLayout +import androidx.activity.viewModels import com.bumptech.glide.RequestBuilder import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions import com.bumptech.glide.request.RequestOptions.bitmapTransform @@ -53,7 +54,7 @@ class BookInfoActivity : private val requestCodeRead = 432 override val viewModel: BookInfoViewModel - get() = getViewModel(BookInfoViewModel::class.java) + by viewModels() override fun getViewBinding(): ActivityBookInfoBinding { return ActivityBookInfoBinding.inflate(layoutInflater) 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 02dc70c1e..4d16ba6bd 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 @@ -6,6 +6,7 @@ import android.net.Uri import android.os.Bundle import android.view.Menu import android.view.MenuItem +import androidx.activity.viewModels import androidx.documentfile.provider.DocumentFile import io.legado.app.R import io.legado.app.base.VMBaseActivity @@ -24,7 +25,7 @@ class BookInfoEditActivity : private val resultSelectCover = 132 override val viewModel: BookInfoEditViewModel - get() = getViewModel(BookInfoEditViewModel::class.java) + by viewModels() override fun getViewBinding(): ActivityBookInfoEditBinding { return ActivityBookInfoEditBinding.inflate(layoutInflater) 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 fcd9b0699..f3f98b81b 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 @@ -8,6 +8,7 @@ import android.os.Bundle import android.provider.DocumentsContract import android.view.Menu import android.view.MenuItem +import androidx.activity.viewModels import androidx.appcompat.widget.PopupMenu import androidx.documentfile.provider.DocumentFile import androidx.lifecycle.LiveData @@ -48,7 +49,7 @@ class ImportBookActivity : VMBaseActivity() { override val viewModel: ReadBookViewModel - get() = getViewModel(ReadBookViewModel::class.java) + by viewModels() var bottomDialog = 0 override fun getViewBinding(): ActivityBookReadBinding { diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt index 12099ab2f..bb40914d2 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt @@ -8,6 +8,7 @@ import android.view.MenuItem import android.view.View import android.view.ViewGroup import androidx.appcompat.widget.Toolbar +import androidx.fragment.app.viewModels import androidx.lifecycle.LiveData import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.App @@ -32,7 +33,7 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { private val binding by viewBinding(DialogRecyclerViewBinding::bind) lateinit var adapter: Adapter - lateinit var viewModel: SpeakEngineViewModel + private val viewModel: SpeakEngineViewModel by viewModels() private var httpTTSData: LiveData>? = null var engineId = App.INSTANCE.getPrefLong(PreferKey.speakEngine) @@ -47,7 +48,6 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener container: ViewGroup?, savedInstanceState: Bundle? ): View? { - viewModel = getViewModel(SpeakEngineViewModel::class.java) return inflater.inflate(R.layout.dialog_recycler_view, container) } diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt index 6030ba0de..ed531176f 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt @@ -9,6 +9,7 @@ import android.view.View import android.view.ViewGroup import androidx.appcompat.widget.Toolbar import androidx.fragment.app.FragmentManager +import androidx.fragment.app.viewModels import androidx.lifecycle.LiveData import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager @@ -42,7 +43,7 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { private var tocRegexLiveData: LiveData>? = null var selectedName: String? = null private var durRegex: String? = null - lateinit var viewModel: TocRegexViewModel + private val viewModel: TocRegexViewModel by viewModels() private val binding by viewBinding(DialogTocRegexBinding::bind) override fun onStart() { @@ -56,7 +57,6 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { container: ViewGroup?, savedInstanceState: Bundle? ): View? { - viewModel = getViewModel(TocRegexViewModel::class.java) return inflater.inflate(R.layout.dialog_toc_regex, container) } 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 5cfce4116..033f3a4d1 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 @@ -6,6 +6,7 @@ import android.view.Menu import android.view.MenuItem import android.view.View.GONE import android.view.View.VISIBLE +import androidx.activity.viewModels import androidx.appcompat.widget.SearchView import androidx.lifecycle.LiveData import androidx.recyclerview.widget.LinearLayoutManager @@ -37,7 +38,7 @@ class SearchActivity : VMBaseActivity() { override val viewModel: BookSourceDebugModel - get() = getViewModel(BookSourceDebugModel::class.java) + by viewModels() private lateinit var adapter: BookSourceDebugAdapter private lateinit var searchView: SearchView diff --git a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt index ed090bbdd..88db0b9f9 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt @@ -10,6 +10,7 @@ import android.view.MenuItem import android.view.ViewTreeObserver import android.widget.EditText import android.widget.PopupWindow +import androidx.activity.viewModels import androidx.recyclerview.widget.LinearLayoutManager import com.google.android.material.tabs.TabLayout import io.legado.app.R @@ -38,7 +39,7 @@ class BookSourceEditActivity : FilePickerDialog.CallBack, KeyboardToolPop.CallBack { override val viewModel: BookSourceEditViewModel - get() = getViewModel(BookSourceEditViewModel::class.java) + by viewModels() private val qrRequestCode = 101 private val selectPathRequestCode = 102 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 c2b739cd2..35b7ab175 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 @@ -7,6 +7,7 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.view.SubMenu +import androidx.activity.viewModels import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.SearchView import androidx.documentfile.provider.DocumentFile @@ -49,7 +50,7 @@ class BookSourceActivity : VMBaseActivity(R.layout.fragment_bookmark), BookmarkAdapter.Callback, ChapterListViewModel.BookmarkCallBack { - override val viewModel: ChapterListViewModel - get() = getViewModelOfActivity(ChapterListViewModel::class.java) + override val viewModel: ChapterListViewModel by activityViewModels() private val binding by viewBinding(FragmentBookmarkBinding::bind) private lateinit var adapter: BookmarkAdapter private var bookmarkLiveData: LiveData>? = null diff --git a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListActivity.kt b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListActivity.kt index 4fa998b22..dfe49d6e7 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListActivity.kt @@ -2,6 +2,7 @@ package io.legado.app.ui.book.toc import android.os.Bundle import android.view.Menu +import androidx.activity.viewModels import androidx.appcompat.widget.SearchView import androidx.core.view.isGone import androidx.fragment.app.Fragment @@ -14,14 +15,14 @@ import io.legado.app.databinding.ActivityChapterListBinding import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.accentColor import io.legado.app.lib.theme.primaryTextColor -import io.legado.app.utils.getViewModel + import io.legado.app.utils.gone import io.legado.app.utils.visible class ChapterListActivity : VMBaseActivity() { override val viewModel: ChapterListViewModel - get() = getViewModel(ChapterListViewModel::class.java) + by viewModels() private lateinit var tabLayout: TabLayout private var searchView: SearchView? = null diff --git a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt index 236afdf10..890f4c5fd 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt @@ -5,6 +5,7 @@ import android.app.Activity.RESULT_OK import android.content.Intent import android.os.Bundle import android.view.View +import androidx.fragment.app.activityViewModels import androidx.lifecycle.LiveData import io.legado.app.App import io.legado.app.R @@ -19,7 +20,7 @@ import io.legado.app.lib.theme.getPrimaryTextColor 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.getViewModelOfActivity + import io.legado.app.utils.observeEvent import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.Dispatchers.IO @@ -32,8 +33,7 @@ import kotlin.math.min class ChapterListFragment : VMBaseFragment(R.layout.fragment_chapter_list), ChapterListAdapter.Callback, ChapterListViewModel.ChapterListCallBack { - override val viewModel: ChapterListViewModel - get() = getViewModelOfActivity(ChapterListViewModel::class.java) + override val viewModel: ChapterListViewModel by activityViewModels() private val binding by viewBinding(FragmentChapterListBinding::bind) lateinit var adapter: ChapterListAdapter private var durChapterIndex = 0 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 8b6c7701c..2d7ab0cbd 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 @@ -1,16 +1,17 @@ package io.legado.app.ui.config import android.os.Bundle +import androidx.activity.viewModels import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.EventBus import io.legado.app.databinding.ActivityConfigBinding -import io.legado.app.utils.getViewModel + import io.legado.app.utils.observeEvent class ConfigActivity : VMBaseActivity() { override val viewModel: ConfigViewModel - get() = getViewModel(ConfigViewModel::class.java) + by viewModels() override fun getViewBinding(): ActivityConfigBinding { return ActivityConfigBinding.inflate(layoutInflater) 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 17005f2cc..673221636 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 @@ -4,6 +4,7 @@ import android.os.Bundle import android.view.KeyEvent import android.view.MenuItem import android.view.ViewGroup +import androidx.activity.viewModels import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentStatePagerAdapter @@ -29,7 +30,7 @@ import io.legado.app.ui.main.explore.ExploreFragment import io.legado.app.ui.main.my.MyFragment import io.legado.app.ui.main.rss.RssFragment import io.legado.app.ui.widget.dialog.TextDialog -import io.legado.app.utils.getViewModel + import io.legado.app.utils.hideSoftInput import io.legado.app.utils.observeEvent import io.legado.app.utils.toastOnUi @@ -39,8 +40,7 @@ class MainActivity : VMBaseActivity(), BottomNavigationView.OnNavigationItemSelectedListener, BottomNavigationView.OnNavigationItemReselectedListener, ViewPager.OnPageChangeListener by ViewPager.SimpleOnPageChangeListener() { - override val viewModel: MainViewModel - get() = getViewModel(MainViewModel::class.java) + override val viewModel: MainViewModel by viewModels() private var exitTime: Long = 0 private var bookshelfReselected: Long = 0 private var exploreReselected: Long = 0 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 6cbf4a678..ab47477dc 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 @@ -9,9 +9,7 @@ import android.view.MenuItem import android.view.View import android.view.ViewGroup import androidx.appcompat.widget.SearchView -import androidx.fragment.app.Fragment -import androidx.fragment.app.FragmentManager -import androidx.fragment.app.FragmentStatePagerAdapter +import androidx.fragment.app.* import androidx.lifecycle.LiveData import com.google.android.material.tabs.TabLayout import io.legado.app.App @@ -49,10 +47,8 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b private val requestCodeImportBookshelf = 312 private val binding by viewBinding(FragmentBookshelfBinding::bind) - override val viewModel: BookshelfViewModel - get() = getViewModel(BookshelfViewModel::class.java) - private val activityViewModel: MainViewModel - get() = getViewModelOfActivity(MainViewModel::class.java) + override val viewModel: BookshelfViewModel by viewModels() + private val activityViewModel: MainViewModel by activityViewModels() private lateinit var adapter: FragmentStatePagerAdapter private lateinit var tabLayout: TabLayout private var bookGroupLiveData: LiveData>? = null diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt index 6f0990a76..09fd306a4 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt @@ -3,6 +3,7 @@ package io.legado.app.ui.main.bookshelf.books import android.os.Bundle import android.view.View import androidx.core.view.isGone +import androidx.fragment.app.activityViewModels import androidx.lifecycle.LiveData import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager @@ -24,7 +25,10 @@ import io.legado.app.ui.audio.AudioPlayActivity import io.legado.app.ui.book.info.BookInfoActivity import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.ui.main.MainViewModel -import io.legado.app.utils.* +import io.legado.app.utils.cnCompare +import io.legado.app.utils.getPrefInt +import io.legado.app.utils.observeEvent +import io.legado.app.utils.startActivity import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlin.math.max @@ -47,7 +51,7 @@ class BooksFragment : BaseFragment(R.layout.fragment_books), private val binding by viewBinding(FragmentBooksBinding::bind) private val activityViewModel: MainViewModel - get() = getViewModelOfActivity(MainViewModel::class.java) + by activityViewModels() private lateinit var booksAdapter: BaseBooksAdapter<*> private var bookshelfLiveData: LiveData>? = null private var position = 0 diff --git a/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt b/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt index ff311f503..56cf3a4d9 100644 --- a/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt @@ -7,6 +7,7 @@ import android.view.SubMenu import android.view.View import androidx.appcompat.widget.SearchView import androidx.core.view.isGone +import androidx.fragment.app.viewModels import androidx.lifecycle.LiveData import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.LinearLayoutManager @@ -23,7 +24,7 @@ import io.legado.app.lib.theme.primaryTextColor import io.legado.app.ui.book.explore.ExploreShowActivity import io.legado.app.ui.book.source.edit.BookSourceEditActivity import io.legado.app.utils.cnCompare -import io.legado.app.utils.getViewModel + import io.legado.app.utils.splitNotBlank import io.legado.app.utils.startActivity import io.legado.app.utils.viewbindingdelegate.viewBinding @@ -33,8 +34,7 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding */ class ExploreFragment : VMBaseFragment(R.layout.fragment_explore), ExploreAdapter.CallBack { - override val viewModel: ExploreViewModel - get() = getViewModel(ExploreViewModel::class.java) + override val viewModel: ExploreViewModel by viewModels() private val binding by viewBinding(FragmentExploreBinding::bind) private lateinit var adapter: ExploreAdapter private lateinit var linearLayoutManager: LinearLayoutManager diff --git a/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt b/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt index 62a777d37..8e185fd57 100644 --- a/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt @@ -6,6 +6,7 @@ import android.view.MenuItem import android.view.SubMenu import android.view.View import androidx.appcompat.widget.SearchView +import androidx.fragment.app.viewModels import androidx.lifecycle.LiveData import io.legado.app.App import io.legado.app.R @@ -24,7 +25,7 @@ import io.legado.app.ui.rss.source.manage.RssSourceActivity import io.legado.app.ui.rss.source.manage.RssSourceViewModel import io.legado.app.ui.rss.subscription.RuleSubActivity import io.legado.app.utils.cnCompare -import io.legado.app.utils.getViewModel + import io.legado.app.utils.splitNotBlank import io.legado.app.utils.startActivity import io.legado.app.utils.viewbindingdelegate.viewBinding @@ -39,7 +40,7 @@ class RssFragment : VMBaseFragment(R.layout.fragment_rss), private lateinit var adapter: RssAdapter private lateinit var searchView: SearchView override val viewModel: RssSourceViewModel - get() = getViewModel(RssSourceViewModel::class.java) + by viewModels() private var liveRssData: LiveData>? = null private val groups = linkedSetOf() private var liveGroup: LiveData>? = null diff --git a/app/src/main/java/io/legado/app/ui/replace/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/replace/GroupManageDialog.kt index 28cfffae6..a589a84db 100644 --- a/app/src/main/java/io/legado/app/ui/replace/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/replace/GroupManageDialog.kt @@ -9,6 +9,7 @@ import android.view.View import android.view.ViewGroup import androidx.appcompat.widget.Toolbar import androidx.fragment.app.DialogFragment +import androidx.fragment.app.activityViewModels import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.App import io.legado.app.R @@ -27,7 +28,7 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { - private lateinit var viewModel: ReplaceRuleViewModel + private val viewModel: ReplaceRuleViewModel by activityViewModels() private lateinit var adapter: GroupAdapter private val binding by viewBinding(DialogRecyclerViewBinding::bind) @@ -42,7 +43,6 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { container: ViewGroup?, savedInstanceState: Bundle? ): View? { - viewModel = getViewModelOfActivity(ReplaceRuleViewModel::class.java) return inflater.inflate(R.layout.dialog_recycler_view, container) } 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 edeafbe3a..f7b32f174 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 @@ -7,6 +7,7 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.view.SubMenu +import androidx.activity.viewModels import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.SearchView import androidx.documentfile.provider.DocumentFile @@ -50,8 +51,7 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.fragme private val binding by viewBinding(FragmentRssArticlesBinding::bind) private val activityViewModel: RssSortViewModel - get() = getViewModelOfActivity(RssSortViewModel::class.java) + by activityViewModels() override val viewModel: RssArticlesViewModel - get() = getViewModel(RssArticlesViewModel::class.java) + by viewModels() lateinit var adapter: BaseRssArticlesAdapter<*> private lateinit var loadMoreView: LoadMoreView private var rssArticlesData: LiveData>? = null 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 705276072..2dcc2d5b8 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 @@ -5,6 +5,7 @@ import android.content.Intent import android.os.Bundle import android.view.Menu import android.view.MenuItem +import androidx.activity.viewModels import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentStatePagerAdapter @@ -12,7 +13,7 @@ import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.databinding.ActivityRssArtivlesBinding import io.legado.app.ui.rss.source.edit.RssSourceEditActivity -import io.legado.app.utils.getViewModel + import io.legado.app.utils.gone import io.legado.app.utils.visible import org.jetbrains.anko.startActivityForResult @@ -20,7 +21,7 @@ import org.jetbrains.anko.startActivityForResult class RssSortActivity : VMBaseActivity() { override val viewModel: RssSortViewModel - get() = getViewModel(RssSortViewModel::class.java) + by viewModels() private val editSource = 12319 private val fragments = linkedMapOf() private lateinit var adapter: TabFragmentPageAdapter 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 0a7498c08..ae843eaeb 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 @@ -10,6 +10,7 @@ import android.os.Bundle import android.os.Environment import android.view.* import android.webkit.* +import androidx.activity.viewModels import androidx.core.view.size import io.legado.app.R import io.legado.app.base.VMBaseActivity @@ -34,7 +35,7 @@ class ReadRssActivity : VMBaseActivity ReadRssViewModel.CallBack { override val viewModel: ReadRssViewModel - get() = getViewModel(ReadRssViewModel::class.java) + by viewModels() private val savePathRequestCode = 132 private val imagePathKey = "" private var starMenuItem: MenuItem? = null 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 2a54205cc..e5cb45db4 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 @@ -2,12 +2,13 @@ package io.legado.app.ui.rss.source.debug import android.os.Bundle import android.widget.SearchView +import androidx.activity.viewModels import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.databinding.ActivitySourceDebugBinding import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.accentColor -import io.legado.app.utils.getViewModel + import io.legado.app.utils.gone import io.legado.app.utils.toastOnUi import kotlinx.coroutines.launch @@ -16,7 +17,7 @@ import kotlinx.coroutines.launch class RssSourceDebugActivity : VMBaseActivity() { override val viewModel: RssSourceDebugModel - get() = getViewModel(RssSourceDebugModel::class.java) + by viewModels() private lateinit var adapter: RssSourceDebugAdapter 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 faf737a90..98f128fba 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 @@ -10,6 +10,7 @@ import android.view.MenuItem import android.view.ViewTreeObserver import android.widget.EditText import android.widget.PopupWindow +import androidx.activity.viewModels import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.AppConst @@ -42,7 +43,7 @@ class RssSourceEditActivity : } override val viewModel: RssSourceEditViewModel - get() = getViewModel(RssSourceEditViewModel::class.java) + by viewModels() override fun onActivityCreated(savedInstanceState: Bundle?) { initView() diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/GroupManageDialog.kt index 2469e587c..c38bb3d71 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/GroupManageDialog.kt @@ -8,6 +8,7 @@ import android.view.MenuItem import android.view.View import android.view.ViewGroup import androidx.appcompat.widget.Toolbar +import androidx.fragment.app.activityViewModels import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.App import io.legado.app.R @@ -27,7 +28,7 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding class GroupManageDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { - private lateinit var viewModel: RssSourceViewModel + private val viewModel: RssSourceViewModel by activityViewModels() private lateinit var adapter: GroupAdapter private val binding by viewBinding(DialogRecyclerViewBinding::bind) @@ -42,7 +43,6 @@ class GroupManageDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener container: ViewGroup?, savedInstanceState: Bundle? ): View? { - viewModel = getViewModelOfActivity(RssSourceViewModel::class.java) return inflater.inflate(R.layout.dialog_recycler_view, container) } 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 013ae9fd1..1e4a0ea44 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 @@ -7,6 +7,7 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.view.SubMenu +import androidx.activity.viewModels import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.SearchView import androidx.documentfile.provider.DocumentFile @@ -46,7 +47,7 @@ class RssSourceActivity : VMBaseActivity AppCompatActivity.getViewModel(clazz: Class) = - ViewModelProvider(this).get(clazz) - -fun Fragment.getViewModel(clazz: Class) = - ViewModelProvider(this).get(clazz) - -/** - * 与activity数据同步 - */ -fun Fragment.getViewModelOfActivity(clazz: Class) = - ViewModelProvider(requireActivity()).get(clazz) \ No newline at end of file