Skip to content

Commit

Permalink
Move collectLatestWhileStarted to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanLongstaff committed Jan 9, 2025
1 parent 7f9a146 commit 730a893
Show file tree
Hide file tree
Showing 27 changed files with 45 additions and 41 deletions.
17 changes: 1 addition & 16 deletions app/src/main/kotlin/artemis/agent/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import artemis.agent.UserSettingsSerializer.userSettings
import artemis.agent.databinding.ActivityMainBinding
import artemis.agent.game.GameFragment
Expand All @@ -41,6 +37,7 @@ import artemis.agent.help.HelpFragment
import artemis.agent.setup.ConnectFragment
import artemis.agent.setup.SetupFragment
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted
import com.google.android.play.core.review.ReviewManager
import com.google.android.play.core.review.ReviewManagerFactory
import com.google.firebase.crashlytics.FirebaseCrashlytics
Expand All @@ -49,10 +46,7 @@ import com.walkertribe.ian.iface.DisconnectCause
import com.walkertribe.ian.protocol.core.comm.CommsIncomingPacket
import com.walkertribe.ian.util.Version
import java.io.FileNotFoundException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

/** The main application activity. */
Expand Down Expand Up @@ -795,12 +789,3 @@ class MainActivity : AppCompatActivity() {
)
}
}

fun <T> LifecycleOwner.collectLatestWhileStarted(
flow: Flow<T>,
block: suspend CoroutineScope.(T) -> Unit,
) {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) { flow.collectLatest { this.block(it) } }
}
}
2 changes: 1 addition & 1 deletion app/src/main/kotlin/artemis/agent/game/GameFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import artemis.agent.AgentViewModel
import artemis.agent.R
import artemis.agent.collectLatestWhileStarted
import artemis.agent.databinding.GameFragmentBinding
import artemis.agent.databinding.SelectorEntryBinding
import artemis.agent.databinding.SelectorPopupBinding
Expand All @@ -31,6 +30,7 @@ import artemis.agent.game.route.RouteObjective
import artemis.agent.game.stations.StationsFragment
import artemis.agent.help.HelpFragment
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted
import com.walkertribe.ian.enums.AlertStatus
import com.walkertribe.ian.enums.OrdnanceType
import com.walkertribe.ian.protocol.core.comm.ToggleRedAlertPacket
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import artemis.agent.AgentViewModel
import artemis.agent.R
import artemis.agent.collectLatestWhileStarted
import artemis.agent.databinding.AlliesEntryBinding
import artemis.agent.databinding.AlliesFragmentBinding
import artemis.agent.databinding.fragmentViewBinding
import artemis.agent.game.ObjectEntry.Ally
import artemis.agent.generic.GenericDataAdapter
import artemis.agent.generic.GenericDataEntry
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted
import com.walkertribe.ian.enums.GoDefend
import com.walkertribe.ian.enums.OtherMessage
import com.walkertribe.ian.protocol.core.comm.CommsOutgoingPacket
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import artemis.agent.AgentViewModel
import artemis.agent.R
import artemis.agent.collectLatestWhileStarted
import artemis.agent.databinding.BiomechEntryBinding
import artemis.agent.databinding.BiomechsFragmentBinding
import artemis.agent.databinding.fragmentViewBinding
import artemis.agent.util.collectLatestWhileStarted

