From 8068f663062a99921a871b7ea21b4d18fe371dbc Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 2 Dec 2016 23:12:05 +0100 Subject: [PATCH] implement editing songs + cleanup --- .../musicplayer/activities/MainActivity.kt | 36 +------------------ .../activities/SettingsActivity.kt | 12 ++----- .../activities/WidgetConfigureActivity.kt | 6 ++-- .../musicplayer/adapters/SongAdapter.kt | 25 ++++++++----- .../musicplayer/dialogs/EditDialog.kt | 8 ++--- .../musicplayer/extensions/Context.kt | 4 +-- .../musicplayer/helpers/Constants.kt | 6 ++-- .../musicplayer/services/MusicService.kt | 5 +-- 8 files changed, 31 insertions(+), 71 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/MainActivity.kt index 8e4efc71..629adf58 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/MainActivity.kt @@ -5,8 +5,6 @@ import android.content.Intent import android.content.pm.PackageManager import android.graphics.Bitmap import android.media.AudioManager -import android.media.MediaScannerConnection -import android.net.Uri import android.os.Bundle import android.support.v4.app.ActivityCompat import android.support.v4.content.ContextCompat @@ -30,7 +28,7 @@ import com.squareup.otto.Subscribe import kotlinx.android.synthetic.main.activity_main.* import java.util.* -class MainActivity : SimpleActivity(), MediaScannerConnection.OnScanCompletedListener, SeekBar.OnSeekBarChangeListener { +class MainActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListener { companion object { private val STORAGE_PERMISSION = 1 @@ -181,37 +179,6 @@ class MainActivity : SimpleActivity(), MediaScannerConnection.OnScanCompletedLis fillSongsListView(event.songs) } - /* - override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { - return when (item.itemId) { - R.id.cab_edit -> { - displayEditDialog() - mode.finish() - true - } - R.id.cab_delete -> { - prepareForDeleting() - mode.finish() - true - } - else -> false - } - } - - private fun displayEditDialog() { - val songIndex = selectedSongIndex - if (songIndex == -1) - return - - val selectedSong = mSongs[songIndex] - - EditDialog(this, selectedSong) - }*/ - - override fun onScanCompleted(path: String, uri: Uri) { - sendIntent(REFRESH_LIST) - } - @Subscribe fun songChangedEvent(event: Events.ProgressUpdated) { progressbar.progress = event.progress @@ -228,7 +195,6 @@ class MainActivity : SimpleActivity(), MediaScannerConnection.OnScanCompletedLis } override fun onStartTrackingTouch(seekBar: SeekBar) { - } override fun onStopTrackingTouch(seekBar: SeekBar) { diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/SettingsActivity.kt index c5a557a0..a67b2217 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/SettingsActivity.kt @@ -7,10 +7,10 @@ import android.view.View import android.widget.AdapterView import android.widget.ArrayAdapter import com.simplemobiletools.musicplayer.R +import com.simplemobiletools.musicplayer.extensions.sendIntent import com.simplemobiletools.musicplayer.helpers.EQUALIZER import com.simplemobiletools.musicplayer.helpers.REFRESH_LIST import com.simplemobiletools.musicplayer.helpers.SET_EQUALIZER -import com.simplemobiletools.musicplayer.helpers.UPDATE_ACTIVITY import com.simplemobiletools.musicplayer.services.MusicService import kotlinx.android.synthetic.main.activity_settings.* @@ -56,7 +56,7 @@ class SettingsActivity : SimpleActivity() { settings_sorting.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { mConfig.sorting = settings_sorting.selectedItemPosition - updatePlaylist() + sendIntent(REFRESH_LIST) } override fun onNothingSelected(parent: AdapterView<*>?) { @@ -97,12 +97,4 @@ class SettingsActivity : SimpleActivity() { private fun restartActivity() { TaskStackBuilder.create(applicationContext).addNextIntentWithParentStack(intent).startActivities() } - - private fun updatePlaylist() { - Intent(this, MusicService::class.java).apply { - putExtra(UPDATE_ACTIVITY, true) - action = REFRESH_LIST - startService(this) - } - } } diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/WidgetConfigureActivity.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/WidgetConfigureActivity.kt index f3014f10..8c9b320d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/WidgetConfigureActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/WidgetConfigureActivity.kt @@ -12,7 +12,7 @@ import android.widget.RemoteViews import android.widget.SeekBar import com.simplemobiletools.musicplayer.R import com.simplemobiletools.musicplayer.helpers.MyWidgetProvider -import com.simplemobiletools.musicplayer.helpers.PREFS +import com.simplemobiletools.musicplayer.helpers.PREFS_KEY import com.simplemobiletools.musicplayer.helpers.WIDGET_BG_COLOR import com.simplemobiletools.musicplayer.helpers.WIDGET_TEXT_COLOR import kotlinx.android.synthetic.main.widget.* @@ -49,7 +49,7 @@ class WidgetConfigureActivity : AppCompatActivity() { } private fun initVariables() { - val prefs = getSharedPreferences(PREFS, Context.MODE_PRIVATE) + val prefs = getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE) mBgColor = prefs.getInt(WIDGET_BG_COLOR, 1) if (mBgColor == 1) { mBgColor = Color.BLACK @@ -112,7 +112,7 @@ class WidgetConfigureActivity : AppCompatActivity() { } private fun storeWidgetColors() { - getSharedPreferences(PREFS, Context.MODE_PRIVATE).apply { + getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE).apply { edit().putInt(WIDGET_BG_COLOR, mBgColor).putInt(WIDGET_TEXT_COLOR, mTextColor).apply() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/adapters/SongAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/adapters/SongAdapter.kt index 7a76ccbf..84566c0f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/adapters/SongAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/adapters/SongAdapter.kt @@ -1,6 +1,5 @@ package com.simplemobiletools.musicplayer.adapters -import android.content.Intent import android.support.v7.view.ActionMode import android.support.v7.widget.RecyclerView import android.view.* @@ -12,10 +11,10 @@ import com.simplemobiletools.filepicker.extensions.scanPaths import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog import com.simplemobiletools.musicplayer.R import com.simplemobiletools.musicplayer.activities.SimpleActivity +import com.simplemobiletools.musicplayer.dialogs.EditDialog +import com.simplemobiletools.musicplayer.extensions.sendIntent import com.simplemobiletools.musicplayer.helpers.REFRESH_LIST -import com.simplemobiletools.musicplayer.helpers.UPDATE_ACTIVITY import com.simplemobiletools.musicplayer.models.Song -import com.simplemobiletools.musicplayer.services.MusicService import kotlinx.android.synthetic.main.song.view.* import java.io.File import java.util.* @@ -47,6 +46,10 @@ class SongAdapter(val activity: SimpleActivity, val songs: ArrayList, val showProperties() true } + R.id.cab_edit -> { + displayEditDialog() + true + } R.id.cab_delete -> { askConfirmDelete() true @@ -86,6 +89,16 @@ class SongAdapter(val activity: SimpleActivity, val songs: ArrayList, val } } + private fun displayEditDialog() { + val selections = multiSelector.selectedPositions + if (selections.size == 1) { + EditDialog(activity, songs[selections[0]]) { + activity.sendIntent(REFRESH_LIST) + activity.runOnUiThread { actMode?.finish() } + } + } + } + private fun askConfirmDelete() { ConfirmationDialog(activity) { actMode?.finish() @@ -102,11 +115,7 @@ class SongAdapter(val activity: SimpleActivity, val songs: ArrayList, val } activity.scanPaths(paths) { - Intent(activity, MusicService::class.java).apply { - putExtra(UPDATE_ACTIVITY, true) - action = REFRESH_LIST - activity.startService(this) - } + activity.sendIntent(REFRESH_LIST) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/dialogs/EditDialog.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/dialogs/EditDialog.kt index 73ccda2b..8d319a3f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/dialogs/EditDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/dialogs/EditDialog.kt @@ -10,13 +10,11 @@ import android.view.WindowManager import com.simplemobiletools.filepicker.extensions.* import com.simplemobiletools.musicplayer.R import com.simplemobiletools.musicplayer.activities.SimpleActivity -import com.simplemobiletools.musicplayer.extensions.sendIntent -import com.simplemobiletools.musicplayer.helpers.REFRESH_LIST import com.simplemobiletools.musicplayer.models.Song import kotlinx.android.synthetic.main.rename_song.view.* import java.io.File -class EditDialog(val activity: SimpleActivity, val song: Song) { +class EditDialog(val activity: SimpleActivity, val song: Song, val callback: () -> Unit) { init { val view = LayoutInflater.from(activity).inflate(R.layout.rename_song, null) view.title.setText(song.title) @@ -52,15 +50,15 @@ class EditDialog(val activity: SimpleActivity, val song: Song) { val file = File(song.path) val newFile = File(file.parent, "$newFilename.$newFileExtension") if (file == newFile) { + callback.invoke() dismiss() return@setOnClickListener } if (file.renameTo(newFile)) { context.scanFiles(arrayListOf(file, newFile)) { - context.sendIntent(REFRESH_LIST) + callback.invoke() } - dismiss() return@setOnClickListener } diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Context.kt index 1be681bb..fc12d777 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Context.kt @@ -2,7 +2,7 @@ package com.simplemobiletools.musicplayer.extensions import android.content.Context import android.content.Intent -import com.simplemobiletools.musicplayer.helpers.PREFS +import com.simplemobiletools.musicplayer.helpers.PREFS_KEY import com.simplemobiletools.musicplayer.services.MusicService fun Context.sendIntent(action: String) { @@ -12,4 +12,4 @@ fun Context.sendIntent(action: String) { } } -fun Context.getSharedPrefs() = getSharedPreferences(PREFS, Context.MODE_PRIVATE) +fun Context.getSharedPrefs() = getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE) diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/Constants.kt index e7682641..617f2b61 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/Constants.kt @@ -1,12 +1,8 @@ package com.simplemobiletools.musicplayer.helpers -val PREFS = "prefs" -val WIDGET_BG_COLOR = "widget_bg_color" -val WIDGET_TEXT_COLOR = "widget_text_color" val SONG_POS = "song_position" val PROGRESS = "progress" val EDITED_SONG = "edited_song" -val UPDATE_ACTIVITY = "update_activity" private val PATH = "com.simplemobiletools.musicplayer.action." @@ -33,3 +29,5 @@ val NUMERIC_PROGRESS = "numeric_progress" val SORTING = "sorting" val EQUALIZER = "equalizer" val REPEAT_SONG = "repeat_song" +val WIDGET_BG_COLOR = "widget_bg_color" +val WIDGET_TEXT_COLOR = "widget_text_color" diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/services/MusicService.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/services/MusicService.kt index a8bfb007..600323cc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/services/MusicService.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/services/MusicService.kt @@ -137,10 +137,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr } REFRESH_LIST -> { getSortedSongs() - - if (intent.extras?.containsKey(UPDATE_ACTIVITY) == true) { - mBus!!.post(Events.PlaylistUpdated(mSongs!!)) - } + mBus!!.post(Events.PlaylistUpdated(mSongs!!)) } SET_PROGRESS -> { if (mPlayer != null) {