From 31063fb40f982fe021a0b57fd8b9b895fa58eec1 Mon Sep 17 00:00:00 2001 From: Antoine POPINEAU Date: Mon, 21 Oct 2019 11:51:32 +0200 Subject: [PATCH] Misc refactoring. --- .../apognu/otter/activities/LoginActivity.kt | 1 - .../apognu/otter/adapters/AlbumsAdapter.kt | 2 +- .../otter/adapters/AlbumsGridAdapter.kt | 4 +- .../apognu/otter/adapters/ArtistsAdapter.kt | 4 +- .../apognu/otter/adapters/FavoritesAdapter.kt | 44 +------------------ .../otter/adapters/PlaylistTracksAdapter.kt | 2 +- .../apognu/otter/adapters/PlaylistsAdapter.kt | 4 +- .../otter/adapters/SearchResultsAdapter.kt | 32 -------------- .../apognu/otter/adapters/TracksAdapter.kt | 4 +- .../apognu/otter/fragments/AlbumsFragment.kt | 2 +- .../otter/fragments/AlbumsGridFragment.kt | 2 +- .../apognu/otter/fragments/TracksFragment.kt | 8 ++-- .../otter/playback/MediaControlsManager.kt | 5 +-- .../apognu/otter/playback/PlayerService.kt | 3 +- .../otter/repositories/SearchRepository.kt | 2 - .../github/apognu/otter/utils/Extensions.kt | 30 +++---------- .../apognu/otter/views/ExplodeReveal.kt | 2 +- 17 files changed, 29 insertions(+), 122 deletions(-) delete mode 100644 app/src/main/java/com/github/apognu/otter/adapters/SearchResultsAdapter.kt diff --git a/app/src/main/java/com/github/apognu/otter/activities/LoginActivity.kt b/app/src/main/java/com/github/apognu/otter/activities/LoginActivity.kt index 6d5a540..578c1f5 100644 --- a/app/src/main/java/com/github/apognu/otter/activities/LoginActivity.kt +++ b/app/src/main/java/com/github/apognu/otter/activities/LoginActivity.kt @@ -7,7 +7,6 @@ import androidx.appcompat.app.AppCompatActivity import com.github.apognu.otter.R import com.github.apognu.otter.fragments.LoginDialog import com.github.apognu.otter.utils.AppContext -import com.github.apognu.otter.utils.log import com.github.kittinunf.fuel.Fuel import com.github.kittinunf.fuel.coroutines.awaitObjectResult import com.github.kittinunf.fuel.gson.gsonDeserializerOf diff --git a/app/src/main/java/com/github/apognu/otter/adapters/AlbumsAdapter.kt b/app/src/main/java/com/github/apognu/otter/adapters/AlbumsAdapter.kt index f82d1b9..c852736 100644 --- a/app/src/main/java/com/github/apognu/otter/adapters/AlbumsAdapter.kt +++ b/app/src/main/java/com/github/apognu/otter/adapters/AlbumsAdapter.kt @@ -43,7 +43,7 @@ class AlbumsAdapter(val context: Context?, val listener: OnAlbumClickListener) : holder.artist.text = album.artist.name } - inner class ViewHolder(view: View, val listener: OnAlbumClickListener) : RecyclerView.ViewHolder(view), View.OnClickListener { + inner class ViewHolder(view: View, private val listener: OnAlbumClickListener) : RecyclerView.ViewHolder(view), View.OnClickListener { val art = view.art val title = view.title val artist = view.artist diff --git a/app/src/main/java/com/github/apognu/otter/adapters/AlbumsGridAdapter.kt b/app/src/main/java/com/github/apognu/otter/adapters/AlbumsGridAdapter.kt index afbc227..3c5b08b 100644 --- a/app/src/main/java/com/github/apognu/otter/adapters/AlbumsGridAdapter.kt +++ b/app/src/main/java/com/github/apognu/otter/adapters/AlbumsGridAdapter.kt @@ -13,7 +13,7 @@ import com.squareup.picasso.Picasso import jp.wasabeef.picasso.transformations.RoundedCornersTransformation import kotlinx.android.synthetic.main.row_album_grid.view.* -class AlbumsGridAdapter(val context: Context?, val listener: OnAlbumClickListener) : FunkwhaleAdapter() { +class AlbumsGridAdapter(val context: Context?, private val listener: OnAlbumClickListener) : FunkwhaleAdapter() { interface OnAlbumClickListener { fun onClick(view: View?, album: Album) } @@ -41,7 +41,7 @@ class AlbumsGridAdapter(val context: Context?, val listener: OnAlbumClickListene holder.title.text = album.title } - inner class ViewHolder(view: View, val listener: OnAlbumClickListener) : RecyclerView.ViewHolder(view), View.OnClickListener { + inner class ViewHolder(view: View, private val listener: OnAlbumClickListener) : RecyclerView.ViewHolder(view), View.OnClickListener { val cover = view.cover val title = view.title diff --git a/app/src/main/java/com/github/apognu/otter/adapters/ArtistsAdapter.kt b/app/src/main/java/com/github/apognu/otter/adapters/ArtistsAdapter.kt index ecb2859..b532c15 100644 --- a/app/src/main/java/com/github/apognu/otter/adapters/ArtistsAdapter.kt +++ b/app/src/main/java/com/github/apognu/otter/adapters/ArtistsAdapter.kt @@ -13,7 +13,7 @@ import com.squareup.picasso.Picasso import jp.wasabeef.picasso.transformations.RoundedCornersTransformation import kotlinx.android.synthetic.main.row_artist.view.* -class ArtistsAdapter(val context: Context?, val listener: OnArtistClickListener) : FunkwhaleAdapter() { +class ArtistsAdapter(val context: Context?, private val listener: OnArtistClickListener) : FunkwhaleAdapter() { interface OnArtistClickListener { fun onClick(holder: View?, artist: Artist) } @@ -53,7 +53,7 @@ class ArtistsAdapter(val context: Context?, val listener: OnArtistClickListener) } } - inner class ViewHolder(view: View, val listener: OnArtistClickListener) : RecyclerView.ViewHolder(view), View.OnClickListener { + inner class ViewHolder(view: View, private val listener: OnArtistClickListener) : RecyclerView.ViewHolder(view), View.OnClickListener { val art = view.art val name = view.name val albums = view.albums diff --git a/app/src/main/java/com/github/apognu/otter/adapters/FavoritesAdapter.kt b/app/src/main/java/com/github/apognu/otter/adapters/FavoritesAdapter.kt index 8fceb78..5c0af21 100644 --- a/app/src/main/java/com/github/apognu/otter/adapters/FavoritesAdapter.kt +++ b/app/src/main/java/com/github/apognu/otter/adapters/FavoritesAdapter.kt @@ -2,16 +2,13 @@ package com.github.apognu.otter.adapters import android.annotation.SuppressLint import android.content.Context -import android.graphics.Color import android.graphics.Typeface -import android.graphics.drawable.ColorDrawable import android.os.Build import android.view.Gravity import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.appcompat.widget.PopupMenu -import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.RecyclerView import com.github.apognu.otter.R import com.github.apognu.otter.fragments.FunkwhaleAdapter @@ -21,7 +18,7 @@ import jp.wasabeef.picasso.transformations.RoundedCornersTransformation import kotlinx.android.synthetic.main.row_track.view.* import java.util.* -class FavoritesAdapter(private val context: Context?, val favoriteListener: OnFavoriteListener, val fromQueue: Boolean = false) : FunkwhaleAdapter() { +class FavoritesAdapter(private val context: Context?, private val favoriteListener: OnFavoriteListener, val fromQueue: Boolean = false) : FunkwhaleAdapter() { interface OnFavoriteListener { fun onToggleFavorite(id: Int, state: Boolean) } @@ -109,7 +106,7 @@ class FavoritesAdapter(private val context: Context?, val favoriteListener: OnFa fun onItemMove(oldPosition: Int, newPosition: Int) { if (oldPosition < newPosition) { - for (i in oldPosition.rangeTo(newPosition - 1)) { + for (i in oldPosition.until(newPosition)) { Collections.swap(data, i, i + 1) } } else { @@ -123,7 +120,6 @@ class FavoritesAdapter(private val context: Context?, val favoriteListener: OnFa } inner class ViewHolder(view: View, val context: Context?) : RecyclerView.ViewHolder(view), View.OnClickListener { - val handle = view.handle val cover = view.cover val title = view.title val artist = view.artist @@ -144,40 +140,4 @@ class FavoritesAdapter(private val context: Context?, val favoriteListener: OnFa } } } - - inner class TouchHelperCallback : ItemTouchHelper.Callback() { - override fun isLongPressDragEnabled() = false - - override fun isItemViewSwipeEnabled() = false - - override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) = - makeMovementFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN, 0) - - override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean { - onItemMove(viewHolder.adapterPosition, target.adapterPosition) - - return true - } - - override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {} - - @SuppressLint("NewApi") - override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) { - if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) { - context?.let { - Build.VERSION_CODES.M.onApi( - { viewHolder?.itemView?.background = ColorDrawable(context.resources.getColor(R.color.colorSelected, null)) }, - { viewHolder?.itemView?.background = ColorDrawable(context.resources.getColor(R.color.colorSelected)) }) - } - } - - super.onSelectedChanged(viewHolder, actionState) - } - - override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { - viewHolder.itemView.background = ColorDrawable(Color.TRANSPARENT) - - super.clearView(recyclerView, viewHolder) - } - } } diff --git a/app/src/main/java/com/github/apognu/otter/adapters/PlaylistTracksAdapter.kt b/app/src/main/java/com/github/apognu/otter/adapters/PlaylistTracksAdapter.kt index 0229ee0..f209266 100644 --- a/app/src/main/java/com/github/apognu/otter/adapters/PlaylistTracksAdapter.kt +++ b/app/src/main/java/com/github/apognu/otter/adapters/PlaylistTracksAdapter.kt @@ -112,7 +112,7 @@ class PlaylistTracksAdapter(private val context: Context?, val fromQueue: Boolea fun onItemMove(oldPosition: Int, newPosition: Int) { if (oldPosition < newPosition) { - for (i in oldPosition.rangeTo(newPosition - 1)) { + for (i in oldPosition.until(newPosition)) { Collections.swap(data, i, i + 1) } } else { diff --git a/app/src/main/java/com/github/apognu/otter/adapters/PlaylistsAdapter.kt b/app/src/main/java/com/github/apognu/otter/adapters/PlaylistsAdapter.kt index ed410dd..7e22931 100644 --- a/app/src/main/java/com/github/apognu/otter/adapters/PlaylistsAdapter.kt +++ b/app/src/main/java/com/github/apognu/otter/adapters/PlaylistsAdapter.kt @@ -11,7 +11,7 @@ import com.github.apognu.otter.utils.Playlist import com.squareup.picasso.Picasso import kotlinx.android.synthetic.main.row_playlist.view.* -class PlaylistsAdapter(val context: Context?, val listener: OnPlaylistClickListener) : FunkwhaleAdapter() { +class PlaylistsAdapter(val context: Context?, private val listener: OnPlaylistClickListener) : FunkwhaleAdapter() { interface OnPlaylistClickListener { fun onClick(holder: View?, playlist: Playlist) } @@ -49,7 +49,7 @@ class PlaylistsAdapter(val context: Context?, val listener: OnPlaylistClickListe } } - inner class ViewHolder(view: View, val listener: OnPlaylistClickListener) : RecyclerView.ViewHolder(view), View.OnClickListener { + inner class ViewHolder(view: View, private val listener: OnPlaylistClickListener) : RecyclerView.ViewHolder(view), View.OnClickListener { val name = view.name val summary = view.summary diff --git a/app/src/main/java/com/github/apognu/otter/adapters/SearchResultsAdapter.kt b/app/src/main/java/com/github/apognu/otter/adapters/SearchResultsAdapter.kt deleted file mode 100644 index aa25302..0000000 --- a/app/src/main/java/com/github/apognu/otter/adapters/SearchResultsAdapter.kt +++ /dev/null @@ -1,32 +0,0 @@ -package com.github.apognu.otter.adapters - -import android.content.Context -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import androidx.recyclerview.widget.RecyclerView -import com.github.apognu.otter.R -import com.github.apognu.otter.utils.Track -import kotlinx.android.synthetic.main.row_track.view.* - -class SearchResultsAdapter(val context: Context?) : RecyclerView.Adapter() { - var tracks: List = listOf() - - override fun getItemCount() = tracks.size - - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { - val view = LayoutInflater.from(context).inflate(R.layout.row_track, parent, false) - - return ViewHolder(view) - } - - override fun onBindViewHolder(holder: ViewHolder, position: Int) { - val artist = tracks[position] - - holder.title.text = artist.title - } - - inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { - val title = view.title - } -} \ No newline at end of file diff --git a/app/src/main/java/com/github/apognu/otter/adapters/TracksAdapter.kt b/app/src/main/java/com/github/apognu/otter/adapters/TracksAdapter.kt index 7079e92..432a7ec 100644 --- a/app/src/main/java/com/github/apognu/otter/adapters/TracksAdapter.kt +++ b/app/src/main/java/com/github/apognu/otter/adapters/TracksAdapter.kt @@ -18,7 +18,7 @@ import jp.wasabeef.picasso.transformations.RoundedCornersTransformation import kotlinx.android.synthetic.main.row_track.view.* import java.util.* -class TracksAdapter(private val context: Context?, val favoriteListener: OnFavoriteListener? = null, val fromQueue: Boolean = false) : FunkwhaleAdapter() { +class TracksAdapter(private val context: Context?, private val favoriteListener: OnFavoriteListener? = null, val fromQueue: Boolean = false) : FunkwhaleAdapter() { interface OnFavoriteListener { fun onToggleFavorite(id: Int, state: Boolean) } @@ -132,7 +132,7 @@ class TracksAdapter(private val context: Context?, val favoriteListener: OnFavor fun onItemMove(oldPosition: Int, newPosition: Int) { if (oldPosition < newPosition) { - for (i in oldPosition.rangeTo(newPosition - 1)) { + for (i in oldPosition.until(newPosition)) { Collections.swap(data, i, i + 1) } } else { diff --git a/app/src/main/java/com/github/apognu/otter/fragments/AlbumsFragment.kt b/app/src/main/java/com/github/apognu/otter/fragments/AlbumsFragment.kt index de746b8..df8675f 100644 --- a/app/src/main/java/com/github/apognu/otter/fragments/AlbumsFragment.kt +++ b/app/src/main/java/com/github/apognu/otter/fragments/AlbumsFragment.kt @@ -64,7 +64,7 @@ class AlbumsFragment : FunkwhaleFragment() { } inner class OnAlbumClickListener : AlbumsAdapter.OnAlbumClickListener { - override fun onClick(holder: View?, album: Album) { + override fun onClick(view: View?, album: Album) { (context as? MainActivity)?.let { activity -> exitTransition = Fade().apply { duration = AppContext.TRANSITION_DURATION diff --git a/app/src/main/java/com/github/apognu/otter/fragments/AlbumsGridFragment.kt b/app/src/main/java/com/github/apognu/otter/fragments/AlbumsGridFragment.kt index c463d97..e0c56ed 100644 --- a/app/src/main/java/com/github/apognu/otter/fragments/AlbumsGridFragment.kt +++ b/app/src/main/java/com/github/apognu/otter/fragments/AlbumsGridFragment.kt @@ -29,7 +29,7 @@ class AlbumsGridFragment : FunkwhaleFragment() { } inner class OnAlbumClickListener : AlbumsGridAdapter.OnAlbumClickListener { - override fun onClick(holder: View?, album: Album) { + override fun onClick(view: View?, album: Album) { (context as? MainActivity)?.let { activity -> onViewPager { exitTransition = Fade().apply { diff --git a/app/src/main/java/com/github/apognu/otter/fragments/TracksFragment.kt b/app/src/main/java/com/github/apognu/otter/fragments/TracksFragment.kt index 3b19c08..098b7c0 100644 --- a/app/src/main/java/com/github/apognu/otter/fragments/TracksFragment.kt +++ b/app/src/main/java/com/github/apognu/otter/fragments/TracksFragment.kt @@ -21,10 +21,10 @@ class TracksFragment : FunkwhaleFragment() { lateinit var favoritesRepository: FavoritesRepository - var albumId = 0 - var albumArtist = "" - var albumTitle = "" - var albumCover = "" + private var albumId = 0 + private var albumArtist = "" + private var albumTitle = "" + private var albumCover = "" companion object { fun new(album: Album): TracksFragment { diff --git a/app/src/main/java/com/github/apognu/otter/playback/MediaControlsManager.kt b/app/src/main/java/com/github/apognu/otter/playback/MediaControlsManager.kt index 1855ebc..4bef0be 100644 --- a/app/src/main/java/com/github/apognu/otter/playback/MediaControlsManager.kt +++ b/app/src/main/java/com/github/apognu/otter/playback/MediaControlsManager.kt @@ -20,16 +20,15 @@ import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch -class MediaControlsManager(val context: Service, val mediaSession: MediaSessionCompat) { +class MediaControlsManager(val context: Service, private val mediaSession: MediaSessionCompat) { companion object { const val NOTIFICATION_ACTION_OPEN_QUEUE = 0 const val NOTIFICATION_ACTION_PREVIOUS = 1 const val NOTIFICATION_ACTION_TOGGLE = 2 const val NOTIFICATION_ACTION_NEXT = 3 - const val NOTIFICATION_ACTION_FAVORITE = 4 } - var notification: Notification? = null + private var notification: Notification? = null fun updateNotification(track: Track?, playing: Boolean) { if (notification == null && !playing) return diff --git a/app/src/main/java/com/github/apognu/otter/playback/PlayerService.kt b/app/src/main/java/com/github/apognu/otter/playback/PlayerService.kt index 31ff816..2a47582 100644 --- a/app/src/main/java/com/github/apognu/otter/playback/PlayerService.kt +++ b/app/src/main/java/com/github/apognu/otter/playback/PlayerService.kt @@ -9,6 +9,7 @@ import android.media.AudioAttributes import android.media.AudioFocusRequest import android.media.AudioManager import android.os.Build +import android.os.IBinder import android.support.v4.media.session.MediaSessionCompat import android.view.KeyEvent import com.github.apognu.otter.R @@ -214,7 +215,7 @@ class PlayerService : Service() { }) } - override fun onBind(intent: Intent?) = null + override fun onBind(intent: Intent?): IBinder? = null @SuppressLint("NewApi") override fun onDestroy() { diff --git a/app/src/main/java/com/github/apognu/otter/repositories/SearchRepository.kt b/app/src/main/java/com/github/apognu/otter/repositories/SearchRepository.kt index 0713d79..df51ea8 100644 --- a/app/src/main/java/com/github/apognu/otter/repositories/SearchRepository.kt +++ b/app/src/main/java/com/github/apognu/otter/repositories/SearchRepository.kt @@ -17,8 +17,6 @@ class SearchRepository(override val context: Context?, query: String) : Reposito override fun cache(data: List) = TracksCache(data) override fun uncache(reader: BufferedReader) = gsonDeserializerOf(TracksCache::class.java).deserialize(reader) - var query: String? = null - override fun onDataFetched(data: List): List = runBlocking { val favorites = FavoritesRepository(context).fetch(Origin.Network.origin).receive().data diff --git a/app/src/main/java/com/github/apognu/otter/utils/Extensions.kt b/app/src/main/java/com/github/apognu/otter/utils/Extensions.kt index 283c664..c99dc23 100644 --- a/app/src/main/java/com/github/apognu/otter/utils/Extensions.kt +++ b/app/src/main/java/com/github/apognu/otter/utils/Extensions.kt @@ -1,11 +1,7 @@ package com.github.apognu.otter.utils import android.os.Build -import android.view.ViewGroup -import android.view.animation.Interpolator -import androidx.core.view.doOnPreDraw import androidx.fragment.app.Fragment -import androidx.transition.TransitionSet import com.github.apognu.otter.fragments.BrowseFragment import com.github.apognu.otter.repositories.Repository import kotlinx.coroutines.Dispatchers.Main @@ -35,14 +31,6 @@ inline fun Channel>.untilNetwork(context: CoroutineCo } } -fun TransitionSet.setCommonInterpolator(interpolator: Interpolator): TransitionSet { - (0 until transitionCount) - .map { index -> getTransitionAt(index) } - .forEach { transition -> transition.interpolator = interpolator } - - return this -} - fun Fragment.onViewPager(block: Fragment.() -> Unit) { for (f in activity?.supportFragmentManager?.fragments ?: listOf()) { if (f is BrowseFragment) { @@ -51,12 +39,6 @@ fun Fragment.onViewPager(block: Fragment.() -> Unit) { } } -fun Fragment.startTransitions() { - (view?.parent as? ViewGroup)?.doOnPreDraw { - startPostponedEnterTransition() - } -} - fun Int.onApi(block: () -> T) { if (Build.VERSION.SDK_INT >= this) { block() @@ -72,17 +54,17 @@ fun Int.onApi(block: () -> T, elseBlock: (() -> U)) { } fun Int.onApiForResult(block: () -> T, elseBlock: (() -> T)): T { - if (Build.VERSION.SDK_INT >= this) { - return block() + return if (Build.VERSION.SDK_INT >= this) { + block() } else { - return elseBlock() + elseBlock() } } fun T.applyOnApi(api: Int, block: T.() -> T): T { - if (Build.VERSION.SDK_INT >= api) { - return block() + return if (Build.VERSION.SDK_INT >= api) { + block() } else { - return this + this } } \ No newline at end of file diff --git a/app/src/main/java/com/github/apognu/otter/views/ExplodeReveal.kt b/app/src/main/java/com/github/apognu/otter/views/ExplodeReveal.kt index 8692d89..33b553f 100644 --- a/app/src/main/java/com/github/apognu/otter/views/ExplodeReveal.kt +++ b/app/src/main/java/com/github/apognu/otter/views/ExplodeReveal.kt @@ -9,7 +9,7 @@ import androidx.transition.TransitionValues import androidx.transition.Visibility class ExplodeReveal : Visibility() { - val SCREEN_BOUNDS = "screenBounds" + private val SCREEN_BOUNDS = "screenBounds" private val locations = IntArray(2)