Skip to content

Consume Android's Broadcast Receivers in a fun and easy way from your Jetpack Composables ๐Ÿ“ก

License

Notifications You must be signed in to change notification settings

shubhamsinghshubham777/ComposeBroadcasts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

31 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Compose Broadcasts

Compose Broadcasts Logo

Compose Badge Android Badge Kotlin Badge

๐Ÿš€ Introduction

Compose Broadcasts is a powerful Jetpack Compose library that simplifies the process of working with Android's BroadcastReceivers in a composable environment. It provides an intuitive API to observe and react to system-wide events and changes in your Compose UI.

โœจ Features

  • ๐Ÿ”„ Easy integration with Jetpack Compose
  • ๐Ÿ“ก Observe system events like battery level, airplane mode, and more
  • ๐ŸŽ›๏ธ Custom BroadcastReceiver support
  • ๐Ÿงฉ Composable functions for common system events
  • ๐Ÿ› ๏ธ Flexible API for creating custom broadcast listeners
  • โ˜ฎ๏ธ No need to worry about registering / unregistering listeners anymore

๐Ÿ“ฆ Installation

Sonatype Nexus (Releases)

Add the following to your app's build.gradle.kts:

// Get the latest version from GitHub Releases/Tags
implementation("io.github.shubhamsinghshubham777:composebroadcasts:0.0.1")

For SNAPSHOT versions

Add the following to your project level settings.gradle.kts:

dependencyResolutionManagement {
    repositories {
        ...
        // Add this
        maven("https://s01.oss.sonatype.org/content/repositories/snapshots/")
    }
}

๐Ÿ› ๏ธ Usage

Here's the complete list of composables Compose Broadcasts provides at the moment:

Composable Return Type
rememberBroadcastReceiverAsState Generic (T)
rememberIsAirplaneModeOn Boolean
rememberBatteryLevel Int
rememberIsCharging Boolean
rememberPackageInfo CBPackageInfo?
rememberCurrentTimeMillis Long
rememberSystemLocale Locale
rememberIsScreenOn Boolean
rememberIsHeadsetConnected Boolean
rememberCurrentInputMethod CBInputMethodInfo?

And here are some examples of how to use them in your project:

Observe Airplane Mode

val isAirplaneModeOn by rememberIsAirplaneModeOn()
Text("Airplane mode is ${if (isAirplaneModeOn) "ON" else "OFF"}")

Monitor Battery Level

val batteryLevel by rememberBatteryLevel()
Text("Current battery level: $batteryLevel%")

Track Device Charging Status

val isCharging by rememberIsCharging()
Text("Device is ${if (isCharging) "charging" else "not charging"}")

Observe Package Changes

Check out the ๐Ÿงฉ Custom BroadcastReceivers section below to learn how to create PackageInfoReceiver.

val packageInfoReceiver = PackageInfoReceiver()
val packageInfo by rememberPackageInfo(packageInfoReceiver)
Text("Last package change: $packageInfo")

Monitor System Time

val currentTimeMillis by rememberCurrentTimeMillis()
Text("Current time: ${convertMillisToTimeString(currentTimeMillis)}")

Track System Locale Changes

Check out the ๐Ÿงฉ Custom BroadcastReceivers section below to learn how to create LocaleReceiver.

val localeReceiver = LocaleReceiver()
val currentLocale by rememberSystemLocale(localeReceiver)
Text("Current system locale: ${currentLocale.toLanguageTag()}")

๐Ÿงฉ Custom BroadcastReceivers

You can create custom BroadcastReceivers by extending the CBBroadcastReceiver class:

class MyCustomReceiver : CBBroadcastReceiver(tag = "my_custom_receiver") {
    override fun onReceive(context: Context?, intent: Intent?) {
        super.onReceive(context, intent)
        // Your custom logic here (if you like the old way of writing receivers)
        // Ideally, your logic should be a part of the composable itself
        // This class should just be left blank, for example:
        // class MyCustomReceiver : CBBroadcastReceiver(tag = "my_custom_receiver")
    }
}

Then, register the receiver in your AndroidManifest.xml file:

<manifest>
    <application>
        <receiver android:name=".MyCustomReceiver" android:exported="false">
            <intent-filter>
                <!-- Example: android.intent.action.PACKAGE_ADDED -->
                <action android:name="YOUR_CUSTOM_ACTION" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

Finally, use it in your composable:

val customState by rememberBroadcastReceiverAsState(
    initialValue = initialState,
    // Example: CBIntentAction.Custom(Intent.ACTION_PACKAGE_ADDED)
    intentFilters = listOf(CBIntentFilter(CBIntentAction.Custom("YOUR_CUSTOM_ACTION"))),
    broadcastReceiver = MyCustomReceiver(),
) { context, intent ->
    // Map the received intent to your state
}

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

๐Ÿ“„ License

This library is licensed under the Apache 2.0 License. See the LICENSE file for details.