Skip to content

Commit

Permalink
Allow system volume customization when using audio fade
Browse files Browse the repository at this point in the history
  • Loading branch information
orkun1675 committed Dec 4, 2024
1 parent ce3ca93 commit 090b9d6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 30 deletions.
3 changes: 2 additions & 1 deletion example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
<application
android:label="alarm_example"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
android:icon="@mipmap/ic_launcher"
android:enableOnBackInvokedCallback="true">
<service android:name="com.gdelataillade.alarm.services.NotificationOnKillService" />
<activity
android:name=".MainActivity"
Expand Down
7 changes: 4 additions & 3 deletions example/lib/screens/edit_alarm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ class _ExampleAlarmEditScreenState extends State<ExampleAlarmEditScreen> {
creating = widget.alarmSettings == null;

if (creating) {
selectedDateTime = DateTime.now().add(const Duration(minutes: 1));
selectedDateTime = selectedDateTime.copyWith(second: 0, millisecond: 0);
selectedDateTime = DateTime.now().add(const Duration(seconds: 10));
loopAudio = true;
vibrate = true;
volume = null;
Expand Down Expand Up @@ -98,6 +97,7 @@ class _ExampleAlarmEditScreenState extends State<ExampleAlarmEditScreen> {
final VolumeSettings volumeSettings;
if (staircaseFade) {
volumeSettings = VolumeSettings.staircaseFade(
volume: volume,
fadeSteps: [
VolumeFadeStep(Duration.zero, 0),
VolumeFadeStep(const Duration(seconds: 15), 0.03),
Expand All @@ -107,7 +107,8 @@ class _ExampleAlarmEditScreenState extends State<ExampleAlarmEditScreen> {
);
} else if (fadeDuration != null) {
volumeSettings = VolumeSettings.fade(
fadeDuration: fadeDuration,
volume: volume,
fadeDuration: fadeDuration!,
);
} else {
volumeSettings = VolumeSettings.fixed(volume: volume);
Expand Down
1 change: 1 addition & 0 deletions ios/Classes/api/AlarmApiImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ public class AlarmApiImpl: NSObject, AlarmApi {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
if let slider = volumeView.subviews.first(where: { $0 is UISlider }) as? UISlider {
self.previousVolume = enable ? slider.value : nil
print("[SwiftAlarmPlugin] Setting system volume to \(volume).")
slider.value = volume
}
volumeView.removeFromSuperview()
Expand Down
60 changes: 34 additions & 26 deletions lib/model/volume_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,48 @@ class VolumeSettings extends Equatable {
this.fadeDuration,
this.fadeSteps = const [],
this.volumeEnforced = false,
});

/// Constructs [VolumeSettings] with fixed volume level.
const VolumeSettings.fixed({
this.volume,
this.volumeEnforced = false,
}) : assert(
volume == null || (volume >= 0 && volume <= 1),
'volume must be NULL or in the range [0, 1]',
),
fadeDuration = null,
fadeSteps = const [];
assert(
fadeDuration == null || fadeDuration > Duration.zero,
'fadeDuration must be NULL or stricly positive',
);

/// Constructs [VolumeSettings] with fixed volume level.
const VolumeSettings.fixed({
double? volume,
bool volumeEnforced = false,
}) : this._(
volume: volume,
volumeEnforced: volumeEnforced,
);

/// Constructs [VolumeSettings] with fading volume level.
VolumeSettings.fade({
this.fadeDuration,
this.volumeEnforced = false,
}) : assert(
fadeDuration == null || !fadeDuration.isNegative,
'fadeDuration must be positive',
),
volume = null,
fadeSteps = const [];
const VolumeSettings.fade({
required Duration fadeDuration,
double? volume,
bool volumeEnforced = false,
}) : this._(
volume: volume,
fadeDuration: fadeDuration,
volumeEnforced: volumeEnforced,
);

/// Constructs [VolumeSettings] with slowly increasing (stepped) volume level.
const VolumeSettings.staircaseFade({
required this.fadeSteps,
this.volumeEnforced = false,
}) : assert(
fadeSteps.length > 0,
'fadeSteps must not be empty',
),
volume = null,
fadeDuration = null;
factory VolumeSettings.staircaseFade({
required List<VolumeFadeStep> fadeSteps,
double? volume,
bool volumeEnforced = false,
}) {
assert(fadeSteps.isNotEmpty, 'fadeSteps must not be empty');
return VolumeSettings._(
volume: volume,
fadeSteps: fadeSteps,
volumeEnforced: volumeEnforced,
);
}

/// Converts the JSON object to a `VolumeSettings` instance.
factory VolumeSettings.fromJson(Map<String, dynamic> json) =>
Expand Down

0 comments on commit 090b9d6

Please sign in to comment.