Skip to content

Commit 4dbcfd8

Browse files
committed
Don't design Core::AirPods::Manager as a singleton class
1 parent 45cae93 commit 4dbcfd8

File tree

11 files changed

+46
-60
lines changed

11 files changed

+46
-60
lines changed

Source/Application.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,21 +151,15 @@ bool ApdApplication::Prepare()
151151
Core::OS::Windows::Winrt::Initialize();
152152
#endif
153153

154-
connect(this, &ApdApplication::aboutToQuit, this, &ApdApplication::QuitHandler);
155154
return true;
156155
}
157156

158157
int ApdApplication::Run()
159158
{
160-
Core::AirPods::StartScanner();
159+
_mainWindow->GetApdMgr().StartScanner();
161160
return exec();
162161
}
163162

164-
void ApdApplication::QuitHandler()
165-
{
166-
Core::AirPods::OnQuit();
167-
}
168-
169163
void ApdApplication::QuitSafety()
170164
{
171165
QMetaObject::invokeMethod(qApp, &QApplication::quit, Qt::QueuedConnection);

Source/Application.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ class ApdApplication : public SingleApplication
7272

7373
static void InitSettings();
7474
static void FirstTimeUse();
75-
76-
void QuitHandler();
7775
};
7876

7977
#define ApdApp (dynamic_cast<ApdApplication *>(QCoreApplication::instance()))

Source/Core/AirPods.cpp

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ void StateManager::DoStateReset(Side side)
357357
adv.reset();
358358
}
359359
}
360+
} // namespace Details
360361

361362
//
362363
// Manager
@@ -395,12 +396,6 @@ void Manager::StopScanner()
395396
}
396397
}
397398

398-
QString Manager::GetDisplayName()
399-
{
400-
std::lock_guard<std::mutex> lock{_mutex};
401-
return _displayName;
402-
}
403-
404399
void Manager::OnRssiMinChanged(int16_t rssiMin)
405400
{
406401
std::lock_guard<std::mutex> lock{_mutex};
@@ -451,11 +446,6 @@ void Manager::OnBoundDeviceAddressChanged(uint64_t address)
451446
OnBoundDeviceConnectionStateChanged(currentState);
452447
}
453448

454-
void Manager::OnQuit()
455-
{
456-
StopScanner();
457-
}
458-
459449
void Manager::OnBoundDeviceConnectionStateChanged(Bluetooth::DeviceState state)
460450
{
461451
bool newDeviceConnected = state == Bluetooth::DeviceState::Connected;
@@ -472,10 +462,12 @@ void Manager::OnBoundDeviceConnectionStateChanged(Bluetooth::DeviceState state)
472462
GlobalMedia::OnLimitedDeviceStateChanged((_displayName + " Stereo").toStdString());
473463
}
474464

475-
void Manager::OnStateChanged(StateManager::UpdateEvent updateEvent)
465+
void Manager::OnStateChanged(Details::StateManager::UpdateEvent updateEvent)
476466
{
477467
const auto &oldState = updateEvent.oldState;
478-
const auto &newState = updateEvent.newState;
468+
auto &newState = updateEvent.newState;
469+
470+
newState.displayName = _displayName;
479471

480472
ApdApp->GetMainWindow()->UpdateStateSafety(newState);
481473

@@ -533,7 +525,7 @@ void Manager::OnBothInEar(bool isBothInEar)
533525

534526
bool Manager::OnAdvertisementReceived(const Bluetooth::AdvertisementWatcher::ReceivedData &data)
535527
{
536-
if (!Advertisement::IsDesiredAdv(data)) {
528+
if (!Details::Advertisement::IsDesiredAdv(data)) {
537529
return false;
538530
}
539531

@@ -542,12 +534,12 @@ bool Manager::OnAdvertisementReceived(const Bluetooth::AdvertisementWatcher::Rec
542534
return false;
543535
}
544536

545-
Advertisement adv{data};
537+
Details::Advertisement adv{data};
546538

547539
LOG(Trace, "AirPods advertisement received. Data: {}, Address Hash: {}, RSSI: {}",
548540
Helper::ToString(adv.GetDesensitizedData()), Helper::Hash(data.address), data.rssi);
549541

550-
auto optUpdateEvent = _stateMgr.OnAdvReceived(Advertisement{data});
542+
auto optUpdateEvent = _stateMgr.OnAdvReceived(Details::Advertisement{data});
551543
if (optUpdateEvent.has_value()) {
552544
OnStateChanged(std::move(optUpdateEvent.value()));
553545
}
@@ -573,8 +565,6 @@ void Manager::OnAdvWatcherStateChanged(
573565
}
574566
}
575567

576-
} // namespace Details
577-
578568
std::vector<Bluetooth::Device> GetDevices()
579569
{
580570
std::vector<Bluetooth::Device> devices =

Source/Core/AirPods.h

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ struct State {
6262
Model model{Model::Unknown};
6363
PodsState pods;
6464
CaseState caseBox;
65+
QString displayName;
6566

6667
bool operator==(const State &rhs) const = default;
6768
};
@@ -140,49 +141,37 @@ class StateManager
140141
void DoLost();
141142
void DoStateReset(Side side);
142143
};
144+
} // namespace Details
143145

