Working on implementing the Toggle controls like Repeat and Shuffle. Having a little issues with Repeat, haven't started with Shuffle

This commit is contained in:
kdeng00
2019-10-28 21:47:04 -04:00
parent b1176ca5ba
commit 6221969d6b
14 changed files with 144 additions and 166 deletions
+4 -1
View File
@@ -29,10 +29,13 @@ namespace repository { namespace local {
const auto dbPath = pathOfDatabase(appPath); const auto dbPath = pathOfDatabase(appPath);
SQLite::Database db(dbPath, SQLite::OPEN_READONLY); SQLite::Database db(dbPath, SQLite::OPEN_READONLY);
return db.tableExists(m_tableName); auto result = db.tableExists(m_tableName);
return result;
} catch (std::exception& ex) { } catch (std::exception& ex) {
auto msg = ex.what(); auto msg = ex.what();
} }
return false;
} }
bool BaseRepository::isTableEmpty(const std::string& appPath) bool BaseRepository::isTableEmpty(const std::string& appPath)
+56 -4
View File
@@ -18,13 +18,13 @@
#include <android/asset_manager_jni.h> #include <android/asset_manager_jni.h>
#include <android/log.h> #include <android/log.h>
#include <android/sharedmem.h> #include <android/sharedmem.h>
#include <curl/curl.h>
#include <nlohmann/json.hpp>
#include <sqlite3.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <nlohmann/json.hpp>
#include <curl/curl.h>
#include <sqlite3.h>
#include "APIRepository.h" #include "APIRepository.h"
#include "RepeatRepository.h"
#include "SongRepository.h" #include "SongRepository.h"
#include "Tok.h" #include "Tok.h"
#include "TokenRepository.h" #include "TokenRepository.h"
@@ -75,6 +75,12 @@ jobject songToObj(JNIEnv *env, const model::Song& song)
} }
std::string jstringToString(JNIEnv *env, jstring& val)
{
return env->GetStringUTFChars(val, nullptr);
}
model::APIInfo retrieveAPIInfo(const std::string& path) model::APIInfo retrieveAPIInfo(const std::string& path)
{ {
repository::local::APIRepository apiRepo; repository::local::APIRepository apiRepo;
@@ -127,6 +133,19 @@ model::User retrieveCredentials(const std::string& dataPath)
} }
int retrieveRepeatMode(const std::string& path)
{
repository::local::RepeatRepository repeatRepo;
if (!repeatRepo.doesTableExist(path)) {
repeatRepo.createRepeatTable(path);
}
auto repeatMode = repeatRepo.retrieveRepeatMode(path);
return static_cast<int>(repeatMode);
}
bool doesDatabaseExist(const std::string& dataPath) bool doesDatabaseExist(const std::string& dataPath)
{ {
repository::local::UserRepository userRepo; repository::local::UserRepository userRepo;
@@ -207,6 +226,12 @@ void saveToken(const model::Token& token, const std::string& path)
tokenRepo.saveToken(token, path); tokenRepo.saveToken(token, path);
} }
void updateRepeatMode(const std::string& path)
{
repository::local::RepeatRepository repeatRepo;
repeatRepo.updateRepeat(path);
}
extern "C" extern "C"
JNIEXPORT jobjectArray JNIEXPORT jobjectArray
@@ -380,6 +405,21 @@ Java_com_example_mear_repositories_UserRepository_logUser(
} }
extern "C"
JNIEXPORT jint
JNICALL
Java_com_example_mear_repositories_RepeatRepository_retrieveRepeatMode(
JNIEnv *env,
jobject thisObj,
jstring pathStr
) {
const auto dataPath = jstringToString(env, pathStr);
auto repeatMode = retrieveRepeatMode(dataPath);
return repeatMode;
}
extern "C" extern "C"
JNIEXPORT jboolean JNIEXPORT jboolean
JNICALL JNICALL
@@ -501,3 +541,15 @@ Java_com_example_mear_repositories_TokenRepository_saveTokenRecord(
saveToken(token, path); saveToken(token, path);
} }
extern "C"
JNIEXPORT void
JNICALL
Java_com_example_mear_repositories_RepeatRepository_updateRepeatMode(
JNIEnv *env,
jobject thisObj,
jstring pathStr
) {
const auto dataPath = jstringToString(env, pathStr);
updateRepeatMode(dataPath);
}
+9 -3
View File
@@ -19,24 +19,30 @@ std::vector<model::Song> retrieveSongs(const model::Token&, const std::string&);
jobject songToObj(JNIEnv *env, const model::Song&); jobject songToObj(JNIEnv *env, const model::Song&);
std::string jstringToString(JNIEnv*, jstring&);
model::APIInfo retrieveAPIInfo(const std::string&); model::APIInfo retrieveAPIInfo(const std::string&);
model::Song retrieveSong(const std::string&, const std::string&, const int); model::Song retrieveSong(const std::string&, const std::string&, const int);
model::Song retrieveSong(const model::Token&, const model::Song&, const std::string&); model::Song retrieveSong(const model::Token&, const model::Song&, const std::string&);
model::Token fettchToken(const model::User&, const std::string&); model::Token fetchToken(const model::User&, const std::string&);
model::Token retrieveSavedToken(const std::string&); model::Token retrieveSavedToken(const std::string&);
void saveToken(const model::Token&, const std::string&);
model::User retrieveCredentials(const std::string&); model::User retrieveCredentials(const std::string&);
int retrieveRepeatMode(const std::string&);
bool doesDatabaseExist(const std::string&); bool doesDatabaseExist(const std::string&);
bool apiInformationExist(const std::string&); bool apiInformationExist(const std::string&);
bool doesTokenExist(const std::string&); bool doesTokenExist(const std::string&);
bool userCredentialExist(const std::string&); bool userCredentialExist(const std::string&);
void saveApiInfo(const model::APIInfo&, const std::string&); void saveAPIInfo(const model::APIInfo&, const std::string&);
void saveCredentials(const model::User&, const std::string&); void saveCredentials(const model::User&, const std::string&);
void saveToken(const model::Token&, const std::string&);
void updateRepeatMode(const std::string&);
#endif //MEAR_DEMO_H #endif //MEAR_DEMO_H
+4 -2
View File
@@ -21,7 +21,9 @@ namespace repository { namespace local {
const auto result = query.executeStep(); const auto result = query.executeStep();
return static_cast<RepeatTypes>(query.getColumn(1).getInt()); auto repeatType = query.getColumn(1).getInt();
auto val = static_cast<RepeatTypes>(result);
return val;
} catch (std::exception& ex) { } catch (std::exception& ex) {
auto msg = ex.what(); auto msg = ex.what();
} }
@@ -83,5 +85,5 @@ namespace repository { namespace local {
} }
std::string RepeatRepository::repeatTable() { return "Repeat"; } std::string RepeatRepository::repeatTable() noexcept { return "Repeat"; }
}} }}
+1 -1
View File
@@ -20,7 +20,7 @@ namespace repository { namespace local {
void createRepeatTable(const std::string&); void createRepeatTable(const std::string&);
void updateRepeat(const std::string&); void updateRepeat(const std::string&);
private: private:
constexpr std::string repeatTable() noexcept; std::string repeatTable() noexcept;
}; };
}} }}
+1 -1
View File
@@ -8,7 +8,7 @@
enum class RepeatTypes { enum class RepeatTypes {
RepeatSong = 0, RepeatSong = 0,
RepeatOff RepeatOff = 1
}; };
+2 -142
View File
@@ -50,6 +50,7 @@ namespace repository {
songJson["artist"].get<std::string>(), songJson["album"].get<std::string>(), songJson["artist"].get<std::string>(), songJson["album"].get<std::string>(),
songJson["genre"].get<std::string>(), songJson["duration"].get<int>(), songJson["genre"].get<std::string>(), songJson["duration"].get<int>(),
songJson["year"].get<int>()); songJson["year"].get<int>());
song.coverArtId = songJson["coverart_id"].get<int>();
songs.push_back(song); songs.push_back(song);
} }
@@ -59,148 +60,6 @@ namespace repository {
} }
[[deprecated]]
model::Song SongRepository::retrieveSong(const std::string& token, const int id) {
std::string uri("");
uri.append(std::to_string(id));
model::Song song;
CURL *curl;
CURLcode res;
struct curl_slist *chunk = NULL;
curl = curl_easy_init();
if (!curl) {
return song;
}
auto resp = std::make_unique<char[]>(10000);
std::string authInfo("Authorization: Bearer ");
authInfo.append(token);
chunk = curl_slist_append(chunk, authInfo.c_str());
curl_easy_setopt(curl, CURLOPT_URL, uri.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, respBodyRetriever);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, resp.get());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res == CURLE_OK) {
auto s = nlohmann::json::parse(resp.get());
song.id = s["id"].get<int>();
song.title = s["title"].get<std::string>();
song.album = s["album"].get<std::string>();
song.artist = s["artist"].get<std::string>();
song.genre = s["genre"].get<std::string>();
song.duration = s["duration"].get<int>();
song.year = s["year"].get<int>();
return song;
}
return song;
}
[[deprecated]]
model::Song SongRepository::retrieveSong(const std::string& token, const std::string& baseUri,
const int id) {
std::string uri(baseUri);
uri.append("/api/v1/song/");
uri.append(std::to_string(id));
model::Song song;
CURL *curl;
CURLcode res;
struct curl_slist *chunk = NULL;
curl = curl_easy_init();
if (!curl) {
return song;
}
auto resp = std::make_unique<char[]>(10000);
std::string authInfo("Authorization: Bearer ");
authInfo.append(token);
chunk = curl_slist_append(chunk, authInfo.c_str());
curl_easy_setopt(curl, CURLOPT_URL, uri.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, respBodyRetriever);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, resp.get());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res == CURLE_OK) {
auto s = nlohmann::json::parse(resp.get());
song.id = s["id"].get<int>();
song.title = s["title"].get<std::string>();
song.album = s["album"].get<std::string>();
song.artist = s["artist"].get<std::string>();
song.genre = s["genre"].get<std::string>();
song.duration = s["duration"].get<int>();
song.year = s["year"].get<int>();
return song;
}
return song;
}
[[deprecated]]
model::Song SongRepository::retrieveSong(const std::string& token, const std::string& baseUri,
const model::Song& sng) {
std::string uri(baseUri);
uri.append("/api/v1/song/");
uri.append(std::to_string(sng.id));
model::Song song;
CURL *curl;
CURLcode res;
struct curl_slist *chunk = NULL;
curl = curl_easy_init();
if (!curl) {
return sng;
}
auto resp = std::make_unique<char[]>(10000);
std::string authInfo("Authorization: Bearer ");
authInfo.append(token);
chunk = curl_slist_append(chunk, authInfo.c_str());
curl_easy_setopt(curl, CURLOPT_URL, uri.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, respBodyRetriever);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, resp.get());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res == CURLE_OK) {
auto s = nlohmann::json::parse(resp.get());
song.id = s["id"].get<int>();
song.title = s["title"].get<std::string>();
song.album = s["album"].get<std::string>();
song.artist = s["artist"].get<std::string>();
song.genre = s["genre"].get<std::string>();
song.duration = s["duration"].get<int>();
song.year = s["year"].get<int>();
return song;
}
return sng;
}
model::Song SongRepository::retrieveSong(const model::Token& token, const model::Song& sng, model::Song SongRepository::retrieveSong(const model::Token& token, const model::Song& sng,
const std::string& baseUri) { const std::string& baseUri) {
std::string uri(baseUri); std::string uri(baseUri);
@@ -241,6 +100,7 @@ namespace repository {
song.genre = s["genre"].get<std::string>(); song.genre = s["genre"].get<std::string>();
song.duration = s["duration"].get<int>(); song.duration = s["duration"].get<int>();
song.year = s["year"].get<int>(); song.year = s["year"].get<int>();
song.coverArtId = s["coverart_id"].get<int>();
return song; return song;
} }
-3
View File
@@ -17,9 +17,6 @@ namespace repository {
public: public:
std::vector<model::Song> fetchSongs(const model::Token&, const std::string&); std::vector<model::Song> fetchSongs(const model::Token&, const std::string&);
model::Song retrieveSong(const std::string&, const int);
model::Song retrieveSong(const std::string&, const std::string&, const int);
model::Song retrieveSong(const std::string&, const std::string&, const model::Song&);
model::Song retrieveSong(const model::Token&, const model::Song&, const std::string&); model::Song retrieveSong(const model::Token&, const model::Song&, const std::string&);
private: private:
static size_t respBodyRetriever(void*, size_t, size_t, char*); static size_t respBodyRetriever(void*, size_t, size_t, char*);
+1
View File
@@ -34,6 +34,7 @@ public:
std::string genre; std::string genre;
int duration; int duration;
int year; int year;
int coverArtId;
}; };
} }
@@ -164,12 +164,13 @@ class MainActivity : BaseServiceActivity() {
} }
} }
private fun initializeRepeat() { private fun initializeRepeat() {
val repeatMode = RepeatRepository(this).getRepeatMode() val repeatRepo = RepeatRepository(null)
val repeatMode = repeatRepo.repeatMode(appDirectory())
when (repeatMode) { when (repeatMode) {
ControlTypes.REPEAT_ON -> { RepeatRepository.RepeatTypes.RepeatSong -> {
RepeatTrack.text = resources.getText(R.string.repeat_on) RepeatTrack.text = resources.getText(R.string.repeat_on)
} }
ControlTypes.REPEAT_OFF -> { RepeatRepository.RepeatTypes.RepeatOff -> {
RepeatTrack.text = resources.getText(R.string.repeat_off) RepeatTrack.text = resources.getText(R.string.repeat_off)
} }
} }
@@ -197,6 +198,7 @@ class MainActivity : BaseServiceActivity() {
ShuffleRepository(this).updateShuffleMode(playC) ShuffleRepository(this).updateShuffleMode(playC)
} }
private fun toggleRepeat() { private fun toggleRepeat() {
/**
val repeatText = RepeatTrack.text.toString() val repeatText = RepeatTrack.text.toString()
val on = resources.getString(R.string.repeat_on) val on = resources.getString(R.string.repeat_on)
val off = resources.getString(R.string.repeat_off) val off = resources.getString(R.string.repeat_off)
@@ -216,6 +218,23 @@ class MainActivity : BaseServiceActivity() {
} }
val playC = PlayControls(null, repeatOn) val playC = PlayControls(null, repeatOn)
RepeatRepository(this).updateRepeatMode(playC) RepeatRepository(this).updateRepeatMode(playC)
*/
val repeatRepo = RepeatRepository(null)
val appPath = appDirectory()
repeatRepo.alterRepeatMode(appPath)
val repeatMode = repeatRepo.repeatMode(appPath)
when (repeatMode) {
RepeatRepository.RepeatTypes.RepeatOff -> {
repeatOn = false
RepeatTrack.text = resources.getText(R.string.repeat_off)
}
RepeatRepository.RepeatTypes.RepeatSong -> {
repeatOn = true
RepeatTrack.text = resources.getText(R.string.repeat_on)
}
}
} }
private fun playSongTrack() { private fun playSongTrack() {
@@ -0,0 +1,5 @@
package com.example.mear.constants
object CPPLib {
const val NATIVE_LIB = "native-lib"
}
@@ -1,6 +1,7 @@
package com.example.mear.repositories package com.example.mear.repositories
import android.net.Uri import android.net.Uri
import com.example.mear.constants.CPPLib
import com.example.mear.models.APIInfo import com.example.mear.models.APIInfo
import com.example.mear.models.Song import com.example.mear.models.Song
import com.example.mear.models.Token import com.example.mear.models.Token
@@ -9,7 +10,7 @@ class APIRepository: BaseRepository() {
companion object { companion object {
init { init {
System.loadLibrary("native-lib") System.loadLibrary(CPPLib.NATIVE_LIB)
} }
@@ -5,18 +5,24 @@ import android.content.Context
import org.jetbrains.anko.db.* import org.jetbrains.anko.db.*
import com.example.mear.constants.ControlTypes import com.example.mear.constants.ControlTypes
import com.example.mear.constants.CPPLib
import com.example.mear.database import com.example.mear.database
import com.example.mear.models.PlayControls import com.example.mear.models.PlayControls
class RepeatRepository { class RepeatRepository(var context: Context?) {
var context: Context? = null
companion object {
constructor(context: Context) { init {
this.context = context System.loadLibrary(CPPLib.NATIVE_LIB)
}
} }
private external fun retrieveRepeatMode(path: String): Int
private external fun updateRepeatMode(path: String)
fun getRepeatMode(): String = context!!.database.use { fun getRepeatMode(): String = context!!.database.use {
select("Repeat").limit(1) select("Repeat").limit(1)
.parseSingle(object: MapRowParser<String>{ .parseSingle(object: MapRowParser<String>{
@@ -27,6 +33,18 @@ class RepeatRepository {
}) })
} }
fun repeatMode(path: String): RepeatTypes {
val repeatType = RepeatTypes.valueOf(retrieveRepeatMode(path))
return repeatType!!
}
fun alterRepeatMode(path: String) {
updateRepeatMode(path)
}
fun updateRepeatMode(playControls: PlayControls?) = context!!.database.use { fun updateRepeatMode(playControls: PlayControls?) = context!!.database.use {
var repeatMode = ControlTypes.REPEAT_OFF var repeatMode = ControlTypes.REPEAT_OFF
if (playControls!!.repeatOn!!) { if (playControls!!.repeatOn!!) {
@@ -35,4 +53,13 @@ class RepeatRepository {
update("Repeat", update("Repeat",
"Mode" to repeatMode).exec() "Mode" to repeatMode).exec()
} }
enum class RepeatTypes(val value: Int) {
RepeatSong(0),
RepeatOff(1);
companion object {
fun valueOf(value: Int) = values().find { it.value == value }
}
}
} }
@@ -17,6 +17,11 @@ class ShuffleRepository {
} }
companion object {
init {
System.loadLibrary("native-lib")
}
}
fun getShuffleMode(): String = context!!.database.use { fun getShuffleMode(): String = context!!.database.use {
select("Shuffle").limit(1) select("Shuffle").limit(1)
.parseSingle(object : MapRowParser<String> { .parseSingle(object : MapRowParser<String> {