Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/shubhamsinghshubham777/composebroadcasts

Consume Android's Broadcast Receivers in a fun and easy way from your Jetpack Composables 📡
https://github.com/shubhamsinghshubham777/composebroadcasts

android android-library broadcast-reciever jetpack-compose kotlin-library

Last synced: 3 months ago
JSON representation

Consume Android's Broadcast Receivers in a fun and easy way from your Jetpack Composables 📡

Awesome Lists containing this project

README

        

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`:

```kotlin
// 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`:

```kotlin
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

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

### Monitor Battery Level

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

### Track Device Charging Status

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

### Observe Package Changes

Check out the [🧩 Custom BroadcastReceivers](#-custom-broadcastreceivers) section below to learn
how to create PackageInfoReceiver.

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

### Monitor System Time

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

### Track System Locale Changes

Check out the [🧩 Custom BroadcastReceivers](#-custom-broadcastreceivers) section below to learn
how to create LocaleReceiver.

```kotlin
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:

```kotlin
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:

```xml








```

Finally, use it in your composable:

```kotlin
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](LICENSE) file for details.