implement the playlist creation from folder

This commit is contained in:
tibbi 2017-12-02 20:49:33 +01:00
parent 33058569b4
commit de43b0d4fe
18 changed files with 62 additions and 11 deletions

View file

@ -42,7 +42,7 @@ ext {
}
dependencies {
implementation 'com.simplemobiletools:commons:3.0.22'
implementation 'com.simplemobiletools:commons:3.0.23'
implementation 'com.squareup:otto:1.3.8'
implementation 'com.facebook.stetho:stetho:1.5.0'

View file

@ -29,6 +29,7 @@ import com.simplemobiletools.musicplayer.extensions.sendIntent
import com.simplemobiletools.musicplayer.helpers.*
import com.simplemobiletools.musicplayer.inlines.indexOfFirstOrNull
import com.simplemobiletools.musicplayer.models.Events
import com.simplemobiletools.musicplayer.models.Playlist
import com.simplemobiletools.musicplayer.models.Song
import com.simplemobiletools.musicplayer.services.MusicService
import com.squareup.otto.Bus
@ -126,6 +127,7 @@ class MainActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListener {
R.id.toggle_autoplay -> toggleAutoplay()
R.id.add_folder_to_playlist -> addFolderToPlaylist()
R.id.add_file_to_playlist -> addFileToPlaylist()
R.id.create_playlist_from_folder -> createPlaylistFromFolder()
R.id.remove_playlist -> removePlaylist()
R.id.settings -> launchSettings()
R.id.about -> launchAbout()
@ -208,8 +210,7 @@ class MainActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListener {
}
private fun addFolderToPlaylist() {
val initialPath = if (songs.isEmpty()) Environment.getExternalStorageDirectory().toString() else songs[0].path
FilePickerDialog(this, initialPath, pickFile = false) {
FilePickerDialog(this, getFilePickerInitialPath(), pickFile = false) {
toast(R.string.fetching_songs)
Thread {
val songs = getFolderSongs(File(it))
@ -233,8 +234,7 @@ class MainActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListener {
}
private fun addFileToPlaylist() {
val initialPath = if (songs.isEmpty()) Environment.getExternalStorageDirectory().toString() else songs[0].path
FilePickerDialog(this, initialPath) {
FilePickerDialog(this, getFilePickerInitialPath()) {
if (it.isAudioFast()) {
dbHelper.addSongToPlaylist(it)
sendIntent(REFRESH_LIST)
@ -244,6 +244,44 @@ class MainActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListener {
}
}
private fun createPlaylistFromFolder() {
FilePickerDialog(this, getFilePickerInitialPath(), pickFile = false) {
Thread {
createPlaylistFrom(it)
}.start()
}
}
private fun createPlaylistFrom(path: String) {
val songs = getFolderSongs(File(path))
if (songs.isEmpty()) {
toast(R.string.folder_contains_no_audio)
return
}
val folderName = path.getFilenameFromPath()
var playlistName = folderName
var curIndex = 1
val playlistIdWithTitle = dbHelper.getPlaylistIdWithTitle(folderName)
if (playlistIdWithTitle != -1) {
while (true) {
playlistName = "${folderName}_$curIndex"
if (dbHelper.getPlaylistIdWithTitle(playlistName) == -1) {
break
}
curIndex++
}
}
val playlist = Playlist(0, playlistName)
val newPlaylistId = dbHelper.insertPlaylist(playlist)
dbHelper.addSongsToPlaylist(songs, newPlaylistId)
playlistChanged(newPlaylistId)
}
private fun getFilePickerInitialPath() = if (songs.isEmpty()) Environment.getExternalStorageDirectory().toString() else songs[0].path
private fun initializePlayer() {
if (isThirdPartyIntent) {
Intent(this, MusicService::class.java).apply {

View file

@ -62,8 +62,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
fun insertPlaylist(playlist: Playlist, db: SQLiteDatabase = mDb): Int {
val values = ContentValues().apply { put(COL_TITLE, playlist.title) }
val insertedId = db.insert(TABLE_NAME_PLAYLISTS, null, values).toInt()
return insertedId
return db.insert(TABLE_NAME_PLAYLISTS, null, values).toInt()
}
fun removePlaylist(id: Int) {
@ -94,8 +93,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
addSongsToPlaylist(ArrayList<String>().apply { add(path) })
}
fun addSongsToPlaylist(paths: ArrayList<String>) {
val playlistId = context.config.currentPlaylist
fun addSongsToPlaylist(paths: ArrayList<String>, playlistId: Int = context.config.currentPlaylist) {
for (path in paths) {
ContentValues().apply {
put(COL_PATH, path)

View file

@ -32,8 +32,6 @@
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="@dimen/medium_margin"
android:layout_marginRight="@dimen/medium_margin"
android:clickable="false"
android:src="@drawable/ic_music_note"
android:visibility="invisible"/>

View file

@ -34,6 +34,10 @@
android:id="@+id/add_file_to_playlist"
android:title="@string/add_file_to_playlist"
app:showAsAction="never"/>
<item
android:id="@+id/create_playlist_from_folder"
android:title="@string/create_playlist_from_folder"
app:showAsAction="never"/>
<item
android:id="@+id/remove_playlist"
android:icon="@drawable/ic_delete"

View file

@ -43,6 +43,7 @@
<string name="fetching_songs">Rufe Lieder ab…</string>
<string name="all_songs">Alle Lieder</string>
<string name="create_playlist_from_folder">Create new playlist from folder</string>
<string name="folder_contains_no_audio">The selected folder contains no audio files</string>
<!-- Settings -->
<string name="equalizer">Equalizer</string>

View file

@ -43,6 +43,7 @@
<string name="fetching_songs">Fetching songs…</string>
<string name="all_songs">All songs</string>
<string name="create_playlist_from_folder">Create new playlist from folder</string>
<string name="folder_contains_no_audio">The selected folder contains no audio files</string>
<!-- Settings -->
<string name="equalizer">Ecualizador</string>

View file

@ -43,6 +43,7 @@
<string name="fetching_songs">Számok lekérése…</string>
<string name="all_songs">Összes szám</string>
<string name="create_playlist_from_folder">Create new playlist from folder</string>
<string name="folder_contains_no_audio">The selected folder contains no audio files</string>
<!-- Settings -->
<string name="equalizer">Hangszínszabályzó</string>

View file

@ -43,6 +43,7 @@
<string name="fetching_songs">Fetching songs…</string>
<string name="all_songs">All songs</string>
<string name="create_playlist_from_folder">Create new playlist from folder</string>
<string name="folder_contains_no_audio">The selected folder contains no audio files</string>
<!-- Settings -->
<string name="equalizer">Equalizzatore</string>

View file

@ -43,6 +43,7 @@
<string name="fetching_songs">Fetching songs…</string>
<string name="all_songs">All songs</string>
<string name="create_playlist_from_folder">Create new playlist from folder</string>
<string name="folder_contains_no_audio">The selected folder contains no audio files</string>
<!-- Settings -->
<string name="equalizer">イコライザー</string>

View file

@ -43,6 +43,7 @@
<string name="fetching_songs">Nummers laden…</string>
<string name="all_songs">Alle nummers</string>
<string name="create_playlist_from_folder">Create new playlist from folder</string>
<string name="folder_contains_no_audio">The selected folder contains no audio files</string>
<!-- Settings -->
<string name="equalizer">Equalizer</string>

View file

@ -43,6 +43,7 @@
<string name="fetching_songs">Otrzymywanie utworów…</string>
<string name="all_songs">Wszystkie utwory</string>
<string name="create_playlist_from_folder">Create new playlist from folder</string>
<string name="folder_contains_no_audio">The selected folder contains no audio files</string>
<!-- Settings -->
<string name="equalizer">Korektor</string>

View file

@ -43,6 +43,7 @@
<string name="fetching_songs">A obter músicas…</string>
<string name="all_songs">Todas as faixas</string>
<string name="create_playlist_from_folder">Create new playlist from folder</string>
<string name="folder_contains_no_audio">The selected folder contains no audio files</string>
<!-- Settings -->
<string name="equalizer">Equalizador</string>

View file

@ -43,6 +43,7 @@
<string name="fetching_songs">Загрузка композиций…</string>
<string name="all_songs">Все композиции</string>
<string name="create_playlist_from_folder">Create new playlist from folder</string>
<string name="folder_contains_no_audio">The selected folder contains no audio files</string>
<!-- Settings -->
<string name="equalizer">Эквалайзер</string>

View file

@ -43,6 +43,7 @@
<string name="fetching_songs">Pridávajú sa skladby…</string>
<string name="all_songs">Všetky skladby</string>
<string name="create_playlist_from_folder">Vytvoriť nový zoznam skladieb z priečinka</string>
<string name="folder_contains_no_audio">Zvolený priečinok neobsahuje žiadne audio súbory</string>
<!-- Settings -->
<string name="equalizer">Ekvalizér</string>

View file

@ -43,6 +43,7 @@
<string name="fetching_songs">Fetching songs…</string>
<string name="all_songs">All songs</string>
<string name="create_playlist_from_folder">Create new playlist from folder</string>
<string name="folder_contains_no_audio">The selected folder contains no audio files</string>
<!-- Settings -->
<string name="equalizer">Equalizer</string>

View file

@ -43,6 +43,7 @@
<string name="fetching_songs">Fetching songs…</string>
<string name="all_songs">All songs</string>
<string name="create_playlist_from_folder">Create new playlist from folder</string>
<string name="folder_contains_no_audio">The selected folder contains no audio files</string>
<!-- Settings -->
<string name="equalizer">Equalizer</string>

View file

@ -1,4 +1,5 @@
<resources>
<style name="AppTheme" parent="AppTheme.Base"/>
</resources>