Skip to content

Commit

Permalink
Merge pull request #2641 from kylecorry31/tool-summaries
Browse files Browse the repository at this point in the history
Add tool summaries support
  • Loading branch information
kylecorry31 authored Nov 9, 2024
2 parents 23fb4a4 + 86ee3aa commit 20f53d8
Show file tree
Hide file tree
Showing 13 changed files with 292 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.kylecorry.trail_sense.tools.tools.infrastructure.ToolQuickAction
import com.kylecorry.trail_sense.tools.tools.infrastructure.ToolRegistration
import com.kylecorry.trail_sense.tools.tools.infrastructure.Tools
import com.kylecorry.trail_sense.tools.tools.infrastructure.diagnostics.ToolDiagnostic
import com.kylecorry.trail_sense.tools.tools.quickactions.QuickActionToolSummaries

object SettingsToolRegistration : ToolRegistration {
override fun getTool(context: Context): Tool {
Expand Down Expand Up @@ -63,6 +64,11 @@ object SettingsToolRegistration : ToolRegistration {
Tools.QUICK_ACTION_SETTINGS,
context.getString(R.string.settings),
::QuickActionSettings
),
ToolQuickAction(
Tools.QUICK_ACTION_TOOL_SUMMARIES,
context.getString(R.string.tool_summaries),
::QuickActionToolSummaries
)
),
services = listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.kylecorry.trail_sense.shared.debugging.isDebug
import com.kylecorry.trail_sense.tools.tools.infrastructure.Tool
import com.kylecorry.trail_sense.tools.tools.infrastructure.ToolCategory
import com.kylecorry.trail_sense.tools.tools.infrastructure.ToolRegistration
import com.kylecorry.trail_sense.tools.tools.infrastructure.ToolSummary
import com.kylecorry.trail_sense.tools.tools.infrastructure.ToolSummarySize
import com.kylecorry.trail_sense.tools.tools.infrastructure.Tools

object ExperimentationToolRegistration : ToolRegistration {
Expand All @@ -17,7 +19,21 @@ object ExperimentationToolRegistration : ToolRegistration {
R.id.experimentationFragment,
ToolCategory.Other,
isExperimental = true,
isAvailable = { isDebug() }
isAvailable = { isDebug() },
summaries = listOf(
ToolSummary(
"experimentation-summary",
"Experimentation 1",
ToolSummarySize.Half,
create = ::ExperimentationToolSummaryView
),
ToolSummary(
"experimentation-summary-2",
"Experimentation 2",
ToolSummarySize.Full,
create = ::ExperimentationToolSummaryView
)
)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.kylecorry.trail_sense.tools.experimentation

import android.widget.FrameLayout
import androidx.fragment.app.Fragment
import com.kylecorry.trail_sense.shared.views.Views
import com.kylecorry.trail_sense.tools.tools.ui.ToolSummaryView

class ExperimentationToolSummaryView(root: FrameLayout, fragment: Fragment) : ToolSummaryView(
root,
fragment
) {

override fun onCreate() {
super.onCreate()
val textView = Views.text(context, "This is a test")
root.addView(textView)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ data class Tool(
val isExperimental: Boolean = false,
@IdRes val settingsNavAction: Int? = null,
val quickActions: List<ToolQuickAction> = emptyList(),
val summaries: List<ToolSummary> = emptyList(),
val additionalNavigationIds: List<Int> = emptyList(),
val volumeActions: List<ToolVolumeAction> = emptyList(),
val tiles: List<String> = emptyList(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.kylecorry.trail_sense.tools.tools.infrastructure

import android.widget.FrameLayout
import androidx.fragment.app.Fragment
import com.kylecorry.trail_sense.tools.tools.ui.ToolSummaryView

data class ToolSummary(
val id: String,
val name: String,
val size: ToolSummarySize = ToolSummarySize.Full,
val create: (root: FrameLayout, fragment: Fragment) -> ToolSummaryView
)

enum class ToolSummarySize {
Half,
Full
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ object Tools {
const val QUICK_ACTION_WHISTLE = 8
const val QUICK_ACTION_WHITE_NOISE = 9
const val QUICK_ACTION_LOW_POWER_MODE = 10
const val QUICK_ACTION_SUNRISE_ALERT = 25
const val QUICK_ACTION_SUNSET_ALERT = 13
const val QUICK_ACTION_NIGHT_MODE = 14
const val QUICK_ACTION_BACKTRACK = 15
Expand All @@ -239,4 +238,6 @@ object Tools {
const val QUICK_ACTION_OPEN_PHOTO_MAP = 22
const val QUICK_ACTION_USER_GUIDE = 23
const val QUICK_ACTION_SETTINGS = 24
const val QUICK_ACTION_SUNRISE_ALERT = 25
const val QUICK_ACTION_TOOL_SUMMARIES = 26
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,20 @@ class MainActivityQuickActionBinder(
val activeTools = tools
.filter { it.isOpen(currentDestination) || it.settingsNavAction == currentDestination }

val activeToolQuickActions = activeTools.flatMap { it.quickActions }.map { it.id }.sorted()

val recommended = activeToolQuickActions + listOf(
val alwaysRecommended = listOf(
Tools.QUICK_ACTION_USER_GUIDE,
Tools.QUICK_ACTION_SETTINGS
Tools.QUICK_ACTION_SETTINGS,
Tools.QUICK_ACTION_TOOL_SUMMARIES
)

val activeToolQuickActions = activeTools
.flatMap { it.quickActions }
.map { it.id }
.filterNot { it in alwaysRecommended }
.sorted()

val recommended = activeToolQuickActions + alwaysRecommended

val factory = QuickActionFactory()

recommended.distinct().forEach {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.kylecorry.trail_sense.tools.tools.quickactions

import android.widget.ImageButton
import androidx.fragment.app.Fragment
import com.kylecorry.andromeda.fragments.show
import com.kylecorry.trail_sense.R
import com.kylecorry.trail_sense.shared.QuickActionButton
import com.kylecorry.trail_sense.tools.tools.ui.ToolSummaryViewBottomSheet

class QuickActionToolSummaries(button: ImageButton, fragment: Fragment) : QuickActionButton(
button,
fragment
) {

private var sheet: ToolSummaryViewBottomSheet? = null

override fun onCreate() {
super.onCreate()
setIcon(R.drawable.ic_summary)
}

override fun onClick() {
super.onClick()
sheet?.dismiss()
sheet = ToolSummaryViewBottomSheet()
sheet?.show(fragment)
}

override fun onDestroy() {
super.onDestroy()
sheet?.dismiss()
sheet = null
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.kylecorry.trail_sense.tools.tools.ui

import android.content.Context
import android.view.View
import android.widget.FrameLayout
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner

abstract class ToolSummaryView(
protected val root: FrameLayout,
protected val fragment: Fragment
) {
protected val context: Context by lazy { fragment.requireContext() }

private val observer = LifecycleEventObserver { _, event ->
when (event) {
Lifecycle.Event.ON_CREATE -> onCreate()
Lifecycle.Event.ON_RESUME -> onResume()
Lifecycle.Event.ON_PAUSE -> onPause()
Lifecycle.Event.ON_DESTROY -> onDestroy()
else -> {
// Do nothing
}
}
}

private val attachStateChangeListener = object : View.OnAttachStateChangeListener {
override fun onViewAttachedToWindow(v: View) {
// Do nothing
}

override fun onViewDetachedFromWindow(v: View) {
onDestroy()
}
}

fun bind(lifecycleOwner: LifecycleOwner) {
lifecycleOwner.lifecycle.addObserver(observer)
}

fun unbind(lifecycleOwner: LifecycleOwner) {
lifecycleOwner.lifecycle.removeObserver(observer)
}

open fun onCreate() {
root.removeAllViews()
root.isVisible = true
root.addOnAttachStateChangeListener(attachStateChangeListener)
}

open fun onResume() {
// Do nothing
}

open fun onPause() {
// Do nothing
}

open fun onDestroy() {
root.removeOnAttachStateChangeListener(attachStateChangeListener)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.kylecorry.trail_sense.tools.tools.ui

import android.content.res.ColorStateList
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.core.view.setPadding
import com.google.android.flexbox.FlexboxLayout
import com.kylecorry.andromeda.core.system.Resources
import com.kylecorry.andromeda.fragments.BoundBottomSheetDialogFragment
import com.kylecorry.trail_sense.R
import com.kylecorry.trail_sense.databinding.FragmentToolSummarySheetBinding
import com.kylecorry.trail_sense.tools.tools.infrastructure.ToolSummarySize
import com.kylecorry.trail_sense.tools.tools.infrastructure.Tools

class ToolSummaryViewBottomSheet :
BoundBottomSheetDialogFragment<FragmentToolSummarySheetBinding>() {
override fun generateBinding(
layoutInflater: LayoutInflater,
container: ViewGroup?
): FragmentToolSummarySheetBinding {
return FragmentToolSummarySheetBinding.inflate(layoutInflater, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

// TODO: Filter to what the user selected
val summaries = Tools.getTools(requireContext())
.flatMap { it.summaries }
.sortedByDescending { it.size.ordinal }

binding.summaries.removeAllViews()

val summaryHeight = Resources.dp(requireContext(), 150f).toInt()
val summaryGap = Resources.dp(requireContext(), 4f).toInt()
val summaryElevation = Resources.dp(requireContext(), 4f)
val summaryBackgroundColor = Resources.androidBackgroundColorSecondary(requireContext())
val summaryPadding = Resources.dp(requireContext(), 8f).toInt()

// For each summary, create a linear layout with the title and the summary view
summaries.forEach { summary ->
// The root of the summary
val root = FrameLayout(requireContext())
root.layoutParams = FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT
)
root.setPadding(summaryPadding)
root.setBackgroundResource(R.drawable.rounded_rectangle)
root.backgroundTintList = ColorStateList.valueOf(summaryBackgroundColor)
root.elevation = summaryElevation

// The wrapper which allows for a gap between the summaries
val layout = FrameLayout(requireContext())
layout.addView(root)
layout.layoutParams = FlexboxLayout.LayoutParams(
FlexboxLayout.LayoutParams.MATCH_PARENT,
summaryHeight
).apply {
flexBasisPercent = when (summary.size) {
ToolSummarySize.Full -> 1f
ToolSummarySize.Half -> 0.5f
}
}
layout.setPadding(summaryGap)

binding.summaries.addView(layout)

val summaryView = summary.create(root, this)
summaryView.bind(viewLifecycleOwner)
}

}
}
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_summary.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!-- drawable/post_outline.xml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#ffffff"
android:pathData="M19 5V19H5V5H19M21 3H3V21H21V3M17 17H7V16H17V17M17 15H7V14H17V15M17 12H7V7H17V12Z" />
</vector>
31 changes: 31 additions & 0 deletions app/src/main/res/layout/fragment_tool_summary_sheet.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.kylecorry.andromeda.views.toolbar.Toolbar
android:id="@+id/tool_summaries_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:showSubtitle="false"
app:title="@string/tool_summaries" />

<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">

<com.google.android.flexbox.FlexboxLayout
android:id="@+id/summaries"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:alignItems="center"
app:flexWrap="wrap"
app:justifyContent="flex_start">

</com.google.android.flexbox.FlexboxLayout>
</androidx.core.widget.NestedScrollView>

</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1498,4 +1498,5 @@
<string name="unable_to_create_an_automatic_backup">Unable to create an automatic backup.</string>
<string name="automatic_backup_invalid_directory">Invalid directory</string>
<string name="automatic_backup_invalid_directory_resolution">Turn automatic backup off and then turn it back on to select a new directory.</string>
<string name="tool_summaries">Tool summaries</string>
</resources>

0 comments on commit 20f53d8

Please sign in to comment.