mirror of
https://github.com/JetBrains/intellij-platform-plugin-template.git
synced 2026-01-22 08:39:27 +00:00
Refactor: Convert WeatherForecastService to an interface-based implementation and improve coroutine handling
This commit is contained in:
parent
4673a445bf
commit
85f3a32137
@ -1,36 +1,43 @@
|
|||||||
package org.jetbrains.plugins.template.weatherApp.services
|
package org.jetbrains.plugins.template.weatherApp.services
|
||||||
|
|
||||||
import com.intellij.openapi.components.Service
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.withContext
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import org.jetbrains.plugins.template.weatherApp.model.*
|
import org.jetbrains.plugins.template.weatherApp.model.*
|
||||||
import java.time.LocalDate
|
import java.time.LocalDate
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
import java.time.LocalTime
|
import java.time.LocalTime
|
||||||
|
import kotlin.coroutines.CoroutineContext
|
||||||
import kotlin.random.Random
|
import kotlin.random.Random
|
||||||
|
|
||||||
@Service
|
|
||||||
class WeatherForecastService(private val cs: CoroutineScope) {
|
|
||||||
private val _weatherForecast: MutableStateFlow<WeatherForecastData> = MutableStateFlow(WeatherForecastData.EMPTY)
|
|
||||||
|
|
||||||
val weatherForecast: StateFlow<WeatherForecastData> = _weatherForecast.asStateFlow()
|
interface WeatherForecastServiceApi {
|
||||||
|
/**
|
||||||
|
* Suspending function that returns Result<WeatherForecastData>.
|
||||||
|
* This allows callers to handle success/failure explicitly.
|
||||||
|
*
|
||||||
|
* @param location The location to get weather data for
|
||||||
|
* @return Result containing WeatherForecastData on success or exception on failure
|
||||||
|
*/
|
||||||
|
suspend fun loadWeatherForecastFor(location: Location): Result<WeatherForecastData>
|
||||||
|
}
|
||||||
|
|
||||||
fun loadWeatherForecastFor(location: Location) {
|
class WeatherForecastService(
|
||||||
cs.launch(Dispatchers.IO) {
|
private val networkCoroutineContext: CoroutineContext = Dispatchers.IO,
|
||||||
// TODO Cache data
|
) : WeatherForecastServiceApi {
|
||||||
emit(getWeatherData(location))
|
|
||||||
|
/**
|
||||||
|
* Function that returns a weather forecast for provided [location] param.
|
||||||
|
*
|
||||||
|
* @param location The location to get weather data for
|
||||||
|
* @return Result containing WeatherForecastData on success or exception on failure
|
||||||
|
*/
|
||||||
|
override suspend fun loadWeatherForecastFor(location: Location): Result<WeatherForecastData> {
|
||||||
|
return withContext(networkCoroutineContext) {
|
||||||
|
runCatching { getWeatherData(location) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun emit(weatherData: WeatherForecastData) {
|
|
||||||
_weatherForecast.value = weatherData
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides mock weather data for demonstration purposes.
|
* Provides mock weather data for demonstration purposes.
|
||||||
* In a real application, this would fetch data from a weather API.
|
* In a real application, this would fetch data from a weather API.
|
||||||
@ -41,6 +48,7 @@ class WeatherForecastService(private val cs: CoroutineScope) {
|
|||||||
// Generate 7-day forecast data
|
// Generate 7-day forecast data
|
||||||
val dailyForecasts = generateDailyForecasts(currentTime)
|
val dailyForecasts = generateDailyForecasts(currentTime)
|
||||||
|
|
||||||
|
// Simulates a network request
|
||||||
delay(100)
|
delay(100)
|
||||||
|
|
||||||
return WeatherForecastData(
|
return WeatherForecastData(
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user