Skip to content

Commit 932dcc8

Browse files
committed
fixed snackbar issue on android#1047
1 parent ee66e15 commit 932dcc8

19 files changed

Lines changed: 161 additions & 29 deletions

File tree

.kotlin/sessions/kotlin-compiler-5981389976571422887.salive

Whitespace-only changes.

app/src/androidTest/java/com/example/android/architecture/blueprints/todoapp/addedittask/AddEditTaskScreenTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class AddEditTaskScreenTest {
7777
topBarTitle = R.string.add_task,
7878
onTaskUpdate = { },
7979
onBack = { },
80+
taskId = null
8081
)
8182
}
8283
}

app/src/androidTest/java/com/example/android/architecture/blueprints/todoapp/tasks/TasksScreenTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ class TasksScreenTest {
8080
composeTestRule.onNodeWithText("TITLE1").assertIsDisplayed()
8181
}
8282

83+
84+
8385
@Test
8486
fun displayActiveTask() = runTest {
8587
repository.createTask("TITLE1", "DESCRIPTION1")
@@ -259,7 +261,6 @@ class TasksScreenTest {
259261
Surface {
260262
TasksScreen(
261263
viewModel = TasksViewModel(repository, SavedStateHandle()),
262-
userMessage = R.string.successfully_added_task_message,
263264
onUserMessageDisplayed = { },
264265
onAddTask = { },
265266
onTaskClick = { },

app/src/main/java/com/example/android/architecture/blueprints/todoapp/TodoNavGraph.kt

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,15 @@ fun TodoNavGraph(
6262
startDestination = startDestination,
6363
modifier = modifier
6464
) {
65-
composable(
66-
TodoDestinations.TASKS_ROUTE,
67-
arguments = listOf(
68-
navArgument(USER_MESSAGE_ARG) { type = NavType.IntType; defaultValue = 0 }
69-
)
70-
) { entry ->
65+
composable(TodoDestinations.TASKS_ROUTE,) { entry ->
7166
AppModalDrawer(drawerState, currentRoute, navActions) {
7267
TasksScreen(
73-
userMessage = entry.arguments?.getInt(USER_MESSAGE_ARG)!!,
7468
onUserMessageDisplayed = { entry.arguments?.putInt(USER_MESSAGE_ARG, 0) },
7569
onAddTask = { navActions.navigateToAddEditTask(R.string.add_task, null) },
7670
onTaskClick = { task -> navActions.navigateToTaskDetail(task.id) },
7771
openDrawer = { coroutineScope.launch { drawerState.open() } }
7872
)
73+
7974
}
8075
}
8176
composable(TodoDestinations.STATISTICS_ROUTE) {
@@ -93,10 +88,9 @@ fun TodoNavGraph(
9388
val taskId = entry.arguments?.getString(TASK_ID_ARG)
9489
AddEditTaskScreen(
9590
topBarTitle = entry.arguments?.getInt(TITLE_ARG)!!,
91+
taskId = taskId,
9692
onTaskUpdate = {
97-
navActions.navigateToTasks(
98-
if (taskId == null) ADD_EDIT_RESULT_OK else EDIT_RESULT_OK
99-
)
93+
navActions.navigateToTasksV2()
10094
},
10195
onBack = { navController.popBackStack() }
10296
)
@@ -107,7 +101,7 @@ fun TodoNavGraph(
107101
navActions.navigateToAddEditTask(R.string.edit_task, taskId)
108102
},
109103
onBack = { navController.popBackStack() },
110-
onDeleteTask = { navActions.navigateToTasks(DELETE_RESULT_OK) }
104+
onDeleteTask = { navActions.navigateToTasks() }
111105
)
112106
}
113107
}
@@ -117,3 +111,4 @@ fun TodoNavGraph(
117111
const val ADD_EDIT_RESULT_OK = Activity.RESULT_FIRST_USER + 1
118112
const val DELETE_RESULT_OK = Activity.RESULT_FIRST_USER + 2
119113
const val EDIT_RESULT_OK = Activity.RESULT_FIRST_USER + 3
114+
const val NO_MESSAGE = Activity.RESULT_FIRST_USER - 1

app/src/main/java/com/example/android/architecture/blueprints/todoapp/TodoNavigation.kt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.example.android.architecture.blueprints.todoapp
1818

19+
import androidx.compose.ui.window.Popup
1920
import androidx.navigation.NavGraph.Companion.findStartDestination
2021
import androidx.navigation.NavHostController
2122
import com.example.android.architecture.blueprints.todoapp.TodoDestinationsArgs.TASK_ID_ARG
@@ -76,7 +77,36 @@ class TodoNavigationActions(private val navController: NavHostController) {
7677
}
7778
}
7879

79-
fun navigateToStatistics() {
80+
fun navigateToTasks() {
81+
navController.navigate(
82+
TASKS_SCREEN
83+
) {
84+
popUpTo(navController.graph.findStartDestination().id) {
85+
inclusive = true
86+
saveState = false
87+
}
88+
launchSingleTop = true
89+
restoreState = false
90+
}
91+
}
92+
93+
fun navigateToTasksV2() {
94+
navController.navigate(
95+
TASKS_SCREEN
96+
){
97+
popUpTo(navController.graph.findStartDestination().id) {
98+
inclusive = true
99+
saveState = false
100+
}
101+
launchSingleTop = true
102+
restoreState = false
103+
}
104+
}
105+
106+
107+
108+
109+
fun navigateToStatistics() {
80110
navController.navigate(TodoDestinations.STATISTICS_ROUTE) {
81111
// Pop up to the start destination of the graph to
82112
// avoid building up a large stack of destinations

app/src/main/java/com/example/android/architecture/blueprints/todoapp/addedittask/AddEditTaskScreen.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,15 @@ import androidx.compose.ui.text.font.FontWeight
5454
import androidx.compose.ui.unit.dp
5555
import androidx.hilt.navigation.compose.hiltViewModel
5656
import androidx.lifecycle.compose.collectAsStateWithLifecycle
57+
import com.example.android.architecture.blueprints.todoapp.ADD_EDIT_RESULT_OK
58+
import com.example.android.architecture.blueprints.todoapp.EDIT_RESULT_OK
5759
import com.example.android.architecture.blueprints.todoapp.R
5860
import com.example.android.architecture.blueprints.todoapp.util.AddEditTaskTopAppBar
5961

6062
@Composable
6163
fun AddEditTaskScreen(
6264
@StringRes topBarTitle: Int,
65+
taskId: String?,
6366
onTaskUpdate: () -> Unit,
6467
onBack: () -> Unit,
6568
modifier: Modifier = Modifier,
@@ -90,8 +93,12 @@ fun AddEditTaskScreen(
9093
// Check if the task is saved and call onTaskUpdate event
9194
LaunchedEffect(uiState.isTaskSaved) {
9295
if (uiState.isTaskSaved) {
96+
viewModel.setPendingMessage(if (taskId != null)
97+
ADD_EDIT_RESULT_OK else EDIT_RESULT_OK)
9398
onTaskUpdate()
9499
}
100+
101+
95102
}
96103

97104
// Check for user messages to display on the screen

app/src/main/java/com/example/android/architecture/blueprints/todoapp/addedittask/AddEditTaskViewModel.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ data class AddEditTaskUiState(
4848
@HiltViewModel
4949
class AddEditTaskViewModel @Inject constructor(
5050
private val taskRepository: TaskRepository,
51-
savedStateHandle: SavedStateHandle
51+
private val savedStateHandle: SavedStateHandle
5252
) : ViewModel() {
5353

5454
private val taskId: String? = savedStateHandle[TodoDestinationsArgs.TASK_ID_ARG]
@@ -77,7 +77,7 @@ class AddEditTaskViewModel @Inject constructor(
7777
if (taskId == null) {
7878
createNewTask()
7979
} else {
80-
updateTask()
80+
updateTask()
8181
}
8282
}
8383

@@ -106,6 +106,10 @@ class AddEditTaskViewModel @Inject constructor(
106106
}
107107
}
108108

109+
fun setPendingMessage(message: Int){
110+
taskRepository.addPendingMessage(message)
111+
}
112+
109113
private fun updateTask() {
110114
if (taskId == null) {
111115
throw RuntimeException("updateTask() was called but task is new.")

app/src/main/java/com/example/android/architecture/blueprints/todoapp/data/DefaultTaskRepository.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.example.android.architecture.blueprints.todoapp.data.source.local.Tas
2020
import com.example.android.architecture.blueprints.todoapp.data.source.network.NetworkDataSource
2121
import com.example.android.architecture.blueprints.todoapp.di.ApplicationScope
2222
import com.example.android.architecture.blueprints.todoapp.di.DefaultDispatcher
23+
import com.example.android.architecture.blueprints.todoapp.util.AppMessageQueue
2324
import kotlinx.coroutines.CoroutineDispatcher
2425
import kotlinx.coroutines.CoroutineScope
2526
import kotlinx.coroutines.flow.Flow
@@ -44,8 +45,9 @@ import javax.inject.Singleton
4445
class DefaultTaskRepository @Inject constructor(
4546
private val networkDataSource: NetworkDataSource,
4647
private val localDataSource: TaskDao,
48+
private val appMessageQueue: AppMessageQueue,
4749
@DefaultDispatcher private val dispatcher: CoroutineDispatcher,
48-
@ApplicationScope private val scope: CoroutineScope,
50+
@ApplicationScope private val scope: CoroutineScope
4951
) : TaskRepository {
5052

5153
override suspend fun createTask(title: String, description: String): String {
@@ -64,6 +66,7 @@ class DefaultTaskRepository @Inject constructor(
6466
return taskId
6567
}
6668

69+
6770
override suspend fun updateTask(taskId: String, title: String, description: String) {
6871
val task = getTask(taskId)?.copy(
6972
title = title,
@@ -137,6 +140,14 @@ class DefaultTaskRepository @Inject constructor(
137140
saveTasksToNetwork()
138141
}
139142

143+
override fun addPendingMessage(message: Int) {
144+
appMessageQueue.setHasPendingMessage(message = message)
145+
}
146+
147+
override fun readOncePendingMessage(): Int {
148+
return appMessageQueue.getPendingMessageOnce()
149+
}
150+
140151
/**
141152
* The following methods load tasks from (refresh), and save tasks to, the network.
142153
*

app/src/main/java/com/example/android/architecture/blueprints/todoapp/data/TaskRepository.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,9 @@ interface TaskRepository {
4848
suspend fun deleteAllTasks()
4949

5050
suspend fun deleteTask(taskId: String)
51+
52+
fun addPendingMessage(message: Int)
53+
54+
fun readOncePendingMessage(): Int
55+
5156
}

app/src/main/java/com/example/android/architecture/blueprints/todoapp/di/DataModules.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import com.example.android.architecture.blueprints.todoapp.data.source.local.Tas
2424
import com.example.android.architecture.blueprints.todoapp.data.source.local.ToDoDatabase
2525
import com.example.android.architecture.blueprints.todoapp.data.source.network.NetworkDataSource
2626
import com.example.android.architecture.blueprints.todoapp.data.source.network.TaskNetworkDataSource
27+
import com.example.android.architecture.blueprints.todoapp.util.AppMessageQueue
28+
import com.example.android.architecture.blueprints.todoapp.util.AppMessageQueueImpl
2729
import dagger.Binds
2830
import dagger.Module
2931
import dagger.Provides
@@ -66,4 +68,10 @@ object DatabaseModule {
6668

6769
@Provides
6870
fun provideTaskDao(database: ToDoDatabase): TaskDao = database.taskDao()
71+
72+
@Provides
73+
fun provideAppMessageQueue(@ApplicationContext context: Context): AppMessageQueue {
74+
return AppMessageQueueImpl(context
75+
.getSharedPreferences("message_queue", Context.MODE_PRIVATE))
76+
}
6977
}

0 commit comments

Comments
 (0)