Downloading songs needs some work

This commit is contained in:
kdeng00
2020-01-07 22:27:51 -05:00
parent 1fa7df4aa2
commit 65dbe0d69f
12 changed files with 74 additions and 39 deletions
+4 -4
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -48,7 +48,7 @@ android {
dependencies { dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar']) implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.50" implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61'
implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:support-v4:28.0.0' implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:design:28.0.0' implementation 'com.android.support:design:28.0.0'
+7 -11
View File
@@ -15,7 +15,7 @@
<activity android:name=".activities.IcarusSongActivity"></activity> <activity android:name=".activities.IcarusSongActivity"></activity>
<activity <activity
android:name=".activities.LoginActivity" android:name=".activities.LoginActivity"
android:label="@string/title_activity_demo_stream" android:label="@string/title_activity_main"
android:theme="@style/AppTheme.NoActionBar"> android:theme="@style/AppTheme.NoActionBar">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
@@ -23,15 +23,6 @@
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity
android:name=".activities.SongViewActivity"
android:label="@string/title_activity_song_view"
android:parentActivityName=".activities.MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.mear.activities.MainActivity" />
</activity>
<activity <activity
android:name=".activities.SettingsActivity" android:name=".activities.SettingsActivity"
android:label="@string/title_activity_settings" android:label="@string/title_activity_settings"
@@ -45,13 +36,18 @@
<activity <activity
android:name=".activities.MainActivity" android:name=".activities.MainActivity"
android:label="@string/title_activity_main" android:label="@string/title_activity_main"
android:parentActivityName=".activities.LoginActivity"
android:screenOrientation="portrait" android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar"> android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.mear.activities.LoginActivity" />
<!--
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
-->
</activity> </activity>
<service android:name=".playback.service.MusicService" /> <service android:name=".playback.service.MusicService" />
+4 -7
View File
@@ -271,24 +271,21 @@ void downloadSong(Song& song, const Token& token, const Str& path) {
repository::local::APIRepository apiRepo; repository::local::APIRepository apiRepo;
auto apiInfo = apiRepo.retrieveAPIInfo(path); auto apiInfo = apiRepo.retrieveAPIInfo(path);
repository::SongRepository songRepo; repository::SongRepository songRepo;
auto downloadedSong = songRepo.downloadSong(token, song, apiInfo);
manager::DirectoryManager dirMgr; manager::DirectoryManager dirMgr;
if (dirMgr.doesSongExist(song, path)) { if (dirMgr.doesSongExist(song, path)) {
std::cout << "song already exists\n"; std::cout << "song already exists\n";
return; return;
} }
auto downloadedSong = songRepo.downloadSong(token, song, apiInfo);
dirMgr.createSongDirectory(song, path); dirMgr.createSongDirectory(song, path);
downloadedSong.path = dirMgr.fullSongPath(song, path); downloadedSong.path = dirMgr.fullSongPath(song, path);
std::fstream saveSong(downloadedSong.path, std::ios::out | std::ios::binary); std::fstream saveSong(downloadedSong.path, std::ios::out | std::ios::binary);
saveSong.write((char*)&downloadedSong.data[0], downloadedSong.data.size()); saveSong.write((char*)&downloadedSong.data[0], downloadedSong.data.size());
saveSong.close(); saveSong.close();
repository::local::SongRepository localSongRepo; repository::local::SongRepository localSongRepo(path);
if (!localSongRepo.databaseExist(path)) {
localSongRepo.initializedDatabase(path);
}
if (!localSongRepo.doesTableExist(path)) { if (!localSongRepo.doesTableExist(path)) {
localSongRepo.createSongTable(path); localSongRepo.createSongTable(path);
} }
@@ -405,7 +402,7 @@ Java_com_example_mear_repositories_TrackRepository_retrieveSongsIncludingDownloa
auto i = 0; auto i = 0;
repository::local::SongRepository localSongRepo(appPath); repository::local::SongRepository localSongRepo(appPath);
auto localSongs = (localSongRepo.isTableEmpty(appPath)) ? auto localSongs = (!localSongRepo.isTableEmpty(appPath)) ?
localSongRepo.retrieveAllSongs(appPath) : std::vector<model::Song>(); localSongRepo.retrieveAllSongs(appPath) : std::vector<model::Song>();
for (auto& sng: allSongs) { for (auto& sng: allSongs) {
+3 -2
View File
@@ -52,11 +52,12 @@ namespace repository { namespace local {
queryStr.append(m_tableName); queryStr.append(m_tableName);
SQLite::Statement query(db, queryStr); SQLite::Statement query(db, queryStr);
auto r = query.exec();
auto r = query.executeStep();
const auto result = query.hasRow(); const auto result = query.hasRow();
return result; return !result;
} catch (std::exception& ex) { } catch (std::exception& ex) {
auto msg = ex.what(); auto msg = ex.what();
} }
+1 -2
View File
@@ -298,7 +298,6 @@ namespace repository {
std::string queryString("DELETE FROM "); std::string queryString("DELETE FROM ");
queryString.append(m_tableName); queryString.append(m_tableName);
queryString.append(" WHERE Id = ?"); queryString.append(" WHERE Id = ?");
// queryString.append("Duration, Track, Disc, Filename, Path, CoverArtId)");
SQLite::Statement query(db, queryString); SQLite::Statement query(db, queryString);
query.bind(1, song.id); query.bind(1, song.id);
@@ -334,7 +333,7 @@ namespace repository {
query.bind(12, song.path); query.bind(12, song.path);
query.bind(13, song.coverArtId); query.bind(13, song.coverArtId);
query.exec(); auto result = query.exec();
} catch (std::exception& ex) { } catch (std::exception& ex) {
auto msg = ex.what(); auto msg = ex.what();
} }
+2 -5
View File
@@ -34,11 +34,8 @@ namespace repository { namespace local {
auto result = query.executeStep(); auto result = query.executeStep();
auto valZero = query.getColumn(1); user.username = std::move(query.getColumn(1).getString());
auto valOne = query.getColumn(2); user.password = std::move(query.getColumn(2).getString());
user.username = valZero.getString();
user.password = valOne.getString();
return user; return user;
@@ -27,7 +27,6 @@ import android.widget.Toast
import org.jetbrains.anko.imageBitmap import org.jetbrains.anko.imageBitmap
import com.example.mear.listeners.TrackElaspingChange import com.example.mear.listeners.TrackElaspingChange
import com.example.mear.models.Song
import com.example.mear.R import com.example.mear.R
import com.example.mear.repositories.* import com.example.mear.repositories.*
import com.example.mear.repositories.RepeatRepository.RepeatTypes import com.example.mear.repositories.RepeatRepository.RepeatTypes
@@ -55,8 +54,6 @@ class MainActivity : BaseServiceActivity() {
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
setSupportActionBar(toolbar) setSupportActionBar(toolbar)
//permissionPrompt()
initialize() initialize()
} }
catch (ex: Exception) { catch (ex: Exception) {
@@ -358,7 +355,12 @@ class MainActivity : BaseServiceActivity() {
private fun showPopup(view: View) { private fun showPopup(view: View) {
try { try {
var popup = PopupMenu(this, view) var popup = PopupMenu(this, view)
popup.inflate(R.menu.popup_menu) if (musicService!!.getCurrentSong().downloaded) {
popup.inflate(R.menu.popup_menu_song_downloaded)
}
else {
popup.inflate(R.menu.popup_menu)
}
popup.setOnMenuItemClickListener{ item: MenuItem? -> popup.setOnMenuItemClickListener{ item: MenuItem? ->
when (item!!.itemId) { when (item!!.itemId) {
@@ -368,8 +370,11 @@ class MainActivity : BaseServiceActivity() {
R.id.action_song_view -> { R.id.action_song_view -> {
startActivity(Intent(this, IcarusSongActivity::class.java)) startActivity(Intent(this, IcarusSongActivity::class.java))
} }
R.id.action_song_delete -> {
// TODO: handle song deletion
val ss = true
}
R.id.action_song_download -> { R.id.action_song_download -> {
// TODO: implement download functionality
val appPath = appDirectory() val appPath = appDirectory()
val apiRepo = APIRepository() val apiRepo = APIRepository()
val tokenRepo = TokenRepository() val tokenRepo = TokenRepository()
@@ -379,6 +384,10 @@ class MainActivity : BaseServiceActivity() {
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) trackRepo.download(token, song, appPath)
musicService!!.changeSongDownloadStatus()
// TODO: implement something to include the downloaded song into the songQueue
// essentially replacing the song that already exists, that way one can
// actually access the file without having to restart the app
} }
R.id.action_song_play_count-> { R.id.action_song_play_count-> {
val trk = musicService!!.getCurrentSong() val trk = musicService!!.getCurrentSong()
@@ -86,7 +86,6 @@ class SongAdapter(val mOnClickListener: (Song) -> Unit,
v.setOnClickListener { clickList(songItem!!)} v.setOnClickListener { clickList(songItem!!)}
if (songItems.downloaded) { if (songItems.downloaded) {
var rs = v.resources
v.setBackgroundColor(v.resources.getColor(R.color.track_seek)) v.setBackgroundColor(v.resources.getColor(R.color.track_seek))
} }
} }
@@ -89,6 +89,17 @@ class MusicService(var appPath: String = ""): Service() {
} }
} }
fun changeSongDownloadStatus() {
currentSong.downloaded = true
songQueue.forEach {
s ->
if (s.id == currentSong.id) {
s.downloaded = true
}
}
// songQueue[currentSongIndex!!].downloaded = true
}
fun goToPosition(progress: Int) { trackPlayer!!.seekTo(progress) } fun goToPosition(progress: Int) { trackPlayer!!.seekTo(progress) }
fun playSongTrack() { fun playSongTrack() {
@@ -225,7 +236,8 @@ 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.fetchSongs(token, apiInfo.uri)
val songs = trackRepo.fetchSongsIncludingDownloaded(token, apiInfo.uri, appPath)
songQueue = songs.toMutableList() songQueue = songs.toMutableList()
if (retrieveShuffleMode()!!) { if (retrieveShuffleMode()!!) {
@@ -280,6 +292,14 @@ class MusicService(var appPath: String = ""): Service() {
private fun offlinePlaySong(song: Song) { private fun offlinePlaySong(song: Song) {
try { try {
currentSong = song
songQueue.forEach{ s ->
if (s.id == currentSong.id) {
s.downloaded = currentSong.downloaded
s.filename = currentSong.filename
s.path = currentSong.path
}
}
trackPlayer!!.reset() trackPlayer!!.reset()
trackPlayer!!.setDataSource(song.path) trackPlayer!!.setDataSource(song.path)
trackPlayer!!.prepare() trackPlayer!!.prepare()
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_settings"
android:title="@string/settings"
app:showAsAction="never"/>
<item android:id="@+id/action_song_view"
android:title="@string/song_view"
app:showAsAction="never" />
<item android:id="@+id/action_song_delete"
android:title="@string/song_delete"
app:showAsAction="never" />
<item android:id="@+id/action_song_play_count"
android:title="@string/song_play_count"
app:showAsAction="never" />
</menu>
+1
View File
@@ -38,6 +38,7 @@
<string name="song_view">Songs</string> <string name="song_view">Songs</string>
<string name="song_play_count">Play Count</string> <string name="song_play_count">Play Count</string>
<string name="song_download">Download</string> <string name="song_download">Download</string>
<string name="song_delete">Delete</string>
<!-- Popups --> <!-- Popups -->
<string name="popup_about_header">About</string> <string name="popup_about_header">About</string>