Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes and improvements #76

Merged
merged 4 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions open_earable/lib/controls_tab/views/audio_and_led.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,8 @@ class AudioAndLed extends StatelessWidget {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (!isV2)
AudioPlayerCard(
Provider.of<BluetoothController>(
context,
listen: false,
).openEarableLeft,
),
LEDColorCard(),
if (!isV2) AudioPlayerCard(),
LedColorCard(),
],
);
},
Expand Down
206 changes: 101 additions & 105 deletions open_earable/lib/controls_tab/views/audio_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,40 @@ import 'package:provider/provider.dart';
import '../models/open_earable_settings.dart';
import '../../shared/dynamic_value_picker.dart';

class AudioPlayerCard extends StatefulWidget {
class AudioPlayerCard extends StatelessWidget {
const AudioPlayerCard({
super.key,
});

@override
Widget build(BuildContext context) {
return Consumer<BluetoothController>(
builder: (context, bleController, child) {
return _InternalAudioPlayerCard(
openEarable: bleController.currentOpenEarable,
connected: bleController.currentOpenEarable.bleManager.connected,
);
},
);
}
}

class _InternalAudioPlayerCard extends StatefulWidget {
final OpenEarable openEarable;
final bool connected;

const AudioPlayerCard(this.openEarable, {super.key});
const _InternalAudioPlayerCard({
required this.openEarable,
required this.connected,
});

@override
State<AudioPlayerCard> createState() => _AudioPlayerCardState();
State<_InternalAudioPlayerCard> createState() =>
_InternalAudioPlayerCardState();
}

class _AudioPlayerCardState extends State<AudioPlayerCard> {
class _InternalAudioPlayerCardState extends State<_InternalAudioPlayerCard> {
late TextEditingController _filenameTextController;
late TextEditingController _jingleTextController;

late TextEditingController _frequencyTextController;
late TextEditingController _frequencyVolumeTextController;
Expand All @@ -28,9 +50,6 @@ class _AudioPlayerCardState extends State<AudioPlayerCard> {
_filenameTextController = TextEditingController(
text: OpenEarableSettings().selectedFilename,
);
_jingleTextController = TextEditingController(
text: OpenEarableSettings().selectedJingle,
);
_frequencyTextController = TextEditingController(
text: OpenEarableSettings().selectedFrequency,
);
Expand Down Expand Up @@ -81,11 +100,9 @@ class _AudioPlayerCardState extends State<AudioPlayerCard> {
}

void _setJingle() {
int jingleIndex =
OpenEarableSettings().getJingleIndex(_jingleTextController.text);
print(
"Setting source to jingle '${_jingleTextController.text}' with index $jingleIndex",
);
String jingleName = OpenEarableSettings().selectedJingle;
int jingleIndex = OpenEarableSettings().jingleMap[jingleName]!;
print("Setting source to jingle '$jingleName' with index $jingleIndex");
widget.openEarable.audioPlayer.jingle(jingleIndex);
}

Expand Down Expand Up @@ -152,69 +169,59 @@ class _AudioPlayerCardState extends State<AudioPlayerCard> {

@override
Widget build(BuildContext context) {
updateText(widget.connected);

return Padding(
padding: const EdgeInsets.symmetric(horizontal: 5.0),
child: Selector<BluetoothController, bool>(
selector: (_, bleController) => bleController.connected,
builder: (context, connected, child) {
updateText(connected);
return Card(
//Audio Player Card
color: Theme.of(context).colorScheme.primary,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Audio Player',
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
_getFileNameRow(),
_getJingleRow(),
_getFrequencyRow(),
SizedBox(height: 12),
_getMaterialButtonRow(connected),
],
child: Card(
//Audio Player Card
color: Theme.of(context).colorScheme.primary,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Audio Player',
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
),
);
},
_getFileNameRow(),
_getJingleRow(),
_getFrequencyRow(),
SizedBox(height: 12),
_getMaterialButtonRow(widget.connected),
],
),
),
),
);
}

Widget _getAudioPlayerRadio(int index) {
return Selector<BluetoothController, bool>(
selector: (_, bleController) => bleController.connected,
builder: (context, connected, child) {
return SizedBox(
height: 38,
width: 44,
child: Radio(
value: index,
groupValue: OpenEarableSettings().selectedAudioPlayerRadio,
onChanged: !connected
? null
: (int? value) {
setState(() {
OpenEarableSettings().selectedAudioPlayerRadio =
value ?? 0;
});
},
fillColor: WidgetStateProperty.resolveWith((states) {
if (states.contains(WidgetState.selected)) {
return Theme.of(context).colorScheme.secondary;
}
return Colors.grey;
}),
),
);
},
return SizedBox(
height: 38,
width: 44,
child: Radio(
value: index,
groupValue: OpenEarableSettings().selectedAudioPlayerRadio,
onChanged: !widget.connected
? null
: (int? value) {
setState(() {
OpenEarableSettings().selectedAudioPlayerRadio = value ?? 0;
});
},
fillColor: WidgetStateProperty.resolveWith((states) {
if (states.contains(WidgetState.selected)) {
return Theme.of(context).colorScheme.secondary;
}
return Colors.grey;
}),
),
);
}

Expand Down Expand Up @@ -257,29 +264,26 @@ class _AudioPlayerCardState extends State<AudioPlayerCard> {
String? placeholder,
int? maxLength,
) {
return Selector<BluetoothController, bool>(
selector: (_, controller) => controller.connected,
builder: (context, connected, child) {
return TextField(
controller: textController,
obscureText: false,
enabled: connected,
style: TextStyle(color: connected ? Colors.black : Colors.grey[700]),
decoration: InputDecoration(
labelText: placeholder,
contentPadding: EdgeInsets.fromLTRB(8, 0, 0, 0),
border: OutlineInputBorder(),
floatingLabelBehavior: FloatingLabelBehavior.never,
labelStyle:
TextStyle(color: connected ? Colors.black : Colors.grey[700]),
filled: true,
fillColor: connected ? Colors.white : Colors.grey,
),
keyboardType: keyboardType,
maxLength: maxLength,
maxLines: 1,
);
},
return TextField(
controller: textController,
obscureText: false,
enabled: widget.connected,
style:
TextStyle(color: widget.connected ? Colors.black : Colors.grey[700]),
decoration: InputDecoration(
labelText: placeholder,
contentPadding: EdgeInsets.fromLTRB(8, 0, 0, 0),
border: OutlineInputBorder(),
floatingLabelBehavior: FloatingLabelBehavior.never,
labelStyle: TextStyle(
color: widget.connected ? Colors.black : Colors.grey[700],
),
filled: true,
fillColor: widget.connected ? Colors.white : Colors.grey,
),
keyboardType: keyboardType,
maxLength: maxLength,
maxLines: 1,
);
}

Expand All @@ -302,9 +306,7 @@ class _AudioPlayerCardState extends State<AudioPlayerCard> {
Expanded(
child: Container(
decoration: BoxDecoration(
color: Provider.of<BluetoothController>(context).connected
? Colors.white
: Colors.grey,
color: widget.connected ? Colors.white : Colors.grey,
borderRadius: BorderRadius.circular(4.0),
),
child: SizedBox(
Expand All @@ -318,7 +320,7 @@ class _AudioPlayerCardState extends State<AudioPlayerCard> {
OpenEarableSettings().selectedJingle = newValue;
});
},
Provider.of<BluetoothController>(context).connected,
widget.connected,
false,
),
),
Expand Down Expand Up @@ -364,9 +366,7 @@ class _AudioPlayerCardState extends State<AudioPlayerCard> {
child: Text(
'Hz',
style: TextStyle(
color: Provider.of<BluetoothController>(context).connected
? Colors.white
: Colors.grey,
color: widget.connected ? Colors.white : Colors.grey,
), // Set text color to white
),
),
Expand All @@ -389,18 +389,14 @@ class _AudioPlayerCardState extends State<AudioPlayerCard> {
child: Text(
'%',
style: TextStyle(
color: Provider.of<BluetoothController>(context).connected
? Colors.white
: Colors.grey,
color: widget.connected ? Colors.white : Colors.grey,
), // Set text color to white
),
),
Spacer(),
Container(
decoration: BoxDecoration(
color: Provider.of<BluetoothController>(context).connected
? Colors.white
: Colors.grey,
color: widget.connected ? Colors.white : Colors.grey,
borderRadius: BorderRadius.circular(4.0),
),
child: SizedBox(
Expand All @@ -417,7 +413,7 @@ class _AudioPlayerCardState extends State<AudioPlayerCard> {
},
);
},
Provider.of<BluetoothController>(context).connected,
widget.connected,
false,
),
),
Expand Down
Loading