Implemented shuffle functionality. Not able to reproduce the bug with repeating the song.

This commit is contained in:
kdeng00
2019-11-05 22:02:43 -05:00
parent 2ba9dfd57a
commit 76a6a763c6
11 changed files with 239 additions and 231 deletions
+46 -10
View File
@@ -25,6 +25,7 @@
#include "APIRepository.h"
#include "RepeatRepository.h"
#include "ShuffleRepository.h"
#include "SongRepository.h"
#include "Tok.h"
#include "TokenRepository.h"
@@ -92,15 +93,6 @@ model::APIInfo retrieveAPIInfo(const std::string& path)
}
model::Song retrieveSong(const std::string& token, const std::string& baseUri, const int songId)
{
repository::SongRepository songRepo;
model::Song song(songId, "Smooth", "Amaze", "The Herb", "Blazing",
420, 420);
return songRepo.retrieveSong(token, song, baseUri);
}
model::Song retrieveSong(const model::Token& token, const model::Song& song,
const std::string& baseUri)
{
@@ -148,6 +140,18 @@ int retrieveRepeatMode(const std::string& path)
return static_cast<int>(repeatMode);
}
int retrieveShuffleMode(const std::string& path)
{
repository::local::ShuffleRepository shuffleRepo;
if (!shuffleRepo.doesTableExist(path)) {
shuffleRepo.createShuffleTable(path);
}
auto shuffleMode = shuffleRepo.retrieveShuffleMode(path);
return static_cast<int>(shuffleMode);
}
bool doesDatabaseExist(const std::string& dataPath)
{
@@ -185,7 +189,7 @@ void saveAPIInfo(const model::APIInfo& apiInfo, const std::string& path)
if (!apiRepo.databaseExist(path)) {
apiRepo.initializedDatabase(path);
}
if (!apiRepo.doesAPIInfoTableExist(path)) {
if (!apiRepo.doesTableExist(path)) {
apiRepo.createAPiInfoTable(path);
}
if (!apiRepo.isTableEmpty(path)) {
@@ -235,6 +239,12 @@ void updateRepeatMode(const std::string& path)
repeatRepo.updateRepeat(path);
}
void updateShuffleMode(const std::string& path)
{
repository::local::ShuffleRepository shuffleRepo;
shuffleRepo.updateShuffle(path);
}
extern "C"
JNIEXPORT jobjectArray
@@ -422,6 +432,20 @@ Java_com_example_mear_repositories_RepeatRepository_retrieveRepeatMode(
return repeatMode;
}
extern "C"
JNIEXPORT jint
JNICALL
Java_com_example_mear_repositories_ShuffleRepository_retrieveShuffleMode(
JNIEnv *env,
jobject thisObj,
jstring pathStr
) {
const auto dataPath = jstringToString(env, pathStr);
auto shuffleMode = retrieveShuffleMode(dataPath);
return shuffleMode;
}
extern "C"
JNIEXPORT jboolean
@@ -555,4 +579,16 @@ Java_com_example_mear_repositories_RepeatRepository_updateRepeatMode(
) {
const auto dataPath = jstringToString(env, pathStr);
updateRepeatMode(dataPath);
}
extern "C"
JNIEXPORT void
JNICALL
Java_com_example_mear_repositories_ShuffleRepository_updateShuffle(
JNIEnv *env,
jobject thisObj,
jstring pathStr
) {
const auto dataPath = jstringToString(env, pathStr);
updateShuffleMode(dataPath);
}
+2 -1
View File
@@ -23,7 +23,6 @@ std::string jstringToString(JNIEnv*, jstring&);
model::APIInfo retrieveAPIInfo(const std::string&);
model::Song retrieveSong(const std::string&, const std::string&, const int);
model::Song retrieveSong(const model::Token&, const model::Song&, const std::string&);
model::Token fetchToken(const model::User&, const std::string&);
@@ -32,6 +31,7 @@ model::Token retrieveSavedToken(const std::string&);
model::User retrieveCredentials(const std::string&);
int retrieveRepeatMode(const std::string&);
int retrieveShuffleMode(const std::string&);
bool doesDatabaseExist(const std::string&);
bool apiInformationExist(const std::string&);
@@ -42,6 +42,7 @@ void saveAPIInfo(const model::APIInfo&, const std::string&);
void saveCredentials(const model::User&, const std::string&);
void saveToken(const model::Token&, const std::string&);
void updateRepeatMode(const std::string&);
void updateShuffleMode(const std::string&);
+93
View File
@@ -3,3 +3,96 @@
//
#include "ShuffleRepository.h"
namespace repository { namespace local {
ShuffleRepository::ShuffleRepository() {
m_tableName = shuffleTable();
}
ShuffleTypes ShuffleRepository::retrieveShuffleMode(const std::string& path) {
try {
auto db = getDbConn(path, ConnType::ReadOnly);
std::string queryString("SELECT * FROM ");
queryString.append(m_tableName);
queryString.append(" LIMIT 1");
SQLite::Statement query(db, queryString);
const auto result = query.executeStep();
auto shuffleType = query.getColumn(1).getInt();
auto val = static_cast<ShuffleTypes>(shuffleType);
return val;
} catch (std::exception &ex) {
auto msg = ex.what();
}
return ShuffleTypes::ShuffleOff;
}
void ShuffleRepository::createShuffleTable(const std::string& path) {
try {
auto db = getDbConn(path, ConnType::ReadWrite);
std::string queryString("CREATE TABLE ");
queryString.append(m_tableName);
queryString.append(" (Id INTEGER PRIMARY KEY, ShuffleMode INT)");
db.exec(queryString);
queryString = "INSERT INTO ";
queryString.append(m_tableName);
queryString.append(" (ShuffleMode) VALUES(?)");
SQLite::Statement query(db, queryString);
query.bind(1, static_cast<int>(ShuffleTypes::ShuffleOff));
query.exec();
} catch (std::exception& ex) {
auto msg = ex.what();
}
}
void ShuffleRepository::updateShuffle(const std::string& path) {
try {
auto db = getDbConn(path, ConnType::ReadWrite);
if (!doesTableExist(path)) {
createShuffleTable(path);
}
if (isTableEmpty(path)) {
constexpr auto queryString = "INSERT INTO ? (ShuffleMode) VALUES(?)";
SQLite::Statement query(db, queryString);
query.bind(1, m_tableName);
query.bind(2, static_cast<int>(ShuffleTypes::ShuffleOff));
query.exec();
} else {
std::string queryString("UPDATE ");
queryString.append(m_tableName);
queryString.append(" SET ShuffleMode = ?");
auto shuffleMode = retrieveShuffleMode(path);
SQLite::Statement query(db, queryString);
switch (shuffleMode) {
case ShuffleTypes::ShuffleOff:
query.bind(1, static_cast<int>(ShuffleTypes::ShuffleOn));
break;
case ShuffleTypes ::ShuffleOn:
query.bind(1, static_cast<int>(ShuffleTypes::ShuffleOff));
break;
default:
break;
}
query.exec();
}
} catch (std::exception& ex) {
auto msg = ex.what();
}
}
std::string ShuffleRepository::shuffleTable() noexcept { return "Shuffle"; }
}}
+16 -2
View File
@@ -5,10 +5,24 @@
#ifndef MEAR_SHUFFLEREPOSITORY_H
#define MEAR_SHUFFLEREPOSITORY_H
#include <string>
class ShuffleRepository {
#include "BaseRepository.h"
#include "ShuffleTypes.h"
};
namespace repository { namespace local {
class ShuffleRepository : public BaseRepository {
public:
ShuffleRepository();
ShuffleTypes retrieveShuffleMode(const std::string&);
void createShuffleTable(const std::string&);
void updateShuffle(const std::string&);
private:
std::string shuffleTable() noexcept;
};
}}
#endif //MEAR_SHUFFLEREPOSITORY_H
+2 -2
View File
@@ -6,8 +6,8 @@
#define MEAR_SHUFFLETYPES_H
enum class ShuffleTypes {
ShuffleQueue = 0,
ShuffleOff
ShuffleOn = 0,
ShuffleOff = 1
};
#endif //MEAR_SHUFFLETYPES_H