Skip to content

Commit

Permalink
lay out initial settings framework
Browse files Browse the repository at this point in the history
don't implement them yet because we want to merge with 3i's
  • Loading branch information
TheLastGimbus committed Feb 23, 2024
1 parent 7ee949b commit e02da40
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 152 deletions.
39 changes: 33 additions & 6 deletions lib/headphones/huawei/freebuds4i_sim.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,44 @@ import '../framework/anc.dart';
import '../framework/lrc_battery.dart';
import '../simulators/anc_sim.dart';
import '../simulators/bluetooth_headphones_sim.dart';
import '../simulators/headphones_settings_sim.dart';
import '../simulators/lrc_battery_sim.dart';
import 'freebuds4i.dart';
import 'settings.dart';

final class HuaweiFreeBuds4iSim extends HuaweiFreeBuds4i
with
BluetoothHeadphonesSim,
LRCBatteryAlwaysFullSim,
AncSim,
HeadphonesSettingsSim<HuaweiFreeBuds4iSettings> {}
with BluetoothHeadphonesSim, LRCBatteryAlwaysFullSim, AncSim {
// ehhhhhh...

final _settingsCtrl = BehaviorSubject<HuaweiFreeBuds4iSettings>.seeded(
const HuaweiFreeBuds4iSettings(
doubleTapLeft: DoubleTap.playPause,
doubleTapRight: DoubleTap.playPause,
holdBoth: Hold.cycleAnc,
holdBothToggledAncModes: {
AncMode.noiseCancelling,
AncMode.off,
AncMode.transparency,
},
autoPause: true,
),
);

@override
ValueStream<HuaweiFreeBuds4iSettings> get settings => _settingsCtrl.stream;

@override
Future<void> setSettings(HuaweiFreeBuds4iSettings newSettings) async {
_settingsCtrl.add(
_settingsCtrl.value.copyWith(
doubleTapLeft: newSettings.doubleTapLeft,
doubleTapRight: newSettings.doubleTapRight,
holdBoth: newSettings.holdBoth,
holdBothToggledAncModes: newSettings.holdBothToggledAncModes,
autoPause: newSettings.autoPause,
),
);
}
}

/// Class to use as placeholder for Disabled() widget
// this is not done with mixins because we may want to fill it with
Expand Down
17 changes: 17 additions & 0 deletions lib/headphones/huawei/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ class HuaweiFreeBuds4iSettings {
this.holdBothToggledAncModes,
this.autoPause,
});

// don't want to use codegen *yet*
HuaweiFreeBuds4iSettings copyWith({
DoubleTap? doubleTapLeft,
DoubleTap? doubleTapRight,
Hold? holdBoth,
Set<AncMode>? holdBothToggledAncModes,
bool? autoPause,
}) =>
HuaweiFreeBuds4iSettings(
doubleTapLeft: doubleTapLeft ?? this.doubleTapLeft,
doubleTapRight: doubleTapRight ?? this.doubleTapRight,
holdBoth: holdBoth ?? this.holdBoth,
holdBothToggledAncModes:
holdBothToggledAncModes ?? this.holdBothToggledAncModes,
autoPause: autoPause ?? this.autoPause,
);
}

// i don't have idea how to public/privatise those and how to name them
Expand Down
17 changes: 0 additions & 17 deletions lib/headphones/simulators/headphones_settings_sim.dart

This file was deleted.

28 changes: 0 additions & 28 deletions lib/ui/pages/headphones_settings/auto_pause_section.dart

This file was deleted.

39 changes: 25 additions & 14 deletions lib/ui/pages/headphones_settings/headphones_settings_page.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

import '../../../headphones/framework/headphones_settings.dart';
import '../../../headphones/huawei/settings.dart';
import '../../common/headphones_connection_ensuring_overlay.dart';
import 'huawei/auto_pause_section.dart';
import 'huawei/double_tap_section.dart';
import 'huawei/hold_section.dart';

