Skip to content

Commit 0b582b1

Browse files
author
陈绍龙
committed
框架搭建
1 parent c28f038 commit 0b582b1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2695
-51
lines changed

app/build.gradle

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,24 @@ dependencies {
5555
implementation externalRetrofitRxjava2
5656
implementation externalOkhttp3
5757
implementation externalButterknife
58+
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
5859
annotationProcessor(externalButterknifeCompiler) {
5960
exclude module: 'support-annotations'
6061
}
61-
implementation externalRxandroid2
6262
implementation externalRxjava2
63+
implementation externalRxandroid2
6364
implementation externalRxlifecycle2
6465
implementation externalRxlifecycle2Android
6566
implementation externalRxlifecycle2Components
6667
implementation externalMultidex
6768

69+
//dagger2
70+
implementation externalDagger2
71+
implementation externalDagger2Android
72+
implementation externalDagger2AndroidSupport
73+
annotationProcessor externalDagger2Compiler
74+
annotationProcessor externalDagger2AndroidProcessor
75+
6876
testImplementation 'junit:junit:4.12'
6977
androidTestImplementation 'com.android.support.test:runner:1.0.2'
7078
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

app/src/main/AndroidManifest.xml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,32 @@
1010
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
1111
<uses-permission android:name="android.permission.INTERNET" />
1212
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
13-
1413
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
1514
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
1615
<uses-permission android:name="com.xiaomi.permission.AUTH_SERVICE" />
1716

1817
<application
19-
android:name=".App"
18+
android:name=".base.App"
2019
android:allowBackup="true"
2120
android:icon="@mipmap/ic_launcher"
2221
android:label="@string/app_name"
22+
android:networkSecurityConfig="@xml/network_security_config"
2323
android:roundIcon="@mipmap/ic_launcher_round"
2424
android:supportsRtl="true"
2525
android:theme="@style/AppTheme">
26-
<activity android:name=".MainActivity">
26+
<activity android:name=".feature.MainActivity">
27+
28+
</activity>
29+
<activity
30+
android:name=".feature.user.ui.activity.LoginActivity"
31+
android:label="@string/title_activity_login"
32+
android:theme="@style/AppTheme.NoActionBar">
2733
<intent-filter>
2834
<action android:name="android.intent.action.MAIN" />
2935

3036
<category android:name="android.intent.category.LAUNCHER" />
3137
</intent-filter>
3238
</activity>
33-
34-
35-
3639
</application>
3740

3841
</manifest>

app/src/main/java/com/mydagger/demo/App.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

app/src/main/java/com/mydagger/demo/MainActivity.java

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.mydagger.demo.base;
2+
3+
/**
4+
* Created by chenshaolong on 2019/10/17.
5+
*/
6+
7+
public class ApiUrl {
8+
9+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.mydagger.demo.base;
2+
3+
import android.support.multidex.MultiDexApplication;
4+
import android.util.Log;
5+
6+
import com.mydagger.demo.di.component.AppComponent;
7+
import com.mydagger.demo.di.component.DaggerAppComponent;
8+
import com.mydagger.demo.di.module.GlobalConfigModule;
9+
10+
11+
/**
12+
* Created by chenshaolong on 2019/8/15.
13+
*/
14+
15+
public class App extends MultiDexApplication {
16+
17+
AppComponent mAppComponent;
18+
19+
@Override
20+
public void onCreate() {
21+
super.onCreate();
22+
23+
mAppComponent = DaggerAppComponent
24+
.builder()
25+
.application(this)
26+
.globalConfigModule(GlobalConfigModule.builder()
27+
.baseurl("http://192.168.2.203:8003")
28+
.globalHttpHandler(new GlobalHttpHandlerImpl(this))
29+
.retrofitConfiguration((context1, builder1) -> {
30+
Log.e("solo", "app-retrofitConfiguration-----");
31+
32+
}).okhttpConfiguration((context1, builder1) -> {
33+
Log.e("solo", "app-okhttpConfiguration-----");
34+
}).build())
35+
.build();
36+
mAppComponent.inject(this);
37+
}
38+
39+
public AppComponent getAppComponent() {
40+
return mAppComponent;
41+
}
42+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.mydagger.demo.base;
2+
3+
import android.os.Bundle;
4+
import android.support.annotation.Nullable;
5+
import android.support.v7.app.AppCompatActivity;
6+
import android.view.InflateException;
7+
8+
import javax.inject.Inject;
9+
10+
import butterknife.ButterKnife;
11+
import butterknife.Unbinder;
12+
13+
/**
14+
* Created by chenshaolong on 2019/10/17.
15+
*/
16+
17+
public abstract class BaseActivity<P extends IPresenter> extends AppCompatActivity {
18+
19+
private Unbinder mUnBinder;
20+
21+
@Inject
22+
@Nullable
23+
protected P mPresenter;
24+
25+
@Override
26+
protected void onCreate(@Nullable Bundle savedInstanceState) {
27+
super.onCreate(savedInstanceState);
28+
29+
try {
30+
int layoutResID = initView(savedInstanceState);
31+
if (layoutResID != 0) {
32+
setContentView(layoutResID);
33+
mUnBinder = ButterKnife.bind(this);
34+
}
35+
} catch (Exception e) {
36+
if (e instanceof InflateException) throw e;
37+
e.printStackTrace();
38+
}
39+
initData(savedInstanceState);
40+
41+
}
42+
43+
protected abstract int initView(Bundle savedInstanceState);
44+
protected abstract void initData(Bundle savedInstanceState);
45+
46+
@Override
47+
protected void onDestroy() {
48+
super.onDestroy();
49+
if (mUnBinder != null && mUnBinder != Unbinder.EMPTY)
50+
mUnBinder.unbind();
51+
this.mUnBinder = null;
52+
if (mPresenter != null)
53+
mPresenter.onDestroy();
54+
this.mPresenter = null;
55+
}
56+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.mydagger.demo.base;
2+
3+
import io.reactivex.disposables.CompositeDisposable;
4+
import io.reactivex.disposables.Disposable;
5+
6+
/**
7+
* Created by chenshaolong on 2019/10/17.
8+
*/
9+
10+
public class BasePresenter<M extends IModel, V extends IView> implements IPresenter{
11+
12+
protected CompositeDisposable mCompositeDisposable;
13+
protected M mModel;
14+
protected V mRootView;
15+
16+
public BasePresenter(M model, V rootView) {
17+
this.mModel = model;
18+
this.mRootView = rootView;
19+
onStart();
20+
}
21+
22+
public BasePresenter(V rootView) {
23+
this.mRootView = rootView;
24+
onStart();
25+
}
26+
27+
public BasePresenter() {
28+
onStart();
29+
}
30+
31+
@Override
32+
public void onStart() {
33+
34+
}
35+
36+
@Override
37+
public void onDestroy() {
38+
unDispose();
39+
if (mModel != null)
40+
mModel.onDestroy();
41+
this.mModel = null;
42+
this.mRootView = null;
43+
this.mCompositeDisposable = null;
44+
}
45+
46+
public void addDispose(Disposable disposable) {
47+
if (mCompositeDisposable == null) {
48+
mCompositeDisposable = new CompositeDisposable();
49+
}
50+
mCompositeDisposable.add(disposable);
51+
}
52+
53+
public void unDispose() {
54+
if (mCompositeDisposable != null) {
55+
mCompositeDisposable.clear();
56+
}
57+
}
58+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.mydagger.demo.base;
2+
3+
import android.content.Context;
4+
import android.support.annotation.NonNull;
5+
import android.support.annotation.Nullable;
6+
import android.text.TextUtils;
7+
8+
import com.mydagger.demo.http.GlobalHttpHandler;
9+
import com.mydagger.demo.http.log.RequestInterceptor;
10+
11+
import okhttp3.Interceptor;
12+
import okhttp3.Request;
13+
import okhttp3.Response;
14+
15+
/**
16+
* Created by chenshaolong on 2019/10/16.
17+
*/
18+
19+
public class GlobalHttpHandlerImpl implements GlobalHttpHandler {
20+
private Context context;
21+
22+
public GlobalHttpHandlerImpl(Context context) {
23+
this.context = context;
24+
}
25+
26+
/**
27+
* 这里可以先客户端一步拿到每一次 Http 请求的结果, 可以先解析成 Json, 再做一些操作, 如检测到 token 过期后
28+
* 重新请求 token, 并重新执行请求
29+
*
30+
* @param httpResult 服务器返回的结果 (已被框架自动转换为字符串)
31+
* @param chain {@link okhttp3.Interceptor.Chain}
32+
* @param response {@link Response}
33+
* @return {@link Response}
34+
*/
35+
@NonNull
36+
@Override
37+
public Response onHttpResultResponse(@Nullable String httpResult, @NonNull Interceptor.Chain chain, @NonNull Response response) {
38+
if (!TextUtils.isEmpty(httpResult) && RequestInterceptor.isJson(response.body().contentType())) {
39+
try {
40+
} catch (Exception e) {
41+
e.printStackTrace();
42+
return response;
43+
}
44+
}
45+
46+
/* 这里如果发现 token 过期, 可以先请求最新的 token, 然后在拿新的 token 放入 Request 里去重新请求
47+
注意在这个回调之前已经调用过 proceed(), 所以这里必须自己去建立网络请求, 如使用 Okhttp 使用新的 Request 去请求
48+
create a new request and modify it accordingly using the new token
49+
Request newRequest = chain.request().newBuilder().header("token", newToken)
50+
.build();
51+
52+
retry the request
53+
54+
response.body().close();
55+
如果使用 Okhttp 将新的请求, 请求成功后, 再将 Okhttp 返回的 Response return 出去即可
56+
如果不需要返回新的结果, 则直接把参数 response 返回出去即可*/
57+
return response;
58+
}
59+
60+
/**
61+
* 这里可以在请求服务器之前拿到 {@link Request}, 做一些操作比如给 {@link Request} 统一添加 token 或者 header 以及参数加密等操作
62+
*
63+
* @param chain {@link okhttp3.Interceptor.Chain}
64+
* @param request {@link Request}
65+
* @return {@link Request}
66+
*/
67+
@NonNull
68+
@Override
69+
public Request onHttpRequestBefore(@NonNull Interceptor.Chain chain, @NonNull Request request) {
70+
/* 如果需要在请求服务器之前做一些操作, 则重新构建一个做过操作的 Request 并 return, 如增加 Header、Params 等请求信息, 不做操作则直接返回参数 request
71+
return chain.request().newBuilder().header("token", tokenId)
72+
.build(); */
73+
return request;
74+
}
75+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.mydagger.demo.base;
2+
3+
/**
4+
* Created by chenshaolong on 2019/10/17.
5+
*/
6+
7+
public interface IModel {
8+
9+
void onDestroy();
10+
}

0 commit comments

Comments
 (0)