Refactor: Make ViewModel APIs Disposable and rename cancel to dispose for cleanup consistency

This commit is contained in:
Nebojsa Vuksic 2025-08-07 10:11:01 +02:00
parent 9591bc93f3
commit 015bd0426c
3 changed files with 9 additions and 4 deletions

View File

@ -37,7 +37,7 @@ class ComposeSamplesToolWindowFactory : ToolWindowFactory, DumbAware {
DisposableEffect(Unit) {
viewModel.onReloadWeatherForecast()
onDispose { viewModel.cancel() }
onDispose { viewModel.dispose() }
}
WeatherAppSample(

View File

@ -1,5 +1,6 @@
package org.jetbrains.plugins.template.weatherApp.services
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.EDT
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@ -18,7 +19,7 @@ import org.jetbrains.plugins.template.weatherApp.model.WeatherForecastData
* a flow to observe the list of selectable locations. Implementations are expected to handle
* location-related logic and state management.
*/
interface MyLocationsViewModelApi {
interface MyLocationsViewModelApi : Disposable {
fun onAddLocation(locationToAdd: Location)
fun onDeleteLocation(locationToDelete: Location)
@ -32,7 +33,7 @@ interface MyLocationsViewModelApi {
* Interface representing a ViewModel for managing weather-related data
* and user interactions.
*/
interface WeatherViewModelApi {
interface WeatherViewModelApi : Disposable {
val weatherForecast: Flow<WeatherForecastData>
fun onLoadWeatherForecast(location: Location)
@ -134,7 +135,7 @@ class WeatherAppViewModel(
* This method is used to release resources and stop ongoing tasks when the ViewModel
* is no longer needed, ensuring proper cleanup of coroutine-based operations.
*/
fun cancel() {
override fun dispose() {
viewModelScope.cancel()
}
}

View File

@ -176,6 +176,10 @@ internal class MyLocationListTest : ComposeBasedTestCase() {
override fun onLocationSelected(selectedLocationIndex: Int) {
selectedItemIndex.value = selectedLocationIndex
}
override fun dispose() {
}
}
private class MyLocationListRobot(private val composableRule: ComposeTestRule) {