Giter Club home page Giter Club logo

messagebox's Introduction

MessageBox

Platform License License

Messagebox is a lightweight message/event framework for Android.

Screenshot

demo1

Dependencies:

//Kotlin
dependencies {
    implementation 'com.github.allenxuan.MessageBox:messagebox-annotation:1.9.4'
    implementation 'com.github.allenxuan.MessageBox:messagebox-core:1.9.4'
    kapt 'com.github.allenxuan.MessageBox:messagebox-compiler:1.9.4'
}

//Java
dependencies {
    implementation 'com.github.allenxuan.MessageBox:messagebox-annotation:1.9.4'
    implementation 'com.github.allenxuan.MessageBox:messagebox-core:1.9.4'
    annotationProcessor 'com.github.allenxuan.MessageBox:messagebox-compiler:1.9.4'
}

Usages

To obtain the ability of receiving messages, subscribe and unSubscribe the target object at appropriate places, usually within onCreate() and onDestroy() of Activity and Fragment. Make sure that a target object is unSubscribe or you may get memory leaked.

class DemoMainFragmentA : Fragment() {
    ...
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MessageBox.subscribe(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        MessageBox.unSubscribe(this)
    }
    
    ...
}

Define your custom message class which extends MessageCarrier.

class Message1(val text: String) : MessageCarrier()

Annotate a pubic method with @MessageReceive. This method will be invoked when a message is sent from somewhere.

class DemoMainFragmentA : Fragment() {
     ...
     
    @MessageReceive
    fun onReceiveMessage1(message1: Message1) {
        //textReceiver could be a TextView
        textReceiver?.text = message1.text
    }
    ...
}

You can also specify the execution thread and delay through parameters in @MessageReceive. For more details, please refer to

com.allenxuan.xuanyihuang.messagebox.annotation.MessageReceive

and

com.allenxuan.xuanyihuang.messagebox.others.MessageScheduler
class DemoMainFragmentA : Fragment() {
     ...
     
     //This method will be invoked on main thread after 3000 milliseconds since Message1 is sent from somewhere.
    @MessageReceive(executeThread = MessageScheduler.mainThread, executeDelay = 3000)
    fun onReceiveMessage1(message1: Message1) {
        textReceiver?.text = message1.text
    }
    ...
}

Finally, send a message from anywhere you like.

class DemoMainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        ...
        
        findViewById<View>(R.id.sendMessage1)?.setOnClickListener {
            MessageBox.sendMessage(Message1("Message1 received"))
        }
        
        ...
    }
}

License

Copyright 2022 Xuanyi Huang

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

messagebox's People

Contributors

allenxuan avatar

Stargazers

 avatar Yasan avatar  avatar Vinod P avatar Josué Mavárez avatar  avatar  avatar John Wehrle avatar Mazlin Higbee avatar Álvaro García León avatar  avatar Nikhil Maurya avatar Owais avatar Pavitra Golchha avatar

Watchers

James Cloos avatar  avatar

Forkers

lazercorn rgregat

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.