From ce5812d9f05604f55b44f4a0063ed8568abab2cd Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 7 Sep 2017 23:17:18 +0200 Subject: [PATCH] couple code style changes --- .../activities/SettingsActivity.kt | 2 +- .../activities/WidgetConfigureActivity.kt | 4 +- .../musicplayer/adapters/PlaylistsAdapter.kt | 4 +- .../musicplayer/adapters/SongAdapter.kt | 12 +++--- .../dialogs/ChangeSortingDialog.kt | 7 ++-- .../musicplayer/dialogs/NewPlaylistDialog.kt | 2 +- .../dialogs/RemovePlaylistDialog.kt | 2 +- .../musicplayer/helpers/DBHelper.kt | 12 +++--- .../musicplayer/models/Song.kt | 11 ++--- .../musicplayer/services/MusicService.kt | 42 +++++++++---------- .../main/res/layout/dialog_new_playlist.xml | 2 - 11 files changed, 46 insertions(+), 54 deletions(-) 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 35c2a819..303e41ce 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/SettingsActivity.kt @@ -40,7 +40,7 @@ class SettingsActivity : SimpleActivity() { private fun setupEqualizer() { val equalizer = MusicService.mEqualizer ?: return val items = arrayListOf() - (0..equalizer.numberOfPresets - 1).mapTo(items) { RadioItem(it, equalizer.getPresetName(it.toShort())) } + (0 until equalizer.numberOfPresets).mapTo(items) { RadioItem(it, equalizer.getPresetName(it.toShort())) } settings_equalizer.text = items[config.equalizer].title settings_equalizer_holder.setOnClickListener { 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 18ed22ff..b7a4d2ec 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/WidgetConfigureActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/WidgetConfigureActivity.kt @@ -86,14 +86,14 @@ class WidgetConfigureActivity : AppCompatActivity() { finish() } - fun pickBackgroundColor() { + private fun pickBackgroundColor() { ColorPickerDialog(this, mBgColorWithoutTransparency) { mBgColorWithoutTransparency = it updateBackgroundColor() } } - fun pickTextColor() { + private fun pickTextColor() { ColorPickerDialog(this, mTextColor) { mTextColor = it updateTextColor() diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/adapters/PlaylistsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/adapters/PlaylistsAdapter.kt index 9a58c853..8ac389de 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/adapters/PlaylistsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/adapters/PlaylistsAdapter.kt @@ -56,7 +56,7 @@ class PlaylistsAdapter(val activity: SimpleActivity, val mItems: List, itemCnt = mItems.size } - val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) { + private val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) { override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { when (item.itemId) { R.id.cab_delete -> askConfirmDelete() @@ -158,7 +158,7 @@ class PlaylistsAdapter(val activity: SimpleActivity, val mItems: List, return itemView } - fun viewClicked(playlist: Playlist) { + private fun viewClicked(playlist: Playlist) { if (multiSelector.isSelectable) { val isSelected = multiSelector.selectedPositions.contains(layoutPosition) multiSelector.setSelected(this, !isSelected) 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 41700894..a3762cad 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/adapters/SongAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/adapters/SongAdapter.kt @@ -55,12 +55,12 @@ class SongAdapter(val activity: SimpleActivity, var songs: ArrayList, val updateTitle(selectedPositions.size) } - fun updateTitle(cnt: Int) { + private fun updateTitle(cnt: Int) { actMode?.title = "$cnt / ${songs.size}" actMode?.invalidate() } - val adapterListener = object : MyAdapterListener { + private val adapterListener = object : MyAdapterListener { override fun toggleItemSelectionAdapter(select: Boolean, position: Int) { toggleItemSelection(select, position) } @@ -68,7 +68,7 @@ class SongAdapter(val activity: SimpleActivity, var songs: ArrayList, val override fun getSelectedPositions(): HashSet = selectedPositions } - val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) { + private val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) { override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { when (item.itemId) { R.id.cab_properties -> showProperties() @@ -133,7 +133,7 @@ class SongAdapter(val activity: SimpleActivity, var songs: ArrayList, val private fun selectAll() { val cnt = songs.size - for (i in 0..cnt - 1) { + for (i in 0 until cnt) { selectedPositions.add(i) notifyItemChanged(i) } @@ -240,7 +240,7 @@ class SongAdapter(val activity: SimpleActivity, var songs: ArrayList, val toggleItemSelection(true, i) if (min > -1 && min < to) { - (min..to - 1).filter { it != from } + (min until to).filter { it != from } .forEach { toggleItemSelection(false, it) } } if (max > -1) { @@ -257,7 +257,7 @@ class SongAdapter(val activity: SimpleActivity, var songs: ArrayList, val } if (min > -1) { - for (i in min..from - 1) + for (i in min until from) toggleItemSelection(false, i) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/dialogs/ChangeSortingDialog.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/dialogs/ChangeSortingDialog.kt index 0be960f0..ede874b7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/dialogs/ChangeSortingDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/dialogs/ChangeSortingDialog.kt @@ -1,7 +1,6 @@ package com.simplemobiletools.musicplayer.dialogs import android.app.Activity -import android.content.DialogInterface import android.support.v7.app.AlertDialog import android.view.LayoutInflater import android.view.View @@ -15,14 +14,14 @@ import com.simplemobiletools.musicplayer.helpers.SORT_BY_FILE_NAME import com.simplemobiletools.musicplayer.helpers.SORT_BY_TITLE import kotlinx.android.synthetic.main.dialog_change_sorting.view.* -class ChangeSortingDialog(val activity: Activity, val callback: () -> Unit) : DialogInterface.OnClickListener { +class ChangeSortingDialog(val activity: Activity, val callback: () -> Unit) { private var currSorting = 0 var config = activity.config var view: View = LayoutInflater.from(activity).inflate(R.layout.dialog_change_sorting, null) init { AlertDialog.Builder(activity) - .setPositiveButton(R.string.ok, this) + .setPositiveButton(R.string.ok, { dialog, which -> dialogConfirmed() }) .setNegativeButton(R.string.cancel, null) .create().apply { activity.setupDialogStuff(view, this, R.string.sort_by) @@ -57,7 +56,7 @@ class ChangeSortingDialog(val activity: Activity, val callback: () -> Unit) : Di orderBtn.isChecked = true } - override fun onClick(dialog: DialogInterface, which: Int) { + private fun dialogConfirmed() { val sortingRadio = view.sorting_dialog_radio_sorting var sorting = when (sortingRadio.checkedRadioButtonId) { R.id.sorting_dialog_radio_title -> SORT_BY_TITLE diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/dialogs/NewPlaylistDialog.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/dialogs/NewPlaylistDialog.kt index ec18e1ec..08055d5f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/dialogs/NewPlaylistDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/dialogs/NewPlaylistDialog.kt @@ -11,7 +11,7 @@ import com.simplemobiletools.musicplayer.extensions.dbHelper import com.simplemobiletools.musicplayer.models.Playlist import kotlinx.android.synthetic.main.dialog_new_playlist.view.* -class NewPlaylistDialog(val activity: Activity, var playlist: Playlist? = null, val callback: (playlistId: Int) -> Unit) : AlertDialog.Builder(activity) { +class NewPlaylistDialog(val activity: Activity, var playlist: Playlist? = null, val callback: (playlistId: Int) -> Unit) { var isNewPlaylist = playlist == null init { diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/dialogs/RemovePlaylistDialog.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/dialogs/RemovePlaylistDialog.kt index 5a989b98..decefa5f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/dialogs/RemovePlaylistDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/dialogs/RemovePlaylistDialog.kt @@ -8,7 +8,7 @@ import com.simplemobiletools.musicplayer.R import com.simplemobiletools.musicplayer.models.Playlist import kotlinx.android.synthetic.main.dialog_remove_playlist.view.* -class RemovePlaylistDialog(val activity: Activity, val playlist: Playlist? = null, val callback: (deleteFiles: Boolean) -> Unit) : AlertDialog.Builder(activity) { +class RemovePlaylistDialog(val activity: Activity, val playlist: Playlist? = null, val callback: (deleteFiles: Boolean) -> Unit) { init { val view = activity.layoutInflater.inflate(R.layout.dialog_remove_playlist, null).apply { remove_playlist_description.text = getDescriptionText() diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/DBHelper.kt index ca515f26..fcd32ca2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/DBHelper.kt @@ -33,9 +33,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont val DB_NAME = "playlists.db" val INITIAL_PLAYLIST_ID = 1 - fun newInstance(context: Context): DBHelper { - return DBHelper(context) - } + fun newInstance(context: Context) = DBHelper(context) } override fun onCreate(db: SQLiteDatabase) { @@ -160,13 +158,13 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont return null } - fun removeSongFromPlaylist(path: String, playlistId: Int) { + private fun removeSongFromPlaylist(path: String, playlistId: Int) { removeSongsFromPlaylist(ArrayList().apply { add(path) }, playlistId) } fun removeSongsFromPlaylist(paths: ArrayList, playlistId: Int = context.config.currentPlaylist) { val SPLICE_SIZE = 200 - for (i in 0..paths.size - 1 step SPLICE_SIZE) { + for (i in 0 until paths.size step SPLICE_SIZE) { val curPaths = paths.subList(i, Math.min(i + SPLICE_SIZE, paths.size)) val questionMarks = getQuestionMarks(curPaths.size) var selection = "$COL_PATH IN ($questionMarks)" @@ -210,10 +208,10 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont if (paths.isEmpty()) return songs - for (i in 0..paths.size - 1 step SPLICE_SIZE) { + for (i in 0 until paths.size step SPLICE_SIZE) { val curPaths = paths.subList(i, Math.min(i + SPLICE_SIZE, paths.size)) val uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI - val columns = arrayOf(MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.DATA) + val columns = arrayOf(MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.DURATION) val questionMarks = getQuestionMarks(curPaths.size) val selection = "${MediaStore.Audio.Media.DATA} IN ($questionMarks)" val selectionArgs = curPaths.toTypedArray() diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/models/Song.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/models/Song.kt index ffef846b..0feeb8e9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/models/Song.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/models/Song.kt @@ -35,12 +35,9 @@ data class Song(val id: Long, var title: String, var artist: String, var path: S return res } - override fun equals(o: Any?): Boolean { - return if (this === o) - true - else if (o == null) - false - else - id == (o as Song).id + override fun equals(other: Any?): Boolean = when { + this === other -> true + other == null -> false + else -> id == (other as Song).id } } 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 d48c4af4..b41e7cf8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/services/MusicService.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/services/MusicService.kt @@ -84,8 +84,8 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr private fun initService() { mConfig = applicationContext.config - mSongs = ArrayList() - mPlayedSongIndexes = ArrayList() + mSongs = ArrayList() + mPlayedSongIndexes = ArrayList() mCurrSong = null setupIntents() getSortedSongs() @@ -163,7 +163,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr return START_NOT_STICKY } - fun initMediaPlayerIfNeeded() { + private fun initMediaPlayerIfNeeded() { if (mPlayer != null) return @@ -248,7 +248,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr } val notification = NotificationCompat.Builder(this) - .setStyle(NotificationCompat.MediaStyle().setShowActionsInCompactView(*intArrayOf(playPauseButtonPosition, nextButtonPosition))) + .setStyle(NotificationCompat.MediaStyle().setShowActionsInCompactView(playPauseButtonPosition, nextButtonPosition)) .setContentTitle(title) .setContentText(artist) .setSmallIcon(R.drawable.ic_headset_small) @@ -303,17 +303,17 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr private fun getNewSongId(): Int { return if (mConfig!!.isShuffleEnabled) { val cnt = mSongs!!.size - if (cnt == 0) { - -1 - } else if (cnt == 1) { - 0 - } else { - val random = Random() - var newSongIndex = random.nextInt(cnt) - while (mPlayedSongIndexes!!.contains(newSongIndex)) { - newSongIndex = random.nextInt(cnt) + when (cnt) { + 0 -> -1 + 1 -> 0 + else -> { + val random = Random() + var newSongIndex = random.nextInt(cnt) + while (mPlayedSongIndexes!!.contains(newSongIndex)) { + newSongIndex = random.nextInt(cnt) + } + newSongIndex } - newSongIndex } } else { if (mPlayedSongIndexes!!.isEmpty()) { @@ -325,7 +325,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr } } - fun playPreviousSong() { + private fun playPreviousSong() { if (mSongs!!.isEmpty()) { handleEmptyPlaylist() return @@ -343,7 +343,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr } } - fun pauseSong() { + private fun pauseSong() { if (mSongs!!.isEmpty()) return @@ -353,7 +353,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr songStateChanged(false) } - fun resumeSong() { + private fun resumeSong() { if (mSongs!!.isEmpty()) { handleEmptyPlaylist() return @@ -370,7 +370,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr songStateChanged(true) } - fun playNextSong() { + private fun playNextSong() { setSong(getNewSongId(), true) } @@ -384,7 +384,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr setSong(pos, true) } - fun setSong(songIndex: Int, addNewSong: Boolean) { + private fun setSong(songIndex: Int, addNewSong: Boolean) { if (mSongs!!.isEmpty()) { handleEmptyPlaylist() return @@ -471,7 +471,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr stopSelf() } - fun incomingCallStart() { + private fun incomingCallStart() { if (getIsPlaying()) { mWasPlayingAtCall = true pauseSong() @@ -480,7 +480,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr } } - fun incomingCallStop() { + private fun incomingCallStop() { if (mWasPlayingAtCall) resumeSong() diff --git a/app/src/main/res/layout/dialog_new_playlist.xml b/app/src/main/res/layout/dialog_new_playlist.xml index e5b1ff73..814fa95b 100644 --- a/app/src/main/res/layout/dialog_new_playlist.xml +++ b/app/src/main/res/layout/dialog_new_playlist.xml @@ -20,8 +20,6 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="@dimen/activity_margin" - android:layout_marginLeft="@dimen/small_margin" - android:layout_marginStart="@dimen/small_margin" android:inputType="textCapSentences" android:maxLength="50" android:singleLine="true"