Skip to content

Commit

Permalink
Do injection in base activity & fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
nekocode committed Aug 7, 2019
1 parent 4687f05 commit 08226c8
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 25 deletions.
13 changes: 0 additions & 13 deletions app/src/main/java/cn/nekocode/gank/GankApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
package cn.nekocode.gank

import android.app.Application
import android.util.Log
import androidx.lifecycle.ViewModelProvider
import cn.nekocode.gank.backend.di.module.ApiModule
import cn.nekocode.gank.di.DaggerViewModelFactory
import cn.nekocode.gank.di.component.AppComponent
import cn.nekocode.gank.di.component.DaggerAppComponent
import cn.nekocode.gank.di.module.AppModule
Expand Down Expand Up @@ -48,8 +46,6 @@ open class GankApplication : Application() {

if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
} else {
Timber.plant(CrashReportingTree())
}
flipperClient.start()
}
Expand All @@ -64,13 +60,4 @@ open class GankApplication : Application() {
.apiModule(ApiModule(httpClientBuilder, gsonBuilder))
.build()
}

private class CrashReportingTree : Timber.Tree() {
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
if (priority == Log.VERBOSE || priority == Log.DEBUG) {
return
}
// Report
}
}
}
7 changes: 4 additions & 3 deletions app/src/main/java/cn/nekocode/gank/base/BaseActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package cn.nekocode.gank.base

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import cn.nekocode.gank.GankApplication
import cn.nekocode.gank.di.component.buildAndInject
import com.evernote.android.state.StateSaver
import com.uber.autodispose.*
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider
Expand All @@ -34,11 +34,12 @@ import io.reactivex.parallel.ParallelFlowable
/**
* @author nekocode ([email protected])
*/
@SuppressLint("Registered")
open class BaseActivity : AppCompatActivity() {
abstract class BaseActivity : AppCompatActivity() {
private val scopeProvider by lazy { AndroidLifecycleScopeProvider.from(this) }

override fun onCreate(savedInstanceState: Bundle?) {
(application as GankApplication).component
.newActivityComponentBuilder().buildAndInject(this)
super.onCreate(savedInstanceState)
StateSaver.restoreInstanceState(this, savedInstanceState)
}
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/cn/nekocode/gank/base/BaseFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import cn.nekocode.gank.GankApplication
import cn.nekocode.gank.di.component.buildAndInject
import com.evernote.android.state.StateSaver
import com.uber.autodispose.*
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider
Expand All @@ -32,10 +33,12 @@ import io.reactivex.parallel.ParallelFlowable
/**
* @author nekocode ([email protected])
*/
open class BaseFragment : Fragment() {
abstract class BaseFragment : Fragment() {
private val scopeProvider by lazy { AndroidLifecycleScopeProvider.from(this) }

override fun onCreate(savedInstanceState: Bundle?) {
(context?.applicationContext as GankApplication?)?.component
?.newFragmentComponentBuilder()?.buildAndInject(this)
super.onCreate(savedInstanceState)
StateSaver.restoreInstanceState(this, savedInstanceState)
}
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/cn/nekocode/gank/base/BaseViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ import io.reactivex.*
import io.reactivex.annotations.CheckReturnValue
import io.reactivex.parallel.ParallelFlowable
import io.reactivex.subjects.BehaviorSubject
import timber.log.Timber

/**
* Copied and modified from: https://github.com/uber/AutoDispose
*
* @author nekocode ([email protected])
*/
open class BaseViewModel : ViewModel(), LifecycleScopeProvider<BaseViewModel.ViewModelEvent> {
abstract class BaseViewModel : ViewModel(), LifecycleScopeProvider<BaseViewModel.ViewModelEvent> {
private val lifecycleEventsDelegate = lazy { BehaviorSubject.createDefault(ViewModelEvent.CREATED) }
private val lifecycleEvents by lifecycleEventsDelegate

Expand Down
26 changes: 26 additions & 0 deletions app/src/main/java/cn/nekocode/gank/di/FragmentScope.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2019. nekocode ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.nekocode.gank.di

import javax.inject.Scope

/**
* @author nekocode ([email protected])
*/
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class FragmentScope
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class DaggerViewModelFactory @Inject constructor(
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
val creator = providerMap[modelClass] ?: providerMap.entries.firstOrNull {
modelClass.isAssignableFrom(it.key)
}?.value ?: throw IllegalArgumentException("unknown model class $modelClass")
}?.value ?: throw IllegalArgumentException("Unknown model class $modelClass")
try {
@Suppress("UNCHECKED_CAST")
return creator.get() as T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cn.nekocode.gank.di.component
import android.app.Activity
import cn.nekocode.gank.di.ActivityScope
import cn.nekocode.gank.di.module.ActivityModule
import cn.nekocode.gank.ui.MainActivity
import dagger.Subcomponent

/**
Expand All @@ -32,11 +33,19 @@ import dagger.Subcomponent
)
interface ActivityComponent {

fun inject(activity: Activity)

@Subcomponent.Builder
interface Builder {
fun activityModule(module: ActivityModule): Builder
fun build(): ActivityComponent
}

fun inject(activity: MainActivity)
}

fun ActivityComponent.Builder.buildAndInject(activity: Activity) {
val component = activityModule(ActivityModule(activity)).build()

when (activity) {
is MainActivity -> component.inject(activity)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cn.nekocode.gank.di.component

import cn.nekocode.gank.GankApplication
import cn.nekocode.gank.backend.di.module.ApiModule
import cn.nekocode.gank.base.BaseActivity
import cn.nekocode.gank.di.AppScope
import cn.nekocode.gank.di.module.AppModule
import cn.nekocode.gank.di.module.FlipperModule
Expand All @@ -43,5 +42,7 @@ interface AppComponent {

fun inject(app: GankApplication)

fun activityComponent(): ActivityComponent.Builder
fun newActivityComponentBuilder(): ActivityComponent.Builder

fun newFragmentComponentBuilder(): FragmentComponent.Builder
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2019. nekocode ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.nekocode.gank.di.component

import androidx.fragment.app.Fragment
import cn.nekocode.gank.di.FragmentScope
import cn.nekocode.gank.di.module.FragmentModule
import cn.nekocode.gank.ui.home.HomeFragment
import cn.nekocode.gank.ui.pic.PicFragment
import dagger.Subcomponent

/**
* @author nekocode ([email protected])
*/
@FragmentScope
@Subcomponent(
modules = [
FragmentModule::class
]
)
interface FragmentComponent {

@Subcomponent.Builder
interface Builder {
fun fragmentModule(module: FragmentModule): Builder
fun build(): FragmentComponent
}

fun inject(fragment: HomeFragment)
fun inject(fragment: PicFragment)
}

fun FragmentComponent.Builder.buildAndInject(fragment: Fragment) {
val component = fragmentModule(FragmentModule(fragment)).build()

when (fragment) {
is HomeFragment -> component.inject(fragment)
is PicFragment -> component.inject(fragment)
}
}
4 changes: 3 additions & 1 deletion app/src/main/java/cn/nekocode/gank/di/module/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cn.nekocode.gank.di.module
import android.app.Application
import cn.nekocode.gank.di.AppScope
import cn.nekocode.gank.di.component.ActivityComponent
import cn.nekocode.gank.di.component.FragmentComponent
import dagger.Module
import dagger.Provides

Expand All @@ -27,7 +28,8 @@ import dagger.Provides
*/
@Module(
subcomponents = [
ActivityComponent::class
ActivityComponent::class,
FragmentComponent::class
]
)
class AppModule(private val app: Application) {
Expand Down
33 changes: 33 additions & 0 deletions app/src/main/java/cn/nekocode/gank/di/module/FragmentModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2019. nekocode ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.nekocode.gank.di.module

import androidx.fragment.app.Fragment
import cn.nekocode.gank.di.FragmentScope
import dagger.Module
import dagger.Provides

/**
* @author nekocode ([email protected])
*/
@Module
class FragmentModule(private val fragment: Fragment) {

@Provides
@FragmentScope
fun provideFragment(): Fragment = fragment
}

0 comments on commit 08226c8

Please sign in to comment.