Skip to content

Commit

Permalink
Add a go to button if there is an object for the addon
Browse files Browse the repository at this point in the history
  • Loading branch information
levinli303 committed May 31, 2021
1 parent 5bd3262 commit 1adc452
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 8 deletions.
20 changes: 19 additions & 1 deletion app/src/main/java/space/celestia/mobilecelestia/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -115,7 +116,8 @@ class MainActivity : AppCompatActivity(R.layout.activity_main),
ResourceFragment.Listener,
AsyncListFragment.Listener<Any>,
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") }
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()

Expand All @@ -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)

Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ class ResourceItem(val id: String,
val item: String,
val image: String?,
val authors: List<String>?,
val publishTime: Date?): AsyncListTextItem, Serializable
val publishTime: Date?,
val objectName: String?): AsyncListTextItem, Serializable
6 changes: 6 additions & 0 deletions app/src/main/res/layout/fragment_resource_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@
android:layout_width="wrap_content"
android:layout_height="8dp"/>

<include layout="@layout/common_action_button"
android:id="@+id/button_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"/>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@
<color name="colorPrimaryDark">@color/colorThemeSecondaryLabel</color>
<color name="colorAccent">@color/colorThemeLabel</color>

<color name="colorProgressForeground">#4477AA</color>
<color name="colorProgressBackground">@color/colorThemeSecondaryLabel</color>
<color name="colorProgressForeground">@color/colorThemeBackground</color>
<color name="colorProgressBackground">#80114477</color>
</resources>

0 comments on commit 1adc452

Please sign in to comment.