rk
昨天 4a8ff39b0fab0627ef8f7459587d514cc01c3676
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.doumee.lib_coremodel.di.module;
 
 
import android.content.Context;
 
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.room.RoomDatabase;
 
import com.doumee.lib_coremodel.config.AppliesOptions;
import com.doumee.lib_coremodel.config.FrameConfigModule;
import com.doumee.lib_coremodel.config.ManifestParser;
import com.doumee.lib_coremodel.util.Preconditions;
import com.king.retrofit.retrofithelper.RetrofitHelper;
 
import java.util.List;
 
import javax.inject.Singleton;
 
import dagger.Module;
import dagger.Provides;
import dagger.hilt.InstallIn;
import dagger.hilt.android.qualifiers.ApplicationContext;
import dagger.hilt.components.SingletonComponent;
import okhttp3.HttpUrl;
 
 
/**
 * @author <a href="mailto:jenly1314@gmail.com">Jenly</a>
 */
@InstallIn(SingletonComponent.class)
@Module
public class ConfigModule {
 
 
    @Singleton
    @Provides
    HttpUrl provideBaseUrl(@NonNull Builder builder){
        HttpUrl baseUrl = builder.baseUrl;
        if(baseUrl == null){//如果 mBaseUrl 为空表示没有在自定义配置 FrameConfigModule 中配过 BaseUrl
            //尝试去 RetrofitHelper 中取一次 BaseUrl,这里相当于多支持一种配置 BaseUrl 的方式
            baseUrl = RetrofitHelper.getInstance().getBaseUrl();
        }
        //再次检测 mBaseUrl 是否为空,如果依旧为空,表示两种配置方式都没有配置过,则直接抛出异常
        Preconditions.checkNotNull(baseUrl,"Base URL required.");
        return baseUrl;
    }
 
    @Singleton
    @Provides
    @Nullable
    AppliesOptions.RetrofitOptions provideRetrofitOptions(@NonNull Builder builder){
        return builder.retrofitOptions;
    }
 
    @Singleton
    @Provides
    @Nullable
    AppliesOptions.OkHttpClientOptions provideOkHttpClientOptions(@NonNull Builder builder){
        return builder.okHttpClientOptions;
    }
 
    @Singleton
    @Provides
    @Nullable
    AppliesOptions.GsonOptions provideGsonOptions(@NonNull Builder builder){
        return builder.gsonOptions;
    }
 
    @Singleton
    @Provides
    @Nullable
    AppliesOptions.InterceptorConfigOptions provideInterceptorConfigOptions(@NonNull Builder builder){
        return builder.interceptorConfigOptions;
    }
 
    @Singleton
    @Provides
    AppliesOptions.RoomDatabaseOptions provideRoomDatabaseOptions(@NonNull Builder builder){
        if(builder.roomDatabaseOptions != null){
            return builder.roomDatabaseOptions;
        }
        return it -> {};
    }
 
    @Singleton
    @Provides
    Builder provideConfigModuleBuilder(@ApplicationContext Context context){
        Builder builder = new Builder();
        //解析配置
        List<FrameConfigModule> modules = new ManifestParser(context).parse();
        //遍历配置
        for (FrameConfigModule configModule: modules){
            //如果启用则申请配置参数
            if(configModule.isManifestParsingEnabled()){
                configModule.applyOptions(context,builder);
            }
        }
        return builder;
    }
 
    public static final class Builder {
 
        private HttpUrl baseUrl;
 
        private AppliesOptions.RetrofitOptions retrofitOptions;
 
        private AppliesOptions.OkHttpClientOptions okHttpClientOptions;
 
        private AppliesOptions.GsonOptions gsonOptions;
 
        private AppliesOptions.InterceptorConfigOptions interceptorConfigOptions;
 
        private AppliesOptions.RoomDatabaseOptions roomDatabaseOptions;
 
        public Builder(){
 
        }
 
        public Builder baseUrl(@NonNull String baseUrl) {
            this.baseUrl = HttpUrl.parse(baseUrl);
            return this;
        }
 
        public Builder baseUrl(@NonNull HttpUrl baseUrl){
            this.baseUrl = baseUrl;
            return this;
        }
 
        public Builder retrofitOptions(AppliesOptions.RetrofitOptions retrofitOptions) {
            this.retrofitOptions = retrofitOptions;
            return this;
        }
 
        public Builder okHttpClientOptions(AppliesOptions.OkHttpClientOptions okHttpClientOptions) {
            this.okHttpClientOptions = okHttpClientOptions;
            return this;
        }
 
        public Builder gsonOptions(AppliesOptions.GsonOptions gsonOptions){
            this.gsonOptions = gsonOptions;
            return this;
        }
 
        public Builder interceptorConfigOptions(AppliesOptions.InterceptorConfigOptions interceptorConfigOptions){
            this.interceptorConfigOptions = interceptorConfigOptions;
            return this;
        }
 
        public Builder roomDatabaseOptions(AppliesOptions.RoomDatabaseOptions<? extends RoomDatabase> roomDatabaseOptions){
            this.roomDatabaseOptions = roomDatabaseOptions;
            return this;
        }
 
    }
 
 
}