ViewModelExtensions

pull/31/head
Invinciblelee 6 years ago
parent 31d0b722e2
commit f221e23641
  1. 6
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt
  2. 6
      app/src/main/java/io/legado/app/ui/search/SearchActivity.kt
  3. 12
      app/src/main/java/io/legado/app/utils/ViewModelExtensions.kt

@ -7,18 +7,18 @@ import android.view.MenuItem
import androidx.appcompat.app.ActionBarDrawerToggle import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.core.view.GravityCompat import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout import androidx.drawerlayout.widget.DrawerLayout
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.navigation.NavigationView import com.google.android.material.navigation.NavigationView
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseActivity import io.legado.app.base.BaseActivity
import io.legado.app.ui.search.SearchActivity import io.legado.app.ui.search.SearchActivity
import io.legado.app.utils.getViewModel
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.app_bar_main.* import kotlinx.android.synthetic.main.app_bar_main.*
import kotlinx.android.synthetic.main.view_titlebar.*
class MainActivity : BaseActivity<MainDataBinding, MainViewModel>(), NavigationView.OnNavigationItemSelectedListener { class MainActivity : BaseActivity<MainDataBinding, MainViewModel>(), NavigationView.OnNavigationItemSelectedListener {
override val viewModel: MainViewModel override val viewModel: MainViewModel
get() = ViewModelProvider.AndroidViewModelFactory.getInstance(application).create(MainViewModel::class.java) get() = getViewModel(MainViewModel::class.java)
override val layoutID: Int override val layoutID: Int
get() = R.layout.activity_main get() = R.layout.activity_main

@ -1,15 +1,15 @@
package io.legado.app.ui.search package io.legado.app.ui.search
import android.os.Bundle import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseActivity import io.legado.app.base.BaseActivity
import io.legado.app.help.LayoutManager import io.legado.app.search.SearchDataBinding
import io.legado.app.utils.getViewModel
class SearchActivity : BaseActivity<SearchDataBinding, SearchViewModel>() { class SearchActivity : BaseActivity<SearchDataBinding, SearchViewModel>() {
override val viewModel: SearchViewModel override val viewModel: SearchViewModel
get() = ViewModelProvider.AndroidViewModelFactory.getInstance(application).create(SearchViewModel::class.java) get() = getViewModel(SearchViewModel::class.java)
override val layoutID: Int override val layoutID: Int
get() = R.layout.activity_search get() = R.layout.activity_search

@ -0,0 +1,12 @@
package io.legado.app.utils
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
fun <T : ViewModel> AppCompatActivity.getViewModel(clazz: Class<T>) = ViewModelProviders.of(this).get(clazz)
fun <T : ViewModel> Fragment.getViewModel(clazz: Class<T>) = ViewModelProviders.of(this).get(clazz)
fun <T : ViewModel> Fragment.getViewModelOfActivity(clazz: Class<T>) = ViewModelProviders.of(requireActivity()).get(clazz)
Loading…
Cancel
Save