Skip to content

Commit

Permalink
Merge pull request #47 from vicolo-dev/feature-alarm-media-channel
Browse files Browse the repository at this point in the history
Add more audio channels for alarms and timers
  • Loading branch information
AhsanSarwar45 authored Dec 17, 2023
2 parents 8597858 + a9eb39e commit f93a416
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 11 deletions.
16 changes: 11 additions & 5 deletions lib/alarm/data/alarm_settings_schema.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:audio_session/audio_session.dart';
import 'package:clock_app/alarm/data/alarm_task_schemas.dart';
import 'package:clock_app/alarm/types/alarm_task.dart';
import 'package:clock_app/alarm/types/range_interval.dart';
Expand All @@ -8,13 +9,14 @@ import 'package:clock_app/alarm/types/schedules/range_alarm_schedule.dart';
import 'package:clock_app/alarm/types/schedules/weekly_alarm_schedule.dart';
import 'package:clock_app/alarm/widgets/alarm_task_card.dart';
import 'package:clock_app/alarm/widgets/try_alarm_task_button.dart';
import 'package:clock_app/audio/audio_channels.dart';
import 'package:clock_app/audio/logic/audio_session.dart';
import 'package:clock_app/audio/types/audio.dart';
import 'package:clock_app/audio/types/ringtone_player.dart';
import 'package:clock_app/audio/types/ringtone_manager.dart';
import 'package:clock_app/settings/types/setting.dart';
import 'package:clock_app/settings/types/setting_group.dart';
import 'package:clock_app/timer/types/time_duration.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

