Skip to content

Commit 1c9ec11

Browse files
committed
Refactor: Make LocationViewModel methods non-suspend
The suspend modifier has been removed from the `saveToDatabase`, `deleteFromDatabase`, and `updateCurrentLocation` methods in `LocationViewModel.kt`.
1 parent 4714d00 commit 1c9ec11

File tree

1 file changed

+52
-52
lines changed

1 file changed

+52
-52
lines changed

app/src/main/java/com/warbler/ui/location/LocationViewModel.kt

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,67 +17,67 @@ import javax.inject.Inject
1717

1818
@HiltViewModel
1919
class LocationViewModel
20-
@Inject
21-
constructor(
22-
private val locationRepository: LocationRepository,
23-
private val locationNetworkRepository: LocationNetworkRepository,
24-
) : ViewModel() {
25-
private val _locationList = MutableStateFlow<Resource<List<LocationEntity>>>(Resource.Loading)
26-
val locationList: StateFlow<Resource<List<LocationEntity>>>
27-
get() = _locationList
20+
@Inject
21+
constructor(
22+
private val locationRepository: LocationRepository,
23+
private val locationNetworkRepository: LocationNetworkRepository,
24+
) : ViewModel() {
25+
private val _locationList = MutableStateFlow<Resource<List<LocationEntity>>>(Resource.Loading)
26+
val locationList: StateFlow<Resource<List<LocationEntity>>>
27+
get() = _locationList
2828

29-
private val _locationSearchList =
30-
MutableStateFlow<Resource<List<LocationEntity>>>(
31-
Resource.Loading,
32-
)
33-
val locationSearchList: StateFlow<Resource<List<LocationEntity>>>
34-
get() = _locationSearchList
29+
private val _locationSearchList =
30+
MutableStateFlow<Resource<List<LocationEntity>>>(
31+
Resource.Loading,
32+
)
33+
val locationSearchList: StateFlow<Resource<List<LocationEntity>>>
34+
get() = _locationSearchList
3535

36-
init {
37-
viewModelScope.launch {
38-
locationRepository
39-
.getAllLocationsFromDatabase()
40-
.catch {
41-
Log.d("LocationViewModel", "Error: ${it.message}")
42-
_locationList.value = Resource.Error(message = it.message)
43-
}.collect {
44-
Log.d("LocationViewModel", "Success: $it")
45-
_locationList.value = Resource.Success(it)
46-
}
36+
init {
37+
viewModelScope.launch {
38+
locationRepository
39+
.getAllLocationsFromDatabase()
40+
.catch {
41+
Log.d("LocationViewModel", "Error: ${it.message}")
42+
_locationList.value = Resource.Error(message = it.message)
43+
}.collect {
44+
Log.d("LocationViewModel", "Success: $it")
45+
_locationList.value = Resource.Success(it)
46+
}
47+
}
4748
}
48-
}
4949

50-
suspend fun saveToDatabase(location: LocationEntity) {
51-
viewModelScope.launch {
52-
locationRepository.saveLocationToDatabaseAndSetAsCurrent(location)
50+
fun saveToDatabase(location: LocationEntity) {
51+
viewModelScope.launch {
52+
locationRepository.saveLocationToDatabaseAndSetAsCurrent(location)
53+
}
5354
}
54-
}
5555

56-
suspend fun deleteFromDatabase(location: LocationEntity) {
57-
Log.d("LocationViewModel", "deleteFromDatabase launching coroutine...")
58-
viewModelScope.launch {
59-
locationRepository.deleteLocation(location)
56+
fun deleteFromDatabase(location: LocationEntity) {
57+
Log.d("LocationViewModel", "deleteFromDatabase launching coroutine...")
58+
viewModelScope.launch {
59+
locationRepository.deleteLocation(location)
60+
}
6061
}
61-
}
6262

63-
suspend fun updateCurrentLocation(location: LocationEntity) {
64-
viewModelScope.launch(Dispatchers.IO) {
65-
locationRepository.updateToCurrentLocation(location)
63+
fun updateCurrentLocation(location: LocationEntity) {
64+
viewModelScope.launch(Dispatchers.IO) {
65+
locationRepository.updateToCurrentLocation(location)
66+
}
6667
}
67-
}
6868

69-
fun searchForLocation(query: String) {
70-
Log.d("LocationViewModel", "searchForLocation launching coroutine...")
71-
viewModelScope.launch {
72-
Log.d("LocationViewModel", "searchForLocation Attempting to search for: $query")
73-
locationNetworkRepository
74-
.getLocationsFromGeoService(query)
75-
.catch { error ->
76-
Log.d("LocationViewModel", "searchForLocation error: ${error.message}")
77-
}.collect {
78-
Log.i("LocationViewModel", "searchForLocation success: ${it.size}")
79-
_locationSearchList.value = Resource.Success(it)
80-
}
69+
fun searchForLocation(query: String) {
70+
Log.d("LocationViewModel", "searchForLocation launching coroutine...")
71+
viewModelScope.launch {
72+
Log.d("LocationViewModel", "searchForLocation Attempting to search for: $query")
73+
locationNetworkRepository
74+
.getLocationsFromGeoService(query)
75+
.catch { error ->
76+
Log.d("LocationViewModel", "searchForLocation error: ${error.message}")
77+
}.collect {
78+
Log.i("LocationViewModel", "searchForLocation success: ${it.size}")
79+
_locationSearchList.value = Resource.Success(it)
80+
}
81+
}
8182
}
8283
}
83-
}

0 commit comments

Comments
 (0)