1+ /*
2+ * Copyright 2019. nekocode (nekocode.cn@gmail.com)
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package cn.nekocode.gank.di.component
18+
19+ import androidx.fragment.app.Fragment
20+ import cn.nekocode.gank.di.FragmentScope
21+ import cn.nekocode.gank.di.module.FragmentModule
22+ import cn.nekocode.gank.ui.home.HomeFragment
23+ import cn.nekocode.gank.ui.pic.PicFragment
24+ import dagger.Subcomponent
25+
26+ /* *
27+ * @author nekocode (nekocode.cn@gmail.com)
28+ */
29+ @FragmentScope
30+ @Subcomponent(
31+ modules = [
32+ FragmentModule ::class
33+ ]
34+ )
35+ interface FragmentComponent {
36+
37+ @Subcomponent.Builder
38+ interface Builder {
39+ fun fragmentModule (module : FragmentModule ): Builder
40+ fun build (): FragmentComponent
41+ }
42+
43+ fun inject (fragment : HomeFragment )
44+ fun inject (fragment : PicFragment )
45+ }
46+
47+ fun FragmentComponent.Builder.buildAndInject (fragment : Fragment ) {
48+ val component = fragmentModule(FragmentModule (fragment)).build()
49+
50+ when (fragment) {
51+ is HomeFragment -> component.inject(fragment)
52+ is PicFragment -> component.inject(fragment)
53+ }
54+ }
0 commit comments