144-
class Manager : public Helper::Singleton<Manager>
146+
class Manager
145147
{
146-
protected:
148+
public:
147149
Manager();
148-
friend Helper::Singleton<Manager>;
149150

150-
public:
151151
void StartScanner();
152152
void StopScanner();
153153

154-
QString GetDisplayName();
155-
156154
void OnRssiMinChanged(int16_t rssiMin);
157155
void OnAutomaticEarDetectionChanged(bool enable);
158156
void OnBoundDeviceAddressChanged(uint64_t address);
159-
void OnQuit();
160157

161158
private:
159+
std::mutex _mutex;
162160
Bluetooth::AdvertisementWatcher _adWatcher;
163-
StateManager _stateMgr;
161+
Details::StateManager _stateMgr;
164162
std::optional<Bluetooth::Device> _boundDevice;
165163
QString _displayName;
166164
bool _deviceConnected{false};
167-
std::mutex _mutex;
168165
bool _automaticEarDetection{false};
169166

170167
void OnBoundDeviceConnectionStateChanged(Bluetooth::DeviceState state);
171-
void OnStateChanged(StateManager::UpdateEvent updateEvent);
168+
void OnStateChanged(Details::StateManager::UpdateEvent updateEvent);
172169
void OnLidOpened(bool opened);
173170
void OnBothInEar(bool isBothInEar);
174171
bool OnAdvertisementReceived(const Bluetooth::AdvertisementWatcher::ReceivedData &data);
175172
void OnAdvWatcherStateChanged(
176173
Bluetooth::AdvertisementWatcher::State state, const std::optional<std::string> &optError);
177174
};
178-
} // namespace Details
179-
180-
SINGLETON_EXPOSE_FUNCTION(Details::Manager, StartScanner)
181-
SINGLETON_EXPOSE_FUNCTION(Details::Manager, GetDisplayName)
182-
SINGLETON_EXPOSE_FUNCTION(Details::Manager, OnRssiMinChanged)
183-
SINGLETON_EXPOSE_FUNCTION(Details::Manager, OnAutomaticEarDetectionChanged)
184-
SINGLETON_EXPOSE_FUNCTION(Details::Manager, OnBoundDeviceAddressChanged)
185-
SINGLETON_EXPOSE_FUNCTION(Details::Manager, OnQuit)
186175

187176
std::vector<Core::Bluetooth::Device> GetDevices();
188177

Source/Core/Bluetooth_win.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,12 @@ AdvertisementWatcher::AdvertisementWatcher()
281281

282282
AdvertisementWatcher::~AdvertisementWatcher()
283283
{
284-
Stop();
284+
if (!_stop) {
285+
_destroy = true;
286+
Stop();
287+
std::unique_lock<std::mutex> lock{_conVarMutex};
288+
_destroyConVar.wait(lock);
289+
}
285290
}
286291

