Skip to content

Commit

Permalink
完成对日志模块及网络模块的封装
Browse files Browse the repository at this point in the history
  • Loading branch information
chouxingxing committed Dec 28, 2020
1 parent 8c0946c commit 1c504a5
Show file tree
Hide file tree
Showing 29 changed files with 1,581 additions and 19 deletions.
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
package="com.vector.commonlibrary">

<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Vector_CommonLibrary">
android:theme="@style/Theme.Vector_CommonLibrary"
android:usesCleartextTraffic="true">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
34 changes: 34 additions & 0 deletions app/src/main/java/com/vector/commonlibrary/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.vector.commonlibrary;

import android.app.Application;

import androidx.annotation.Nullable;

import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.DiskLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;
import com.vector.common.app.Vector;
import com.vector.common.utils.log.LoggerUtil;

/**
* @author Vector Weng
* @description:
* @date :2020/12/21 16:49
*/
public class App extends Application {

@Override
public void onCreate() {
super.onCreate();

Vector.init(this)
.withLoaderDelayed(2000)
.initLogger(LoggerUtil.LoggerLevel.VERBOSE)
.withApiHost("http://192.168.31.80:20002/api/")
.configure();
}


}
38 changes: 38 additions & 0 deletions app/src/main/java/com/vector/commonlibrary/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,50 @@
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.vector.common.app.ConfigType;
import com.vector.common.app.Vector;
import com.vector.common.net.RestClient;
import com.vector.common.net.callback.IError;
import com.vector.common.net.callback.IFailure;
import com.vector.common.net.callback.ISuccess;
import com.vector.common.utils.log.LoggerUtil;

import java.util.logging.Logger;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

RestClient.bulider()
.loader(MainActivity.this)
.url("https://www.baidu.com")
.success(new ISuccess() {
@Override
public void onSuccess(String response) {
Log.i("MainActivity", response);
}
})
.error(new IError() {
@Override
public void onError(int code, String msg) {
Log.i("MainActivity", code + "");

}
})
.failure(new IFailure() {
@Override
public void onFailure() {
Log.i("MainActivity", "失败");

}
})
.bulid()
.get();

}
}
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#####https://www.cnblogs.com/zhangzhonghui/p/11463250.html
### 单例模式之Holder
https://www.cnblogs.com/zhangzhonghui/p/11463250.html
7 changes: 6 additions & 1 deletion vector_common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ android {
}

