From 841eff56f5a26c12de09549525a461557a87b9c2 Mon Sep 17 00:00:00 2001 From: casey langen Date: Thu, 14 Feb 2019 10:04:24 -0800 Subject: [PATCH] Utilize new plumbing to dynamically push screens or start Activity instances, based on context. --- .../ui/albums/activity/AlbumBrowseActivity.kt | 1 + .../ui/albums/fragment/AlbumBrowseFragment.kt | 15 +++++++++++++-- .../ui/browse/adapter/BrowseFragmentAdapter.kt | 14 ++++++++++---- .../remote/ui/browse/fragment/BrowseFragment.kt | 2 +- .../category/fragment/CategoryBrowseFragment.kt | 11 +++++++---- .../remote/ui/shared/extension/Extensions.kt | 9 ++++++++- 6 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/albums/activity/AlbumBrowseActivity.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/albums/activity/AlbumBrowseActivity.kt index 4247774aa..787661845 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/albums/activity/AlbumBrowseActivity.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/albums/activity/AlbumBrowseActivity.kt @@ -25,6 +25,7 @@ class AlbumBrowseActivity: FragmentActivityWithTransport() { override fun createContentFragment(): BaseFragment = (intent.extras ?: Bundle()).run { AlbumBrowseFragment.create( + applicationContext, getString(Album.Extra.CATEGORY_NAME, ""), getLong(Album.Extra.CATEGORY_ID, -1)) } diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/albums/fragment/AlbumBrowseFragment.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/albums/fragment/AlbumBrowseFragment.kt index 87c2707da..5d0f7d317 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/albums/fragment/AlbumBrowseFragment.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/albums/fragment/AlbumBrowseFragment.kt @@ -1,5 +1,6 @@ package io.casey.musikcube.remote.ui.albums.fragment +import android.content.Context import android.os.Bundle import android.support.v4.view.ViewCompat import android.view.LayoutInflater @@ -16,6 +17,7 @@ import io.casey.musikcube.remote.ui.albums.constant.Album import io.casey.musikcube.remote.ui.shared.activity.IFilterable import io.casey.musikcube.remote.ui.shared.activity.ITitleProvider import io.casey.musikcube.remote.ui.shared.activity.ITransportObserver +import io.casey.musikcube.remote.ui.shared.constant.Shared import io.casey.musikcube.remote.ui.shared.extension.* import io.casey.musikcube.remote.ui.shared.fragment.BaseFragment import io.casey.musikcube.remote.ui.shared.mixin.DataProviderMixin @@ -129,7 +131,8 @@ class AlbumBrowseFragment: BaseFragment(), IFilterable, ITitleProvider, ITranspo appCompatActivity, Metadata.Category.ALBUM, album.id, - album.value))) + album.value)) + .pushTo(pushContainerId)) false -> startActivity( TrackListActivity.getStartIntent( @@ -147,11 +150,19 @@ class AlbumBrowseFragment: BaseFragment(), IFilterable, ITitleProvider, ITranspo companion object { const val TAG = "AlbumBrowseFragment" - fun create(categoryName: String = "", categoryId: Long = -1L): AlbumBrowseFragment = + fun create(context: Context, + categoryName: String = "", + categoryId: Long = -1L, + categoryValue: String = ""): AlbumBrowseFragment = AlbumBrowseFragment().apply { arguments = Bundle().apply { putString(Album.Extra.CATEGORY_NAME, categoryName) putLong(Album.Extra.CATEGORY_ID, categoryId) + if (categoryValue.isNotBlank()) { + putString( + Shared.Extra.TITLE_OVERRIDE, + context.getString(R.string.albums_by_title, categoryValue)) + } } } } diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/browse/adapter/BrowseFragmentAdapter.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/browse/adapter/BrowseFragmentAdapter.kt index 4e547f2dc..2b9220756 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/browse/adapter/BrowseFragmentAdapter.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/browse/adapter/BrowseFragmentAdapter.kt @@ -13,8 +13,12 @@ import io.casey.musikcube.remote.ui.shared.activity.IFilterable import io.casey.musikcube.remote.ui.tracks.fragment.TrackListFragment import io.casey.musikcube.remote.service.playback.impl.remote.Metadata import io.casey.musikcube.remote.ui.shared.activity.ITransportObserver +import io.casey.musikcube.remote.ui.shared.extension.pushTo +import io.casey.musikcube.remote.ui.shared.fragment.BaseFragment -class BrowseFragmentAdapter(private val context: Context, fm: FragmentManager): FragmentPagerAdapter(fm) { +class BrowseFragmentAdapter(private val context: Context, + fm: FragmentManager, + private val containerId: Int = -1): FragmentPagerAdapter(fm) { private val fragments = mutableMapOf() var filter = "" @@ -41,15 +45,17 @@ class BrowseFragmentAdapter(private val context: Context, fm: FragmentManager): else -> 0 } - override fun getItem(index: Int): Fragment = - when (index) { + override fun getItem(index: Int): Fragment { + val fragment: BaseFragment = when (index) { 0 -> CategoryBrowseFragment.create( CategoryBrowseFragment.arguments(context, Metadata.Category.ALBUM_ARTIST)) - 1 -> AlbumBrowseFragment.create() + 1 -> AlbumBrowseFragment.create(context) 2 -> TrackListFragment.create() else -> CategoryBrowseFragment.create( CategoryBrowseFragment.arguments(Metadata.Category.PLAYLISTS, NavigationType.Tracks)) } + return fragment.pushTo(this.containerId) + } override fun getPageTitle(position: Int): CharSequence? = context.getString(when (position) { diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/browse/fragment/BrowseFragment.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/browse/fragment/BrowseFragment.kt index 77a354d2e..2aa2210dc 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/browse/fragment/BrowseFragment.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/browse/fragment/BrowseFragment.kt @@ -26,7 +26,7 @@ class BrowseFragment: BaseFragment(), ITransportObserver, IFilterable, ITitlePro override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? = inflater.inflate(R.layout.browse_fragment, container, false).apply { - adapter = BrowseFragmentAdapter(appCompatActivity, childFragmentManager) + adapter = BrowseFragmentAdapter(appCompatActivity, childFragmentManager, R.id.content_container) val pager = findViewById(R.id.view_pager) pager.adapter = adapter diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/category/fragment/CategoryBrowseFragment.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/category/fragment/CategoryBrowseFragment.kt index d5a6cfd1d..630812d5f 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/category/fragment/CategoryBrowseFragment.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/category/fragment/CategoryBrowseFragment.kt @@ -193,12 +193,14 @@ class CategoryBrowseFragment: BaseFragment(), IFilterable, ITitleProvider, ITran } private fun navigateToAlbums(entry: ICategoryValue) = - when (this.pushContainerId > 0) { + when (pushContainerId > 0) { true -> this.pushWithToolbar( - this.pushContainerId, + pushContainerId, "AlbumsBy($entry.value)", - AlbumBrowseFragment.create(entry.type, entry.id)) + AlbumBrowseFragment + .create(app, entry.type, entry.id, entry.value) + .pushTo(pushContainerId)) false -> startActivity(AlbumBrowseActivity .getStartIntent(appCompatActivity, category, entry)) @@ -212,7 +214,8 @@ class CategoryBrowseFragment: BaseFragment(), IFilterable, ITitleProvider, ITran this.pushContainerId, "TracksBy($entry.value)", TrackListFragment.create(TrackListFragment - .arguments(appCompatActivity, entry.type, entry.id))) + .arguments(appCompatActivity, entry.type, entry.id)) + .pushTo(pushContainerId)) false -> startActivity(TrackListActivity.getStartIntent( appCompatActivity, category, entry.id, entry.value)) diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/extension/Extensions.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/extension/Extensions.kt index 0f66ca35d..acbf7a8ee 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/extension/Extensions.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/extension/Extensions.kt @@ -189,7 +189,14 @@ val BaseFragment.pushContainerId: Int get() = this.extras.getInt(Shared.Extra.PUSH_CONTAINER_ID, -1) inline fun T.pushTo(containerId: Int): T { - this.extras.putInt(Shared.Extra.PUSH_CONTAINER_ID, containerId) + if (containerId > 0) { + this.extras.putInt(Shared.Extra.PUSH_CONTAINER_ID, containerId) + } + return this +} + +inline fun T.pushTo(other: BaseFragment): T { + this.pushTo(other.pushContainerId) return this }