diff --git a/app/src/main/java/com/skydoves/themovies/compose/ViewModelActivity.kt b/app/src/main/java/com/skydoves/themovies/compose/ViewModelActivity.kt new file mode 100644 index 0000000..bb470e4 --- /dev/null +++ b/app/src/main/java/com/skydoves/themovies/compose/ViewModelActivity.kt @@ -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 + viewModel(): Lazy = viewModels { viewModelFactory } + + protected inline fun binding(resId: Int): Lazy = + lazy { DataBindingUtil.setContentView(this, resId) } +} diff --git a/app/src/main/java/com/skydoves/themovies/compose/ViewModelFragment.kt b/app/src/main/java/com/skydoves/themovies/compose/ViewModelFragment.kt new file mode 100644 index 0000000..c97d824 --- /dev/null +++ b/app/src/main/java/com/skydoves/themovies/compose/ViewModelFragment.kt @@ -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 + viewModel(): Lazy = viewModels { viewModelFactory } + + protected inline fun binding( + inflater: LayoutInflater, + resId: Int, + container: ViewGroup? + ): T = DataBindingUtil.inflate(inflater, resId, container, false) +} diff --git a/app/src/main/java/com/skydoves/themovies/di/AppComponent.kt b/app/src/main/java/com/skydoves/themovies/di/AppComponent.kt index fc451f3..0dbb0be 100644 --- a/app/src/main/java/com/skydoves/themovies/di/AppComponent.kt +++ b/app/src/main/java/com/skydoves/themovies/di/AppComponent.kt @@ -34,6 +34,7 @@ import javax.inject.Singleton @Singleton @Component(modules = [ AndroidInjectionModule::class, + ComposeModule::class, ActivityModule::class, ViewModelModule::class, NetworkModule::class, @@ -42,7 +43,7 @@ interface AppComponent : AndroidInjector { @Component.Builder interface Builder { @BindsInstance - fun application(application: Application): AppComponent.Builder + fun application(application: Application): Builder fun build(): AppComponent } diff --git a/app/src/main/java/com/skydoves/themovies/di/ComposeModule.kt b/app/src/main/java/com/skydoves/themovies/di/ComposeModule.kt new file mode 100644 index 0000000..8c5bbf2 --- /dev/null +++ b/app/src/main/java/com/skydoves/themovies/di/ComposeModule.kt @@ -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 +} diff --git a/app/src/main/java/com/skydoves/themovies/di/PersistenceModule.kt b/app/src/main/java/com/skydoves/themovies/di/PersistenceModule.kt index 0a6b92e..9a8e226 100644 --- a/app/src/main/java/com/skydoves/themovies/di/PersistenceModule.kt +++ b/app/src/main/java/com/skydoves/themovies/di/PersistenceModule.kt @@ -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