class HeadphonesSettingsPage extends StatelessWidget {
const HeadphonesSettingsPage({super.key});
Expand All @@ -13,22 +18,28 @@ class HeadphonesSettingsPage extends StatelessWidget {
appBar: AppBar(title: Text(l.pageHeadphonesSettingsTitle)),
body: Center(
child: HeadphonesConnectionEnsuringOverlay(
builder: (_, h) {
return ListView(
children: [
// TODO MIGRATION: hp settings not yet implemented
const Text('HP Settings not yet implemented'),
// AutoPauseSection(headphones: h),
// const Divider(indent: 16, endIndent: 16),
// DoubleTapSection(headphones: h),
// const Divider(indent: 16, endIndent: 16),
// HoldSection(headphones: h),
// const SizedBox(height: 64),
],
);
},
builder: (_, h) =>
ListView(children: widgetsForModel(h as HeadphonesSettings)),
),
),
);
}
}

// this is shitty. and we don't want this. not here.
// ...
// but i have no better idea for now :)))))
List<Widget> widgetsForModel(HeadphonesSettings settings) {
if (settings is HeadphonesSettings<HuaweiFreeBuds4iSettings>) {
return [
AutoPauseSection(settings),
const Divider(indent: 16, endIndent: 16),
DoubleTapSection(settings),
const Divider(indent: 16, endIndent: 16),
HoldSection(settings),
const SizedBox(height: 64),
];
} else {
throw "You shouldn't be on this screen if you don't have settings!";
}
}
31 changes: 31 additions & 0 deletions lib/ui/pages/headphones_settings/huawei/auto_pause_section.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

import '../../../../headphones/framework/headphones_settings.dart';
import '../../../../headphones/huawei/settings.dart';
import '../../../common/list_tile_switch.dart';

