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
Expand Up @@ -39,6 +39,8 @@ class FlashlightSubsystem private constructor(private val context: Context) : IF
override val mode: ITopic<FlashlightMode>
get() = _mode.distinct()

override var selectedMode: FlashlightMode = FlashlightMode.Torch

override val brightnessLevels: Int
get() = (torch?.brightnessLevels ?: 1) - 1

Expand Down Expand Up @@ -116,6 +118,7 @@ class FlashlightSubsystem private constructor(private val context: Context) : IF
transitionTimer.stop()
}
_mode.publish(FlashlightMode.Off)
selectedMode = FlashlightMode.Torch
FlashlightService.stop(context)
torch?.off()
}
Expand All @@ -130,6 +133,9 @@ class FlashlightSubsystem private constructor(private val context: Context) : IF


override fun set(mode: FlashlightMode) {
if (mode != FlashlightMode.Off) {
selectedMode = mode
}
Comment on lines +136 to +138
Copy link
Copy Markdown
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 move this to within the on method so that it is wrapped by the synchronized block?

Moving it there would also allow it to be removed on line 182 as well

when (mode) {
FlashlightMode.Off -> off()
else -> on(mode)
Expand Down Expand Up @@ -173,6 +179,7 @@ class FlashlightSubsystem private constructor(private val context: Context) : IF

if (enabled && getMode() == FlashlightMode.Off) {
setBrightness(1f)
selectedMode = FlashlightMode.Torch
on(FlashlightMode.Torch, true)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.kylecorry.trail_sense.tools.flashlight.domain.FlashlightMode

interface IFlashlightSubsystem {
val mode: ITopic<FlashlightMode>
var selectedMode: FlashlightMode
val brightnessLevels: Int
fun setBrightness(brightness: Float)
fun toggle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ class FragmentToolFlashlight : BoundFragment<FragmentToolFlashlightBinding>() {
turnOn()
}

private var selectedMode = FlashlightMode.Torch

private val cache by lazy { PreferencesSubsystem.getInstance(requireContext()).preferences }
private val prefs by lazy { UserPreferences(requireContext()) }
private val formatter by lazy { FormatService.getInstance(requireContext()) }
Expand Down Expand Up @@ -107,23 +105,40 @@ class FragmentToolFlashlight : BoundFragment<FragmentToolFlashlightBinding>() {
considerShownIfCancelled = false,
) { _, agreed ->
val frequency = if (it == 10) 200 else it
selectedMode = if (agreed) {
val mode = if (agreed) {
getStrobeMode(frequency)
} else {
FlashlightMode.Torch
}
turnOn()
flashlight.set(mode)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I would like both of these flashlight.set lines in this function to be restored to turnOn - that way there's a single point where it is handling that. I think my other comment about the selectedMode bug and bringing back the selectedMode state will allow this to be function to be restored while retaining the behavior we want

}
} else {
selectedMode = when (it) {
val mode = when (it) {
11 -> FlashlightMode.Sos
else -> FlashlightMode.Torch
}
turnOn()
flashlight.set(mode)
}
}
}

private fun getDialIndex(mode: FlashlightMode): Int {
return when (mode) {
FlashlightMode.Strobe1 -> 1
FlashlightMode.Strobe2 -> 2
FlashlightMode.Strobe3 -> 3
FlashlightMode.Strobe4 -> 4
FlashlightMode.Strobe5 -> 5
FlashlightMode.Strobe6 -> 6
FlashlightMode.Strobe7 -> 7
FlashlightMode.Strobe8 -> 8
FlashlightMode.Strobe9 -> 9
FlashlightMode.Strobe200 -> 10
FlashlightMode.Sos -> 11
else -> 0
}
}

private fun getStrobeMode(frequency: Int): FlashlightMode {
return when (frequency) {
1 -> FlashlightMode.Strobe1
Expand All @@ -143,6 +158,9 @@ class FragmentToolFlashlight : BoundFragment<FragmentToolFlashlightBinding>() {
override fun onResume() {
super.onResume()
flashlightMode = flashlight.getMode()
val index = getDialIndex(flashlightMode)
binding.flashlightDial.selected = index
binding.flashlightDial.scrollToOption(index)
updateFlashlightUI()
intervalometer.interval(20)
binding.flashlightDial.areHapticsEnabled = true
Expand Down Expand Up @@ -181,16 +199,22 @@ class FragmentToolFlashlight : BoundFragment<FragmentToolFlashlightBinding>() {
}

private fun turnOn() {
flashlight.set(selectedMode)
flashlight.set(flashlight.selectedMode)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think this should maintain a local selectedMode state (like it did before), and sync that with the flashlight subsystem's mode on resume.

This technique is causing a bug in which when I change the mode and then turn off the flashlight it switches back to 0 without me leaving the page. I would like it to only default back to 0 when I come back to the page and the flashlight is off.

Potentially just the code you have in onResume will do the job, but that will need to be verified.

}

private fun turnOff() {
flashlight.set(FlashlightMode.Off)
}

private fun update() {
flashlightMode = flashlight.getMode()
updateFlashlightUI()
val newMode = flashlight.getMode()
if (newMode != flashlightMode) {
flashlightMode = newMode
updateFlashlightUI()
val index = getDialIndex(flashlightMode)
binding.flashlightDial.selected = index
binding.flashlightDial.scrollToOption(index)
}
}

private fun updateTimer() {
Expand Down
Loading