Skip to content

Commit

Permalink
Make HelpFragment help topics static and lazy-loading
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanLongstaff committed Jan 29, 2025
1 parent 01f42a7 commit ff7e503
Showing 1 changed file with 76 additions and 103 deletions.
179 changes: 76 additions & 103 deletions app/src/main/kotlin/artemis/agent/help/HelpFragment.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package artemis.agent.help

import android.content.Context
import android.content.res.Resources
import android.os.Bundle
import android.util.TypedValue
import android.view.View
Expand All @@ -9,7 +10,9 @@ import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.addCallback
import androidx.annotation.ArrayRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.GridLayoutManager
Expand All @@ -28,105 +31,6 @@ class HelpFragment : Fragment(R.layout.help_fragment) {
private val currentHelpTopicIndex: Int
get() = viewModel.helpTopicIndex.value

private val helpTopics: List<HelpTopic> by lazy {
binding.root.resources.run {
listOf(
HelpTopic(
getString(R.string.help_topics_getting_started),
getStringArray(R.array.help_contents_getting_started)
.map { HelpTopicContent.Text(it) }
.toMutableList<HelpTopicContent>()
.apply {
addImages(
INDEX_PREVIEW_CONNECT to R.drawable.connect_preview,
INDEX_PREVIEW_SHIP to R.drawable.ship_entry_preview,
)
},
),
HelpTopic(
getString(R.string.help_topics_basics),
getStringArray(R.array.help_contents_basics)
.map { HelpTopicContent.Text(it) }
.toMutableList<HelpTopicContent>()
.apply { addImages(1 to R.drawable.game_header_preview) },
),
HelpTopic(
getString(R.string.help_topics_stations),
getStringArray(R.array.help_contents_stations)
.map { HelpTopicContent.Text(it) }
.toMutableList<HelpTopicContent>()
.apply { addImages(1 to R.drawable.station_entry_preview) },
),
HelpTopic(
getString(R.string.help_topics_allies),
getStringArray(R.array.help_contents_allies)
.map { HelpTopicContent.Text(it) }
.toMutableList<HelpTopicContent>()
.apply { addImages(1 to R.drawable.ally_entry_preview) },
),
HelpTopic(
getString(R.string.help_topics_missions),
getStringArray(R.array.help_contents_missions)
.map { HelpTopicContent.Text(it) }
.toMutableList<HelpTopicContent>()
.apply {
addImages(
INDEX_PREVIEW_COMMS_MESSAGE to R.drawable.comms_message,
INDEX_PREVIEW_MISSION to R.drawable.mission_entry_preview,
)
},
),
HelpTopic(
getString(R.string.help_topics_routing),
getStringArray(R.array.help_contents_routing)
.map { HelpTopicContent.Text(it) }
.toMutableList<HelpTopicContent>()
.apply {
addImages(
INDEX_PREVIEW_ROUTE_TASKS to R.drawable.route_tasks_preview,
INDEX_PREVIEW_ROUTE_SUPPLIES to R.drawable.route_supplies_preview,
)
},
),
HelpTopic(
getString(R.string.help_topics_enemies),
getStringArray(R.array.help_contents_enemies)
.map { HelpTopicContent.Text(it) }
.toMutableList<HelpTopicContent>()
.apply {
addImages(
INDEX_PREVIEW_ENEMY to R.drawable.enemy_entry_preview,
INDEX_PREVIEW_INTEL to R.drawable.enemy_intel_preview,
)
},
),
HelpTopic(
getString(R.string.help_topics_biomechs),
getStringArray(R.array.help_contents_biomechs)
.map { HelpTopicContent.Text(it) }
.toMutableList<HelpTopicContent>()
.apply { addImages(1 to R.drawable.biomech_entry_preview) },
),
HelpTopic(
getString(R.string.help_topics_notifications),
getStringArray(R.array.help_contents_notifications).map {
HelpTopicContent.Text(it)
},
),
HelpTopic(
getString(R.string.help_topics_about),
getStringArray(R.array.help_contents_about)
.map { HelpTopicContent.Text(it) }
.toMutableList<HelpTopicContent>()
.apply {
add(0, HelpTopicContent.Text(getString(R.string.app_version)))
add(HelpTopicContent.Image(R.drawable.ic_launcher_foreground))
},
),
)
}
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.settingsPage.value = null
Expand All @@ -140,6 +44,7 @@ class HelpFragment : Fragment(R.layout.help_fragment) {

val layoutManager = GridLayoutManager(binding.root.context, 2)
val adapter = HelpTopicsAdapter()
val resources = view.resources

viewLifecycleOwner.collectLatestWhileStarted(viewModel.helpTopicIndex) { index ->
val headerVisibility =
Expand All @@ -148,8 +53,9 @@ class HelpFragment : Fragment(R.layout.help_fragment) {
onBackPressedCallback.isEnabled = false
View.GONE
} else {
helpTopics[index].initContents(resources)
layoutManager.spanCount = 1
binding.helpTopicTitle.text = helpTopics[index].buttonLabel
binding.helpTopicTitle.setText(helpTopics[index].buttonLabel)
onBackPressedCallback.isEnabled = true
View.VISIBLE
}
Expand All @@ -174,9 +80,25 @@ class HelpFragment : Fragment(R.layout.help_fragment) {
val viewType: Int
}

private class HelpTopic(val buttonLabel: String, val contents: List<HelpTopicContent>) :
ViewProvider {
private class HelpTopic(
@StringRes val buttonLabel: Int,
@ArrayRes private val paragraphs: Int,
private val insert: MutableList<HelpTopicContent>.(Resources) -> Unit,
) : ViewProvider {
override val viewType: Int = MENU

var contents: List<HelpTopicContent> = listOf()
private set

fun initContents(res: Resources) {
if (contents.isEmpty()) {
contents =
res.getStringArray(paragraphs)
.map { HelpTopicContent.Text(it) }
.toMutableList<HelpTopicContent>()
.apply { insert(res) }
}
}
}

private sealed interface HelpTopicContent : ViewProvider {
Expand Down Expand Up @@ -229,7 +151,7 @@ class HelpFragment : Fragment(R.layout.help_fragment) {
when (holder) {
is HelpTopicsViewHolder.MenuButton -> {
with(holder.itemView as Button) {
text = helpTopics[position].buttonLabel
setText(helpTopics[position].buttonLabel)
setOnClickListener {
viewModel.playSound(SoundEffect.BEEP_2)
viewModel.helpTopicIndex.value = position
Expand Down Expand Up @@ -262,6 +184,57 @@ class HelpFragment : Fragment(R.layout.help_fragment) {
}

companion object {
private val helpTopics: List<HelpTopic> =
listOf(
HelpTopic(
R.string.help_topics_getting_started,
R.array.help_contents_getting_started,
) {
addImages(
INDEX_PREVIEW_CONNECT to R.drawable.connect_preview,
INDEX_PREVIEW_SHIP to R.drawable.ship_entry_preview,
)
},
HelpTopic(R.string.help_topics_basics, R.array.help_contents_basics) {
addImages(1 to R.drawable.game_header_preview)
},
HelpTopic(R.string.help_topics_stations, R.array.help_contents_stations) {
addImages(1 to R.drawable.station_entry_preview)
},
HelpTopic(R.string.help_topics_allies, R.array.help_contents_allies) {
addImages(1 to R.drawable.ally_entry_preview)
},
HelpTopic(R.string.help_topics_missions, R.array.help_contents_missions) {
addImages(
INDEX_PREVIEW_COMMS_MESSAGE to R.drawable.comms_message,
INDEX_PREVIEW_MISSION to R.drawable.mission_entry_preview,
)
},
HelpTopic(R.string.help_topics_routing, R.array.help_contents_routing) {
addImages(
INDEX_PREVIEW_ROUTE_TASKS to R.drawable.route_tasks_preview,
INDEX_PREVIEW_ROUTE_SUPPLIES to R.drawable.route_supplies_preview,
)
},
HelpTopic(R.string.help_topics_enemies, R.array.help_contents_enemies) {
addImages(
INDEX_PREVIEW_ENEMY to R.drawable.enemy_entry_preview,
INDEX_PREVIEW_INTEL to R.drawable.enemy_intel_preview,
)
},
HelpTopic(R.string.help_topics_biomechs, R.array.help_contents_biomechs) {
addImages(1 to R.drawable.biomech_entry_preview)
},
HelpTopic(
R.string.help_topics_notifications,
R.array.help_contents_notifications,
) {},
HelpTopic(R.string.help_topics_about, R.array.help_contents_about) { res ->
add(0, HelpTopicContent.Text(res.getString(R.string.app_version)))
add(HelpTopicContent.Image(R.drawable.ic_launcher_foreground))
},
)

const val MENU = -1
const val IMAGE = 0
const val TEXT = 1
Expand Down

0 comments on commit ff7e503

Please sign in to comment.