From 1adc452209326be73d4236e75033f12f891e320c Mon Sep 17 00:00:00 2001 From: Levin Li Date: Mon, 31 May 2021 19:02:09 +0800 Subject: [PATCH] Add a go to button if there is an object for the addon --- .../celestia/mobilecelestia/MainActivity.kt | 20 ++++++++- .../mobilecelestia/info/InfoFragment.kt | 2 +- .../resource/ResourceItemFragment.kt | 44 +++++++++++++++++-- .../resource/model/ResourceItem.kt | 3 +- .../res/layout/fragment_resource_item.xml | 6 +++ app/src/main/res/values/colors.xml | 4 +- 6 files changed, 71 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/space/celestia/mobilecelestia/MainActivity.kt b/app/src/main/java/space/celestia/mobilecelestia/MainActivity.kt index dd2ac316..5f2ebbab 100644 --- a/app/src/main/java/space/celestia/mobilecelestia/MainActivity.kt +++ b/app/src/main/java/space/celestia/mobilecelestia/MainActivity.kt @@ -66,6 +66,7 @@ import space.celestia.mobilecelestia.loading.LoadingFragment import space.celestia.mobilecelestia.resource.AsyncListFragment import space.celestia.mobilecelestia.resource.DestinationDetailFragment import space.celestia.mobilecelestia.resource.ResourceFragment +import space.celestia.mobilecelestia.resource.ResourceItemFragment import space.celestia.mobilecelestia.resource.model.ResourceCategory import space.celestia.mobilecelestia.resource.model.ResourceItem import space.celestia.mobilecelestia.resource.model.ResourceManager @@ -115,7 +116,8 @@ class MainActivity : AppCompatActivity(R.layout.activity_main), ResourceFragment.Listener, AsyncListFragment.Listener, DestinationDetailFragment.Listener, - GoToInputFragment.Listener { + GoToInputFragment.Listener, + ResourceItemFragment.Listener { private val preferenceManager by lazy { PreferenceManager(this, "celestia") } private val settingManager by lazy { PreferenceManager(this, "celestia_setting") } @@ -924,6 +926,22 @@ class MainActivity : AppCompatActivity(R.layout.activity_main), CelestiaView.callOnRenderThread { core.charEnter(item.value) } } + override fun objectExistsWithName(name: String): Boolean { + return !core.simulation.findObject(name).isEmpty + } + + override fun onGoToObject(name: String) { + val sel = core.simulation.findObject(name) + if (sel.isEmpty) { + showAlert(CelestiaString("Object not found", "")) + return + } + CelestiaView.callOnRenderThread { + core.simulation.selection = sel + core.charEnter(CelestiaAction.GoTo.value) + } + } + override fun onBottomControlHide() { hideOverlay(true) } diff --git a/app/src/main/java/space/celestia/mobilecelestia/info/InfoFragment.kt b/app/src/main/java/space/celestia/mobilecelestia/info/InfoFragment.kt index 08831eae..3734842e 100644 --- a/app/src/main/java/space/celestia/mobilecelestia/info/InfoFragment.kt +++ b/app/src/main/java/space/celestia/mobilecelestia/info/InfoFragment.kt @@ -67,7 +67,7 @@ class InfoFragment : NavigationFragment.SubFragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? - ): View? { + ): View { recyclerView = inflater.inflate(R.layout.fragment_info_list, container, false) as RecyclerView if (embeddedInNavigation) diff --git a/app/src/main/java/space/celestia/mobilecelestia/resource/ResourceItemFragment.kt b/app/src/main/java/space/celestia/mobilecelestia/resource/ResourceItemFragment.kt index 7c6620c2..2b8b6bed 100644 --- a/app/src/main/java/space/celestia/mobilecelestia/resource/ResourceItemFragment.kt +++ b/app/src/main/java/space/celestia/mobilecelestia/resource/ResourceItemFragment.kt @@ -11,6 +11,7 @@ package space.celestia.mobilecelestia.resource +import android.content.Context import android.os.Bundle import android.view.LayoutInflater import android.view.View @@ -42,6 +43,8 @@ class ResourceItemFragment : NavigationFragment.SubFragment(), ResourceManager.L private var item: ResourceItem? = null private lateinit var progressView: ProgressView private lateinit var progressViewText: TextView + private lateinit var goToButtonContainer: View + private lateinit var goToButtonTextView: TextView private var currentState: ResourceItemState = ResourceItemState.None private var imageView: ImageView? = null @@ -50,8 +53,15 @@ class ResourceItemFragment : NavigationFragment.SubFragment(), ResourceManager.L private var authorsLabel: TextView? = null private var releaseDateLabel: TextView? = null + private var listener: Listener? = null + private val formatter = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault()) + interface Listener { + fun objectExistsWithName(name: String): Boolean + fun onGoToObject(name: String) + } + class GlideUrlCustomCacheKey(url: String, val key: String) : GlideUrl(url) { override fun getCacheKey(): String { return key @@ -95,6 +105,10 @@ class ResourceItemFragment : NavigationFragment.SubFragment(), ResourceManager.L this.imageView = image this.authorsLabel = authors this.releaseDateLabel = releaseDate + goToButtonContainer = view.findViewById(R.id.button_container) + goToButtonTextView = view.findViewById(R.id.button) + goToButtonTextView.text = CelestiaString("Go", "") + goToButtonContainer.visibility = View.GONE updateContents() updateUI() @@ -120,6 +134,20 @@ class ResourceItemFragment : NavigationFragment.SubFragment(), ResourceManager.L } } + override fun onAttach(context: Context) { + super.onAttach(context) + if (context is Listener) { + listener = context + } else { + throw RuntimeException("$context must implement ResourceItemFragment.Listener") + } + } + + override fun onDetach() { + super.onDetach() + listener = null + } + override fun onDestroy() { ResourceManager.shared.removeListener(this) @@ -225,13 +253,13 @@ class ResourceItemFragment : NavigationFragment.SubFragment(), ResourceManager.L } private fun updateUI() { - val id = item?.id ?: return + val item = this.item ?: return // Ensure we are up to date with these cases val dm = ResourceManager.shared - if (dm.isInstalled(id)) + if (dm.isInstalled(item.id)) currentState = ResourceItemState.Installed - if (dm.isDownloading(id)) + if (dm.isDownloading(item.id)) currentState = ResourceItemState.Downloading when (currentState) { @@ -247,6 +275,16 @@ class ResourceItemFragment : NavigationFragment.SubFragment(), ResourceManager.L progressViewText.text = CelestiaString("INSTALLED", "") } } + + val objectName = item.objectName + if (currentState == ResourceItemState.Installed && objectName != null && listener?.objectExistsWithName(objectName) == true) { + goToButtonContainer.visibility = View.VISIBLE + goToButtonContainer.setOnClickListener { + listener?.onGoToObject(objectName) + } + } else { + goToButtonContainer.visibility = View.GONE + } } companion object { diff --git a/app/src/main/java/space/celestia/mobilecelestia/resource/model/ResourceItem.kt b/app/src/main/java/space/celestia/mobilecelestia/resource/model/ResourceItem.kt index a52e809a..f17de2b7 100644 --- a/app/src/main/java/space/celestia/mobilecelestia/resource/model/ResourceItem.kt +++ b/app/src/main/java/space/celestia/mobilecelestia/resource/model/ResourceItem.kt @@ -21,4 +21,5 @@ class ResourceItem(val id: String, val item: String, val image: String?, val authors: List?, - val publishTime: Date?): AsyncListTextItem, Serializable \ No newline at end of file + val publishTime: Date?, + val objectName: String?): AsyncListTextItem, Serializable \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_resource_item.xml b/app/src/main/res/layout/fragment_resource_item.xml index 27a262fe..fdc5fa90 100644 --- a/app/src/main/res/layout/fragment_resource_item.xml +++ b/app/src/main/res/layout/fragment_resource_item.xml @@ -76,6 +76,12 @@ android:layout_width="wrap_content" android:layout_height="8dp"/> + + diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index ff7adc9e..18e4e148 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -39,6 +39,6 @@ @color/colorThemeSecondaryLabel @color/colorThemeLabel - #4477AA - @color/colorThemeSecondaryLabel + @color/colorThemeBackground + #80114477 \ No newline at end of file