buildTypes {


release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
Expand All @@ -34,7 +36,8 @@ dependencies {
implementation 'com.google.android.material:material:1.2.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

//logger
api 'com.orhanobut:logger:2.2.0'
//字体图标库
implementation 'com.joanzapata.iconify:android-iconify-fontawesome:2.2.2'
implementation 'com.joanzapata.iconify:android-iconify-ionicons:2.2.2'
Expand All @@ -43,4 +46,6 @@ dependencies {
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.3.0'
//AVLoadingIndicatorView
implementation 'com.wang.avi:library:2.1.3'
}
5 changes: 5 additions & 0 deletions vector_common/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vector.common">

<!--网络权限-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ public enum ConfigType {
CONFIG_READY,
ICON,
LOADER_DELAYED,
INTERCEPTOR
INTERCEPTOR,
HANDLER,
LOGGER_LEVEL
}
50 changes: 39 additions & 11 deletions vector_common/src/main/java/com/vector/common/app/Configurator.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.vector.common.app;

import android.content.Context;
import android.os.Handler;

import com.joanzapata.iconify.IconFontDescriptor;
import com.joanzapata.iconify.Iconify;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -17,12 +21,16 @@
*/
public class Configurator {

private static final HashMap<String, Object> VECTOR_CONFIGS = new HashMap<>();
private static final HashMap<ConfigType, Object> VECTOR_CONFIGS = new HashMap<>();
private static final Handler HANDLER = new Handler();

private static final ArrayList<IconFontDescriptor> ICONS = new ArrayList<>();
private static final ArrayList<Interceptor> INTERCEPTORS = new ArrayList<>();

private Configurator() {
VECTOR_CONFIGS.put(ConfigType.CONFIG_READY.name(), false);
VECTOR_CONFIGS.put(ConfigType.CONFIG_READY, false);
VECTOR_CONFIGS.put(ConfigType.HANDLER, HANDLER);

}

private static final class Holder {
Expand All @@ -35,20 +43,40 @@ public static Configurator getInstance() {

public final void configure() {
initIcons();
VECTOR_CONFIGS.put(ConfigType.CONFIG_READY.name(), true);
VECTOR_CONFIGS.put(ConfigType.CONFIG_READY, true);
}

final HashMap<String, Object> getConfigs() {
final HashMap<ConfigType, Object> getConfigs() {
return VECTOR_CONFIGS;
}

public final Configurator initLogger(int level) {
initLogger(level, null);
return this;
}

public final Configurator initLogger(int level, FormatStrategy formatStrategy) {
if (formatStrategy != null) {
Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
} else {
Logger.addLogAdapter(new AndroidLogAdapter());
}
VECTOR_CONFIGS.put(ConfigType.LOGGER_LEVEL, level);
return this;
}

public final Configurator withContext(Context context) {
VECTOR_CONFIGS.put(ConfigType.APPLICATION_CONTEXT.name(), context);
VECTOR_CONFIGS.put(ConfigType.APPLICATION_CONTEXT, context);
return this;
}

public final Configurator withApiHost(String host) {
VECTOR_CONFIGS.put(ConfigType.API_HOST.name(), host);
VECTOR_CONFIGS.put(ConfigType.API_HOST, host);
return this;
}

public final Configurator withLoaderDelayed(long delayed) {
VECTOR_CONFIGS.put(ConfigType.LOADER_DELAYED, delayed);
return this;
}

Expand All @@ -59,13 +87,13 @@ public final Configurator withIcon(IconFontDescriptor descriptor) {

public final Configurator withInterceptor(Interceptor interceptor) {
INTERCEPTORS.add(interceptor);
VECTOR_CONFIGS.put(ConfigType.INTERCEPTOR.name(), INTERCEPTORS);
VECTOR_CONFIGS.put(ConfigType.INTERCEPTOR, INTERCEPTORS);
return this;
}

public final Configurator withInterceptor(ArrayList<Interceptor> interceptors) {
INTERCEPTORS.addAll(interceptors);
VECTOR_CONFIGS.put(ConfigType.INTERCEPTOR.name(), INTERCEPTORS);
VECTOR_CONFIGS.put(ConfigType.INTERCEPTOR, INTERCEPTORS);
return this;
}

Expand All @@ -79,14 +107,14 @@ private void initIcons() {
}

private void checkConfiguration() {
final boolean isReady = (boolean) VECTOR_CONFIGS.get(ConfigType.CONFIG_READY.name());
final boolean isReady = (boolean) VECTOR_CONFIGS.get(ConfigType.CONFIG_READY);
if (!isReady)
throw new RuntimeException("Configuration is not ready,call configure");
}

final <T> T getConfiguration(Enum<ConfigType> key) {
final <T> T getConfiguration(ConfigType key) {
checkConfiguration();
return (T) VECTOR_CONFIGS.get(key.name());
return (T) VECTOR_CONFIGS.get(key);
}


Expand Down
21 changes: 17 additions & 4 deletions vector_common/src/main/java/com/vector/common/app/Vector.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.vector.common.app;

import android.app.Application;
import android.content.Context;
import android.os.Handler;

import java.util.HashMap;

Expand All @@ -12,16 +14,27 @@
public class Vector {

public static final Configurator init(Context context) {
getConfigurator()
.getConfigs()
.put(ConfigType.APPLICATION_CONTEXT, context.getApplicationContext());
return Configurator.getInstance();
}

public static HashMap<String, Object> getConfigs() {
return Configurator.getInstance().getConfigs();
public static Configurator getConfigurator() {
return Configurator.getInstance();
}

public static Context getApplicationContext() {
return (Context) getConfigs().get(ConfigType.APPLICATION_CONTEXT);
public static <T> T getConfiguration(ConfigType key) {
return getConfigurator().getConfiguration(key);
}

public static Application getApplicationContext() {
return getConfiguration(ConfigType.APPLICATION_CONTEXT);
}

public static Handler getHandler() {
return getConfiguration(ConfigType.HANDLER);
}


}
18 changes: 18 additions & 0 deletions vector_common/src/main/java/com/vector/common/net/HttpMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.vector.common.net;

/**
* @author Vector Weng
* @description:
* @date :2020/12/9 18:44
*/
public enum HttpMethod {

GET,
POST,
POST_RAW,
PUT,
PUT_RAW,
DELETE,
UPLOAD

}
Loading

0 comments on commit 1c504a5

Please sign in to comment.