Working on deleting a song and a slight change to how songs are downloaded

This commit is contained in:
kdeng00
2020-01-09 21:13:04 -05:00
parent 65dbe0d69f
commit 8755cb1137
6 changed files with 55 additions and 89 deletions
+23 -24
View File
@@ -285,11 +285,8 @@ void downloadSong(Song& song, const Token& token, const Str& path) {
saveSong.write((char*)&downloadedSong.data[0], downloadedSong.data.size()); saveSong.write((char*)&downloadedSong.data[0], downloadedSong.data.size());
saveSong.close(); saveSong.close();
song.path = downloadedSong.path;
repository::local::SongRepository localSongRepo(path); repository::local::SongRepository localSongRepo(path);
if (!localSongRepo.doesTableExist(path)) {
localSongRepo.createSongTable(path);
}
localSongRepo.saveSong(downloadedSong, path); localSongRepo.saveSong(downloadedSong, path);
} }
@@ -524,6 +521,28 @@ Java_com_example_mear_repositories_TokenRepository_retrieveTokenRecord(
return tokenObj; return tokenObj;
} }
extern "C"
JNIEXPORT jobject
JNICALL
Java_com_example_mear_repositories_TrackRepository_downloadSong(
JNIEnv *env,
jobject thisObj,
jobject tokenObj,
jobject songObj,
jstring pathStr
) {
auto tokenClass = env->GetObjectClass(tokenObj);
auto accessTokenId = env->GetFieldID(tokenClass, "accessToken", "Ljava/lang/String;");
auto accessTokenVal = (jstring) env->GetObjectField(tokenObj, accessTokenId);
model::Token token(env->GetStringUTFChars(accessTokenVal, nullptr));
auto song = ObjToSong<jobject, JNIEnv>(env, songObj);
auto path = env->GetStringUTFChars(pathStr, nullptr);
downloadSong(song, token, path);
return songToObj(env, song);
}
extern "C" extern "C"
JNIEXPORT jobject JNIEXPORT jobject
JNICALL JNICALL
@@ -764,26 +783,6 @@ Java_com_example_mear_repositories_TokenRepository_saveTokenRecord(
saveToken(token, path); saveToken(token, path);
} }
extern "C"
JNIEXPORT void
JNICALL
Java_com_example_mear_repositories_TrackRepository_downloadSong(
JNIEnv *env,
jobject thisObj,
jobject tokenObj,
jobject songObj,
jstring pathStr
) {
auto tokenClass = env->GetObjectClass(tokenObj);
auto accessTokenId = env->GetFieldID(tokenClass, "accessToken", "Ljava/lang/String;");
auto accessTokenVal = (jstring) env->GetObjectField(tokenObj, accessTokenId);
model::Token token(env->GetStringUTFChars(accessTokenVal, nullptr));
auto song = ObjToSong<jobject, JNIEnv>(env, songObj);
auto path = env->GetStringUTFChars(pathStr, nullptr);
downloadSong(song, token, path);
}
extern "C" extern "C"
JNIEXPORT void JNIEXPORT void
JNICALL JNICALL
+3 -17
View File
@@ -93,14 +93,9 @@ namespace manager {
template<typename Str = std::string, typename B = bool> template<typename Str = std::string, typename B = bool>
B deleteSong(const Song& song, const Str& path) { B deleteSong(const Song& song, const Str& path) {
auto s = fullSongPath(song, path); const auto s = fullSongPath(song, path);
auto result = std::remove(s.c_str());
if (result == 0) { return (remove(s.c_str()) == 0) ? true : false;
return true;
} else {
return false;
}
} }
@@ -128,16 +123,7 @@ namespace manager {
albumPath.append(song.albumArtist); albumPath.append(song.albumArtist);
albumPath.append("/"); albumPath.append("/");
albumPath.append(song.album); albumPath.append(song.album);
/**
if (song.disc == 0) {
albumPath.append("/");
albumPath.append("disc1");
} else {
albumPath.append("/");
albumPath.append("disc");
albumPath.append(std::to_string(song.disc));
}
*/
DIR* dir = opendir(albumPath.c_str()); DIR* dir = opendir(albumPath.c_str());
if (dir) { if (dir) {
@@ -151,16 +151,6 @@ namespace repository {
auto res = curl_easy_perform(curl); auto res = curl_easy_perform(curl);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
downloadedSong.data = std::move(std::vector<char>(data.begin(), data.end())); downloadedSong.data = std::move(std::vector<char>(data.begin(), data.end()));
/**
downloadedSong.filename = "track";
if (song.track < 10) {
downloadedSong.filename.append("0");
downloadedSong.filename.append(std::to_string(song.track));
} else {
downloadedSong.filename.append(std::to_string(song.track));
}
downloadedSong.filename.append(".mp3");
*/
return downloadedSong; return downloadedSong;
} }
@@ -373,17 +373,22 @@ class MainActivity : BaseServiceActivity() {
R.id.action_song_delete -> { R.id.action_song_delete -> {
// TODO: handle song deletion // TODO: handle song deletion
val ss = true val ss = true
val appPath = appDirectory()
val trackRepo = TrackRepository()
// The method I am calling has not been implemented on the C++
// side
trackRepo.delete(musicService!!.getCurrentSong(), appPath)
} }
R.id.action_song_download -> { R.id.action_song_download -> {
val appPath = appDirectory() val appPath = appDirectory()
val apiRepo = APIRepository() val apiRepo = APIRepository()
val tokenRepo = TokenRepository() val tokenRepo = TokenRepository()
val trackRepo = TrackRepository() val trackRepo = TrackRepository()
val song = musicService!!.getCurrentSong() var song = musicService!!.getCurrentSong()
val token = tokenRepo.retrieveToken(appPath) val token = tokenRepo.retrieveToken(appPath)
val apiInfo = apiRepo.retrieveRecord(appPath) val apiInfo = apiRepo.retrieveRecord(appPath)
trackRepo.download(token, song, appPath) song = trackRepo.download(token, song, appPath)
musicService!!.changeSongDownloadStatus() musicService!!.changeSongDownloadStatus()
// TODO: implement something to include the downloaded song into the songQueue // TODO: implement something to include the downloaded song into the songQueue
// essentially replacing the song that already exists, that way one can // essentially replacing the song that already exists, that way one can
@@ -97,7 +97,6 @@ class MusicService(var appPath: String = ""): Service() {
s.downloaded = true s.downloaded = true
} }
} }
// songQueue[currentSongIndex!!].downloaded = true
} }
fun goToPosition(progress: Int) { trackPlayer!!.seekTo(progress) } fun goToPosition(progress: Int) { trackPlayer!!.seekTo(progress) }
@@ -142,11 +141,8 @@ class MusicService(var appPath: String = ""): Service() {
if (retrieveShuffleMode()!! && !parseRepeatMode(repeatMode)) { if (retrieveShuffleMode()!! && !parseRepeatMode(repeatMode)) {
currentSongIndex = Random.nextInt(0, songQueue.size - 1) currentSongIndex = Random.nextInt(0, songQueue.size - 1)
} else if (!parseRepeatMode(repeatMode)) { } else if (!parseRepeatMode(repeatMode)) {
if (currentSongIndex == 0) { currentSongIndex = if (currentSongIndex == 0)
currentSongIndex = songQueue.size - 1 songQueue.size - 1 else currentSongIndex!! - 1
} else {
currentSongIndex = currentSongIndex!! - 1
}
} }
currentSong = songQueue[currentSongIndex!!] currentSong = songQueue[currentSongIndex!!]
@@ -184,12 +180,8 @@ class MusicService(var appPath: String = ""): Service() {
if (retrieveShuffleMode()!! && !parseRepeatMode(repeatMode)) { if (retrieveShuffleMode()!! && !parseRepeatMode(repeatMode)) {
currentSongIndex = Random.nextInt(0, songQueue.size - 1) currentSongIndex = Random.nextInt(0, songQueue.size - 1)
} else if (!parseRepeatMode(repeatMode)) { } else if (!parseRepeatMode(repeatMode)) {
if ((currentSongIndex!! + 1) == songQueue.size) { currentSongIndex = if ((currentSongIndex!! + 1) == songQueue.size)
currentSongIndex = 0 0 else currentSongIndex!! + 1
}
else {
currentSongIndex = currentSongIndex!! + 1
}
} }
currentSong = songQueue[currentSongIndex!!] currentSong = songQueue[currentSongIndex!!]
@@ -236,26 +228,22 @@ class MusicService(var appPath: String = ""): Service() {
val token = tokenRepo.retrieveToken(appPath) val token = tokenRepo.retrieveToken(appPath)
val apiInfo = apiRepo.retrieveRecord(appPath) val apiInfo = apiRepo.retrieveRecord(appPath)
// val songs = trackRepo.fetchSongs(token, apiInfo.uri)
val songs = trackRepo.fetchSongsIncludingDownloaded(token, apiInfo.uri, appPath) val songs = trackRepo.fetchSongsIncludingDownloaded(token, apiInfo.uri, appPath)
songQueue = songs.toMutableList() songQueue = songs.toMutableList()
if (retrieveShuffleMode()!!) { currentSongIndex = if (retrieveShuffleMode()!!)
currentSongIndex = Random.nextInt(0, songs.size - 1) Random.nextInt(0, songQueue.size - 1) else 0
} else {
currentSongIndex = 0
}
currentSong = songQueue[currentSongIndex!!] currentSong = songQueue[currentSongIndex!!]
if (currentSong.downloaded) { if (currentSong.downloaded) {
trackPlayer!!.setDataSource(currentSong.path) trackPlayer!!.setDataSource(currentSong.path)
} }
else { else {
trackPlayer!!.setDataSource(this, trackPlayer!!.setDataSource(this,
APIRepository.retrieveSongStreamUri(apiInfo, currentSong), APIRepository.retrieveSongStreamUri(apiInfo, currentSong),
APIRepository.retrieveSongStreamHeader(token)) APIRepository.retrieveSongStreamHeader(token))
} }
trackPlayer!!.prepareAsync() trackPlayer!!.prepareAsync()
trackPlayer!!.setOnCompletionListener { trackPlayer!!.setOnCompletionListener {
playNextTrack() playNextTrack()
@@ -269,25 +257,19 @@ class MusicService(var appPath: String = ""): Service() {
private fun retrieveShuffleMode(): Boolean? { private fun retrieveShuffleMode(): Boolean? {
val shuffleRepo = ShuffleRepository(null) val shuffleRepo = ShuffleRepository(null)
val shuffleMode = shuffleRepo.shuffleMode(appPath)
var shuffleOn = false
when (shuffleMode) { return when (shuffleRepo.shuffleMode(appPath)) {
ShuffleTypes.ShuffleOn -> shuffleOn = true ShuffleTypes.ShuffleOn -> true
ShuffleTypes.ShuffleOff -> shuffleOn = false ShuffleTypes.ShuffleOff -> false
else -> false
} }
return shuffleOn
} }
private fun parseRepeatMode(repeatMode: RepeatTypes): Boolean { private fun parseRepeatMode(repeatMode: RepeatTypes): Boolean {
var repeatOn: Boolean? = null return when (repeatMode) {
when (repeatMode) { RepeatTypes.RepeatOff -> false
RepeatTypes.RepeatOff -> repeatOn = false RepeatTypes.RepeatSong -> true
RepeatTypes.RepeatSong -> repeatOn = true
} }
return repeatOn!!
} }
private fun offlinePlaySong(song: Song) { private fun offlinePlaySong(song: Song) {
@@ -22,8 +22,9 @@ class TrackRepository(var context: Context? = null) {
private external fun retrieveSongsIncludingDownloaded(token: Token, uri: String, path: String): Array<Song> private external fun retrieveSongsIncludingDownloaded(token: Token, uri: String, path: String): Array<Song>
private external fun retrieveSong(token: Token, song: Song, uri: String): Song private external fun retrieveSong(token: Token, song: Song, uri: String): Song
private external fun downloadSong(token: Token, song: Song, path: String): Song
private external fun downloadSong(token: Token, song: Song, path: String) private external fun deleteSong(song: Song, path: String)
fun fetchSongs(token: Token, uri: String): Array<Song> { fun fetchSongs(token: Token, uri: String): Array<Song> {
@@ -43,8 +44,11 @@ class TrackRepository(var context: Context? = null) {
downloadSong(token, song, uri) downloadSong(token, song, uri)
} }
fun download(token: Token, song: Song, path: String) { fun download(token: Token, song: Song, path: String): Song {
downloadSong(token, song, path) return downloadSong(token, song, path)
val s = 4 }
fun delete(song: Song, path: String) {
deleteSong(song, path)
} }
} }