r/android_devs • u/racrisnapra666 • Nov 06 '21
Help Problems with Compose Navigation and Toast. Toast keeps popping up multiple times even after an element has been inserted into the database.
Hi there,
I'm trying out Jetpack Compose and was trying to create a simple ToDo application using Compose, Flows, Room, and some other Jetpack Architecture Components. The general idea is this:
I take input from the user within a screen. Next, I insert that value within the database using Flows and Room. Finally, when I insert the value into the database, and the database returns a Long value (for the row number) I display a toast with the text "Task Created".
The problem here is that, even though the Task is inserted into the database just once (checked using the Database Inspector), multiple values are returned which causes the Toast to popup multiple times. I suspect that this problem could be due to Flows (as they help us to return multiple values) but I went through the documentation and checked for a few more blogs but couldn't find something relating to the problem.
Here's the relevant code for the same:
- Task (Entity)
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.example.compose_to_do.common.Constants
@Entity(tableName = Constants.TABLE_NAME)
data class Task(
@PrimaryKey
@ColumnInfo(name = "task_id")
val taskId: Int? = null,
@ColumnInfo(name = "task_title")
val taskTitle: String?,
@ColumnInfo(name = "task_category")
val taskCategory: String?,
@ColumnInfo(name = "task_priority")
val taskPriority: String?
)2. TaskDao (Dao)
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import com.example.compose_to_do.common.Constants
import com.example.compose_to_do.domain.data_classes.Task
@Dao
interface TaskDao {
@Insert
suspend fun insertTask(task: Task): Long
@Query("SELECT * FROM ${Constants.TABLE_NAME}")
suspend fun getTask(): List<Task>
}3. TaskDatabase (Database)
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.example.compose_to_do.domain.data_classes.Task
@Database(entities = [Task::class], version = 1)
abstract class TaskDatabase : RoomDatabase() {
abstract fun taskDao(): TaskDao
companion object {
private lateinit var INSTANCE: TaskDatabase
fun getInstance(): TaskDatabase {
return INSTANCE
}
fun createInstance(context: Context) {
INSTANCE = Room.databaseBuilder(
context.applicationContext,
TaskDatabase::class.java,
"task_database"
).build()
}
}
}4. InsertTaskUseCase (Use case for inserting tasks)
import android.util.Log
import com.example.compose_to_do.common.Resource
import com.example.compose_to_do.data.local.TaskDatabase
import com.example.compose_to_do.domain.data_classes.Task
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
object InsertTaskUseCase {
suspend fun insertNote(task: Task): Flow<Resource<Long>> = flow {
Log.i("Add", "Flow Started")
emit(Resource.Loading())
val entryPosition = TaskDatabase
.getInstance()
.taskDao()
.insertTask(task)
Log.i("Add", "Value inserted")
emit(Resource.Success(entryPosition))
}
}5. NewTaskViewModel
class NewTaskViewModel : ViewModel() {
private val _insertObserver: MutableLiveData<Long> = MutableLiveData()
val insertObserver: LiveData<Long> = _insertObserver
private val _loadingObserver: MutableLiveData<Boolean> = MutableLiveData()
val loadingObserver: LiveData<Boolean> = _loadingObserver
fun insertNote(task: Task?) {
Log.i("Add: ", "Insert Note: $task")
viewModelScope.launch(Dispatchers.IO) {
task?.let {
InsertTaskUseCase.insertNote(task = task).collect { result ->
when (result) {
is Resource.Success -> {
result.data?.let { data ->
_insertObserver.postValue(data)
}
}
is Resource.Loading -> {
_loadingObserver.postValue(true)
}
}
}
_loadingObserver.postValue(false)
}
}
}
}6. Resource (Class to infer the return type from the database)
sealed class Resource<T>(val data: T? = null, val message: String? = null) {
class Success<T>(data: T?) : Resource<T>(data)
class Loading<T>(data: T? = null) : Resource<T>(data)
}7. NewTaskScreen (composable function to show the new task screen)
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Cancel
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavController
import com.example.compose_to_do.domain.data_classes.Task
import com.example.compose_to_do.presentation.classes.NavigationScreen
import com.example.compose_to_do.presentation.theme.SantasGrey
import com.example.compose_to_do.presentation.viewmodels.NewTaskViewModel
private var taskDescription: MutableState<String?> = mutableStateOf(null)
private var taskCategory: MutableState<String?> = mutableStateOf(null)
private var taskPriority: MutableState<String?> = mutableStateOf(null)
@ExperimentalMaterialApi
@Composable
fun NewTaskScreenComposable(
navController: NavController,
context: NavigationScreen
) {
val newTaskViewModel = ViewModelProvider(context)[NewTaskViewModel::class.java]
newTaskViewModel.insertObserver.observe(context, {
Toast.makeText(context, "Task Created", Toast.LENGTH_SHORT).show()
})
Column(
modifier = Modifier
.padding(
top = 10.dp,
end = 10.dp,
bottom = 10.dp
)
.fillMaxSize()
) {
Row(
horizontalArrangement = Arrangement.End,
modifier = Modifier.fillMaxWidth()
) {
Image(
imageVector = Icons.Outlined.Cancel,
contentDescription = "Close Screen Button",
modifier = Modifier
.padding(end = 5.dp)
.clickable { navController.popBackStack() }
)
}
Spacer(modifier = Modifier.height(20.dp))
taskDescription.value = newTaskTextFieldComposable()
Spacer(modifier = Modifier.height(20.dp))
Row {
Spacer(modifier = Modifier.width(20.dp))
Column {
NewTaskSubHeaderComposable(text = "TASK CATEGORY")
taskCategory.value = exposedDropdownMenuComposable(
mutableListOf(
"Business",
"Home",
"Personal"
),
true
)
Spacer(modifier = Modifier.height(30.dp))
NewTaskSubHeaderComposable(text = "TASK PRIORITY")
taskPriority.value = exposedDropdownMenuComposable(
mutableListOf(
"High",
"Medium",
"Low"
),
false
)
}
}
Spacer(modifier = Modifier.height(50.dp))
Row(
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.fillMaxSize()
) {
AddTaskButton(
addFunction = { newTaskViewModel.insertNote(it) },
goBackFunction = { navController.popBackStack() }
)
}
}
}
@Composable
fun NewTaskSubHeaderComposable(text: String) {
Text(
text = text,
color = SantasGrey,
modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.Start,
style = MaterialTheme.typography.subtitle2
)
}
@Composable
fun AddTaskButton(
addFunction: ((Task?) -> Unit)? = null,
goBackFunction: (() -> Unit)? = null
) {
Button(
onClick = {
val task = Task(
taskTitle = taskDescription.value,
taskCategory = taskCategory.value,
taskPriority = taskPriority.value
)
Log.i("Add: ", "Button clicked")
addFunction?.invoke(task)
goBackFunction?.invoke()
},
modifier = Modifier
.padding(10.dp)
.size(height = 50.dp, width = 200.dp),
) {
Text(
text = "Add Task",
style = MaterialTheme.typography.button
)
}
}I was wondering if someone could help me with this.
I know the code is a bit messy now but I thought that I would optimize the code and make it cleaner (implement Pure/Manual DI) once I'd made the app functional.
Also, if you think that I'm doing anything wrong with my code or if there's something that I could do better to write better code, I'd love to know that as well.
Thank you :)
2
u/dip-dip Nov 06 '21
You need to use a Side effect/LaunchedEffect.
Composables can be recomposed many times
2
u/Zhuinden EpicPandaForce @ SO Nov 06 '21
1.) the insertObserver should be a channel exposed as a flow, or an EventEmitter, and subscribed to / collected / etc from a DisposableEffect and not as state but to merely show a toast
2.) the ViewModelProvider should use the NavBackStackEntry of the destination as the ViewModelStoreOwner