287292
bool AdvertisementWatcher::Start()
@@ -344,7 +349,6 @@ void AdvertisementWatcher::OnReceived(const BluetoothLEAdvertisementReceivedEven
344349

345350
void AdvertisementWatcher::OnStopped(const BluetoothLEAdvertisementWatcherStoppedEventArgs &args)
346351
{
347-
348352
static std::unordered_map<BluetoothError, std::string> errorReasons = {
349353
{BluetoothError::Success, "Success"},
350354
{BluetoothError::RadioNotAvailable, "RadioNotAvailable"},
@@ -390,9 +394,14 @@ void AdvertisementWatcher::OnStopped(const BluetoothLEAdvertisementWatcherStoppe
390394

391395
CbStateChanged().Invoke(State::Stopped, optError);
392396

393-
do {
394-
std::unique_lock<std::mutex> lock{_conVarMutex};
395-
_stopConVar.wait_until(lock, _lastStartTime.load() + kRetryInterval);
396-
} while (!_stop && !Start());
397+
if (!_destroy) {
398+
do {
399+
std::unique_lock<std::mutex> lock{_conVarMutex};
400+
_stopConVar.wait_until(lock, _lastStartTime.load() + kRetryInterval);
401+
} while (!_stop && !Start());
402+
}
403+
else {
404+
_destroyConVar.notify_all();
405+
}
397406
}
398407
} // namespace Core::Bluetooth

Source/Core/Bluetooth_win.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ class AdvertisementWatcher final
125125
WinrtBlutoothAdv::BluetoothLEAdvertisementWatcher _bleWatcher;
126126
std::mutex _mutex;
127127

128-
std::atomic<bool> _stop{false};
128+
std::atomic<bool> _stop{false}, _destroy{false};
129129
std::atomic<std::chrono::steady_clock::time_point> _lastStartTime;
130130
std::mutex _conVarMutex;
131-
std::condition_variable _stopConVar;
131+
std::condition_variable _stopConVar, _destroyConVar;
132132

133133
void OnReceived(const WinrtBlutoothAdv::BluetoothLEAdvertisementReceivedEventArgs &args);
134134
void OnStopped(const WinrtBlutoothAdv::BluetoothLEAdvertisementWatcherStoppedEventArgs &args);

Source/Core/Settings.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,15 @@ void OnApply_automatic_ear_detection(const Fields &newFields)
7272
{
7373
LOG(Info, "OnApply_automatic_ear_detection: {}", newFields.automatic_ear_detection);
7474

75-
AirPods::OnAutomaticEarDetectionChanged(newFields.automatic_ear_detection);
75+
ApdApp->GetMainWindow()->GetApdMgr().OnAutomaticEarDetectionChanged(
76+
newFields.automatic_ear_detection);
7677
}
7778

7879
void OnApply_rssi_min(const Fields &newFields)
7980
{
8081
LOG(Info, "OnApply_rssi_min: {}", newFields.rssi_min);
8182

82-
AirPods::OnRssiMinChanged(newFields.rssi_min);
83+
ApdApp->GetMainWindow()->GetApdMgr().OnRssiMinChanged(newFields.rssi_min);
8384
}
8485

8586
void OnApply_reduce_loud_sounds(const Fields &newFields)
@@ -111,7 +112,7 @@ void OnApply_device_address(const Fields &newFields)
111112
ApdApp->GetMainWindow()->BindSafety();
112113
}
113114

114-
AirPods::OnBoundDeviceAddressChanged(newFields.device_address);
115+
ApdApp->GetMainWindow()->GetApdMgr().OnBoundDeviceAddressChanged(newFields.device_address);
115116
}
116117

117118
void OnApply_tray_icon_battery(const Fields &newFields)

Source/Gui/MainWindow.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,7 @@ void MainWindow::Repaint()
543543

544544
const auto &state = _cachedState.value();
545545

546-
// _ui.deviceLabel->setText(Helper::ToString(state.model));
547-
_ui.deviceLabel->setText(Core::AirPods::GetDisplayName());
546+
_ui.deviceLabel->setText(state.displayName);
548547

549548
SetAnimation(state.model);
550549

Source/Gui/MainWindow.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ class MainWindow : public QDialog
5050
public:
5151
MainWindow(QWidget *parent = nullptr);
5252

53+
inline auto &GetApdMgr()
54+
{
55+
return _apdMgr;
56+
}
57+
5358
void UpdateState(const Core::AirPods::State &state);
5459
void Available();
5560
void Unavailable();
@@ -83,6 +88,7 @@ class MainWindow : public QDialog
8388
Widget::Battery *_rightBattery = new Widget::Battery{this};
8489
Widget::Battery *_caseBattery = new Widget::Battery{this};
8590

91+
Core::AirPods::Manager _apdMgr;
8692
Core::Update::AsyncChecker _updateChecker{[this](auto &&...args) {
8793
VersionUpdateAvailableSafety(std::forward<decltype(args)>(args)...);
8894
}};

Source/Gui/TrayIcon.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ void TrayIcon::Repaint()
118118
}
119119
const auto &state = _airPodsState.value();
120120

121-
// toolTipContent += Helper::ToString(state.model);
122-
toolTipContent += Core::AirPods::GetDisplayName();
121+
toolTipContent += state.displayName;
123122

124123
// clang-format off
125124
if (state.pods.left.battery.Available()) {

0 commit comments

Comments
 (0)