From 5e2da898c9d94502a8d2f1a8808d4d6b87ea6198 Mon Sep 17 00:00:00 2001 From: casey langen Date: Sat, 9 Feb 2019 14:07:35 -0800 Subject: [PATCH] Some refactors to prep for experimental Android tab UI. --- .../activity/CategoryBrowseActivity.kt | 102 ++++++++---------- .../category/adapter/AllCategoriesAdapter.kt | 4 +- .../remote/ui/category/constant/Category.kt | 58 ++++++++++ .../ui/category/constant/CategoryType.kt | 24 ----- .../ui/shared/mixin/ItemContextMenuMixin.kt | 5 +- src/musikdroid/build.gradle | 4 +- 6 files changed, 109 insertions(+), 88 deletions(-) create mode 100644 src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/category/constant/Category.kt delete mode 100644 src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/category/constant/CategoryType.kt diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/category/activity/CategoryBrowseActivity.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/category/activity/CategoryBrowseActivity.kt index 984d565ec..66d91c977 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/category/activity/CategoryBrowseActivity.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/category/activity/CategoryBrowseActivity.kt @@ -12,8 +12,7 @@ import io.casey.musikcube.remote.service.websocket.model.ICategoryValue import io.casey.musikcube.remote.service.websocket.model.IDataProvider import io.casey.musikcube.remote.ui.albums.activity.AlbumBrowseActivity import io.casey.musikcube.remote.ui.category.adapter.CategoryBrowseAdapter -import io.casey.musikcube.remote.ui.category.constant.NavigationType -import io.casey.musikcube.remote.ui.category.constant.categoryToString +import io.casey.musikcube.remote.ui.category.constant.* import io.casey.musikcube.remote.ui.shared.activity.BaseActivity import io.casey.musikcube.remote.ui.shared.activity.Filterable import io.casey.musikcube.remote.ui.shared.extension.* @@ -25,6 +24,7 @@ import io.casey.musikcube.remote.ui.shared.view.EmptyListView import io.casey.musikcube.remote.ui.tracks.activity.TrackListActivity import io.casey.musikcube.remote.util.Debouncer import io.reactivex.rxkotlin.subscribeBy +import java.lang.IllegalArgumentException import io.casey.musikcube.remote.service.websocket.WebSocketService.State as SocketState class CategoryBrowseActivity : BaseActivity(), Filterable { @@ -47,10 +47,10 @@ class CategoryBrowseActivity : BaseActivity(), Filterable { super.onCreate(savedInstanceState) - category = intent.getStringExtra(EXTRA_CATEGORY) - predicateType = intent.getStringExtra(EXTRA_PREDICATE_TYPE) ?: "" - predicateId = intent.getLongExtra(EXTRA_PREDICATE_ID, -1) - navigationType = NavigationType.get(intent.getIntExtra(EXTRA_NAVIGATION_TYPE, NavigationType.Albums.ordinal)) + category = intent.getStringExtra(Category.Extra.CATEGORY) + predicateType = intent.getStringExtra(Category.Extra.PREDICATE_TYPE) ?: "" + predicateId = intent.getLongExtra(Category.Extra.PREDICATE_ID, -1) + navigationType = NavigationType.get(intent.getIntExtra(Category.Extra.NAVIGATION_TYPE, NavigationType.Albums.ordinal)) adapter = CategoryBrowseAdapter(adapterListener, playback, navigationType, category, prefs) setContentView(R.layout.recycler_view_activity) @@ -119,25 +119,28 @@ class CategoryBrowseActivity : BaseActivity(), Filterable { private val categoryTypeString: String get() { - CATEGORY_NAME_TO_EMPTY_TYPE[category]?.let { + Category.NAME_TO_EMPTY_TYPE[category]?.let { return getString(it) } - return categoryToString(this, category) + return Category.toDisplayString(this, category) } private val categoryTitleString: String get() { - CATEGORY_NAME_TO_TITLE[category]?.let { + Category.NAME_TO_TITLE[category]?.let { return getString(it) } - return categoryToString(this, category) + return Category.toDisplayString(this, category) } private fun requery() { - data.provider.getCategoryValues(category, predicateType, predicateId, lastFilter ?: "").subscribeBy( - onNext = { values -> adapter.setModel(values) }, - onError = { }, - onComplete = { emptyView.update(data.provider.state, adapter.itemCount)}) + @Suppress("UNUSED") + data.provider + .getCategoryValues(category, predicateType, predicateId, lastFilter ?: "") + .subscribeBy( + onNext = { values -> adapter.setModel(values) }, + onError = { }, + onComplete = { emptyView.update(data.provider.state, adapter.itemCount)}) } private val filterDebouncer = object : Debouncer(350) { @@ -178,62 +181,45 @@ class CategoryBrowseActivity : BaseActivity(), Filterable { startActivity(TrackListActivity.getStartIntent(this, category, entry.id, entry.value)) private fun navigateToSelect(id: Long, name: String) { - val intent = Intent() - .putExtra(EXTRA_CATEGORY, category) - .putExtra(EXTRA_ID, id) - .putExtra(EXTRA_NAME, name) - setResult(RESULT_OK, intent) + setResult(RESULT_OK, Intent() + .putExtra(Category.Extra.CATEGORY, category) + .putExtra(Category.Extra.ID, id) + .putExtra(Category.Extra.NAME, name)) finish() } companion object { - const val EXTRA_CATEGORY = "extra_category" - const val EXTRA_ID = "extra_id" - const val EXTRA_NAME = "extra_name" - private const val EXTRA_PREDICATE_TYPE = "extra_predicate_type" - private const val EXTRA_PREDICATE_ID = "extra_predicate_id" - private const val EXTRA_NAVIGATION_TYPE = "extra_navigation_type" - private const val EXTRA_TITLE = "extra_title" - - private val CATEGORY_NAME_TO_TITLE: Map = mapOf( - Messages.Category.ALBUM_ARTIST to R.string.artists_title, - Messages.Category.GENRE to R.string.genres_title, - Messages.Category.ARTIST to R.string.artists_title, - Messages.Category.ALBUM to R.string.albums_title, - Messages.Category.PLAYLISTS to R.string.playlists_title) - - private val CATEGORY_NAME_TO_RELATED_TITLE: Map = mapOf( - Messages.Category.ALBUM_ARTIST to R.string.artists_from_category, - Messages.Category.GENRE to R.string.genres_from_category, - Messages.Category.ARTIST to R.string.artists_from_category, - Messages.Category.ALBUM to R.string.albums_by_title) - - private val CATEGORY_NAME_TO_EMPTY_TYPE: Map = mapOf( - Messages.Category.ALBUM_ARTIST to R.string.browse_type_artists, - Messages.Category.GENRE to R.string.browse_type_genres, - Messages.Category.ARTIST to R.string.browse_type_artists, - Messages.Category.ALBUM to R.string.browse_type_albums, - Messages.Category.PLAYLISTS to R.string.browse_type_playlists) - - fun getStartIntent(context: Context, category: String, predicateType: String = "", predicateId: Long = -1, predicateValue: String = ""): Intent { + fun getStartIntent(context: Context, + category: String, + predicateType: String = "", + predicateId: Long = -1, + predicateValue: String = ""): Intent + { val intent = Intent(context, CategoryBrowseActivity::class.java) - .putExtra(EXTRA_CATEGORY, category) - .putExtra(EXTRA_PREDICATE_TYPE, predicateType) - .putExtra(EXTRA_PREDICATE_ID, predicateId) + .putExtra(Category.Extra.CATEGORY, category) + .putExtra(Category.Extra.PREDICATE_TYPE, predicateType) + .putExtra(Category.Extra.PREDICATE_ID, predicateId) - if (predicateValue.isNotBlank() && CATEGORY_NAME_TO_RELATED_TITLE.containsKey(category)) { - val format = CATEGORY_NAME_TO_RELATED_TITLE[category]!! - intent.putExtra(EXTRA_ACTIVITY_TITLE, context.getString(format, predicateValue)) + if (predicateValue.isNotBlank() && Category.NAME_TO_RELATED_TITLE.containsKey(category)) { + val format = Category.NAME_TO_RELATED_TITLE[category] + when (format) { + null -> throw IllegalArgumentException("unknown category $category") + else -> intent.putExtra(EXTRA_ACTIVITY_TITLE, context.getString(format, predicateValue)) + } } return intent } - fun getStartIntent(context: Context, category: String, navigationType: NavigationType, title: String = ""): Intent { + fun getStartIntent(context: Context, + category: String, + navigationType: NavigationType, + title: String = ""): Intent + { return Intent(context, CategoryBrowseActivity::class.java) - .putExtra(EXTRA_CATEGORY, category) - .putExtra(EXTRA_NAVIGATION_TYPE, navigationType.ordinal) - .putExtra(EXTRA_TITLE, title) + .putExtra(Category.Extra.CATEGORY, category) + .putExtra(Category.Extra.NAVIGATION_TYPE, navigationType.ordinal) + .putExtra(Category.Extra.TITLE, title) } } } diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/category/adapter/AllCategoriesAdapter.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/category/adapter/AllCategoriesAdapter.kt index 3af751bc0..7e57f6339 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/category/adapter/AllCategoriesAdapter.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/category/adapter/AllCategoriesAdapter.kt @@ -6,7 +6,7 @@ import android.view.View import android.view.ViewGroup import android.widget.TextView import io.casey.musikcube.remote.R -import io.casey.musikcube.remote.ui.category.constant.categoryToString +import io.casey.musikcube.remote.ui.category.constant.Category.toDisplayString class AllCategoriesAdapter(private val listener: EventListener) : RecyclerView.Adapter() @@ -26,7 +26,7 @@ class AllCategoriesAdapter(private val listener: EventListener) } internal fun bindView(category: String) { - title.text = categoryToString(itemView.context, category) + title.text = toDisplayString(itemView.context, category) itemView.tag = category } } diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/category/constant/Category.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/category/constant/Category.kt new file mode 100644 index 000000000..be897635d --- /dev/null +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/category/constant/Category.kt @@ -0,0 +1,58 @@ +package io.casey.musikcube.remote.ui.category.constant + +import android.content.Context +import io.casey.musikcube.remote.R +import io.casey.musikcube.remote.service.websocket.Messages + +private val categoryToStringIdMap = mapOf( + "album" to R.string.category_artist, + "album_artist" to R.string.category_album_artist, + "artist" to R.string.category_artist, + "bitrate" to R.string.category_bitrate, + "comment" to R.string.category_comment, + "composer" to R.string.category_composer, + "conductor" to R.string.category_conductor, + "copyright" to R.string.category_copyright, + "genre" to R.string.category_genre, + "publisher" to R.string.category_publisher, + "year" to R.string.category_year) + +object Category { + val NAME_TO_TITLE: Map = mapOf( + Messages.Category.ALBUM_ARTIST to R.string.artists_title, + Messages.Category.GENRE to R.string.genres_title, + Messages.Category.ARTIST to R.string.artists_title, + Messages.Category.ALBUM to R.string.albums_title, + Messages.Category.PLAYLISTS to R.string.playlists_title) + + val NAME_TO_RELATED_TITLE: Map = mapOf( + Messages.Category.ALBUM_ARTIST to R.string.artists_from_category, + Messages.Category.GENRE to R.string.genres_from_category, + Messages.Category.ARTIST to R.string.artists_from_category, + Messages.Category.ALBUM to R.string.albums_by_title) + + val NAME_TO_EMPTY_TYPE: Map = mapOf( + Messages.Category.ALBUM_ARTIST to R.string.browse_type_artists, + Messages.Category.GENRE to R.string.browse_type_genres, + Messages.Category.ARTIST to R.string.browse_type_artists, + Messages.Category.ALBUM to R.string.browse_type_albums, + Messages.Category.PLAYLISTS to R.string.browse_type_playlists) + + fun toDisplayString(context: Context, category: String): String { + categoryToStringIdMap[category]?.let { + return context.getString(it) + } + return category + } + + object Extra { + const val CATEGORY = "extra_category" + const val ID = "extra_id" + const val NAME = "extra_name" + const val PREDICATE_TYPE = "extra_predicate_type" + const val PREDICATE_ID = "extra_predicate_id" + const val NAVIGATION_TYPE = "extra_navigation_type" + const val TITLE = "extra_title" + } +} + diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/category/constant/CategoryType.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/category/constant/CategoryType.kt deleted file mode 100644 index 7a44e6154..000000000 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/category/constant/CategoryType.kt +++ /dev/null @@ -1,24 +0,0 @@ -package io.casey.musikcube.remote.ui.category.constant - -import android.content.Context -import io.casey.musikcube.remote.R - -private val categoryToStringIdMap = mapOf( - "album" to R.string.category_artist, - "album_artist" to R.string.category_album_artist, - "artist" to R.string.category_artist, - "bitrate" to R.string.category_bitrate, - "comment" to R.string.category_comment, - "composer" to R.string.category_composer, - "conductor" to R.string.category_conductor, - "copyright" to R.string.category_copyright, - "genre" to R.string.category_genre, - "publisher" to R.string.category_publisher, - "year" to R.string.category_year) - -fun categoryToString(context: Context, category: String): String { - categoryToStringIdMap[category]?.let { - return context.getString(it) - } - return category -} \ No newline at end of file diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/mixin/ItemContextMenuMixin.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/mixin/ItemContextMenuMixin.kt index b0dcdbbc9..26ee5df16 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/mixin/ItemContextMenuMixin.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/mixin/ItemContextMenuMixin.kt @@ -22,6 +22,7 @@ import io.casey.musikcube.remote.service.websocket.model.IDataProvider import io.casey.musikcube.remote.service.websocket.model.ITrack import io.casey.musikcube.remote.ui.albums.activity.AlbumBrowseActivity import io.casey.musikcube.remote.ui.category.activity.CategoryBrowseActivity +import io.casey.musikcube.remote.ui.category.constant.Category import io.casey.musikcube.remote.ui.category.constant.NavigationType import io.casey.musikcube.remote.ui.shared.extension.hideKeyboard import io.casey.musikcube.remote.ui.shared.extension.showErrorSnackbar @@ -80,8 +81,8 @@ class ItemContextMenuMixin(private val activity: AppCompatActivity, override fun onActivityResult(request: Int, result: Int, data: Intent?) { if (pendingCode == request) { if (result == Activity.RESULT_OK && data != null) { - val playlistId = data.getLongExtra(CategoryBrowseActivity.EXTRA_ID, -1L) - val playlistName = data.getStringExtra(CategoryBrowseActivity.EXTRA_NAME) + val playlistId = data.getLongExtra(Category.Extra.ID, -1L) + val playlistName = data.getStringExtra(Category.Extra.NAME) if (playlistId != -1L) { completion?.invoke(playlistId, playlistName) } diff --git a/src/musikdroid/build.gradle b/src/musikdroid/build.gradle index 4076abba1..d2f922230 100644 --- a/src/musikdroid/build.gradle +++ b/src/musikdroid/build.gradle @@ -1,5 +1,5 @@ buildscript { - ext.kotlin_version = '1.3.11' + ext.kotlin_version = '1.3.21' repositories { google() @@ -7,7 +7,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.3.0' + classpath 'com.android.tools.build:gradle:3.3.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } }