Skip to content

Commit

Permalink
Refactor: Rename UiState to State (#584)
Browse files Browse the repository at this point in the history
  • Loading branch information
serbelga authored Aug 27, 2024
1 parent d9b2df9 commit a1d8464
Show file tree
Hide file tree
Showing 28 changed files with 128 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ fun AddTaskScreen(
}
SystemBackHandler(onBack = onBack)

if (viewModel.uiState.errorUi != null) {
if (viewModel.state.errorUi != null) {
LaunchedEffect(snackbarHostState) {
snackbarHostState.showSnackbar(
message = viewModel.uiState.errorUi?.message ?: ""
message = viewModel.state.errorUi?.message ?: ""
)
}
}
Expand All @@ -135,7 +135,7 @@ fun AddTaskScreen(
SaveActionTopAppBar(
navigateBack = onBack,
title = TodometerResources.strings.addTask,
isSaveButtonEnabled = !viewModel.uiState.isAddingTask,
isSaveButtonEnabled = !viewModel.state.isAddingTask,
onSaveButtonClick = {
if (taskTitle.isBlank()) {
taskTitleInputError = true
Expand All @@ -150,15 +150,15 @@ fun AddTaskScreen(
navigateBack()
}
},
saveButtonTintColor = if (viewModel.uiState.isAddingTask) {
saveButtonTintColor = if (viewModel.state.isAddingTask) {
MaterialTheme.colorScheme.onSurface.applyMediumEmphasisAlpha()
} else {
MaterialTheme.colorScheme.primary
}
)
},
content = { paddingValues ->
if (viewModel.uiState.isAddingTask) {
if (viewModel.state.isAddingTask) {
LinearProgressIndicator(modifier = Modifier.fillMaxWidth())
}
LazyColumn(state = lazyListState, modifier = Modifier.padding(paddingValues)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import androidx.compose.runtime.Immutable
import dev.sergiobelda.todometer.common.ui.error.ErrorUi

@Immutable
data class AddTaskUiState(
data class AddTaskState(
val isAddingTask: Boolean = false,
val errorUi: ErrorUi? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AddTaskViewModel(
private val insertTaskInTaskListSelectedUseCase: InsertTaskInTaskListSelectedUseCase
) : ViewModel() {

var uiState by mutableStateOf(AddTaskUiState())
var state by mutableStateOf(AddTaskState())
private set

fun insertTask(
Expand All @@ -42,7 +42,7 @@ class AddTaskViewModel(
dueDate: Long? = null,
taskChecklistItems: List<String> = emptyList()
) = viewModelScope.launch {
uiState = uiState.copy(isAddingTask = true)
state = state.copy(isAddingTask = true)
val result = insertTaskInTaskListSelectedUseCase.invoke(
title,
tag,
Expand All @@ -51,12 +51,12 @@ class AddTaskViewModel(
taskChecklistItems
)
result.doIfSuccess {
uiState = uiState.copy(
state = state.copy(
isAddingTask = false,
errorUi = null
)
}.doIfError { error ->
uiState = uiState.copy(
state = state.copy(
isAddingTask = false,
errorUi = error.mapToErrorUi()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ fun AddTaskListScreen(
var taskListName by rememberSaveable { mutableStateOf("") }
var taskListNameInputError by remember { mutableStateOf(false) }

if (viewModel.uiState.errorUi != null) {
if (viewModel.state.errorUi != null) {
LaunchedEffect(snackbarHostState) {
snackbarHostState.showSnackbar(
message = viewModel.uiState.errorUi?.message ?: ""
message = viewModel.state.errorUi?.message ?: ""
)
}
}
Expand All @@ -72,7 +72,7 @@ fun AddTaskListScreen(
SaveActionTopAppBar(
navigateBack = navigateBack,
title = TodometerResources.strings.addTaskList,
isSaveButtonEnabled = !viewModel.uiState.isAddingTaskList,
isSaveButtonEnabled = !viewModel.state.isAddingTaskList,
onSaveButtonClick = {
if (taskListName.isBlank()) {
taskListNameInputError = true
Expand All @@ -81,7 +81,7 @@ fun AddTaskListScreen(
navigateBack()
}
},
saveButtonTintColor = if (viewModel.uiState.isAddingTaskList) {
saveButtonTintColor = if (viewModel.state.isAddingTaskList) {
MaterialTheme.colorScheme.onSurface.applyMediumEmphasisAlpha()
} else {
MaterialTheme.colorScheme.primary
Expand All @@ -91,7 +91,7 @@ fun AddTaskListScreen(
content = { paddingValues ->
AddTaskListContent(
paddingValues = paddingValues,
showProgress = viewModel.uiState.isAddingTaskList,
showProgress = viewModel.state.isAddingTaskList,
taskListNameValue = taskListName,
taskListNameInputError = taskListNameInputError,
onTaskListNameValueChange = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import androidx.compose.runtime.Immutable
import dev.sergiobelda.todometer.common.ui.error.ErrorUi

@Immutable
data class AddTaskListUiState(
data class AddTaskListState(
val isAddingTaskList: Boolean = false,
val errorUi: ErrorUi? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ class AddTaskListViewModel(
private val insertTaskListUseCase: InsertTaskListUseCase
) : ViewModel() {

var uiState by mutableStateOf(AddTaskListUiState())
var state by mutableStateOf(AddTaskListState())
private set

fun insertTaskList(name: String) = viewModelScope.launch {
uiState = uiState.copy(isAddingTaskList = true)
state = state.copy(isAddingTaskList = true)
val result = insertTaskListUseCase.invoke(name)
result.doIfSuccess {
uiState = uiState.copy(
state = state.copy(
isAddingTaskList = false,
errorUi = null
)
}.doIfError { error ->
uiState = uiState.copy(
state = state.copy(
isAddingTaskList = false,
errorUi = error.mapToErrorUi()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ fun EditTaskScreen(
viewModel: EditTaskViewModel
) {
when {
viewModel.uiState.isLoading -> {
viewModel.state.isLoading -> {
LoadingScreenDialog(navigateBack)
}

!viewModel.uiState.isLoading -> {
viewModel.uiState.task?.let { task ->
!viewModel.state.isLoading -> {
viewModel.state.task?.let { task ->
EditTaskSuccessContent(
task = task,
navigateBack = navigateBack,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import dev.sergiobelda.todometer.common.domain.model.Task
import dev.sergiobelda.todometer.common.ui.error.ErrorUi

@Immutable
data class EditTaskUiState(
data class EditTaskState(
val isLoading: Boolean = false,
val task: Task? = null,
val errorUi: ErrorUi? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class EditTaskViewModel(
private val updateTaskUseCase: UpdateTaskUseCase
) : ViewModel() {

var uiState by mutableStateOf(EditTaskUiState(isLoading = true))
var state by mutableStateOf(EditTaskState(isLoading = true))
private set

init {
Expand All @@ -45,13 +45,13 @@ class EditTaskViewModel(
private fun getTask() = viewModelScope.launch {
getTaskUseCase(taskId).collect { result ->
result.doIfSuccess { task ->
uiState = uiState.copy(
state = state.copy(
isLoading = false,
task = task,
errorUi = null
)
}.doIfError { error ->
uiState = uiState.copy(
state = state.copy(
isLoading = false,
task = null,
errorUi = error.mapToErrorUi()
Expand All @@ -66,7 +66,7 @@ class EditTaskViewModel(
description: String? = null,
dueDate: Long? = null
) = viewModelScope.launch {
uiState.task?.let {
state.task?.let {
updateTaskUseCase(
it.copy(
title = title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ fun EditTaskListScreen(
viewModel: EditTaskListViewModel
) {
when {
viewModel.uiState.isLoading -> {
viewModel.state.isLoading -> {
LoadingScreenDialog(navigateBack)
}

!viewModel.uiState.isLoading -> {
viewModel.uiState.taskList?.let { taskList ->
!viewModel.state.isLoading -> {
viewModel.state.taskList?.let { taskList ->
var taskListName by rememberSaveable { mutableStateOf(taskList.name) }
var taskListNameInputError by remember { mutableStateOf(false) }

Expand All @@ -61,7 +61,7 @@ fun EditTaskListScreen(
SaveActionTopAppBar(
navigateBack = navigateBack,
title = TodometerResources.strings.edit_task_list,
isSaveButtonEnabled = !viewModel.uiState.isLoading,
isSaveButtonEnabled = !viewModel.state.isLoading,
onSaveButtonClick = {
if (taskListName.isBlank()) {
taskListNameInputError = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import dev.sergiobelda.todometer.common.domain.model.TaskList
import dev.sergiobelda.todometer.common.ui.error.ErrorUi

@Immutable
data class EditTaskListUiState(
data class EditTaskListState(
val isLoading: Boolean = false,
val taskList: TaskList? = null,
val errorUi: ErrorUi? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class EditTaskListViewModel(
private val updateTaskListUseCase: UpdateTaskListUseCase
) : ViewModel() {

var uiState by mutableStateOf(EditTaskListUiState(isLoading = true))
var state by mutableStateOf(EditTaskListState(isLoading = true))
private set

init {
Expand All @@ -43,13 +43,13 @@ class EditTaskListViewModel(
private fun getTaskListSelected() = viewModelScope.launch {
getTaskListSelectedUseCase().collect { result ->
result.doIfSuccess { taskList ->
uiState = uiState.copy(
state = state.copy(
isLoading = false,
taskList = taskList,
errorUi = null
)
}.doIfError { error ->
uiState = uiState.copy(
state = state.copy(
isLoading = false,
taskList = null,
errorUi = error.mapToErrorUi()
Expand All @@ -59,7 +59,7 @@ class EditTaskListViewModel(
}

fun updateTaskList(name: String) = viewModelScope.launch {
uiState.taskList?.let {
state.taskList?.let {
updateTaskListUseCase(it.copy(name = name))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ fun HomeScreen(
val cannotDeleteTaskList = TodometerResources.strings.cannot_delete_this_task_list
val snackbarActionLabel = TodometerResources.strings.ok

SystemBackHandler(enabled = viewModel.uiState.selectionMode) { viewModel.clearSelectedTasks() }
SystemBackHandler(enabled = viewModel.state.selectionMode) { viewModel.clearSelectedTasks() }

ModalNavigationDrawer(
drawerState = drawerState,
drawerContent = {
HomeDrawerContent(
viewModel.uiState.taskListSelected?.id ?: "",
viewModel.state.taskListSelected?.id ?: "",
defaultTaskListName,
viewModel.uiState.taskLists,
viewModel.state.taskLists,
onAddTaskList = {
drawerAction(navigateToAddTaskList)
},
Expand All @@ -157,7 +157,7 @@ fun HomeScreen(
}
)
},
gesturesEnabled = !viewModel.uiState.selectionMode
gesturesEnabled = !viewModel.state.selectionMode
) {
Scaffold(
topBar = {
Expand All @@ -167,7 +167,7 @@ fun HomeScreen(
onHomeMoreDropdownDismissRequest = closeHomeMoreDropdown,
homeMoreDropdownExpanded = homeMoreDropdownExpanded,
onEditTaskListClick = {
if (viewModel.uiState.isDefaultTaskListSelected) {
if (viewModel.state.isDefaultTaskListSelected) {
coroutineScope.launch {
snackbarHostState.showSnackbar(
cannotEditTaskList,
Expand All @@ -181,7 +181,7 @@ fun HomeScreen(
closeHomeMoreDropdown()
},
onDeleteTaskListClick = {
if (viewModel.uiState.isDefaultTaskListSelected) {
if (viewModel.state.isDefaultTaskListSelected) {
coroutineScope.launch {
snackbarHostState.showSnackbar(
cannotDeleteTaskList,
Expand All @@ -194,10 +194,10 @@ fun HomeScreen(
}
closeHomeMoreDropdown()
},
taskListName = viewModel.uiState.taskListSelected?.name ?: defaultTaskListName,
tasks = viewModel.uiState.tasks,
selectionMode = viewModel.uiState.selectionMode,
selectedTasks = viewModel.uiState.selectedTasks,
taskListName = viewModel.state.taskListSelected?.name ?: defaultTaskListName,
tasks = viewModel.state.tasks,
selectionMode = viewModel.state.selectionMode,
selectedTasks = viewModel.state.selectedTasks,
onClearSelectedTasksClick = viewModel::clearSelectedTasks,
onToggleSelectedTasksPinnedValueClick = viewModel::toggleSelectedTasksPinnedValue,
onDeleteSelectedTasksClick = { deleteTasksAlertDialogState = true }
Expand Down Expand Up @@ -227,18 +227,18 @@ fun HomeScreen(
onDeleteTaskListClick = viewModel::deleteTaskList
)
}
if (viewModel.uiState.isLoadingTasks) {
if (viewModel.state.isLoadingTasks) {
ContentLoadingProgress()
} else {
TasksListView(
tasksDoingPinned = viewModel.uiState.tasksDoingPinned,
tasksDoingNotPinned = viewModel.uiState.tasksDoingNotPinned,
tasksDone = viewModel.uiState.tasksDone,
selectedTasksIds = viewModel.uiState.selectedTasksIds,
tasksDoingPinned = viewModel.state.tasksDoingPinned,
tasksDoingNotPinned = viewModel.state.tasksDoingNotPinned,
tasksDone = viewModel.state.tasksDone,
selectedTasksIds = viewModel.state.selectedTasksIds,
onDoingClick = { viewModel.setTaskDoing(it) },
onDoneClick = { viewModel.setTaskDone(it) },
onTaskItemClick = { taskId ->
if (viewModel.uiState.selectionMode) {
if (viewModel.state.selectionMode) {
viewModel.toggleSelectTask(taskId)
} else {
navigateToTaskDetails(taskId)
Expand All @@ -249,14 +249,14 @@ fun HomeScreen(
deleteTaskAlertDialogState = true
swipedTaskId = it
},
selectionMode = viewModel.uiState.selectionMode,
selectionMode = viewModel.state.selectionMode,
modifier = Modifier.padding(paddingValues)
)
}
},
floatingActionButton = {
HomeFloatingActionButton(
visible = !viewModel.uiState.selectionMode,
visible = !viewModel.state.selectionMode,
navigateToAddTask = navigateToAddTask
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toPersistentList

@Immutable
data class HomeUiState(
data class HomeState(
val isLoadingTasks: Boolean = false,
val tasks: ImmutableList<TaskItem> = persistentListOf(),
val selectedTasksIds: ImmutableList<String> = persistentListOf(),
Expand Down
Loading

0 comments on commit a1d8464

Please sign in to comment.