Skip to content

Commit

Permalink
derx
Browse files Browse the repository at this point in the history
  • Loading branch information
bqv committed Oct 17, 2023
1 parent 2600a74 commit 374eb86
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rxjava2.subscribeAsState
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
Expand Down Expand Up @@ -114,6 +113,7 @@ class NewAutomatchChallengeBottomSheet : BottomSheetDialogFragment(), OnChartVal
return ComposeView(requireContext()).apply {
setContent {
val state by rememberStateWithLifecycle(viewModel.state)

OnlineGoTheme {
NewAutomatchChallengeBottomSheetContent(
state = state,
Expand Down Expand Up @@ -144,7 +144,7 @@ class NewAutomatchChallengeBottomSheet : BottomSheetDialogFragment(), OnChartVal

@Composable
private fun Chart(state: AutomatchState, modifier: Modifier) {
val challenges by viewModel.challenges.subscribeAsState(initial = listOf())
val challenges = state.challenges
AndroidView(
modifier = Modifier
.fillMaxWidth()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import io.zenandroid.onlinego.data.model.ogs.Speed
import io.zenandroid.onlinego.data.repositories.SeekGraphRepository
import io.zenandroid.onlinego.data.repositories.UserSessionRepository
import io.zenandroid.onlinego.utils.addToDisposable
import io.zenandroid.onlinego.utils.recordException
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update

private const val TAG = "NewAutomatchChallengeVM"
Expand All @@ -36,7 +38,7 @@ class NewAutomatchChallengeViewModel(
private val subscriptions = CompositeDisposable()

private val prefs = PreferenceManager.getDefaultSharedPreferences(OnlineGoApplication.instance)
val state = MutableStateFlow(
private val _state = MutableStateFlow(
AutomatchState(
small = prefs.getBoolean(SEARCH_GAME_SMALL, true),
medium = prefs.getBoolean(SEARCH_GAME_MEDIUM, false),
Expand All @@ -45,15 +47,19 @@ class NewAutomatchChallengeViewModel(
rating = userSessionRepository.uiConfig?.user?.ranking ?: 0
)
)
val state: StateFlow<AutomatchState> = _state

val challenges: Observable<List<SeekGraphChallenge>> =
init {
seekGraphRepository.challengesSubject
.observeOn(Schedulers.single())
.subscribeOn(Schedulers.io())
.map {
it.sortedBy { challenge -> challenge.time_per_move }
}
.distinctUntilChanged()
.subscribe(this::setSeekGraph, this::onError)
.addToDisposable(subscriptions)
}

fun onChallenges(body: (List<SeekGraphChallenge>) -> Unit) {
seekGraphRepository.challengesSubject
Expand Down Expand Up @@ -88,14 +94,26 @@ class NewAutomatchChallengeViewModel(
_state.update { it.copy(speed = speed) }
prefs.edit().putString(SEARCH_GAME_SPEED, speed.toString()).apply()
}

private fun setSeekGraph(challenges: List<SeekGraphChallenge>) {
_state.update {
it.copy(challenges = challenges)
}
}

private fun onError(t: Throwable) {
Log.e(this::class.java.canonicalName, t.message, t)
recordException(t)
}
}

data class AutomatchState(
val small: Boolean = true,
val medium: Boolean = false,
val large: Boolean = false,
val speed: Speed = Speed.NORMAL,
val rating: Int = 0
val challenges: List<SeekGraphChallenge> = emptyList(),
val rating: Int = 0,
) {
val isAnySizeSelected: Boolean
get() = small || medium || large
Expand Down

0 comments on commit 374eb86

Please sign in to comment.