Selaa lähdekoodia

Simplify AudioSettingsController

Instead of looking what changed, compute the state on all changes, and
emit an update only if the result of our computation changed. In a way
more wasteful (but not really), and simpler. It also removes at least
one duplicated line.
Vincent Lang 9 vuotta sitten
vanhempi
commit
fb84f91bbb
1 muutettua tiedostoa jossa 18 lisäystä ja 26 poistoa
  1. 18 26
      src/settings/AudioSettingsController.cpp

+ 18 - 26
src/settings/AudioSettingsController.cpp

@@ -25,37 +25,29 @@ void AudioSettingsController::setHiddenPassthrough(const QStringList& codecs, bo
 /////////////////////////////////////////////////////////////////////////////////////////
 void AudioSettingsController::valuesUpdated(const QVariantMap& values)
 {
-  bool advanced = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "advanced").toBool();
   SettingsSection* audioSection = SettingsComponent::Get().getSection(SETTINGS_SECTION_AUDIO);
+  auto prevDescriptions = audioSection->descriptions();
+
+  bool advanced = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "advanced").toBool();
+  QString type = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "devicetype").toString();
 
-  if (values.contains("devicetype"))
+  if (type == AUDIO_DEVICE_TYPE_BASIC)
   {
-    QString type = values.value("devicetype").toString();
-    if (type == AUDIO_DEVICE_TYPE_BASIC)
-    {
-      setHiddenPassthrough(PlayerComponent::AudioCodecsAll(), true);
-    }
-    else if (type == AUDIO_DEVICE_TYPE_HDMI)
-    {
-      setHiddenPassthrough(PlayerComponent::AudioCodecsAll(), !advanced);
-    }
-    else if (type == AUDIO_DEVICE_TYPE_SPDIF)
-    {
-      setHiddenPassthrough(PlayerComponent::AudioCodecsAll(), true);
-      setHiddenPassthrough(PlayerComponent::AudioCodecsSPDIF(), false);
-    }
-
-    emit settingsUpdated(SETTINGS_SECTION_AUDIO, audioSection->descriptions());
+    setHiddenPassthrough(PlayerComponent::AudioCodecsAll(), true);
   }
-
-  if (values.contains("advanced"))
+  else if (type == AUDIO_DEVICE_TYPE_HDMI)
   {
-    advanced = values.value("advanced").toBool();
-    if (audioSection->value("devicetype") == AUDIO_DEVICE_TYPE_HDMI)
-      setHiddenPassthrough(PlayerComponent::AudioCodecsAll(), !advanced);
+    setHiddenPassthrough(PlayerComponent::AudioCodecsAll(), !advanced);
+  }
+  else if (type == AUDIO_DEVICE_TYPE_SPDIF)
+  {
+    setHiddenPassthrough(PlayerComponent::AudioCodecsAll(), true);
+    setHiddenPassthrough(PlayerComponent::AudioCodecsSPDIF(), false);
+  }
 
-    audioSection->setValueHidden("exclusive", !advanced);
+  audioSection->setValueHidden("exclusive", !advanced);
 
-    emit settingsUpdated(SETTINGS_SECTION_AUDIO, audioSection->descriptions());
-  }
+  auto newDescriptions = audioSection->descriptions();
+  if (prevDescriptions != newDescriptions)
+    emit settingsUpdated(SETTINGS_SECTION_AUDIO, newDescriptions);
 }