Code Monkey home page Code Monkey logo

postevent's Introduction

PostEvent

You can consider PostEvent as a lightweight communication channel for passing data within activities, or services, or fragments or between any of them. Think of PostEvent as an underlying layer in your app which is independent of any active activities or services or fragments and their lifecycle.

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

Step 2. Add the dependency

dependencies {
        implementation 'com.github.amitrawat8:PostEvent:1.0.0'
}

usage of PostEventBus Subscribe in activity or fragment (e.g MainActivity.kt)

 private fun getSubscribeData() {
        PostEvent.subscribe(EventPage.MAIN_ACTIVITY, this) {
            it.runAndConsume {
                when (it.id) {
                    EventConstants.withoutData -> {
                        Toast.makeText(this, "Event Hit", Toast.LENGTH_SHORT).show()
                    }

                    EventConstants.dataWithString -> {
                        var getValue: String = it.value as String
                        Toast.makeText(this, "Event Hit with data" + getValue, Toast.LENGTH_SHORT)
                            .show()
                    }

                    EventConstants.dataWithObject -> {
                       var eventItem: EventItem  = it.value as EventItem

                        Toast.makeText(
                            this,
                            "Event Hit with Object" + eventItem.id,
                            Toast.LENGTH_SHORT
                        )
                            .show()
                    }


                }
            }
        }
    }

usage of calling PostEventBus Subscribe of activity or fragment in other class (e.g SecondActivity.kt)

 
 /*send without data */

 PostEvent.publish(
            EventPage.MAIN_ACTIVITY,
            ConsumableEvent(id = EventConstants.withoutData))

        /*send with data string*/

        PostEvent.publish(
            EventPage.MAIN_ACTIVITY,
            ConsumableEvent(id = EventConstants.dataWithString, value = "hello"))

        /*send with object */

        PostEvent.publish(
            EventPage.MAIN_ACTIVITY,
            ConsumableEvent(id = EventConstants.dataWithObject, value = EventItem(1, 2)) )

UnSubscribe the PostEventBus(e.g MainActivity.kt)

 override fun onDestroy() {
        super.onDestroy()
        /*unregister the Subscriber*/
        PostEvent.unregister(EventPage.MAIN_ACTIVITY)
    }
    

postevent's People

Contributors

amitrawat8 avatar

Stargazers

 avatar

Watchers

 avatar

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.