Skip to content

Commit

Permalink
完成对app初始化相关配置的管理和保持
Browse files Browse the repository at this point in the history
  • Loading branch information
chouxingxing committed Dec 9, 2020
1 parent 8e50c2b commit 8c0946c
Show file tree
Hide file tree
Showing 15 changed files with 256 additions and 3 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
implementation project(path: ':vector_common')
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Dec 07 18:23:08 CST 2020
#Wed Dec 09 17:04:49 CST 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#####https://www.cnblogs.com/zhangzhonghui/p/11463250.html
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
include ':vector_common'
include ':app'
rootProject.name = "Vector_CommonLibrary"
1 change: 1 addition & 0 deletions vector_common/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
46 changes: 46 additions & 0 deletions vector_common/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
plugins {
id 'com.android.library'
}

android {
compileSdkVersion 30
buildToolsVersion "29.0.3"

defaultConfig {
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {

implementation 'androidx.appcompat:appcompat:1.2.0'
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'

//字体图标库
implementation 'com.joanzapata.iconify:android-iconify-fontawesome:2.2.2'
implementation 'com.joanzapata.iconify:android-iconify-ionicons:2.2.2'
//网络请求依赖
implementation 'com.squareup.okio:okio:1.13.0'
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'
}
Empty file.
21 changes: 21 additions & 0 deletions vector_common/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.vector.common;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.vector.common.test", appContext.getPackageName());
}
}
5 changes: 5 additions & 0 deletions vector_common/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vector.common">

</manifest>
14 changes: 14 additions & 0 deletions vector_common/src/main/java/com/vector/common/app/ConfigType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.vector.common.app;

/**
* Created by 瓮少波 on 2018/4/23.
*/

public enum ConfigType {
API_HOST,
APPLICATION_CONTEXT,
CONFIG_READY,
ICON,
LOADER_DELAYED,
INTERCEPTOR
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.vector.common.app;

import android.content.Context;

import com.joanzapata.iconify.IconFontDescriptor;
import com.joanzapata.iconify.Iconify;

import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.Interceptor;

/**
* @author Vector Weng
* @description: App initialization general configuration class
* @date :2020/12/9 17:16
*/
public class Configurator {

private static final HashMap<String, Object> VECTOR_CONFIGS = new HashMap<>();
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);
}

private static final class Holder {
private static final Configurator INSTANCE = new Configurator();
}

public static Configurator getInstance() {
return Holder.INSTANCE;
}

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

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

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

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

public final Configurator withIcon(IconFontDescriptor descriptor) {
ICONS.add(descriptor);
return this;
}

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

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

private void initIcons() {
if (ICONS.size() > 0) {
final Iconify.IconifyInitializer initializer = Iconify.with(ICONS.get(0));
for (int i = 1; i < ICONS.size(); i++) {
initializer.with(ICONS.get(i));
}
}
}

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

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


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

import android.content.Context;

import java.util.HashMap;

/**
* @author Vector Weng
* @description:
* @date :2020/12/9 17:16
*/
public class Vector {

public static final Configurator init(Context context) {
return Configurator.getInstance();
}

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

public static Context getApplicationContext() {
return (Context) getConfigs().get(ConfigType.APPLICATION_CONTEXT);

}

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

import org.junit.Test;

import static org.junit.Assert.*;

/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}
}

0 comments on commit 8c0946c

Please sign in to comment.