Skip to content

Commit

Permalink
Compose ViewModelActivity & ViewModelFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
skydoves committed Aug 9, 2019
1 parent c87ad64 commit 14c4109
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* The MIT License (MIT)
*
* Designed and developed by 2018 skydoves (Jaewoong Eum)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.skydoves.themovies.compose

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import dagger.android.AndroidInjection
import javax.inject.Inject

@SuppressLint("Registered")
open class ViewModelActivity : AppCompatActivity() {

@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory

override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
}

protected inline fun <reified VM : ViewModel>
viewModel(): Lazy<VM> = viewModels { viewModelFactory }

protected inline fun <reified T : ViewDataBinding> binding(resId: Int): Lazy<T> =
lazy { DataBindingUtil.setContentView<T>(this, resId) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* The MIT License (MIT)
*
* Designed and developed by 2018 skydoves (Jaewoong Eum)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.skydoves.themovies.compose

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import dagger.android.support.AndroidSupportInjection
import javax.inject.Inject

open class ViewModelFragment : Fragment() {

@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory

override fun onAttach(context: Context) {
AndroidSupportInjection.inject(this)
super.onAttach(context)
}

protected inline fun <reified VM : ViewModel>
viewModel(): Lazy<VM> = viewModels { viewModelFactory }

protected inline fun <reified T : ViewDataBinding> binding(
inflater: LayoutInflater,
resId: Int,
container: ViewGroup?
): T = DataBindingUtil.inflate<T>(inflater, resId, container, false)
}
3 changes: 2 additions & 1 deletion app/src/main/java/com/skydoves/themovies/di/AppComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import javax.inject.Singleton
@Singleton
@Component(modules = [
AndroidInjectionModule::class,
ComposeModule::class,
ActivityModule::class,
ViewModelModule::class,
NetworkModule::class,
Expand All @@ -42,7 +43,7 @@ interface AppComponent : AndroidInjector<DaggerApplication> {
@Component.Builder
interface Builder {
@BindsInstance
fun application(application: Application): AppComponent.Builder
fun application(application: Application): Builder

fun build(): AppComponent
}
Expand Down
39 changes: 39 additions & 0 deletions app/src/main/java/com/skydoves/themovies/di/ComposeModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* The MIT License (MIT)
*
* Designed and developed by 2018 skydoves (Jaewoong Eum)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.skydoves.themovies.di

import com.skydoves.themovies.compose.ViewModelActivity
import com.skydoves.themovies.compose.ViewModelFragment
import dagger.Module
import dagger.android.ContributesAndroidInjector

@Suppress("unused")
@Module
abstract class ComposeModule {
@ContributesAndroidInjector
internal abstract fun contributeViewModelActivity(): ViewModelActivity

@ContributesAndroidInjector
internal abstract fun contributeViewModelFragment(): ViewModelFragment
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ class PersistenceModule {
@Provides
@Singleton
fun provideDatabase(@NonNull application: Application): AppDatabase {
return Room.databaseBuilder(application, AppDatabase::class.java, "TheMovies.db").allowMainThreadQueries().build()
return Room
.databaseBuilder(application, AppDatabase::class.java, "TheMovies.db")
.allowMainThreadQueries()
.build()
}

@Provides
Expand Down

0 comments on commit 14c4109

Please sign in to comment.