SettingGroup alarmSettingsSchema = SettingGroup(
Expand All @@ -28,7 +30,7 @@ SettingGroup alarmSettingsSchema = SettingGroup(
"Type",
[
SelectSettingOption("Once", OnceAlarmSchedule,
description: "Will ring at the next occurrence of the time."),
description: "Will ring at the next occurrence of the time"),
SelectSettingOption("Daily", DailyAlarmSchedule,
description: "Will ring every day"),
SelectSettingOption("On Specified Week Days", WeeklyAlarmSchedule,
Expand Down Expand Up @@ -100,6 +102,12 @@ SettingGroup alarmSettingsSchema = SettingGroup(
},
shouldCloseOnSelect: false,
),
SelectSetting<AndroidAudioUsage>(
"Audio Channel", audioChannelOptions,
onChange: (context, index) {
RingtonePlayer.stop();
initializeAudioSession(audioChannelOptions[index].value);
}),
SliderSetting("Volume", 0, 100, 100, unit: "%"),
SwitchSetting("Rising Volume", false,
description: "Gradually increase volume over time"),
Expand Down Expand Up @@ -137,9 +145,7 @@ SettingGroup alarmSettingsSchema = SettingGroup(
),
ListSetting<AlarmTask>(
"Tasks",
kDebugMode
? alarmTaskSchemasMap.keys.map((key) => AlarmTask(key)).toList()
: [],
[],
alarmTaskSchemasMap.keys.map((key) => AlarmTask(key)).toList(),
addCardBuilder: (item) => AlarmTaskCard(task: item, isAddCard: true),
cardBuilder: (item) => AlarmTaskCard(task: item, isAddCard: false),
Expand Down
3 changes: 2 additions & 1 deletion lib/alarm/logic/alarm_controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ void triggerScheduledNotification(int scheduleId, Json params) async {
await GetStorage.init();
await RingtoneManager.initialize();
await RingtonePlayer.initialize();
await initializeAudioSession();

if (notificationType == ScheduledNotificationType.alarm) {
await initializeAudioSession(getAlarmByScheduleId(scheduleId).audioChannel);
triggerAlarm(scheduleId, params);
} else if (notificationType == ScheduledNotificationType.timer) {
await initializeAudioSession(getTimerById(scheduleId).audioChannel);
triggerTimer(scheduleId, params);
}
}
Expand Down
3 changes: 3 additions & 0 deletions lib/alarm/types/alarm.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:audio_session/audio_session.dart';
import 'package:clock_app/alarm/logic/schedule_alarm.dart';
import 'package:clock_app/alarm/types/alarm_runner.dart';
import 'package:clock_app/alarm/types/alarm_task.dart';
Expand Down Expand Up @@ -82,6 +83,8 @@ class Alarm extends CustomizableListItem {
_settings.getSetting("Rising Volume").value
? _settings.getSetting("Time To Full Volume").value
: TimeDuration.zero;
AndroidAudioUsage get audioChannel =>
_settings.getSetting("Audio Channel").value;
AlarmSchedule get activeSchedule =>
_schedules.firstWhere((schedule) => schedule.runtimeType == scheduleType);
List<AlarmRunner> get activeAlarmRunners => activeSchedule.alarmRunners;
Expand Down
1 change: 0 additions & 1 deletion lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:clock_app/notifications/types/notifications_controller.dart';
import 'package:clock_app/onboarding/screens/onboarding_screen.dart';
import 'package:clock_app/settings/data/settings_schema.dart';
import 'package:clock_app/settings/types/setting_group.dart';
import 'package:clock_app/theme/text.dart';
import 'package:clock_app/theme/types/color_scheme.dart';
import 'package:clock_app/theme/theme.dart';
import 'package:clock_app/theme/types/style_theme.dart';
Expand Down
9 changes: 9 additions & 0 deletions lib/audio/audio_channels.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:audio_session/audio_session.dart';
import 'package:clock_app/settings/types/setting.dart';

List<SelectSettingOption<AndroidAudioUsage>> audioChannelOptions = [
SelectSettingOption("Alarm", AndroidAudioUsage.alarm),
SelectSettingOption("Media", AndroidAudioUsage.media),
SelectSettingOption("Notification", AndroidAudioUsage.notification),
SelectSettingOption("Ringtone", AndroidAudioUsage.notificationRingtone),
];
7 changes: 4 additions & 3 deletions lib/audio/logic/audio_session.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'package:audio_session/audio_session.dart';

Future<void> initializeAudioSession() async {
Future<void> initializeAudioSession(
[AndroidAudioUsage usage = AndroidAudioUsage.alarm]) async {
final session = await AudioSession.instance;
await session.configure(const AudioSessionConfiguration(
await session.configure(AudioSessionConfiguration(
androidAudioAttributes: AndroidAudioAttributes(
usage: AndroidAudioUsage.alarm,
usage: usage,
contentType: AndroidAudioContentType.music,
),
));
Expand Down
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void main() async {
await AndroidAlarmManager.initialize();
await RingtoneManager.initialize();
await RingtonePlayer.initialize();
await initializeAudioSession();
await initializeAudioSession(); //Needs to be initialized after settings
await BootReceiver.initialize(handleBoot);
await initializeNotifications();
AppVisibility.initialize();
Expand Down
7 changes: 7 additions & 0 deletions lib/settings/types/setting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ abstract class Setting<T> extends SettingItem {

@override
void loadValueFromJson(dynamic value) {
if (value == null) return;
_value = value;
}
}
Expand Down Expand Up @@ -161,6 +162,7 @@ class ListSetting<T extends CustomizableListItem> extends Setting<List<T>> {

@override
void loadValueFromJson(dynamic value) {
if (value == null) return;
_value = (value as List).map((e) => fromJsonFactories[T]!(e) as T).toList();
}
}
Expand Down Expand Up @@ -219,6 +221,7 @@ class CustomSetting<T extends JsonSerializable> extends Setting<T> {

@override
void loadValueFromJson(dynamic value) {
if (value == null) return;
_value = fromJsonFactories[T]!(value);
}
}
Expand Down Expand Up @@ -294,6 +297,7 @@ class ColorSetting extends Setting<Color> {

@override
void loadValueFromJson(dynamic value) {
if (value == null) return;
_value = Color(value);
}

Expand Down Expand Up @@ -536,6 +540,7 @@ class ToggleSetting<T> extends Setting<List<bool>> {

@override
void loadValueFromJson(dynamic value) {
if (value == null) return;
_value = (value as List).map((e) => e == "1").toList();
}
}
Expand Down Expand Up @@ -584,6 +589,7 @@ class DateTimeSetting extends Setting<List<DateTime>> {

@override
void loadValueFromJson(dynamic value) {
if (value == null) return;
_value = (value as List)
.map((e) => DateTime.fromMillisecondsSinceEpoch(e))
.toList();
Expand Down Expand Up @@ -632,6 +638,7 @@ class DurationSetting extends Setting<TimeDuration> {

@override
void loadValueFromJson(dynamic value) {
if (value == null) return;
_value = TimeDuration.fromMilliseconds(value);
}
}
Expand Down
9 changes: 9 additions & 0 deletions lib/timer/data/timer_settings_schema.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'package:audio_session/audio_session.dart';
import 'package:clock_app/audio/audio_channels.dart';
import 'package:clock_app/audio/logic/audio_session.dart';
import 'package:clock_app/audio/types/audio.dart';
import 'package:clock_app/audio/types/ringtone_manager.dart';
import 'package:clock_app/audio/types/ringtone_player.dart';
Expand Down Expand Up @@ -29,6 +32,12 @@ SettingGroup timerSettingsSchema = SettingGroup(
},
shouldCloseOnSelect: false,
),
SelectSetting<AndroidAudioUsage>(
"Audio Channel", audioChannelOptions,
onChange: (context, index) {
RingtonePlayer.stop();
initializeAudioSession(audioChannelOptions[index].value);
}),
SliderSetting("Volume", 0, 100, 100, unit: "%"),
SwitchSetting("Rising Volume", false,
description: "Gradually increase volume over time"),
Expand Down
3 changes: 3 additions & 0 deletions lib/timer/types/timer.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:math' as math;

import 'package:audio_session/audio_session.dart';
import 'package:clock_app/audio/types/audio.dart';
import 'package:clock_app/common/types/json.dart';
import 'package:clock_app/settings/data/settings_schema.dart';
Expand Down Expand Up @@ -38,6 +39,8 @@ class ClockTimer extends CustomizableListItem {
? _settings.getSetting("Label").value
: '${_duration.toString()} timer';
Audio get ringtone => _settings.getSetting("Melody").value;
AndroidAudioUsage get audioChannel =>
_settings.getSetting("Audio Channel").value;
bool get vibrate => _settings.getSetting("Vibration").value;
TimeDuration get risingVolumeDuration =>
_settings.getSetting("Rising Volume").value
Expand Down

0 comments on commit f93a416

Please sign in to comment.