-
Notifications
You must be signed in to change notification settings - Fork 1
Dialogues
To display dialogues, we needed to load a state with the information to the fragment, which will use some extensions methods to show the dialogue. In this update, we have introduced the DialogManager
, which has to be injected in the ViewModel
constructor.
class SearchViewModel @Inject constructor(
private val searchRecipes: SearchRecipes,
private val navManager: NavManager,
private val dialogManager: DialogManager,
) : BaseViewModel() {
// ...
}
We have defined two classes that accept the same parameters but with different types to display information within the dialogues. On the one hand, IntDialog
accepts string resource ids as its parameters. On the other hand, LambdaDialog
uses lambdas with the context as its parameter to determine the content to be displayed.
data class IntDialog(
val title: Int,
val message: Int,
val positiveButtonText: Int = R.string.ok,
val positiveButtonAction: (DialogInterface, Int) -> Unit = { dialog, _ -> dialog.dismiss() },
val negativeButtonText: Int? = null,
val negativeButtonAction: (DialogInterface, Int) -> Unit = { dialog, _ -> dialog.dismiss() },
val neutralButtonText: Int? = null,
val neutralButtonAction: (DialogInterface, Int) -> Unit = { dialog, _ -> dialog.dismiss() },
val isCancelable: Boolean = true,
)
data class LambdaDialog(
val title: (Context) -> String,
val message: (Context) -> String,
val positiveButtonText: (Context) -> String? = { null },
val positiveButtonAction: (DialogInterface, Int) -> Unit = { dialog, _ -> dialog.dismiss() },
val negativeButtonText: (Context) -> String? = { null },
val negativeButtonAction: (DialogInterface, Int) -> Unit = { dialog, _ -> dialog.dismiss() },
val neutralButtonText: (Context) -> String? = { null },
val neutralButtonAction: (DialogInterface, Int) -> Unit = { dialog, _ -> dialog.dismiss() },
val isCancelable: Boolean = true,
)
The DialogManager
class contains some methods to display a dialogue. In addition, there are some methods to display a toast.
abstract fun showDialog(data: IntDialog)
abstract fun showDialog(data: LambdaDialog)
abstract fun showLoadingDialog()
abstract fun cancelLoadingDialog()
abstract fun toast(msg: String, duration: Int = Toast.LENGTH_LONG)
abstract fun toast(@StringRes msgId: Int, duration: Int = Toast.LENGTH_LONG)
abstract fun toast(duration: Int, msg: (Context) -> String)
To test the DialogManager,
we need to use the same approach as we have done with NavManager
.
@MockK
private lateinit var navDirections: NavDirections
@Before
fun setUp() {
dialogManager = mockk()
every { dialogManager.showLoadingDialog() } returns Unit
every { dialogManager.cancelLoadingDialog() } returns Unit
// ...
}
Table of contents
Updates