Remember volume and repeat mode across app launches. Been meaning to do

this for a while.
This commit is contained in:
casey langen 2016-12-03 14:12:54 -08:00
parent 53429bdf92
commit 3bb07daba4
7 changed files with 52 additions and 5 deletions

View File

@ -53,8 +53,8 @@ namespace musik {
};
enum RepeatMode {
RepeatNone,
RepeatTrack,
RepeatList
RepeatNone = 0,
RepeatTrack = 1,
RepeatList = 2
};
} } }

View File

@ -39,11 +39,14 @@ namespace musik { namespace core { namespace prefs {
const std::string components::Settings = "settings";
const std::string components::Libraries = "libraries";
const std::string components::Playback = "playback";
const std::string keys::AutoSyncIntervalMillis = "AutoSyncIntervalMillis";
const std::string keys::MaxTagReadThreads = "MaxTagReadThreads";
const std::string keys::RemoveMissingFiles = "RemoveMissingFiles";
const std::string keys::SyncOnStartup = "SyncOnStartup";
const std::string keys::Volume = "Volume";
const std::string keys::RepeatMode = "RepeatMode";
} } }

View File

@ -41,6 +41,7 @@ namespace musik { namespace core { namespace prefs {
namespace components {
extern const std::string Settings;
extern const std::string Libraries;
extern const std::string Playback;
}
namespace keys {
@ -48,6 +49,8 @@ namespace musik { namespace core { namespace prefs {
extern const std::string MaxTagReadThreads;
extern const std::string RemoveMissingFiles;
extern const std::string SyncOnStartup;
extern const std::string Volume;
extern const std::string RepeatMode;
}
} } }

View File

@ -100,7 +100,7 @@ static bool stringToFile(const std::string& fn, const std::string& str) {
boost::str(boost::format("%s-%s") % name % mode)
std::shared_ptr<Preferences> Preferences::ForComponent(
const std::string& c, Preferences::Mode mode)
const std::string& c, Preferences::Mode mode)
{
boost::mutex::scoped_lock lock(cacheMutex);
@ -156,6 +156,11 @@ int Preferences::GetInt(const std::string& key, int defaultValue) {
RETURN_VALUE(defaultValue);
}
double Preferences::GetDouble(const std::string& key, double defaultValue) {
boost::mutex::scoped_lock lock(this->mutex);
RETURN_VALUE(defaultValue);
}
std::string Preferences::GetString(const std::string& key, const std::string& defaultValue) {
boost::mutex::scoped_lock lock(this->mutex);
RETURN_VALUE(defaultValue);
@ -171,6 +176,11 @@ void Preferences::SetInt(const std::string& key, int value) {
json[key] = value;
}
void Preferences::SetDouble(const std::string& key, double value) {
boost::mutex::scoped_lock lock(this->mutex);
json[key] = value;
}
void Preferences::SetString(const std::string& key, const char* value) {
boost::mutex::scoped_lock lock(this->mutex);
json[key] = value;

View File

@ -59,10 +59,12 @@ namespace musik { namespace core {
bool GetBool(const std::string& key, bool defaultValue = false);
int GetInt(const std::string& key, int defaultValue = 0);
double GetDouble(const std::string& key, double defaultValue = 0.0f);
std::string GetString(const std::string& key, const std::string& defaultValue = "");
void SetBool(const std::string& key, bool value);
void SetInt(const std::string& key, int value);
void SetDouble(const std::string& key, double value);
void SetString(const std::string& key, const char* value);
void GetKeys(std::vector<std::string>& target);

View File

@ -44,6 +44,7 @@
#include <core/library/LocalLibraryConstants.h>
#include <core/library/track/RetainedTrack.h>
#include <core/plugin/PluginFactory.h>
#include <core/support/PreferenceKeys.h>
#include <boost/lexical_cast.hpp>
@ -56,6 +57,7 @@ using cursespp::IMessage;
using namespace musik::core::library;
using namespace musik::core;
using namespace musik::core::prefs;
using namespace musik::core::sdk;
using namespace musik::box;
@ -96,15 +98,39 @@ class StreamMessage : public cursespp::Message {
cursespp::MessageQueue::Instance().Post( \
cursespp::IMessagePtr(new StreamMessage(instance, eventType, uri)));
static inline void loadPreferences(
ITransport& transport,
IPlaybackService& playback,
std::shared_ptr<Preferences> prefs)
{
double volume = prefs->GetDouble(keys::Volume, 1.0f);
volume = std::max(0.0f, std::min(1.0f, (float)volume));
transport.SetVolume(volume);
int repeatMode = prefs->GetInt(keys::RepeatMode, RepeatNone);
repeatMode = (repeatMode > RepeatList || repeatMode < RepeatNone) ? RepeatNone : repeatMode;
playback.SetRepeatMode((RepeatMode) repeatMode);
}
static inline void savePreferences(
IPlaybackService& playback,
std::shared_ptr<Preferences> prefs)
{
prefs->SetDouble(keys::Volume, playback.GetVolume());
prefs->SetInt(keys::RepeatMode, playback.GetRepeatMode());
}
PlaybackService::PlaybackService(LibraryPtr library, ITransport& transport)
: library(library)
, transport(transport)
, playlist(library)
, unshuffled(library)
, repeatMode(RepeatNone) {
, repeatMode(RepeatNone)
, prefs(Preferences::ForComponent(components::Playback)) {
transport.StreamEvent.connect(this, &PlaybackService::OnStreamEvent);
transport.PlaybackEvent.connect(this, &PlaybackService::OnPlaybackEvent);
transport.VolumeChanged.connect(this, &PlaybackService::OnVolumeChanged);
loadPreferences(this->transport, *this, prefs);
this->index = NO_POSITION;
this->nextIndex = NO_POSITION;
this->InitRemotes();
@ -113,6 +139,7 @@ PlaybackService::PlaybackService(LibraryPtr library, ITransport& transport)
PlaybackService::~PlaybackService() {
this->Stop();
this->ResetRemotes();
savePreferences(*this, prefs);
}
void PlaybackService::InitRemotes() {

View File

@ -45,6 +45,7 @@
#include <core/library/track/Track.h>
#include <core/library/ILibrary.h>
#include <core/audio/ITransport.h>
#include <core/support/Preferences.h>
#include <boost/thread/recursive_mutex.hpp>
@ -112,6 +113,7 @@ namespace musik {
boost::recursive_mutex playlistMutex;
std::vector<std::shared_ptr<musik::core::sdk::IPlaybackRemote > > remotes;
std::shared_ptr<musik::core::Preferences> prefs;
musik::core::LibraryPtr library;
musik::core::audio::ITransport& transport;