Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.kylecorry.trail_sense.settings.infrastructure

import android.content.Context
import com.kylecorry.andromeda.preferences.FloatPreference
import com.kylecorry.trail_sense.R
import com.kylecorry.trail_sense.shared.preferences.PreferencesSubsystem

class BubbleLevelPreferences(private val context: Context) : IBubbleLevelPreferences {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this extend the PreferenceRepo as well?

(that removes the need to define cache)


private val cache by lazy { PreferencesSubsystem.getInstance(context).preferences }

override var threshold by FloatPreference(
cache,
context.getString(R.string.pref_bubble_level_threshold),
2.0f
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.kylecorry.trail_sense.settings.infrastructure

interface IBubbleLevelPreferences {
var threshold: Float
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.kylecorry.trail_sense.shared.preferences

import android.widget.NumberPicker
import androidx.preference.Preference
import com.kylecorry.andromeda.alerts.Alerts
import com.kylecorry.andromeda.core.system.Intents
import com.kylecorry.andromeda.fragments.AndromedaPreferenceFragment
import com.kylecorry.andromeda.notify.Notify
Expand Down Expand Up @@ -79,4 +81,37 @@ fun AndromedaPreferenceFragment.setupDistanceSetting(
decimalPlacesOverride ?: Units.getDecimalPlaces(current.units)
)
} ?: getString(R.string.dash)
}

fun AndromedaPreferenceFragment.setupThresholdSetting(
holderKey: String,
getValue: () -> Int,
setValue: (Int) -> Unit,
minValue: Int = 1,
maxValue: Int = 10,
formatValue: (Int) -> String = { it.toString() }
) {
val pref = preferenceManager.findPreference<Preference>(holderKey)

pref?.setOnPreferenceClickListener {
val picker = NumberPicker(requireContext())
picker.minValue = minValue
picker.maxValue = maxValue
picker.value = getValue()
picker.wrapSelectorWheel = false

Alerts.dialog(
requireContext(),
pref.title.toString(),
contentView = picker
) { cancelled ->
if (!cancelled) {
setValue(picker.value)
pref.summary = formatValue(picker.value)
}
}
true
}

pref?.summary = formatValue(getValue())
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ object BubbleLevelToolRegistration : ToolRegistration {
ToolCategory.Angles,
context.getString(R.string.tool_bubble_level_summary),
guideId = R.raw.guide_tool_bubble_level,
settingsNavAction = R.id.bubbleLevelSettingsFragment,
diagnostics = listOf(
*ToolDiagnosticFactory.tilt(context)
).distinctBy { it.id }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.kylecorry.andromeda.canvas.CanvasView
import com.kylecorry.andromeda.canvas.TextMode
import com.kylecorry.andromeda.core.system.Resources
import com.kylecorry.andromeda.core.ui.Colors.withAlpha
import com.kylecorry.trail_sense.settings.infrastructure.BubbleLevelPreferences
import com.kylecorry.trail_sense.shared.CustomUiUtils.getColorOnPrimary
import com.kylecorry.trail_sense.shared.CustomUiUtils.getPrimaryColor
import com.kylecorry.trail_sense.shared.FormatService
Expand All @@ -22,6 +23,7 @@ class BubbleLevel(context: Context?, attrs: AttributeSet? = null) : CanvasView(c
private var bubbleColor: Int = Color.BLACK
private var bubbleRadius: Float = 0f
private var backgroundColor: Int = Color.WHITE
private var levelBackgroundColor: Int = Color.WHITE
private var textColor: Int = Color.BLACK
private var textSize: Float = 0f
private var lineColor: Int = Color.BLACK
Expand All @@ -34,14 +36,17 @@ class BubbleLevel(context: Context?, attrs: AttributeSet? = null) : CanvasView(c
var yAngle: Float = 0f

private val formatter = FormatService.getInstance(this.context)
private lateinit var prefs: BubbleLevelPreferences


override fun setup() {
prefs = BubbleLevelPreferences(context)
bubbleColor = Resources.getPrimaryColor(context)
bubbleRadius = dp(16f)
bubblePadding = dp(8f)
backgroundColor =
Resources.getAndroidColorAttr(context, android.R.attr.colorBackgroundFloating)
levelBackgroundColor = Resources.getPrimaryColor(context).withAlpha(50)
textColor = Resources.getColorOnPrimary(context)
textSize = sp(14f)
lineColor = Resources.androidTextColorPrimary(context).withAlpha(127)
Expand All @@ -63,10 +68,11 @@ class BubbleLevel(context: Context?, attrs: AttributeSet? = null) : CanvasView(c

// Top bar
noStroke()
fill(backgroundColor)
fill(if (isLevel(xAngle)) levelBackgroundColor else backgroundColor)
rect(xBarLeft, xBarTop, barLength, barThickness, barRadius)

// Right bar
fill(if (isLevel(yAngle)) levelBackgroundColor else backgroundColor)
rect(
yBarLeft,
yBarTop,
Expand All @@ -76,6 +82,7 @@ class BubbleLevel(context: Context?, attrs: AttributeSet? = null) : CanvasView(c
)

// Center circle
fill(if (isLevel(xAngle) && isLevel(yAngle)) levelBackgroundColor else backgroundColor)
circle(
xBarLeft + barLength / 2f,
yBarTop + barLength / 2f,
Expand Down Expand Up @@ -135,6 +142,7 @@ class BubbleLevel(context: Context?, attrs: AttributeSet? = null) : CanvasView(c
)

// Center bubble
fill(bubbleColor)
val centerPercent = hypot(xAngle, yAngle).coerceAtMost(90f) / 90f
val centerAngle = atan2(yAngle, xAngle)

Expand All @@ -157,4 +165,8 @@ class BubbleLevel(context: Context?, attrs: AttributeSet? = null) : CanvasView(c
text(yText, yBarLeft + barThickness / 2f, yBarTop + barLength * yPercent)
}

private fun isLevel(angle: Float): Boolean {
return abs(angle) < prefs.threshold
}

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

import android.os.Bundle
import com.kylecorry.andromeda.fragments.AndromedaPreferenceFragment
import com.kylecorry.trail_sense.R
import com.kylecorry.trail_sense.settings.infrastructure.BubbleLevelPreferences
import com.kylecorry.trail_sense.shared.preferences.setupThresholdSetting

class BubbleLevelSettingsFragment : AndromedaPreferenceFragment() {

private val prefs by lazy { BubbleLevelPreferences(requireContext()) }

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.bubble_level_preferences, rootKey)

setupThresholdSetting(
getString(R.string.pref_bubble_level_threshold),
{ prefs.threshold.toInt() },
{ prefs.threshold = it.toFloat() },
minValue = 1,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to make the minimum value 0? That way if a user doesn't want it to show they can set it to 0 and it should never appear

maxValue = 10,
formatValue = { getString(R.string.degree_format, it.toFloat()) }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the FormatService be used?

Available with AppServiceRegistry.get()

)
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,11 @@
android:name="com.kylecorry.trail_sense.settings.ui.CellSignalSettingsFragment"
android:label="CellSignalSettingsFragment" />

<fragment
android:id="@+id/bubbleLevelSettingsFragment"
android:name="com.kylecorry.trail_sense.tools.level.ui.BubbleLevelSettingsFragment"
android:label="BubbleLevelSettingsFragment" />

<fragment
android:id="@+id/clockSettingsFragment"
android:name="com.kylecorry.trail_sense.settings.ui.ClockSettingsFragment"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@
<string name="navigation_nearby_category">Nearby</string>
<string name="tool_bubble_level_title">Bubble Level</string>
<string name="tool_bubble_level_summary">Determine if surfaces are flat</string>
<string name="pref_bubble_level_threshold" translatable="false">pref_bubble_level_threshold</string>
<string name="pref_bubble_level_threshold_title">Level threshold (degrees)</string>
<string name="other">Other</string>
<string name="category_food">Food</string>
<string name="category_hydration">Hydration</string>
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/res/xml/bubble_level_preferences.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">

<PreferenceCategory
app:iconSpaceReserved="false"
app:singleLineTitle="false"
app:title="@string/tool_bubble_level_title">

<Preference
app:iconSpaceReserved="false"
app:key="@string/pref_bubble_level_threshold"
app:singleLineTitle="false"
app:title="@string/pref_bubble_level_threshold_title" />

</PreferenceCategory>

</PreferenceScreen>
Loading