Skip to content

Commit

Permalink
Update Spotless and ktlint, apply rule changes to code
Browse files Browse the repository at this point in the history
  • Loading branch information
dturner committed Apr 8, 2024
1 parent 898d32d commit 7387082
Show file tree
Hide file tree
Showing 24 changed files with 139 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class AddEditTaskScreenTest {
viewModel = AddEditTaskViewModel(repository, SavedStateHandle()),
topBarTitle = R.string.add_task,
onTaskUpdate = { },
onBack = { },
onBack = { }
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ class TaskDaoTest {
ToDoDatabase::class.java
).allowMainThreadQueries().build()
}

@Test
fun insertTaskAndGetById() = runTest {
// GIVEN - insert a task
val task = LocalTask(
title = "title",
description = "description",
id = "id",
isCompleted = false,
isCompleted = false
)
database.taskDao().upsert(task)

Expand All @@ -74,7 +75,7 @@ class TaskDaoTest {
title = "title",
description = "description",
id = "id",
isCompleted = false,
isCompleted = false
)
database.taskDao().upsert(task)

Expand Down Expand Up @@ -102,7 +103,7 @@ class TaskDaoTest {
title = "title",
description = "description",
id = "id",
isCompleted = false,
isCompleted = false
)
database.taskDao().upsert(task)

Expand All @@ -124,7 +125,7 @@ class TaskDaoTest {
title = "title",
description = "description",
id = "id",
isCompleted = false,
isCompleted = false
)

database.taskDao().upsert(originalTask)
Expand Down Expand Up @@ -175,7 +176,7 @@ class TaskDaoTest {
title = "title",
description = "description",
id = "id",
isCompleted = false,
isCompleted = false
)
database.taskDao().upsert(task)

Expand All @@ -195,7 +196,7 @@ class TaskDaoTest {
title = "title",
description = "description",
id = "id",
isCompleted = false,
isCompleted = false
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class TaskDetailScreenTest {
),
onEditTask = { /*TODO*/ },
onBack = { },
onDeleteTask = { },
onDeleteTask = { }
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ fun TodoNavGraph(
composable(
TodoDestinations.TASKS_ROUTE,
arguments = listOf(
navArgument(USER_MESSAGE_ARG) { type = NavType.IntType; defaultValue = 0 }
navArgument(USER_MESSAGE_ARG) {
type = NavType.IntType
defaultValue = 0
}
)
) { entry ->
AppModalDrawer(drawerState, currentRoute, navActions) {
Expand All @@ -87,7 +90,10 @@ fun TodoNavGraph(
TodoDestinations.ADD_EDIT_TASK_ROUTE,
arguments = listOf(
navArgument(TITLE_ARG) { type = NavType.IntType },
navArgument(TASK_ID_ARG) { type = NavType.StringType; nullable = true },
navArgument(TASK_ID_ARG) {
type = NavType.StringType
nullable = true
}
)
) { entry ->
val taskId = entry.arguments?.getString(TASK_ID_ARG)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private fun AddEditTaskContent(
// Show the loading spinner—`loading` is `true` in this code path
state = rememberSwipeRefreshState(true),
onRefresh = { /* DO NOTHING */ },
content = { },
content = { }
)
} else {
Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class AddEditTaskViewModel @Inject constructor(
taskRepository.updateTask(
taskId,
title = uiState.value.title,
description = uiState.value.description,
description = uiState.value.description
)
_uiState.update {
it.copy(isTaskSaved = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class DefaultTaskRepository @Inject constructor(
private val networkDataSource: NetworkDataSource,
private val localDataSource: TaskDao,
@DefaultDispatcher private val dispatcher: CoroutineDispatcher,
@ApplicationScope private val scope: CoroutineScope,
@ApplicationScope private val scope: CoroutineScope
) : TaskRepository {

override suspend fun createTask(title: String, description: String): String {
Expand All @@ -55,7 +55,7 @@ class DefaultTaskRepository @Inject constructor(
val task = Task(
title = title,
description = description,
id = taskId,
id = taskId
)
localDataSource.upsert(task.toLocal())
saveTasksToNetwork()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fun Task.toLocal() = LocalTask(
id = id,
title = title,
description = description,
isCompleted = isCompleted,
isCompleted = isCompleted
)

fun List<Task>.toLocal() = map(Task::toLocal)
Expand All @@ -49,7 +49,7 @@ fun LocalTask.toExternal() = Task(
id = id,
title = title,
description = description,
isCompleted = isCompleted,
isCompleted = isCompleted
)

// Note: JvmName is used to provide a unique name for each extension function with the same name.
Expand All @@ -63,7 +63,7 @@ fun NetworkTask.toLocal() = LocalTask(
id = id,
title = title,
description = shortDescription,
isCompleted = (status == TaskStatus.COMPLETE),
isCompleted = (status == TaskStatus.COMPLETE)
)

@JvmName("networkToLocal")
Expand All @@ -74,7 +74,11 @@ fun LocalTask.toNetwork() = NetworkTask(
id = id,
title = title,
shortDescription = description,
status = if (isCompleted) { TaskStatus.COMPLETE } else { TaskStatus.ACTIVE }
status = if (isCompleted) {
TaskStatus.COMPLETE
} else {
TaskStatus.ACTIVE
}
)

fun List<LocalTask>.toNetwork() = map(LocalTask::toNetwork)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ data class Task(
val title: String = "",
val description: String = "",
val isCompleted: Boolean = false,
val id: String,
val id: String
) {

val titleForList: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ data class LocalTask(
@PrimaryKey val id: String,
var title: String,
var description: String,
var isCompleted: Boolean,
var isCompleted: Boolean
)
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ object CoroutinesModule {
@Provides
@Singleton
@ApplicationScope
fun providesCoroutineScope(
@DefaultDispatcher dispatcher: CoroutineDispatcher
): CoroutineScope = CoroutineScope(SupervisorJob() + dispatcher)
fun providesCoroutineScope(@DefaultDispatcher dispatcher: CoroutineDispatcher): CoroutineScope =
CoroutineScope(SupervisorJob() + dispatcher)
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,22 @@ class StatisticsViewModel @Inject constructor(
}
}

private fun produceStatisticsUiState(taskLoad: Async<List<Task>>) =
when (taskLoad) {
Async.Loading -> {
StatisticsUiState(isLoading = true, isEmpty = true)
}
is Async.Error -> {
// TODO: Show error message?
StatisticsUiState(isEmpty = true, isLoading = false)
}
is Async.Success -> {
val stats = getActiveAndCompletedStats(taskLoad.data)
StatisticsUiState(
isEmpty = taskLoad.data.isEmpty(),
activeTasksPercent = stats.activeTasksPercent,
completedTasksPercent = stats.completedTasksPercent,
isLoading = false
)
}
private fun produceStatisticsUiState(taskLoad: Async<List<Task>>) = when (taskLoad) {
Async.Loading -> {
StatisticsUiState(isLoading = true, isEmpty = true)
}
is Async.Error -> {
// TODO: Show error message?
StatisticsUiState(isEmpty = true, isLoading = false)
}
is Async.Success -> {
val stats = getActiveAndCompletedStats(taskLoad.data)
StatisticsUiState(
isEmpty = taskLoad.data.isEmpty(),
activeTasksPercent = stats.activeTasksPercent,
completedTasksPercent = stats.completedTasksPercent,
isLoading = false
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import com.example.android.architecture.blueprints.todoapp.data.Task
* Function that does some trivial computation. Used to showcase unit tests.
*/
internal fun getActiveAndCompletedStats(tasks: List<Task>): StatsResult {

return if (tasks.isEmpty()) {
StatsResult(0f, 0f)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private fun EditTaskContent(
) {
val screenPadding = Modifier.padding(
horizontal = dimensionResource(id = R.dimen.horizontal_margin),
vertical = dimensionResource(id = R.dimen.vertical_margin),
vertical = dimensionResource(id = R.dimen.vertical_margin)
)
val commonModifier = modifier
.fillMaxWidth()
Expand All @@ -131,7 +131,7 @@ private fun EditTaskContent(
Row(
Modifier
.fillMaxWidth()
.then(screenPadding),
.then(screenPadding)

) {
if (task != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,18 @@ class TaskDetailViewModel @Inject constructor(

val taskId: String = savedStateHandle[TodoDestinationsArgs.TASK_ID_ARG]!!

private val _userMessage: MutableStateFlow<Int?> = MutableStateFlow(null)
private val _isLoading = MutableStateFlow(false)
private val _isTaskDeleted = MutableStateFlow(false)
private val _taskAsync = taskRepository.getTaskStream(taskId)
private val userMessage: MutableStateFlow<Int?> = MutableStateFlow(null)
private val isLoading = MutableStateFlow(false)
private val isTaskDeleted = MutableStateFlow(false)
private val taskAsync = taskRepository.getTaskStream(taskId)
.map { handleTask(it) }
.catch { emit(Async.Error(R.string.loading_task_error)) }

val uiState: StateFlow<TaskDetailUiState> = combine(
_userMessage, _isLoading, _isTaskDeleted, _taskAsync
userMessage,
isLoading,
isTaskDeleted,
taskAsync
) { userMessage, isLoading, isTaskDeleted, taskAsync ->
when (taskAsync) {
Async.Loading -> {
Expand Down Expand Up @@ -94,7 +97,7 @@ class TaskDetailViewModel @Inject constructor(

fun deleteTask() = viewModelScope.launch {
taskRepository.deleteTask(taskId)
_isTaskDeleted.value = true
isTaskDeleted.value = true
}

fun setCompleted(completed: Boolean) = viewModelScope.launch {
Expand All @@ -109,19 +112,19 @@ class TaskDetailViewModel @Inject constructor(
}

fun refresh() {
_isLoading.value = true
isLoading.value = true
viewModelScope.launch {
taskRepository.refreshTask(taskId)
_isLoading.value = false
isLoading.value = false
}
}

fun snackbarMessageShown() {
_userMessage.value = null
userMessage.value = null
}

private fun showSnackbarMessage(message: Int) {
_userMessage.value = message
userMessage.value = message
}

private fun handleTask(task: Task?): Async<Task?> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,14 @@ private fun TasksContent(
}

@Composable
private fun TaskItem(
task: Task,
onCheckedChange: (Boolean) -> Unit,
onTaskClick: (Task) -> Unit
) {
private fun TaskItem(task: Task, onCheckedChange: (Boolean) -> Unit, onTaskClick: (Task) -> Unit) {
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.fillMaxWidth()
.padding(
horizontal = dimensionResource(id = R.dimen.horizontal_margin),
vertical = dimensionResource(id = R.dimen.list_item_padding),
vertical = dimensionResource(id = R.dimen.list_item_padding)
)
.clickable { onTaskClick(task) }
) {
Expand Down Expand Up @@ -263,14 +259,14 @@ private fun TasksContentPreview() {
description = "Description 5",
isCompleted = true,
id = "ID 5"
),
)
),
currentFilteringLabel = R.string.label_all,
noTasksLabel = R.string.no_tasks_all,
noTasksIconRes = R.drawable.logo_no_fill,
onRefresh = { },
onTaskClick = { },
onTaskCheckedChange = { _, _ -> },
onTaskCheckedChange = { _, _ -> }
)
}
}
Expand All @@ -289,7 +285,7 @@ private fun TasksContentEmptyPreview() {
noTasksIconRes = R.drawable.logo_no_fill,
onRefresh = { },
onTaskClick = { },
onTaskCheckedChange = { _, _ -> },
onTaskCheckedChange = { _, _ -> }
)
}
}
Expand Down
Loading

0 comments on commit 7387082

Please sign in to comment.