Skip to content

Commit

Permalink
Reuse loading and error composables and improve battles state updates
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSchubert committed Dec 10, 2022
1 parent e0b080b commit 364ff23
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 219 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@file:OptIn(ExperimentalUnitApi::class, ExperimentalMaterialApi::class)
@file:OptIn(ExperimentalMaterialApi::class)

package com.splintergod.app.abilities

Expand All @@ -13,8 +13,6 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ListItem
import androidx.compose.material.Text
Expand All @@ -28,7 +26,6 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.ExperimentalUnitApi
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.fragment.app.Fragment
Expand Down Expand Up @@ -72,7 +69,7 @@ fun Content(state: AbilitiesViewState) {
BackgroundImage(resId = R.drawable.bg_gate)

when (state) {
is AbilitiesViewState.Loading -> LoadingScreen()
is AbilitiesViewState.Loading -> LoadingScreen(R.drawable.loading)
is AbilitiesViewState.Success -> ReadyScreen(abilities = state.abilities)
is AbilitiesViewState.Error -> ErrorScreen()
}
Expand All @@ -97,8 +94,7 @@ fun ReadyScreen(
fun ErrorScreen() {
Box(
modifier = Modifier
.fillMaxSize()
.verticalScroll(rememberScrollState()), // scroll for swipe refresh
.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Text(
Expand Down
39 changes: 4 additions & 35 deletions app/src/main/java/com/splintergod/app/balances/BalancesFragment.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@file:OptIn(ExperimentalUnitApi::class, ExperimentalMaterialApi::class)
@file:OptIn(ExperimentalMaterialApi::class)

package com.splintergod.app.balances

Expand All @@ -13,8 +13,6 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Text
import androidx.compose.material.pullrefresh.pullRefresh
Expand All @@ -28,13 +26,14 @@ import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.ExperimentalUnitApi
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.fragment.app.Fragment
import coil.compose.AsyncImage
import com.example.splinterlandstest.R
import com.splintergod.app.composables.BackgroundImage
import com.splintergod.app.composables.ErrorScreen
import com.splintergod.app.composables.LoadingScreen
import com.splintergod.app.composables.SplinterPullRefreshIndicator
import com.splintergod.app.models.Balances
import org.koin.androidx.viewmodel.ext.android.viewModel
Expand Down Expand Up @@ -90,7 +89,7 @@ fun Content(state: BalancesViewState) {
BackgroundImage(resId = R.drawable.bg_balance)

when (state) {
is BalancesViewState.Loading -> LoadingScreen()
is BalancesViewState.Loading -> LoadingScreen(R.drawable.balances)
is BalancesViewState.Success -> ReadyScreen(balances = state.balances)
is BalancesViewState.Error -> ErrorScreen()
}
Expand All @@ -99,21 +98,6 @@ fun Content(state: BalancesViewState) {
}
}

@Composable
fun LoadingScreen() {
Box(
modifier = Modifier
.fillMaxSize(),
contentAlignment = Alignment.Center
) {
AsyncImage(
modifier = Modifier.size(100.dp),
model = R.drawable.balances,
contentDescription = null
)
}
}

@Composable
fun ReadyScreen(balances: List<Balances>) {
LazyVerticalGrid(
Expand All @@ -126,21 +110,6 @@ fun ReadyScreen(balances: List<Balances>) {
}
}

@Composable
fun ErrorScreen() {
Box(
modifier = Modifier
.fillMaxSize()
.verticalScroll(rememberScrollState()), // scroll for swipe refresh
contentAlignment = Alignment.Center
) {
Text(
text = "Something went wrong",
color = Color.White
)
}
}

@Composable
fun BalanceItem(balance: Balances) {
Column(
Expand Down
48 changes: 11 additions & 37 deletions app/src/main/java/com/splintergod/app/battles/BattlesFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Text
import androidx.compose.material.pullrefresh.pullRefresh
Expand Down Expand Up @@ -53,6 +51,8 @@ import com.example.splinterlandstest.R
import com.google.accompanist.flowlayout.FlowRow
import com.google.accompanist.flowlayout.MainAxisAlignment
import com.splintergod.app.composables.BackgroundImage
import com.splintergod.app.composables.ErrorScreen
import com.splintergod.app.composables.LoadingScreen
import com.splintergod.app.composables.SplinterPullRefreshIndicator
import com.splintergod.app.models.CardFoilUrl
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -102,7 +102,7 @@ fun Content(state: BattlesViewState) {
BackgroundImage(resId = R.drawable.bg_arena)

when (state) {
is BattlesViewState.Loading -> LoadingScreen()
is BattlesViewState.Loading -> LoadingScreen(R.drawable.battles)
is BattlesViewState.Success -> ReadyScreen(
battles = state.battles,
playerName = state.playerName,
Expand All @@ -121,21 +121,6 @@ fun Content(state: BattlesViewState) {
}
}

@Composable
fun LoadingScreen() {
Box(
modifier = Modifier
.fillMaxSize(),
contentAlignment = Alignment.Center
) {
AsyncImage(
modifier = Modifier.size(100.dp),
model = R.drawable.battles,
contentDescription = null
)
}
}

@Composable
fun ReadyScreen(
battles: List<BattleViewState>,
Expand Down Expand Up @@ -359,26 +344,15 @@ fun RewardChest(chestUrl: String, chestText: String, currentTimestamp: Long, end
color = Color.White
)

val seconds = (endTimestamp - currentTimestamp).seconds
val text = if (seconds.isPositive()) {
"$seconds"
} else {
"Claim reward"
}
Text(
text = "${(endTimestamp - currentTimestamp).seconds}",
color = Color.White
)
}
}

@Composable
fun ErrorScreen() {

Box(
modifier = Modifier
.fillMaxSize()
.verticalScroll(rememberScrollState()), // scroll for swipe refresh
contentAlignment = Alignment.Center
) {

Text(
text = "Something went wrong",
text = text,
color = Color.White
)
}
}
}
87 changes: 55 additions & 32 deletions app/src/main/java/com/splintergod/app/battles/BattlesViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,48 @@ class BattlesViewModel(val session: Session, val cache: Cache, val requests: Req
val state = _state.asStateFlow()
private val numberFormat = NumberFormat.getNumberInstance(Locale.US)

private var rewardsInfo: RewardsInfo? = null
private var playerDetails: PlayerDetails? = null
private var gameSettings: GameSettings? = null
private var battles: List<Battle> = emptyList()
private var cardDetails: List<CardDetail> = emptyList()

fun loadBattles() {
viewModelScope.launch(Dispatchers.IO + coroutineExceptionHandler) {

rewardsInfo = cache.getRewardsInfo(session.player)
playerDetails = cache.getPlayerDetails(session.player)
gameSettings = cache.getSettings()
battles = cache.getBattleHistory(session.player)
cardDetails = cache.getCardDetails()

updateReadyState(
isRefreshing = true,
rewardsInfo = cache.getRewardsInfo(session.player),
playerDetails = cache.getPlayerDetails(session.player),
gameSettings = cache.getSettings(),
battles = cache.getBattleHistory(session.player),
cardDetails = cache.getCardDetails()
isRefreshing = true
)

battles = requests.getBattleHistory(session.player)

updateReadyState(
isRefreshing = false,
rewardsInfo = requests.getRewardsInfo(session.player),
playerDetails = requests.getPlayerDetails(session.player),
gameSettings = requests.getSettings(),
battles = requests.getBattleHistory(session.player),
cardDetails = requests.getCardDetails()
isRefreshing = true
)

rewardsInfo = requests.getRewardsInfo(session.player)

updateReadyState(
isRefreshing = true
)

playerDetails = requests.getPlayerDetails(session.player)

updateReadyState(
isRefreshing = true
)

gameSettings = requests.getSettings()
cardDetails = requests.getCardDetails()

updateReadyState(
isRefreshing = false
)
}
}
Expand All @@ -51,26 +75,25 @@ class BattlesViewModel(val session: Session, val cache: Cache, val requests: Req

_state.value = BattlesViewState.Loading { onRefresh() }

rewardsInfo = requests.getRewardsInfo(session.player)
playerDetails = requests.getPlayerDetails(session.player)
gameSettings = requests.getSettings()
battles = requests.getBattleHistory(session.player)
cardDetails = requests.getCardDetails()

updateReadyState(
isRefreshing = false,
rewardsInfo = requests.getRewardsInfo(session.player),
playerDetails = requests.getPlayerDetails(session.player),
gameSettings = requests.getSettings(),
battles = requests.getBattleHistory(session.player),
cardDetails = requests.getCardDetails()
)
}
}

private fun updateReadyState(
isRefreshing: Boolean,
rewardsInfo: RewardsInfo?,
playerDetails: PlayerDetails?,
gameSettings: GameSettings?,
battles: List<Battle>,
cardDetails: List<CardDetail>
isRefreshing: Boolean
) {
if (rewardsInfo != null && playerDetails != null && gameSettings != null) {
val currentRewardsInfo = rewardsInfo
val currentPlayerDetails = playerDetails
val currentGameSettings = gameSettings
if (currentRewardsInfo != null && currentPlayerDetails != null && currentGameSettings != null) {

val battleViewStates = battles.map {
BattleViewState(
Expand All @@ -93,14 +116,14 @@ class BattlesViewModel(val session: Session, val cache: Cache, val requests: Req
onRefresh = { onRefresh() },
isRefreshing = isRefreshing,
battles = battleViewStates,
playerName = playerDetails.name.uppercase(),
playerRating = "W: ${numberFormat.format(playerDetails.rating)}, M: ${numberFormat.format(playerDetails.modernRating)}",
focusChests = rewardsInfo.questRewardInfo.chestEarned,
focusChestUrl = rewardsInfo.questRewardInfo.getChestUrl(),
focusEndTimestamp = rewardsInfo.questRewardInfo.getEndTimestamp(),
seasonChests = rewardsInfo.seasonRewardInfo.chestEarned,
seasonChestUrl = rewardsInfo.seasonRewardInfo.getChestUrl(),
seasonEndTimestamp = gameSettings.season.getEndTimestamp()
playerName = currentPlayerDetails.name.uppercase(),
playerRating = "W: ${numberFormat.format(currentPlayerDetails.rating)}, M: ${numberFormat.format(currentPlayerDetails.modernRating)}",
focusChests = currentRewardsInfo.questRewardInfo.chestEarned,
focusChestUrl = currentRewardsInfo.questRewardInfo.getChestUrl(),
focusEndTimestamp = currentRewardsInfo.questRewardInfo.getEndTimestamp(),
seasonChests = currentRewardsInfo.seasonRewardInfo.chestEarned,
seasonChestUrl = currentRewardsInfo.seasonRewardInfo.getChestUrl(),
seasonEndTimestamp = currentGameSettings.season.getEndTimestamp()
)
}
}
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/com/splintergod/app/composables/ErrorScreen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.splintergod.app.composables

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color

@Composable
fun ErrorScreen(title: String = "Something went wrong") {
Box(
modifier = Modifier
.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Text(
text = title,
color = Color.White
)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.splintergod.app.composables

import androidx.annotation.DrawableRes
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
Expand All @@ -8,18 +9,17 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.example.splinterlandstest.R

@Composable
fun LoadingScreen() {
fun LoadingScreen(@DrawableRes drawableRes: Int) {
Box(
modifier = Modifier
.fillMaxSize(),
contentAlignment = Alignment.Center
) {
AsyncImage(
modifier = Modifier.size(150.dp),
model = R.drawable.loading,
modifier = Modifier.size(100.dp),
model = drawableRes,
contentDescription = null
)
}
Expand Down
Loading

0 comments on commit 364ff23

Please sign in to comment.