Skip to content

Commit

Permalink
Move whistle creation and tool population to background thread
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Nov 12, 2023
1 parent 209109a commit 73bc4dd
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.annotation.SuppressLint
import android.view.MotionEvent
import android.widget.ImageButton
import androidx.fragment.app.Fragment
import com.kylecorry.andromeda.core.coroutines.onDefault
import com.kylecorry.andromeda.fragments.inBackground
import com.kylecorry.andromeda.sound.ISoundPlayer
import com.kylecorry.trail_sense.R
import com.kylecorry.trail_sense.shared.CustomUiUtils
Expand All @@ -13,20 +15,26 @@ import com.kylecorry.trail_sense.tools.whistle.infrastructure.Whistle
class QuickActionWhistle(btn: ImageButton, fragment: Fragment) :
QuickActionButton(btn, fragment) {

private lateinit var whistle: ISoundPlayer
private var whistle: ISoundPlayer? = null

@SuppressLint("ClickableViewAccessibility")
override fun onCreate() {
super.onCreate()
whistle = Whistle()

fragment.inBackground {
onDefault {
whistle = Whistle()
}
}

button.setImageResource(R.drawable.ic_tool_whistle)
CustomUiUtils.setButtonState(button, false)
button.setOnTouchListener { _, event ->
if (event.action == MotionEvent.ACTION_DOWN) {
whistle.on()
whistle?.on()
CustomUiUtils.setButtonState(button, true)
} else if (event.action == MotionEvent.ACTION_UP) {
whistle.off()
whistle?.off()
CustomUiUtils.setButtonState(button, false)
}
true
Expand All @@ -40,12 +48,12 @@ class QuickActionWhistle(btn: ImageButton, fragment: Fragment) :

override fun onPause() {
super.onPause()
whistle.off()
whistle?.off()
}

override fun onDestroy() {
super.onDestroy()
whistle.release()
whistle?.release()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,18 @@ import com.kylecorry.andromeda.camera.Camera
import com.kylecorry.andromeda.core.coroutines.onDefault
import com.kylecorry.andromeda.core.coroutines.onMain
import com.kylecorry.andromeda.core.system.Resources
import com.kylecorry.andromeda.core.time.CoroutineTimer
import com.kylecorry.andromeda.fragments.BoundFragment
import com.kylecorry.andromeda.fragments.inBackground
import com.kylecorry.andromeda.fragments.observeFlow
import com.kylecorry.luna.coroutines.CoroutineQueueRunner
import com.kylecorry.sol.time.Time
import com.kylecorry.sol.units.Bearing
import com.kylecorry.sol.units.Distance
import com.kylecorry.trail_sense.R
import com.kylecorry.trail_sense.astronomy.domain.AstronomyService
import com.kylecorry.trail_sense.astronomy.ui.MoonPhaseImageMapper
import com.kylecorry.trail_sense.databinding.FragmentAugmentedRealityBinding
import com.kylecorry.trail_sense.navigation.beacons.domain.Beacon
import com.kylecorry.trail_sense.navigation.beacons.infrastructure.persistence.BeaconRepo
import com.kylecorry.trail_sense.navigation.domain.NavigationService
import com.kylecorry.trail_sense.navigation.infrastructure.Navigator
import com.kylecorry.trail_sense.shared.DistanceUtils.toRelativeDistance
import com.kylecorry.trail_sense.shared.FormatService
Expand Down Expand Up @@ -173,7 +170,7 @@ class AugmentedRealityFragment : BoundFragment<FragmentAugmentedRealityBinding>(
override fun onUpdate() {
super.onUpdate()

runInBackground {
inBackground {
fovRunner.enqueue {
if (!isBound) {
return@enqueue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ import androidx.gridlayout.widget.GridLayout
import androidx.navigation.fragment.findNavController
import com.kylecorry.andromeda.alerts.dialog
import com.kylecorry.andromeda.core.capitalizeWords
import com.kylecorry.andromeda.core.coroutines.onDefault
import com.kylecorry.andromeda.core.coroutines.onMain
import com.kylecorry.andromeda.core.system.Resources
import com.kylecorry.andromeda.core.ui.setCompoundDrawables
import com.kylecorry.andromeda.fragments.BoundFragment
import com.kylecorry.andromeda.fragments.inBackground
import com.kylecorry.andromeda.pickers.Pickers
import com.kylecorry.trail_sense.R
import com.kylecorry.trail_sense.databinding.FragmentToolsBinding
import com.kylecorry.trail_sense.quickactions.ToolsQuickActionBinder
import com.kylecorry.trail_sense.shared.CustomUiUtils
import com.kylecorry.trail_sense.shared.UserPreferences
import com.kylecorry.trail_sense.shared.colors.AppColor
import com.kylecorry.trail_sense.shared.extensions.setOnQueryTextListener
import com.kylecorry.trail_sense.tools.guide.infrastructure.UserGuideUtils
import com.kylecorry.trail_sense.tools.ui.sort.AlphabeticalToolSort
Expand Down Expand Up @@ -168,19 +170,30 @@ class ToolsFragment : BoundFragment<FragmentToolsBinding>() {
private fun populateTools(categories: List<CategorizedTools>, grid: GridLayout) {
grid.removeAllViews()

if (categories.size == 1) {
categories.first().tools.forEach {
grid.addView(createToolButton(it))
}
return
}
inBackground {
val viewsToAdd = mutableListOf<View>()

onDefault {
if (categories.size == 1) {
categories.first().tools.forEach {
viewsToAdd.add(createToolButton(it))
}
} else {
categories.forEach {
viewsToAdd.add(createToolCategoryHeader(it.categoryName))
it.tools.forEach {
viewsToAdd.add(createToolButton(it))
}
}
}
}

categories.forEach {
grid.addView(createToolCategoryHeader(it.categoryName))
it.tools.forEach {
grid.addView(createToolButton(it))
onMain {
viewsToAdd.forEach {
grid.addView(it)
}
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import com.kylecorry.andromeda.fragments.BoundFragment
import com.kylecorry.andromeda.fragments.inBackground
import com.kylecorry.andromeda.pickers.Pickers
import com.kylecorry.andromeda.sound.ISoundPlayer
import com.kylecorry.trail_sense.R
Expand All @@ -20,7 +21,7 @@ import java.time.Duration

class ToolWhistleFragment : BoundFragment<FragmentToolWhistleBinding>() {

private lateinit var whistle: ISoundPlayer
private var whistle: ISoundPlayer? = null

private val morseDurationMs = 400L

Expand All @@ -38,18 +39,18 @@ class ToolWhistleFragment : BoundFragment<FragmentToolWhistleBinding>() {
)
)

private val signalWhistle by lazy { SignalPlayer(whistle.asSignal()) }
private var signalWhistle: SignalPlayer? = null

@SuppressLint("ClickableViewAccessibility")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.whistleEmergencyBtn.setOnClickListener {
state = if (state == WhistleState.Emergency) {
signalWhistle.cancel()
signalWhistle?.cancel()
WhistleState.Off
} else {
whistle.off()
signalWhistle.play(emergencySignal, true)
whistle?.off()
signalWhistle?.play(emergencySignal, true)
WhistleState.Emergency
}
binding.whistleEmergencyBtn.setText(getText(R.string.help).toString())
Expand All @@ -64,19 +65,19 @@ class ToolWhistleFragment : BoundFragment<FragmentToolWhistleBinding>() {
options
) {
if (it != null) {
whistle.off()
whistle?.off()
when (it) {
0 -> signalWhistle.play(
0 -> signalWhistle?.play(
whereAreYouAndAcknowledgedSignal, false
) { toggleOffInternationWhistleSignals() }
1 -> signalWhistle.play(
1 -> signalWhistle?.play(
whereAreYouAndAcknowledgedSignal, false
) { toggleOffInternationWhistleSignals() }
2 -> signalWhistle.play(
2 -> signalWhistle?.play(
comeHereSignal,
false
) { toggleOffInternationWhistleSignals() }
3 -> signalWhistle.play(emergencySignal, true)
3 -> signalWhistle?.play(emergencySignal, true)
}
binding.whistleEmergencyBtn.setText(options[it])
state = WhistleState.Emergency
Expand All @@ -88,11 +89,11 @@ class ToolWhistleFragment : BoundFragment<FragmentToolWhistleBinding>() {

binding.whistleSosBtn.setOnClickListener {
state = if (state == WhistleState.Sos) {
signalWhistle.cancel()
signalWhistle?.cancel()
WhistleState.Off
} else {
whistle.off()
signalWhistle.play(sosSignal, true)
whistle?.off()
signalWhistle?.play(sosSignal, true)
WhistleState.Sos
}
updateUI()
Expand All @@ -101,11 +102,11 @@ class ToolWhistleFragment : BoundFragment<FragmentToolWhistleBinding>() {

binding.whistleBtn.setOnTouchListener { _, event ->
if (event.action == MotionEvent.ACTION_DOWN) {
signalWhistle.cancel()
whistle.on()
signalWhistle?.cancel()
whistle?.on()
state = WhistleState.On
} else if (event.action == MotionEvent.ACTION_UP) {
whistle.off()
whistle?.off()
state = WhistleState.Off
}
updateUI()
Expand All @@ -115,18 +116,23 @@ class ToolWhistleFragment : BoundFragment<FragmentToolWhistleBinding>() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
whistle = Whistle()
inBackground {
whistle = Whistle()
whistle?.let {
signalWhistle = SignalPlayer(it.asSignal())
}
}
}

override fun onDestroy() {
super.onDestroy()
whistle.release()
whistle?.release()
}

override fun onPause() {
super.onPause()
whistle.off()
signalWhistle.cancel()
whistle?.off()
signalWhistle?.cancel()
}


Expand All @@ -138,7 +144,7 @@ class ToolWhistleFragment : BoundFragment<FragmentToolWhistleBinding>() {

private fun toggleOffInternationWhistleSignals() {
state = WhistleState.Off
signalWhistle.cancel()
signalWhistle?.cancel()
binding.whistleEmergencyBtn.setText(getText(R.string.help).toString())
updateUI()
}
Expand Down

0 comments on commit 73bc4dd

Please sign in to comment.