Android project initialization and url address storage

When we write some projects, we have many habitual operations, such as the following two operations, which may be commonly used by us:

1. The address of the request network will be written in a class in the form of static variables

2. Initialization methods are often thrown directly and disorderly in the Application

In fact, these habits are not conducive to the use of our memory and subsequent maintenance, so I propose a set of improved methods after learning from muke.com, which can be used for your reference. Special thanks to Mr. Fu ape for his class. All right, let's talk less. Let's start looking at the formal code.

According to the idea of componentization, I put the initialization code in a separate lib. We can rely on it on the modules that need it.
The code is divided into the following categories

Latte.java
/**
 * Entry reference class
 * @author WaterWood
 */
public final class Latte {

    /**
     * Initialize entry and configure context
     */
    public static Configurator init(Context context){
        getConfigurations().put(ConfigType.APPLICATION_CONTEXT.name(),context.getApplicationContext());
        return Configurator.getInstance();
    }

    /**
     * Initialize entry to get configuration collection
     * @return
     */
    public static HashMap<String,Object> getConfigurations(){
        return Configurator.getInstance().getLatteConfigs();
    }

    /**
     * Return the Configurator object directly
     * @return
     */
    public static Configurator getConfigurator(){
        return Configurator.getInstance();
    }
}

This is an entry class. We should start from here

ConfigType.java
/**
 * Various parameters of project initialization
 * @author WaterWood
 */
public enum ConfigType {
    API_HOST,
    API_PIC_HOST,
    APPLICATION_CONTEXT,
    CONFIG_READY,
    ICON,
    //Interface address
    LOGIN,//Sign in
    //Sp storage name
    ACCESSTOKEN,//Login token
}

Related storage can be named and written in this enumeration class

Configurator.java
/**
 * Formal configuration classes and related things to be initialized should be placed in this class
 * @author WaterWood
 */
public class Configurator {

    /**
     * A collection that stores all data
     */
    private static final HashMap<String, Object> LATTE_CONFIGS = new HashMap<>();

    /**
     * Construction method to identify the start of application configuration
     */
    private Configurator() {
        LATTE_CONFIGS.put(ConfigType.CONFIG_READY.name(), false);//Configuration started, but not yet completed
    }

    /**
     * The last step is called to identify the end of application configuration
     */
    public final void configure() {
        LATTE_CONFIGS.put(ConfigType.CONFIG_READY.name(), true);//Tell our APP to initialize
    }

    /**
     * Thread safe lazy singleton mode
     * @return
     */
    public static Configurator getInstance() {
        return Holder.INSTANCE;
    }

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

    /**
     * Gets the method that stores all data sets
     * @return
     */
    protected final HashMap<String, Object> getLatteConfigs() {
        return LATTE_CONFIGS;
    }

    /**
     * Check whether the current configuration has ended, and then the corresponding data can be obtained
     */
    private void checkConfiguration() {
        final boolean isReady = (boolean) LATTE_CONFIGS.get(ConfigType.CONFIG_READY.name());
        if (!isReady) {
            throw new RuntimeException("Configuration is not ready,call configure");
        }
    }

    /**
     * Obtain data according to the corresponding enumeration key
     * @param key
     * @return
     */
    public final Object getConfiguration(Enum<ConfigType> key) {
        checkConfiguration();
        final Object value = LATTE_CONFIGS.get(key.name());
        if (value == null) {
            throw new NullPointerException(key.toString() + " IS NULL");
        }
        return value;
    }

    /**
     * Save various network access addresses
     * @return
     */
    public final Configurator initHttpAddress() {
        LATTE_CONFIGS.put(ConfigType.LOGIN.name(), "login/checkUser");
        return this;
    }

    /**
     * Save the names of the various SP S
     * @return
     */
    public final Configurator initSpName() {
        LATTE_CONFIGS.put(ConfigType.SPSTRING.name(),0x00);
        LATTE_CONFIGS.put(ConfigType.SPINT.name(),0x01);
        LATTE_CONFIGS.put(ConfigType.SPBOOLEAN.name(),0x02);
        LATTE_CONFIGS.put(ConfigType.SPFLOAT.name(),0x03);
        LATTE_CONFIGS.put(ConfigType.SPLONG.name(),0x04);
        //The name of the SP value is saved here for unified management
        LATTE_CONFIGS.put(ConfigType.ACCESSTOKEN.name(), "accesstoken");
        return this;
    }

    /**
     * Initialize Aurora push
     * @return
     */
    public final Configurator initJpush(Context context) {
        JPushInterface.init(context);
        return this;
    }

    /**
     * Interface master address configuration
     * @param host
     * @return
     */
    public final Configurator withApiHost(String host) {
        LATTE_CONFIGS.put(ConfigType.API_HOST.name(), host);
        return this;
    }

    /**
     * Picture primary address configuration
     * @param host
     * @return
     */
    public final Configurator withPicApiHost(String host) {
        LATTE_CONFIGS.put(ConfigType.API_PIC_HOST.name(), host);
        return this;
    }
}

All the main configuration codes should be written in this class

use:

When we use it, we must first call it in the Application, that is, to complete the purpose of our project initialization.

/**
     * Initialize project
     */
    private void initProject() {
        Latte.init(this)
                .withApiHost("http://127.0.0.1:8080/")
                .withPicApiHost("http://127.0.0.1:81/")
                .initHttpAddress()
                .initSpName()
                .initJpush(this)
                .configure();
    }

When we want to use the stored value, we use it like this

Latte.getConfigurator().getConfiguration(ConfigType.API_HOST);

So we can take the value stored in the HashMap.

Note that in this HashMap, value is an Object type, so it can store not only String and int, but also objects. So we can put what we want according to our own needs. In this way, we can store content at will and try to ensure the high quality of the code.

Posted by Drakla on Sun, 22 May 2022 13:10:07 +0300