Skip to content

Commit

Permalink
Optimize the way to get view model
Browse files Browse the repository at this point in the history
  • Loading branch information
Super12138 committed Apr 20, 2024
1 parent 9b8a5e0 commit d0365c3
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,6 +13,7 @@ import cn.super12138.todo.views.BaseActivity
import me.zhanghai.android.fastscroll.FastScrollerBuilder

class AllTasksActivity : BaseActivity() {
private val viewModel by viewModels<AllTasksViewModel>()
private lateinit var binding: ActivityAllTasksBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ 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
import cn.super12138.todo.databinding.FragmentProgressBinding


class ProgressFragment : Fragment() {
private lateinit var viewModel: ProgressFragmentViewModel
private val viewModel by viewModels<ProgressFragmentViewModel>()
private lateinit var binding: FragmentProgressBinding

override fun onCreateView(
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,7 +29,8 @@ import me.zhanghai.android.fastscroll.FastScrollerBuilder
import java.util.UUID

class ToDoFragment : Fragment() {

private val progressViewModel by viewModels<ProgressFragmentViewModel>()
private val todoViewModel by viewModels<ToDoFragmentViewModel>()
private lateinit var binding: FragmentTodoBinding
private lateinit var toDoDialogBinding: DialogAddTodoBinding

Expand All @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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" }
Expand Down

0 comments on commit d0365c3

Please sign in to comment.