class AutoPauseSection extends StatelessWidget {
final HeadphonesSettings<HuaweiFreeBuds4iSettings> headphones;

const AutoPauseSection(this.headphones, {super.key});

@override
Widget build(BuildContext context) {
final l = AppLocalizations.of(context)!;
return StreamBuilder(
stream: headphones.settings.map((s) => s.autoPause),
initialData: false,
builder: (_, snap) {
return ListTileSwitch(
title: Text(l.autoPause),
subtitle: Text(l.autoPauseDesc),
value: snap.data ?? false,
onChanged: (newVal) => headphones.setSettings(
HuaweiFreeBuds4iSettings(autoPause: newVal),
),
);
},
);
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

import '../../../headphones/_old/headphones_base.dart';
import '../../../headphones/_old/headphones_data_objects.dart';
import '../../common/list_tile_radio.dart';
import '../../common/list_tile_switch.dart';
import '../disabled.dart';
import '../../../../headphones/framework/headphones_settings.dart';
import '../../../../headphones/huawei/settings.dart';
import '../../../common/list_tile_radio.dart';
import '../../../common/list_tile_switch.dart';
import '../../disabled.dart';

class DoubleTapSection extends StatelessWidget {
final HeadphonesBase headphones;
final HeadphonesSettings<HuaweiFreeBuds4iSettings> headphones;

const DoubleTapSection({super.key, required this.headphones});
const DoubleTapSection(this.headphones, {super.key});

@override
Widget build(BuildContext context) {
final t = Theme.of(context);
final tt = t.textTheme;
final l = AppLocalizations.of(context)!;
return StreamBuilder<HeadphonesGestureSettings>(
stream: headphones.gestureSettings,
initialData: headphones.gestureSettings.valueOrNull ??
const HeadphonesGestureSettings(),
builder: (context, snapshot) {
final gs = snapshot.data!;
return StreamBuilder(
stream: headphones.settings
.map((s) => (l: s.doubleTapLeft, r: s.doubleTapRight)),
initialData: (l: null, r: null),
builder: (context, snap) {
final dt = snap.data!;
final enabled =
(gs.doubleTapLeft != HeadphonesGestureDoubleTap.nothing ||
gs.doubleTapRight != HeadphonesGestureDoubleTap.nothing);
(dt.l != DoubleTap.nothing || dt.r != DoubleTap.nothing);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expand All @@ -34,14 +33,9 @@ class DoubleTapSection extends StatelessWidget {
subtitle: Text(l.pageHeadphonesSettingsDoubleTapDesc),
value: enabled,
onChanged: (newVal) {
final g = newVal
? HeadphonesGestureDoubleTap.playPause
: HeadphonesGestureDoubleTap.nothing;
headphones.setGestureSettings(
HeadphonesGestureSettings(
doubleTapLeft: g,
doubleTapRight: g,
),
final g = newVal ? DoubleTap.playPause : DoubleTap.nothing;
headphones.setSettings(
HuaweiFreeBuds4iSettings(doubleTapLeft: g, doubleTapRight: g),
);
},
),
Expand All @@ -57,10 +51,10 @@ class DoubleTapSection extends StatelessWidget {
l.pageHeadphonesSettingsLeftBud,
style: tt.titleMedium,
),
value: gs.doubleTapLeft,
value: dt.l,
onChanged: enabled
? (g) => headphones.setGestureSettings(
HeadphonesGestureSettings(doubleTapLeft: g),
? (g) => headphones.setSettings(
HuaweiFreeBuds4iSettings(doubleTapLeft: g),
)
: null,
),
Expand All @@ -71,10 +65,10 @@ class DoubleTapSection extends StatelessWidget {
l.pageHeadphonesSettingsRightBud,
style: tt.titleMedium,
),
value: gs.doubleTapRight,
value: dt.r,
onChanged: enabled
? (g) => headphones.setGestureSettings(
HeadphonesGestureSettings(doubleTapRight: g),
? (g) => headphones.setSettings(
HuaweiFreeBuds4iSettings(doubleTapRight: g),
)
: null,
),
Expand All @@ -92,8 +86,8 @@ class DoubleTapSection extends StatelessWidget {

class _DoubleTapSetting extends StatelessWidget {
final Widget? title;
final HeadphonesGestureDoubleTap? value;
final void Function(HeadphonesGestureDoubleTap?)? onChanged;
final DoubleTap? value;
final void Function(DoubleTap?)? onChanged;

const _DoubleTapSetting({
required this.value,
Expand All @@ -117,35 +111,35 @@ class _DoubleTapSetting extends StatelessWidget {
],
ListTileRadio(
title: Text(l.pageHeadphonesSettingsDoubleTapPlayPause),
value: HeadphonesGestureDoubleTap.playPause,
value: DoubleTap.playPause,
dense: true,
groupValue: value,
onChanged: onChanged,
),
ListTileRadio(
title: Text(l.pageHeadphonesSettingsDoubleTapNextSong),
value: HeadphonesGestureDoubleTap.next,
value: DoubleTap.next,
dense: true,
groupValue: value,
onChanged: onChanged,
),
ListTileRadio(
title: Text(l.pageHeadphonesSettingsDoubleTapPrevSong),
value: HeadphonesGestureDoubleTap.previous,
value: DoubleTap.previous,
dense: true,
groupValue: value,
onChanged: onChanged,
),
ListTileRadio(
title: Text(l.pageHeadphonesSettingsDoubleTapAssist),
value: HeadphonesGestureDoubleTap.voiceAssistant,
value: DoubleTap.voiceAssistant,
dense: true,
groupValue: value,
onChanged: onChanged,
),
ListTileRadio(
title: Text(l.pageHeadphonesSettingsDoubleTapNone),
value: HeadphonesGestureDoubleTap.nothing,
value: DoubleTap.nothing,
dense: true,
groupValue: value,
onChanged: onChanged,
Expand Down
Loading

0 comments on commit e02da40

Please sign in to comment.