StateFramework: A Rapid Development Framework for Android Status Notification Communication

Keywords: Attribute Gradle REST Database

StateFramework

brief introduction

StateFramework is a highly cohesive and low-coupling development framework with "state observer" events as its core, and has high compatibility.

Advantage

  • This framework provides code and annotation to realize the use of the framework.
  • Annotations exist only at compile time, and there are no performance problems caused by annotations

current version

0.1.6

Download method

Before using, make sure that the gradle version under buildscript is larger than 2.2.0

compile 'com.virtualightning.library:stateframework:0.1.6'
annotationProcessor 'com.virtualightning.library:stateframework-compiler:0.1.6'//StateFramework Annotation Processor, if you don't want to bind annotations to this line

Core function: state observer

A Brief Introduction to State Observers

"State" belongs to a threshold-free Abstract state, and only serves as a unique identifier, while stateId (state ID) is its entity.

Each Observer (state observer) corresponds to a stateId, which can be used for notification if Observer is required to perform an action.

If you want to use Observer, you have to have a StateRecord as a collection of Observers, and use "key - value" to establish a connection between stateId and Observer, that is to say, each Observer must belong to StateRecord, and StateRecord and Observer belong to a one-to-many relationship.

Use of state observers

First you need an instance of StateRecord

StateRecord stateRecord = StateRecord.newInstance(null);//The parameter is ClassKey, which registers the global state observer as the unique identifier of the type to which it belongs.

Then register Observer in the form of code or annotations

  • Code, Observer Builder to build Observer parameters and complete registration
    ObserverBuilder observerBuilder = new ObserverBuilder()
        .stateId("S_1")//State ID, type String
        .allowStop(false)//Whether to run pause state 
        .refType(ReferenceType.WEAK)//Reference types held by state observers (affecting memory leaks)
        .runType(RunType.MAIN_LOOP)//State Observer Execution Type
        .observer(new BaseObserver() {
            @Override
            public void notify(Object... objects) {
                //Something to do after notifying Observer
            }
        });
    stateRecord.registerObserver(observerBuilder);//Register internal status observers
    //stateRecord.registerWholeObserver(observerBuilder); registers global state observers, and throws exceptions when ClassKey is empty
  • Annotation mode requires delegation object and delegation method. The following example is DemoActivity (dependencies of annotation interpreter need to be added)

    public class DemoActivity extends Activity {
    
    StateRecord stateRecord;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        stateRecord = StateRecord.newInstance(DemoActivity.class);
           Analyzer.analyzeState(this,stateRecord);//Binding DemoActivity annotations to a specified StateRecord
       }
    
    @BindObserver(
            stateId = "S_1",//Registration status ID 
            allowStop = false,//Is Stopping Permissible
            runType = RunType.MAIN_LOOP,//Run type, currently running in the main thread
            isVarParameters = true,//Whether to use SELF-VARYING parameters
            isWholeObserver = false//Is it a global state observer?
    )
    void onNotifyState_1(Object... args) {
        //Something to do after notifying Observer
    }
    }

    To complete registration, here is the notification method

    stateRecord.notifyState("S_1");
    stateRecord.notifyState("S_1",1,2,3,4);//If you need to pass parameters, you can do this.

    Global state observer

    StateRecord.notifyWholeState("S_1");//Notify the status observer of all subscriptions to "S_1"

    Of course, you can also notify the designated global state observer

    StateRecord.notifyWholeState(ClassKey,"S_1");//Notify the specified ClassKey status observer for subscribing to "S_1"

    Logged Off state

    It's better to log out after use.

    stateRecord.unregisterObserver();

    Memory leaks can occur if global status is registered but not registered.

    About allowStop attribute

    If allowStop is enabled when registering Observer, you can temporarily disable Observer by changing the status of StateRecord

    stateRecord.setRecordState(false);

    When the status of StateRecord is false (stop), the notification will not reach the status observer with allowStop enabled.

    About the isVarParameters attribute in @BindObserver

    If you know exactly what type of parameters the delegate function requires, you can disable isVarParameters and notify Observer that the parameters it carries will automatically transition to fill the delegate function parameter list.

    @BindObserver(
    stateId = "S_1",//Registration status ID
    isVarParameters = false
    )
    void onNotifyState_1(String str1,Integer i) {
    //Something to do after notifying Observer
    }

    The following notifications can be used

    stateRecord.notifyState("S_1","HelloWorld",520);

    However, it should be noted that when isVarParameters is disabled, the type, order and length of parameters passed in notification must strictly follow the list of parameters defined by the delegate function, otherwise an exception will be thrown.

Auxiliary Functions: Annotation Binding Injection (Dependencies of Annotation Interpreter need to be added)

Binding View

Binding controls with @BindView

public class DemoActivity extends Activity {

    @BindView(R.id.list)// ID is R.id.list must exist in R.layout.main
    ListView list;

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

        Analyzer.analyzeView(this);//Controls bound to DemoActivity
    }
}

Binding resources

public class DemoActivity extends Activity {
    //Resource id = R.array.mainItem, resource type = string array
    @BindResources(resId = R.array.mainItem,type = ResType.STRING_ARRAY)
    String[] strArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Analyzer.analyzeResources(this);//Binding DemoActivity resources
    }
}

Binding events

Take click events as an example. For more events, please refer to Annotation type set

public class DemoActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Analyzer.analyzeEvent(this);//Binding DemoActivity resources
     }

    @OnClick(R.id.list)
    void OnClick() {
        //Click on Event Handling
    }
}

The rest are not on-line yet

  • HTTP Protocol Related
  • Database correlation

Android Studio plug-in

Layout Quick Injection Tool Installation Guide

Contact information

Binding resources and event bindings are not comprehensive, but only for my project at present. If you need to expand more automatic annotation bindings, please contact me.

QQ mailbox: 1152564696@qq.com

QQ : 1152564696

Posted by Ironphp on Fri, 19 Apr 2019 22:57:32 -0700