Skip to content

Commit

Permalink
feat: make audio source setting configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmo314 committed Sep 3, 2021
1 parent 7bef86f commit 1e1c799
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 56 deletions.
14 changes: 13 additions & 1 deletion lib/models/audio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class AudioModel extends ChangeNotifier {

bool _isOnline = false;
bool _isSettingsVisible = false;
bool _isAlwaysEnabled = false;

Channel? _hostChannel;
StreamSubscription? _hostChannelStateSubscription;
Expand Down Expand Up @@ -86,9 +87,16 @@ class AudioModel extends ChangeNotifier {
_syncWebViews();
}

bool get isAlwaysEnabled => _isAlwaysEnabled;

set isAlwaysEnabled(bool value) {
_isAlwaysEnabled = value;
notifyListeners();
}

List<AudioSource> get sources => _sources;

bool get enabled => _isOnline || _isSettingsVisible;
bool get enabled => _isOnline || _isSettingsVisible || _isAlwaysEnabled;

Future<void> addSource(AudioSource source) async {
if (_sources.contains(source)) {
Expand Down Expand Up @@ -174,9 +182,13 @@ class AudioModel extends ChangeNotifier {
}
notifyListeners();
}
if (json['isAlwaysEnabled'] != null) {
_isAlwaysEnabled = json['isAlwaysEnabled'];
}
}

Map<String, dynamic> toJson() => {
"sources": _sources.map((source) => source.toJson()).toList(),
"isAlwaysEnabled": _isAlwaysEnabled,
};
}
121 changes: 66 additions & 55 deletions lib/screens/settings/audio_sources.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,62 +54,73 @@ class _AudioSourcesScreenState extends State<AudioSourcesScreen> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Audio sources")),
body: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
const Padding(
padding: EdgeInsets.all(16),
child: Text(
"Audio sources are automatically muted when your stream is offline.")),
const Divider(),
Expanded(
child: Consumer<AudioModel>(builder: (context, audioModel, child) {
return ListView(
children: audioModel.sources.map((source) {
final name = source.name;
return Dismissible(
key: ValueKey(source),
background: const DismissibleDeleteBackground(),
child: CheckboxListTile(
title:
name == null ? Text(source.url.toString()) : Text(name),
subtitle: name == null ? null : Text(source.url.toString()),
value: !source.muted,
onChanged: (value) {
audioModel.toggleSource(source);
}),
onDismissed: (direction) {
audioModel.removeSource(source);
},
);
}).toList(),
);
})),
const Divider(),
Padding(
padding: const EdgeInsets.only(left: 16, bottom: 16),
child: Form(
key: _formKey,
child: Row(children: [
Expanded(
child: TextFormField(
controller: _textEditingController,
decoration: const InputDecoration(hintText: "URL"),
validator: (value) {
if (value == null ||
value.isEmpty ||
Uri.tryParse(value) == null) {
return "This doesn't look like a valid URL.";
}
return null;
},
keyboardType: TextInputType.url,
textInputAction: TextInputAction.done,
onEditingComplete: add),
),
IconButton(icon: const Icon(Icons.add), onPressed: add),
]),
body: Consumer<AudioModel>(builder: (context, model, child) {
return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
SwitchListTile.adaptive(
title: const Text('Play off-stream (uses more battery)'),
subtitle: model.isAlwaysEnabled
? const Text(
'Audio sources will play when your stream is offline')
: const Text(
'Audio sources will play only when your stream is online'),
value: model.isAlwaysEnabled,
onChanged: (value) {
model.isAlwaysEnabled = value;
},
),
),
]),
const Divider(),
Expanded(
child: ListView(
children: model.sources.map((source) {
final name = source.name;
return Dismissible(
key: ValueKey(source),
background: const DismissibleDeleteBackground(),
child: CheckboxListTile(
title: name == null
? Text(source.url.toString())
: Text(name),
subtitle:
name == null ? null : Text(source.url.toString()),
value: !source.muted,
onChanged: (value) {
model.toggleSource(source);
}),
onDismissed: (direction) {
model.removeSource(source);
},
);
}).toList(),
),
),
const Divider(),
Padding(
padding: const EdgeInsets.only(left: 16, bottom: 16),
child: Form(
key: _formKey,
child: Row(children: [
Expanded(
child: TextFormField(
controller: _textEditingController,
decoration: const InputDecoration(hintText: "URL"),
validator: (value) {
if (value == null ||
value.isEmpty ||
Uri.tryParse(value) == null) {
return "This doesn't look like a valid URL.";
}
return null;
},
keyboardType: TextInputType.url,
textInputAction: TextInputAction.done,
onEditingComplete: add),
),
IconButton(icon: const Icon(Icons.add), onPressed: add),
]),
),
),
]);
}),
);
}
}

0 comments on commit 1e1c799

Please sign in to comment.