Skip to content

Commit e8a5f83

Browse files
authored
fix(UiState): change UI State problem (#11)
1 parent 3526000 commit e8a5f83

File tree

3 files changed

+31
-43
lines changed

3 files changed

+31
-43
lines changed

app/src/main/java/com/xpeho/xpeapp/data/dateConverter/DateTypeAdapter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import java.util.Locale
1010
class DateTypeAdapter : JsonSerializer<Date>, JsonDeserializer<Date> {
1111
// Format for input dates
1212
private val inputFormats = listOf(
13+
SimpleDateFormat("yyyy-MM-dd", Locale.FRENCH),
1314
SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.FRENCH),
14-
SimpleDateFormat("yyyy-MM-dd", Locale.FRENCH)
1515
)
1616
// Format for output dates
1717
private val outputFormat = SimpleDateFormat("dd/MM/yyyy", Locale.FRENCH)
@@ -28,7 +28,7 @@ class DateTypeAdapter : JsonSerializer<Date>, JsonDeserializer<Date> {
2828
return format.parse(dateStr)
2929
} catch (e: ParseException) {
3030
// Ignore and try the next format
31-
println(e.message)
31+
println("Failed to parse date: $e")
3232
}
3333
}
3434
}

app/src/main/java/com/xpeho/xpeapp/ui/page/AgendaPage.kt

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,38 @@ import androidx.compose.ui.res.stringResource
1010
import androidx.compose.ui.unit.dp
1111
import androidx.lifecycle.viewmodel.compose.viewModel
1212
import com.xpeho.xpeapp.R
13+
import com.xpeho.xpeapp.ui.components.AppLoader
1314
import com.xpeho.xpeapp.ui.components.CustomDialog
1415
import com.xpeho.xpeapp.ui.components.agenda.AgendaBirthdayItem
1516
import com.xpeho.xpeapp.ui.components.agenda.AgendaCardList
1617
import com.xpeho.xpeapp.ui.components.agenda.AgendaEventItem
1718
import com.xpeho.xpeapp.ui.components.layout.Title
1819
import com.xpeho.xpeapp.ui.sendAnalyticsEvent
1920
import com.xpeho.xpeapp.ui.uiState.AgendaUiState
20-
import com.xpeho.xpeapp.ui.uiState.QvstUiState
2121
import com.xpeho.xpeapp.ui.viewModel.agenda.AgendaViewModel
22-
import com.xpeho.xpeapp.ui.viewModel.viewModelFactory
2322

2423
@Composable
25-
fun AgendaPage() {
26-
val agendaViewModel = viewModel<AgendaViewModel>(
27-
factory = viewModelFactory {
28-
AgendaViewModel()
29-
}
30-
)
24+
fun AgendaPage(agendaViewModel : AgendaViewModel = viewModel()) {
25+
val agendaState = agendaViewModel.state
3126

3227
sendAnalyticsEvent("agenda_page")
3328

34-
LaunchedEffect(Unit) {
35-
agendaViewModel.updateState()
36-
}
37-
3829
LazyColumn(
3930
modifier = Modifier
4031
.padding(horizontal = 24.dp, vertical = 10.dp)
41-
.fillMaxSize(),
32+
.fillMaxSize()
4233
) {
43-
when (agendaViewModel.state) {
34+
item {
35+
Title(label = "Agenda")
36+
}
37+
38+
when (agendaState) {
4439
is AgendaUiState.SUCCESS -> {
40+
val state = agendaState
41+
val events = state.agendaEvent.map { AgendaEventItem(it) }
42+
val birthdays = state.agendaBirthday.map { AgendaBirthdayItem(it) }
43+
val items = (events + birthdays).sortedBy { it.date }
4544
item {
46-
Title(label = "Agenda")
47-
val state = agendaViewModel.state as AgendaUiState.SUCCESS
48-
val events = state.agendaEvent.map { AgendaEventItem(it) }
49-
val birthdays = state.agendaBirthday.map { AgendaBirthdayItem(it) }
50-
val items = (events + birthdays).sortedBy { it.date }
5145
AgendaCardList(
5246
items = items,
5347
eventsTypes = state.agendaEventType,
@@ -65,6 +59,15 @@ fun AgendaPage() {
6559
}
6660
}
6761
}
62+
63+
is AgendaUiState.LOADING -> {
64+
item {
65+
LaunchedEffect(Unit) {
66+
agendaViewModel.updateState()
67+
}
68+
AppLoader()
69+
}
70+
}
6871
}
6972
}
7073
}

app/src/main/java/com/xpeho/xpeapp/ui/viewModel/agenda/AgendaViewModel.kt

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@ import androidx.compose.runtime.setValue
66
import androidx.lifecycle.ViewModel
77
import androidx.lifecycle.viewModelScope
88
import com.xpeho.xpeapp.XpeApp
9-
import com.xpeho.xpeapp.data.model.agenda.AgendaBirthday
10-
import com.xpeho.xpeapp.data.model.agenda.AgendaEvent
11-
import com.xpeho.xpeapp.data.model.agenda.AgendaEventType
129
import com.xpeho.xpeapp.ui.uiState.AgendaUiState
1310

1411
import kotlinx.coroutines.launch
1512

1613
class AgendaViewModel : ViewModel() {
1714

18-
var state: AgendaUiState by mutableStateOf(AgendaUiState.EMPTY)
15+
var state: AgendaUiState by mutableStateOf(AgendaUiState.LOADING)
1916

2017
private fun getAllEvents(page: String = "") {
2118
viewModelScope.launch {
@@ -24,12 +21,8 @@ class AgendaViewModel : ViewModel() {
2421

2522
// Update state
2623
state = if (result != null) {
27-
if (result.isEmpty()) {
28-
AgendaUiState.EMPTY
29-
} else {
30-
val currentState = state as? AgendaUiState.SUCCESS ?: AgendaUiState.SUCCESS()
31-
currentState.copy(agendaEvent = result)
32-
}
24+
val currentState = state as? AgendaUiState.SUCCESS ?: AgendaUiState.SUCCESS()
25+
currentState.copy(agendaEvent = result)
3326
} else {
3427
AgendaUiState.ERROR("Erreur de chargement de tous les événements")
3528
}
@@ -43,12 +36,8 @@ class AgendaViewModel : ViewModel() {
4336

4437
// Update state
4538
state = if (result != null) {
46-
if (result.isEmpty()) {
47-
AgendaUiState.EMPTY
48-
} else {
49-
val currentState = state as? AgendaUiState.SUCCESS ?: AgendaUiState.SUCCESS()
50-
currentState.copy(agendaEventType = result)
51-
}
39+
val currentState = state as? AgendaUiState.SUCCESS ?: AgendaUiState.SUCCESS()
40+
currentState.copy(agendaEventType = result)
5241
} else {
5342
AgendaUiState.ERROR("Erreur de chargement de tous les types d'événements")
5443
}
@@ -62,12 +51,8 @@ class AgendaViewModel : ViewModel() {
6251

6352
// Update state
6453
state = if (result != null) {
65-
if (result.isEmpty()) {
66-
AgendaUiState.EMPTY
67-
} else {
68-
val currentState = state as? AgendaUiState.SUCCESS ?: AgendaUiState.SUCCESS()
69-
currentState.copy(agendaBirthday = result)
70-
}
54+
val currentState = state as? AgendaUiState.SUCCESS ?: AgendaUiState.SUCCESS()
55+
currentState.copy(agendaBirthday = result)
7156
} else {
7257
AgendaUiState.ERROR("Erreur de chargement de tous les anniversaires")
7358
}

0 commit comments

Comments
 (0)