Skip to content
Snippets Groups Projects
Commit fb2b4114 authored by Baryshnikov Oleg's avatar Baryshnikov Oleg
Browse files

Add the refresh button when the character list is empty.

Add the surface color to the dark app theme.
Fix the null safety bug.
parent c1857eb4
No related branches found
No related tags found
No related merge requests found
......@@ -9,7 +9,7 @@ import cz.fel.barysole.ackeetesttask.model.PaginationInfo
@Database(
entities = [CharacterInfo::class, PaginationInfo::class],
version = 4,
version = 1,
exportSchema = false
)
abstract class AppDatabase : RoomDatabase() {
......
......@@ -20,7 +20,7 @@ interface CharacterDao {
fun getAll(): PagingSource<Int, CharacterInfo>
@Query("SELECT * FROM CharacterInfo WHERE id=:id")
fun getById(id: Long): CharacterInfo
fun getById(id: Long): CharacterInfo?
@Query(
"SELECT * FROM CharacterInfo WHERE " +
......
......@@ -88,7 +88,7 @@ class CharacterRemoteMediator(
appDatabase.characterDao().deleteNotFavorite()
for (item in items) {
val itemInDb = appDatabase.characterDao().getById(item.id)
if (itemInDb.isFavorite) {
if (itemInDb?.isFavorite == true) {
appDatabase.characterDao().insert(item.copy(isFavorite = true))
} else {
appDatabase.characterDao().insert(item)
......
package cz.fel.barysole.ackeetesttask.ui.screen.characterlist
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.paging.LoadState
import androidx.paging.compose.collectAsLazyPagingItems
......@@ -19,7 +28,7 @@ fun CharacterListScreen(
color = MaterialTheme.colorScheme.background
) {
characterListViewModel.pagingDataFlow.collectAsLazyPagingItems().let {
if (it.loadState.mediator?.refresh is LoadState.Error) {
if (it.loadState.mediator?.refresh is LoadState.Error || it.loadState.mediator?.append is LoadState.Error) {
characterListViewModel.showLoadingError(true)
} else {
characterListViewModel.showLoadingError(false)
......@@ -32,7 +41,23 @@ fun CharacterListScreen(
)
}
}
CharacterList(it)
// todo: there is no pullToRefresh in MD3 at this moment :c Add it later.
if (it.itemCount == 0) {
Column(
modifier = Modifier.fillMaxWidth().fillMaxHeight(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Button(
onClick = { characterListViewModel.refreshList() },
colors = ButtonDefaults.buttonColors(containerColor = MaterialTheme.colorScheme.surface, contentColor = MaterialTheme.colorScheme.primary)
) {
Text("Reload")
}
}
} else {
CharacterList(it)
}
}
}
}
......
......@@ -14,9 +14,9 @@ import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch
import javax.inject.Inject
@OptIn(ExperimentalCoroutinesApi::class)
......@@ -34,7 +34,6 @@ class CharacterListViewModel @Inject constructor(
init {
pagingDataFlow = getAllCharacters
.distinctUntilChanged()
.onStart { emit(Unit) }
.flatMapLatest { getCharactersPagingData() }
.cachedIn(viewModelScope)
......@@ -44,6 +43,11 @@ class CharacterListViewModel @Inject constructor(
_uiState.value = UiState(showError = hasError)
}
fun refreshList() {
showLoadingError(false)
viewModelScope.launch { getAllCharacters.emit(Unit) }
}
private fun getCharactersPagingData(): Flow<PagingData<CharacterInfo>> =
characterRepository.getCharactersFlow()
......
......@@ -9,5 +9,6 @@ val Pink80 = Color(0xFFEFB8C8)
val Pink40 = Color(0xFF7D5260)
val White = Color(0xFFFFFFFF)
val DarkGray = Color(0xFF212224)
val LightGray = Color(0xFFF4F4F9)
val Gray = Color(0xFFbCBCBC)
\ No newline at end of file
......@@ -19,7 +19,8 @@ import androidx.core.view.WindowCompat
private val DarkColorScheme = darkColorScheme(
primary = Purple80,
secondary = PurpleGrey80,
tertiary = Pink80
tertiary = Pink80,
surface = DarkGray
)
private val LightColorScheme = lightColorScheme(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment