From d0365c3e6795a1ffade7403a48de0e99781166a7 Mon Sep 17 00:00:00 2001 From: Super12138 <70494801+Super12138@users.noreply.github.com> Date: Sat, 20 Apr 2024 16:09:54 +0800 Subject: [PATCH] Optimize the way to get view model --- app/build.gradle.kts | 2 ++ .../cn/super12138/todo/views/all/AllTasksActivity.kt | 5 ++--- .../super12138/todo/views/progress/ProgressFragment.kt | 4 ++-- .../kotlin/cn/super12138/todo/views/todo/ToDoFragment.kt | 9 +++------ gradle/libs.versions.toml | 8 ++++++-- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 44feb5a..1f1e192 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -71,6 +71,8 @@ dependencies { // AndroidX implementation(libs.androidx.core.ktx) implementation(libs.androidx.appcompat) + implementation(libs.androidx.activity) + implementation(libs.androidx.activity.ktx) implementation(libs.androidx.constraintlayout) implementation(libs.androidx.fragment) implementation(libs.androidx.fragment.ktx) diff --git a/app/src/main/kotlin/cn/super12138/todo/views/all/AllTasksActivity.kt b/app/src/main/kotlin/cn/super12138/todo/views/all/AllTasksActivity.kt index ab6982f..34a5075 100644 --- a/app/src/main/kotlin/cn/super12138/todo/views/all/AllTasksActivity.kt +++ b/app/src/main/kotlin/cn/super12138/todo/views/all/AllTasksActivity.kt @@ -3,8 +3,8 @@ package cn.super12138.todo.views.all import android.os.Bundle import android.view.View import android.view.WindowManager +import androidx.activity.viewModels import androidx.lifecycle.Observer -import androidx.lifecycle.ViewModelProvider import androidx.recyclerview.widget.LinearLayoutManager import cn.super12138.todo.ToDoApp import cn.super12138.todo.constant.GlobalValues @@ -13,6 +13,7 @@ import cn.super12138.todo.views.BaseActivity import me.zhanghai.android.fastscroll.FastScrollerBuilder class AllTasksActivity : BaseActivity() { + private val viewModel by viewModels() private lateinit var binding: ActivityAllTasksBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -29,8 +30,6 @@ class AllTasksActivity : BaseActivity() { binding = ActivityAllTasksBinding.inflate(layoutInflater) setContentView(binding.root) - val viewModel = ViewModelProvider(this)[AllTasksViewModel::class.java] - val layoutManager = LinearLayoutManager(ToDoApp.context) binding.allTasksList.layoutManager = layoutManager val adapter = AllTasksAdapter(viewModel.todoListAll, supportFragmentManager) diff --git a/app/src/main/kotlin/cn/super12138/todo/views/progress/ProgressFragment.kt b/app/src/main/kotlin/cn/super12138/todo/views/progress/ProgressFragment.kt index 9e7ad33..2fdc2fa 100644 --- a/app/src/main/kotlin/cn/super12138/todo/views/progress/ProgressFragment.kt +++ b/app/src/main/kotlin/cn/super12138/todo/views/progress/ProgressFragment.kt @@ -5,6 +5,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment +import androidx.fragment.app.viewModels import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider import cn.super12138.todo.R @@ -12,7 +13,7 @@ import cn.super12138.todo.databinding.FragmentProgressBinding class ProgressFragment : Fragment() { - private lateinit var viewModel: ProgressFragmentViewModel + private val viewModel by viewModels() private lateinit var binding: FragmentProgressBinding override fun onCreateView( @@ -27,7 +28,6 @@ class ProgressFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - viewModel = ViewModelProvider(requireActivity())[ProgressFragmentViewModel::class.java] viewModel.progress.observe(viewLifecycleOwner, Observer { value -> binding.progressBar.setProgressCompat(value, true) diff --git a/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragment.kt b/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragment.kt index f6c42f4..cbc1b49 100644 --- a/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragment.kt +++ b/app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragment.kt @@ -6,6 +6,7 @@ import android.view.View import android.view.ViewGroup import androidx.appcompat.app.AlertDialog import androidx.fragment.app.Fragment +import androidx.fragment.app.viewModels import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.lifecycleScope @@ -28,7 +29,8 @@ import me.zhanghai.android.fastscroll.FastScrollerBuilder import java.util.UUID class ToDoFragment : Fragment() { - + private val progressViewModel by viewModels() + private val todoViewModel by viewModels() private lateinit var binding: FragmentTodoBinding private lateinit var toDoDialogBinding: DialogAddTodoBinding @@ -54,11 +56,6 @@ class ToDoFragment : Fragment() { WindowInsetsCompat.CONSUMED }*/ - val progressViewModel = - ViewModelProvider(requireActivity())[ProgressFragmentViewModel::class.java] - val todoViewModel = - ViewModelProvider(requireActivity())[ToDoFragmentViewModel::class.java] - val todoList = todoViewModel.todoList val layoutManager = LinearLayoutManager(ToDoApp.context) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 6543566..34b12b1 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,7 +1,9 @@ [versions] +coreKtx = "1.13.0" appcompat = "1.6.1" +activity = "1.8.2" +activityKtx = "1.8.2" constraintlayout = "2.1.4" -coreKtx = "1.13.0" preference = "1.2.1" preferenceKtx = "1.2.1" lifecycleViewmodelKtx = "2.7.0" @@ -24,9 +26,11 @@ kotlin-android = "1.9.23" ksp = "1.9.23-1.0.20" [libraries] +androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "coreKtx" } androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" } +androidx-activity = { group = "androidx.activity", name = "activity", version.ref = "activity" } +androidx-activity-ktx = { group = "androidx.activity", name = "activity", version.ref = "activityKtx" } androidx-constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintlayout" } -androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "coreKtx" } androidx-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espressoCore" } androidx-junit = { module = "androidx.test.ext:junit", version.ref = "junitVersion" } androidx-preference-ktx = { module = "androidx.preference:preference-ktx", version.ref = "preferenceKtx" }