Skip to content

Commit

Permalink
Add tests for fadeDuration and isRinging to example app (#272)
Browse files Browse the repository at this point in the history
* Add tests for fadeDuration and isRinging to example app

* Use alarmPrint instead of debugPrint
  • Loading branch information
orkun1675 authored Oct 29, 2024
1 parent 450fb77 commit 13cedf6
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 28 deletions.
71 changes: 47 additions & 24 deletions example/lib/screens/edit_alarm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class _ExampleAlarmEditScreenState extends State<ExampleAlarmEditScreen> {
late bool loopAudio;
late bool vibrate;
late double? volume;
late double fadeDuration;
late String assetAudio;

@override
Expand All @@ -33,12 +34,14 @@ class _ExampleAlarmEditScreenState extends State<ExampleAlarmEditScreen> {
loopAudio = true;
vibrate = true;
volume = null;
fadeDuration = 0;
assetAudio = 'assets/marimba.mp3';
} else {
selectedDateTime = widget.alarmSettings!.dateTime;
loopAudio = widget.alarmSettings!.loopAudio;
vibrate = widget.alarmSettings!.vibrate;
volume = widget.alarmSettings!.volume;
fadeDuration = widget.alarmSettings!.fadeDuration;
assetAudio = widget.alarmSettings!.assetAudioPath;
}
}
Expand Down Expand Up @@ -94,6 +97,7 @@ class _ExampleAlarmEditScreenState extends State<ExampleAlarmEditScreen> {
loopAudio: loopAudio,
vibrate: vibrate,
volume: volume,
fadeDuration: fadeDuration,
assetAudioPath: assetAudio,
warningNotificationOnKill: Platform.isIOS,
notificationSettings: NotificationSettings(
Expand Down Expand Up @@ -251,30 +255,49 @@ class _ExampleAlarmEditScreenState extends State<ExampleAlarmEditScreen> {
),
],
),
SizedBox(
height: 30,
child: volume != null
? Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Icon(
volume! > 0.7
? Icons.volume_up_rounded
: volume! > 0.1
? Icons.volume_down_rounded
: Icons.volume_mute_rounded,
),
Expanded(
child: Slider(
value: volume!,
onChanged: (value) {
setState(() => volume = value);
},
),
),
],
)
: const SizedBox(),
if (volume != null)
SizedBox(
height: 45,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Icon(
volume! > 0.7
? Icons.volume_up_rounded
: volume! > 0.1
? Icons.volume_down_rounded
: Icons.volume_mute_rounded,
),
Expanded(
child: Slider(
value: volume!,
onChanged: (value) {
setState(() => volume = value);
},
),
),
],
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Fade duration',
style: Theme.of(context).textTheme.titleMedium,
),
DropdownButton<double>(
value: fadeDuration,
items: List.generate(
6,
(index) => DropdownMenuItem<double>(
value: index * 3.0,
child: Text('${index * 3}s'),
),
),
onChanged: (value) => setState(() => fadeDuration = value!),
),
],
),
if (!creating)
TextButton(
Expand Down
36 changes: 32 additions & 4 deletions example/lib/screens/ring.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
import 'dart:async';

import 'package:alarm/alarm.dart';
import 'package:flutter/material.dart';

class ExampleAlarmRingScreen extends StatelessWidget {
class ExampleAlarmRingScreen extends StatefulWidget {
const ExampleAlarmRingScreen({required this.alarmSettings, super.key});

final AlarmSettings alarmSettings;

@override
State<ExampleAlarmRingScreen> createState() => _ExampleAlarmRingScreenState();
}

class _ExampleAlarmRingScreenState extends State<ExampleAlarmRingScreen> {
@override
void initState() {
super.initState();
Timer.periodic(const Duration(seconds: 1), (timer) async {
if (!mounted) {
timer.cancel();
return;
}

final isRinging = await Alarm.isRinging(widget.alarmSettings.id);
if (isRinging) {
alarmPrint('Alarm ${widget.alarmSettings.id} is still ringing...');
return;
}

alarmPrint('Alarm ${widget.alarmSettings.id} stopped ringing.');
timer.cancel();
if (mounted) Navigator.pop(context);
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand All @@ -14,7 +42,7 @@ class ExampleAlarmRingScreen extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text(
'You alarm (${alarmSettings.id}) is ringing...',
'You alarm (${widget.alarmSettings.id}) is ringing...',
style: Theme.of(context).textTheme.titleLarge,
),
const Text('🔔', style: TextStyle(fontSize: 50)),
Expand All @@ -25,7 +53,7 @@ class ExampleAlarmRingScreen extends StatelessWidget {
onPressed: () {
final now = DateTime.now();
Alarm.set(
alarmSettings: alarmSettings.copyWith(
alarmSettings: widget.alarmSettings.copyWith(
dateTime: DateTime(
now.year,
now.month,
Expand All @@ -45,7 +73,7 @@ class ExampleAlarmRingScreen extends StatelessWidget {
),
RawMaterialButton(
onPressed: () {
Alarm.stop(alarmSettings.id).then((_) {
Alarm.stop(widget.alarmSettings.id).then((_) {
if (context.mounted) Navigator.pop(context);
});
},
Expand Down

0 comments on commit 13cedf6

Please sign in to comment.