class BiomechsFragment : Fragment(R.layout.biomechs_fragment) {
private val viewModel: AgentViewModel by activityViewModels()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import artemis.agent.AgentViewModel
import artemis.agent.R
import artemis.agent.collectLatestWhileStarted
import artemis.agent.databinding.EnemiesEntryBinding
import artemis.agent.databinding.EnemiesFragmentBinding
import artemis.agent.databinding.TauntEntryBinding
import artemis.agent.databinding.fragmentViewBinding
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted
import com.walkertribe.ian.enums.EnemyMessage
import com.walkertribe.ian.protocol.core.comm.CommsOutgoingPacket
import com.walkertribe.ian.vesseldata.Taunt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import artemis.agent.AgentViewModel
import artemis.agent.R
import artemis.agent.collectLatestWhileStarted
import artemis.agent.databinding.AudioEntryBinding
import artemis.agent.databinding.MiscFragmentBinding
import artemis.agent.databinding.fragmentViewBinding
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import androidx.recyclerview.widget.RecyclerView
import artemis.agent.AgentViewModel
import artemis.agent.AgentViewModel.Companion.formatString
import artemis.agent.R
import artemis.agent.collectLatestWhileStarted
import artemis.agent.databinding.CompletedMissionsEntryBinding
import artemis.agent.databinding.MissionsEntryBinding
import artemis.agent.databinding.MissionsFragmentBinding
import artemis.agent.databinding.fragmentViewBinding
import artemis.agent.game.ObjectEntry
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted
import kotlin.math.sign
import kotlin.time.Duration.Companion.milliseconds
import kotlinx.coroutines.flow.Flow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import artemis.agent.AgentViewModel
import artemis.agent.R
import artemis.agent.collectLatestWhileStarted
import artemis.agent.databinding.RouteEntryBinding
import artemis.agent.databinding.RouteFragmentBinding
import artemis.agent.databinding.SelectorPopupBinding
Expand All @@ -24,6 +23,7 @@ import artemis.agent.game.ObjectEntry
import artemis.agent.game.stations.StationsFragment
import artemis.agent.generic.GenericDataViewHolder
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted
import com.walkertribe.ian.enums.BaseMessage
import com.walkertribe.ian.enums.OrdnanceType
import com.walkertribe.ian.protocol.core.comm.CommsOutgoingPacket
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import artemis.agent.AgentViewModel
import artemis.agent.R
import artemis.agent.collectLatestWhileStarted
import artemis.agent.databinding.SimpleListViewBinding
import artemis.agent.databinding.fragmentViewBinding
import artemis.agent.generic.GenericDataAdapter
import artemis.agent.generic.GenericDataEntry
import artemis.agent.util.collectLatestWhileStarted
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import artemis.agent.AgentViewModel
import artemis.agent.R
import artemis.agent.collectLatestWhileStarted
import artemis.agent.databinding.EnemyStationEntryBinding
import artemis.agent.databinding.EnemyStationsFragmentBinding
import artemis.agent.databinding.fragmentViewBinding
import artemis.agent.game.ObjectEntry.Station
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted
import com.walkertribe.ian.enums.BaseMessage
import com.walkertribe.ian.protocol.core.comm.CommsOutgoingPacket

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import artemis.agent.AgentViewModel
import artemis.agent.R
import artemis.agent.collectLatestWhileStarted
import artemis.agent.databinding.SelectorEntryBinding
import artemis.agent.databinding.SelectorPopupBinding
import artemis.agent.databinding.StationEntryBinding
Expand All @@ -28,6 +27,7 @@ import artemis.agent.game.ObjectEntry.Station
import artemis.agent.game.route.RouteObjective
import artemis.agent.generic.GenericDataViewHolder
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted
import com.walkertribe.ian.enums.BaseMessage
import com.walkertribe.ian.enums.OrdnanceType
import com.walkertribe.ian.protocol.core.comm.CommsOutgoingPacket
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import androidx.fragment.app.activityViewModels
import androidx.fragment.app.commit
import artemis.agent.AgentViewModel
import artemis.agent.R
import artemis.agent.collectLatestWhileStarted
import artemis.agent.databinding.StationsFragmentBinding
import artemis.agent.databinding.fragmentViewBinding
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted

class StationsFragment : Fragment(R.layout.stations_fragment) {
private val viewModel: AgentViewModel by activityViewModels()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/kotlin/artemis/agent/help/HelpFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import artemis.agent.AgentViewModel
import artemis.agent.R
import artemis.agent.collectLatestWhileStarted
import artemis.agent.databinding.HelpFragmentBinding
import artemis.agent.databinding.fragmentViewBinding
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted

class HelpFragment : Fragment(R.layout.help_fragment) {
private val viewModel: AgentViewModel by activityViewModels()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/kotlin/artemis/agent/setup/ConnectFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import androidx.fragment.app.activityViewModels
import artemis.agent.AgentViewModel
import artemis.agent.R
import artemis.agent.UserSettingsSerializer.userSettings
import artemis.agent.collectLatestWhileStarted
import artemis.agent.databinding.ConnectFragmentBinding
import artemis.agent.databinding.fragmentViewBinding
import artemis.agent.generic.GenericDataAdapter
import artemis.agent.generic.GenericDataEntry
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted
import com.walkertribe.ian.protocol.udp.PrivateNetworkType
import dev.tmapps.konnection.Konnection
import dev.tmapps.konnection.NetworkConnection
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/kotlin/artemis/agent/setup/SetupFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import androidx.fragment.app.activityViewModels
import androidx.fragment.app.commit
import artemis.agent.AgentViewModel
import artemis.agent.R
import artemis.agent.collectLatestWhileStarted
import artemis.agent.databinding.SetupFragmentBinding
import artemis.agent.databinding.fragmentViewBinding
import artemis.agent.help.HelpFragment
import artemis.agent.setup.settings.SettingsFragment
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted

class SetupFragment : Fragment(R.layout.setup_fragment) {
private val viewModel: AgentViewModel by activityViewModels()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/kotlin/artemis/agent/setup/ShipsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import artemis.agent.AgentViewModel
import artemis.agent.R
import artemis.agent.collectLatestWhileStarted
import artemis.agent.databinding.ShipEntryBinding
import artemis.agent.databinding.ShipsFragmentBinding
import artemis.agent.databinding.fragmentViewBinding
import artemis.agent.util.collectLatestWhileStarted
import com.walkertribe.ian.protocol.core.setup.Ship

class ShipsFragment : Fragment(R.layout.ships_fragment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import artemis.agent.AgentViewModel
import artemis.agent.R
import artemis.agent.UserSettingsKt
import artemis.agent.UserSettingsSerializer.userSettings
import artemis.agent.collectLatestWhileStarted
import artemis.agent.copy
import artemis.agent.databinding.SettingsAlliesBinding
import artemis.agent.databinding.fragmentViewBinding
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted
import kotlinx.coroutines.launch

class AllySettingsFragment : Fragment(R.layout.settings_allies) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import artemis.agent.AgentViewModel
import artemis.agent.R
import artemis.agent.UserSettingsKt
import artemis.agent.UserSettingsSerializer.userSettings
import artemis.agent.collectLatestWhileStarted
import artemis.agent.copy
import artemis.agent.databinding.SettingsBiomechsBinding
import artemis.agent.databinding.fragmentViewBinding
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted
import kotlinx.coroutines.launch

class BiomechSettingsFragment : Fragment(R.layout.settings_biomechs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import artemis.agent.AgentViewModel
import artemis.agent.AgentViewModel.Companion.formatString
import artemis.agent.R
import artemis.agent.UserSettingsSerializer.userSettings
import artemis.agent.collectLatestWhileStarted
import artemis.agent.copy
import artemis.agent.databinding.SettingsClientBinding
import artemis.agent.databinding.fragmentViewBinding
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted
import kotlinx.coroutines.launch

class ClientSettingsFragment : Fragment(R.layout.settings_client) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import androidx.lifecycle.viewModelScope
import artemis.agent.AgentViewModel
import artemis.agent.R
import artemis.agent.UserSettingsSerializer.userSettings
import artemis.agent.collectLatestWhileStarted
import artemis.agent.copy
import artemis.agent.databinding.SettingsConnectionBinding
import artemis.agent.databinding.fragmentViewBinding
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted
import kotlinx.coroutines.launch

class ConnectionSettingsFragment : Fragment(R.layout.settings_connection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import artemis.agent.AgentViewModel.Companion.formatString
import artemis.agent.R
import artemis.agent.UserSettingsKt
import artemis.agent.UserSettingsSerializer.userSettings
import artemis.agent.collectLatestWhileStarted
import artemis.agent.copy
import artemis.agent.databinding.SettingsEnemiesBinding
import artemis.agent.databinding.fragmentViewBinding
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted
import kotlinx.coroutines.launch

class EnemySettingsFragment : Fragment(R.layout.settings_enemies) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import artemis.agent.AgentViewModel
import artemis.agent.R
import artemis.agent.UserSettingsKt
import artemis.agent.UserSettingsSerializer.userSettings
import artemis.agent.collectLatestWhileStarted
import artemis.agent.copy
import artemis.agent.databinding.SettingsMissionsBinding
import artemis.agent.databinding.fragmentViewBinding
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted
import kotlinx.coroutines.launch

class MissionSettingsFragment : Fragment(R.layout.settings_missions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import artemis.agent.AgentViewModel
import artemis.agent.AgentViewModel.Companion.formatString
import artemis.agent.R
import artemis.agent.UserSettingsSerializer.userSettings
import artemis.agent.collectLatestWhileStarted
import artemis.agent.copy
import artemis.agent.databinding.SettingsPersonalBinding
import artemis.agent.databinding.fragmentViewBinding
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted
import kotlinx.coroutines.launch

class PersonalSettingsFragment : Fragment(R.layout.settings_personal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import artemis.agent.AgentViewModel.Companion.formatString
import artemis.agent.R
import artemis.agent.UserSettingsKt
import artemis.agent.UserSettingsSerializer.userSettings
import artemis.agent.collectLatestWhileStarted
import artemis.agent.copy
import artemis.agent.databinding.SettingsRoutingBinding
import artemis.agent.databinding.fragmentViewBinding
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted
import kotlin.reflect.KMutableProperty1
import kotlinx.coroutines.launch

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import artemis.agent.UserSettingsOuterClass.UserSettings
import artemis.agent.UserSettingsSerializer
import artemis.agent.UserSettingsSerializer.defaultValue
import artemis.agent.UserSettingsSerializer.userSettings
import artemis.agent.collectLatestWhileStarted
import artemis.agent.copy
import artemis.agent.databinding.SettingsFragmentBinding
import artemis.agent.databinding.fragmentViewBinding
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted
import kotlin.reflect.KMutableProperty1
import kotlinx.coroutines.launch

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import androidx.recyclerview.widget.RecyclerView
import artemis.agent.AgentViewModel
import artemis.agent.R
import artemis.agent.UserSettingsSerializer.userSettings
import artemis.agent.collectLatestWhileStarted
import artemis.agent.copy
import artemis.agent.databinding.SettingsMenuBinding
import artemis.agent.databinding.SettingsMenuEntryBinding
import artemis.agent.databinding.fragmentViewBinding
import artemis.agent.util.SoundEffect
import artemis.agent.util.collectLatestWhileStarted
import kotlinx.coroutines.launch

class SettingsMenuFragment : Fragment(R.layout.settings_menu) {
Expand Down
Loading

0 comments on commit 730a893

Please sign in to comment.