-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do injection in base activity & fragment
- Loading branch information
Showing
11 changed files
with
140 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
app/src/main/java/cn/nekocode/gank/di/component/FragmentComponent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
app/src/main/java/cn/nekocode/gank/di/module/FragmentModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |