From 015bd0426c1a8afc9872dac05fb452b22f32e4ed Mon Sep 17 00:00:00 2001 From: Nebojsa Vuksic Date: Thu, 7 Aug 2025 10:11:01 +0200 Subject: [PATCH] Refactor: Make ViewModel APIs Disposable and rename `cancel` to `dispose` for cleanup consistency --- .../template/toolWindow/ComposeSamplesToolWindowFactory.kt | 2 +- .../template/weatherApp/services/WeatherAppViewModel.kt | 7 ++++--- .../org/jetbrains/plugins/template/MyLocationListTest.kt | 4 ++++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/org/jetbrains/plugins/template/toolWindow/ComposeSamplesToolWindowFactory.kt b/src/main/kotlin/org/jetbrains/plugins/template/toolWindow/ComposeSamplesToolWindowFactory.kt index f75481e..00c97ac 100644 --- a/src/main/kotlin/org/jetbrains/plugins/template/toolWindow/ComposeSamplesToolWindowFactory.kt +++ b/src/main/kotlin/org/jetbrains/plugins/template/toolWindow/ComposeSamplesToolWindowFactory.kt @@ -37,7 +37,7 @@ class ComposeSamplesToolWindowFactory : ToolWindowFactory, DumbAware { DisposableEffect(Unit) { viewModel.onReloadWeatherForecast() - onDispose { viewModel.cancel() } + onDispose { viewModel.dispose() } } WeatherAppSample( diff --git a/src/main/kotlin/org/jetbrains/plugins/template/weatherApp/services/WeatherAppViewModel.kt b/src/main/kotlin/org/jetbrains/plugins/template/weatherApp/services/WeatherAppViewModel.kt index 9078f4f..da469b5 100644 --- a/src/main/kotlin/org/jetbrains/plugins/template/weatherApp/services/WeatherAppViewModel.kt +++ b/src/main/kotlin/org/jetbrains/plugins/template/weatherApp/services/WeatherAppViewModel.kt @@ -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 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() } } diff --git a/src/test/kotlin/org/jetbrains/plugins/template/MyLocationListTest.kt b/src/test/kotlin/org/jetbrains/plugins/template/MyLocationListTest.kt index 5e7d1c8..48f4093 100644 --- a/src/test/kotlin/org/jetbrains/plugins/template/MyLocationListTest.kt +++ b/src/test/kotlin/org/jetbrains/plugins/template/MyLocationListTest.kt @@ -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) {