GithubHelp home page GithubHelp logo

trendingtechnology / pubsubmessenger Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wada811/pubsubmessenger

0.0 1.0 0.0 219 KB

PubSubMessenger is a Pub/Sub pattern library from ViewModel/Fragment to Activity/Fragment.

License: Apache License 2.0

Kotlin 100.00%

pubsubmessenger's Introduction

PubSubMessenger

PubSubMessenger is a Pub/Sub pattern library from ViewModel/Fragment to Activity/Fragment.

PubSubMessenger

Overview

  • PubSubMessenger is a Pub/Sub pattern library.
    • Messaging from ViewModel to Activity/Fragment. For example, Navigation, Dialog, SnackBar and so on.
    • Messaging from Fragment to Activity/Fragment. For example, Fragment callback, DialogFragment callback.
  • PubSubMessenger is Lifecycle-aware like LiveData.
    • You can subscribe to messages at least Lifecycle.State.STARTED.
    • If you publish a message when lifecycle isn't at least STARTED, the message is suspend until at least STARTED.
    • A message consumed automatically when you observed unlike LiveData, SingleLiveEvent and LiveData<Event>.
    • You can subscribe to messages multiple times unlike LiveData and SingleLiveEvent.
    • You cannot use to communication with another Activity. There is risk of losing the message because of killing Activity by OS.
  • PubSubMessenger restricts where you publish and where you subscribe for preventing chaos by like EventBus.
    • You can publish in ViewModel or Fragment.
    • You can subscribe in Activity or Fragment.

Usage

Define a message

The message needs to implement PubSubMessage.

class SampleMessage(val param: String): PubSubMessage

Publish a message

You can call pubSubMessenger.publish(message) in ViewModel or Fragment.

pubSubMessenger.publish(SampleMessage("You can pass parameters via the message"))

Subscribe to messages

You can call pubSubMessenger.subscribe<PubSubMessage> { } in Activity or Fragment.

pubSubMessenger.subscribe<SampleMessage> { message ->
    Log.d(TAG, "Receive $message")
}

Gradle

repositories {
    maven { url "https://jitpack.io" }
}

dependencies {
    implementation 'com.github.wada811:PubSubMessenger:x.y.z'
}

License

Copyright (C) 2020 wada811

Licensed under the Apache License, Version 2.0

pubsubmessenger's People

Contributors

wada811 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.