|
1 | 1 | --- |
2 | 2 | title: Showing dynamic UI |
| 3 | +description: Configure TriggerX to show dynamic UI based on your Room database |
3 | 4 | sidebar_position: 1 |
4 | 5 | --- |
5 | 6 |
|
| 7 | +# 📦 Dynamic UI with Room Database using TriggerX |
6 | 8 |
|
| 9 | +TriggerX makes it super simple to launch time-triggered UI in Android. But did you know you can also show **dynamic UI backed by Room database**? This example shows how to use `TriggerXDataProvider` to fetch up-to-date data from a Room database and display it in a custom alarm screen. |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## 🚀 What You'll Build |
| 14 | + |
| 15 | +A fully working demo where: |
| 16 | +- A user taps a button to schedule an alarm. |
| 17 | +- After 1 minute, a custom activity launches even if the app is killed. |
| 18 | +- The UI pulls fresh data from a Room database and displays it. |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## 🛠️ Prerequisites |
| 23 | + |
| 24 | +- TriggerX library already integrated |
| 25 | +- Basic Room setup with DAO and Entity |
| 26 | +- Compose UI |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## 1. Setup TriggerX in `Application` class |
| 31 | + |
| 32 | +```kotlin |
| 33 | +class MyApplication : Application() { |
| 34 | + override fun onCreate() { |
| 35 | + super.onCreate() |
| 36 | + |
| 37 | + TriggerX.init(this) { |
| 38 | + activityClass = MyAlarmActivity::class.java |
| 39 | + |
| 40 | + useDefaultNotification( |
| 41 | + title = "Alarm running", |
| 42 | + message = "Your scheduled screen is opening", |
| 43 | + channelName = "TriggerX Notifications" |
| 44 | + ) |
| 45 | + |
| 46 | + alarmDataProvider = object : TriggerXDataProvider { |
| 47 | + override suspend fun provideData(alarmId: Int, alarmType: String): Bundle { |
| 48 | + val db = AppDatabase.getInstance(this@MyApplication) |
| 49 | + val task = db.taskDao().getTaskById(alarmId) |
| 50 | + return bundleOf( |
| 51 | + "task_title" to task?.title, |
| 52 | + "task_description" to task?.description |
| 53 | + ) |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +## 2. Build the UI with Compose |
| 62 | +Inherit from TriggerXActivity and override AlarmContent() to render your dynamic screen: |
| 63 | + |
| 64 | +```kotlin |
| 65 | +class MyAlarmActivity : TriggerXActivity() { |
| 66 | + |
| 67 | + @Composable |
| 68 | + override fun AlarmContent() { |
| 69 | + val title = intent.getStringExtra("task_title") ?: "No Title" |
| 70 | + val description = intent.getStringExtra("task_description") ?: "No Description" |
| 71 | + |
| 72 | + Column( |
| 73 | + modifier = Modifier |
| 74 | + .fillMaxSize() |
| 75 | + .background(Color.White) |
| 76 | + .padding(32.dp), |
| 77 | + verticalArrangement = Arrangement.Center, |
| 78 | + horizontalAlignment = Alignment.CenterHorizontally |
| 79 | + ) { |
| 80 | + Text(text = title, fontSize = 28.sp, fontWeight = FontWeight.Bold) |
| 81 | + Spacer(modifier = Modifier.height(16.dp)) |
| 82 | + Text(text = description, fontSize = 18.sp) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +## 3. Schedule the Alarm |
| 89 | +Use this code in your Composable to schedule the alarm after inserting the task: |
| 90 | + |
| 91 | +```kotlin |
| 92 | +@Composable |
| 93 | +fun HomeScreen() { |
| 94 | + val context = LocalContext.current |
| 95 | + val alarmScheduler = remember { TriggerXAlarmScheduler() } |
| 96 | + val permissionState = rememberAppPermissionState() |
| 97 | + |
| 98 | + val scope = rememberCoroutineScope() |
| 99 | + |
| 100 | + |
| 101 | + ElevatedButton( |
| 102 | + onClick = { |
| 103 | + if (permissionState.allRequiredGranted()) { |
| 104 | + val db = AppDatabase.getInstance(context) |
| 105 | + scope.launch { |
| 106 | + db.taskDao().insert(Task(1, "Meeting", "Discuss the roadmap")) |
| 107 | + |
| 108 | + val triggerTime = Calendar.getInstance().apply { |
| 109 | + add(Calendar.MINUTE, 1) |
| 110 | + }.timeInMillis |
| 111 | + |
| 112 | + alarmScheduler.scheduleAlarm( |
| 113 | + context = context, |
| 114 | + triggerAtMillis = triggerTime, |
| 115 | + type = "TASK", |
| 116 | + alarmId = 1 |
| 117 | + ) |
| 118 | + } |
| 119 | + } else { |
| 120 | + permissionState.requestPermission() |
| 121 | + } |
| 122 | + }) { |
| 123 | + Text("Schedule Dynamic Alarm") |
| 124 | + } |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +## ✅ Done! |
| 129 | +Now, close your app and wait for a minute. You'll see the UI pop up with dynamic data fetched from the Room database even if the app was killed. |
| 130 | + |
| 131 | +Made with ❤️ by [Meticha](https://meticha.com/) |
0 commit comments